hook = add_submenu_page( // Constants::ADPRESSO_SLUG, null, __( 'Ads, Groups & Placements', 'adpresso' ), __( 'List View', 'adpresso' ), adpresso_get_capability( 'edit_ad' ), $this->get_id(), [ $this, 'render' ] ); } /** * Renders the root element for the React application. * This is the only HTML output this PHP page generates. */ public function render(): void { // This div is the mounting point for our entire React application. // The JavaScript bundle loaded on this page will take over from here. echo '
'; echo '
'; } /** * Enqueues the React application's scripts and styles, and localizes data. * This is hooked into 'admin_enqueue_scripts'. * * @param WP_Screen|null $current_screen * */ public function enqueue_assets( WP_Screen $current_screen = null ): void { // We need the hook from add_submenu_page to check if we are on the correct page. if ( ! $current_screen ) { return; } /* @var Assets $assets_manager */ $assets_manager = Plugin::get()->globals[ Assets::class ] ?? null; if ( ! $assets_manager ) { return; } if ( $current_screen->base !== 'admin_page_' . $this->get_id() ) { return; } // Get asset manifest from build process $script_asset_path = ADPRESSO_ABSPATH . 'admin/assets/app/build/index.asset.php'; if ( ! file_exists( $script_asset_path ) ) { wp_die( 'AdPresso React App Error: You need to run `npm run build`. Asset file not found.' ); } $script_asset = require( $script_asset_path ); wp_enqueue_media(); wp_enqueue_style( 'wp-components' ); wp_enqueue_code_editor( [ 'type' => 'text/html' ] ); wp_enqueue_editor(); // Enqueue the main React app script $script_handle = 'adpresso-react-app'; wp_enqueue_script( $script_handle, ADPRESSO_BASE_URL . 'admin/assets/app/build/index.js', $script_asset['dependencies'], $script_asset['version'], true ); wp_set_script_translations( $script_handle, 'adpresso' ); // Enqueue the app's CSS wp_enqueue_style( $script_handle, ADPRESSO_BASE_URL . 'admin/assets/app/build/index.css', [ 'wp-components' ], $script_asset['version'] ); $admin_data = [ 'ajaxUrl' => esc_url_raw( rest_url( 'adpresso/v1/entities' ) ), 'apiNonce' => wp_create_nonce( 'wp_rest' ), 'initialCounts' => $this->get_initial_entity_counts(), 'uiConfig' => $this->get_list_table_ui_config(), 'logoUrl' => ADPRESSO_BASE_URL . 'admin/assets/img/adpresso-logo.svg', // 'upgradeUrl' => function_exists('adpresso_fs') ? adpresso_fs()->get_upgrade_url() : 'https://wpadpresso.com/pricing', 'dateFormat' => get_option( 'date_format' ), 'timeFormat' => get_option( 'time_format' ), 'isPro' => Util::is_pro_active(), 'pricingUrl' => adpresso_fs()->get_upgrade_url(), 'upgradeUrl' => admin_url( 'admin.php?page=adpresso-pricing' ), 'homeUrl' => home_url(), 'docsUrlScriptLifecycle' => Constants::DOCS_URL_SCRIPT_LIFECYCLE, 'frontendPrefix' => Settings::get_setting( 'frontend_prefix', Constants::DEFAULT_FRONTEND_PREFIX ), ]; if ( Util::is_pro_active() ) { $admin_data['linkbase'] = Settings::get_setting('linkbase', Constants::LINK_MASKING_LINKBASE); } // Localize all necessary data for the React app wp_localize_script( $script_handle, 'adpressoAdminData', $admin_data ); $wp_scripts = wp_scripts(); if ( isset( $wp_scripts->registered['adpresso-modal'] ) ) { $wp_scripts->registered['adpresso-modal']->deps[] = $script_handle; } wp_enqueue_script( 'adpresso-modal' ); wp_set_script_translations( 'adpresso-modal', 'adpresso' ); wp_enqueue_style( 'adpresso-modal' ); } /** * Gets the total counts for each entity type. * @return array */ private function get_initial_entity_counts(): array { return Util::get_entity_status_counts(); } /** * A reusable helper method to get the counts for a given entity type. * * @param array $options The source array containing 'value' und 'label'. * @param array $counts An associative Array [value => count]. * * @return array The extended $options-Array with a 'count'-key. */ private function merge_counts_into_options( array $options, array $counts ): array { return array_map( function( $item ) use ( $counts ) { $value = $item['value']; return [ 'value' => $value, 'label' => $item['label'], 'count' => $counts[ $value ] ?? 0, ]; }, $options ); } /** * Defines and returns the entire UI configuration for the list table. * This includes column and filter definitions and is made filterable for extensions. * @return array */ private function get_list_table_ui_config(): array { $stati = get_post_stati( [], 'objects' ); $status_filter_options = array_map( function( $status ) { return [ 'value' => $status->name, 'label' => $status->label, ]; }, array_values( $stati ) ); $ad_type_options = []; foreach ( AdTypeRegistry::get()->get_all_types() as $type ) { $ad_type_options[] = [ 'value' => $type->get_id(), 'label' => $type->get_title() ]; } $ad_type_counts = adpresso_get_ad_repository()->counts_by_type( array_column( $ad_type_options, 'value' ), Util::LISTED_POST_STATUSES ); $ad_type_options = $this->merge_counts_into_options( $ad_type_options, $ad_type_counts ); $placement_type_options = []; foreach ( PlacementTypeRegistry::get()->get_all_types() as $type ) { $placement_type_options[] = [ 'value' => $type->get_id(), 'label' => $type->get_title() ]; } $placement_type_counts = adpresso_get_placement_repository()->counts_by_type( array_column( $placement_type_options, 'value' ), Util::LISTED_POST_STATUSES ); $placement_type_options = $this->merge_counts_into_options( $placement_type_options, $placement_type_counts ); $add_placement_shortcode = ! empty( $placement_type_counts['manual'] ) && $placement_type_counts['manual'] > 0; $conditions_options = []; foreach ( Condition_Registry::get()->get_all_conditions() as $type ) { $conditions_options[] = [ 'value' => $type->get_id(), 'label' => $type->get_label() ]; } $columns = [ 'ads' => [ [ 'key' => 'title', 'label' => __( 'Name', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'text', ], ], [ 'key' => 'id', 'label' => __( 'ID', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'text', ], ], [ 'key' => 'type', 'label' => __( 'Type', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, 'filter' => [ 'type' => 'checkbox_list', 'options' => $ad_type_options, 'searchable' => true, ], ], [ 'key' => 'status', 'label' => __( 'Status', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, 'filter' => [ 'type' => 'checkbox_list', 'exclusiveValue' => 'trash', 'options' => $status_filter_options, // 'selection' => 'single', ], ], [ 'key' => 'shortcode', 'label' => __( 'Shortcode', 'adpresso' ), 'defaultVisible' => false, 'sortable' => false, ], [ 'key' => 'conditions', 'label' => __( 'Conditions', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'checkbox_list', 'options' => $conditions_options, // 'selection' => 'single', ], ], [ 'key' => 'used_in', 'label' => __( 'Used In', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, 'filter' => [ 'type' => 'checkbox_list', 'isSingle' => false, 'options' => [ [ 'label' => __( 'Placements', 'adpresso' ), 'options' => adpresso_get_placement_repository()->get_placements_for_select(), ], [ 'label' => __( 'Groups', 'adpresso' ), 'options' => adpresso_get_group_repository()->get_groups_for_select(), ], ], // 'selection' => 'single', 'searchable' => true, ], ], [ 'key' => 'preview', 'label' => __( 'Preview', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true ], [ 'key' => 'target_url', 'label' => __( 'Target URL', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true ], [ 'key' => 'author', 'label' => __( 'Author', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'searchable_list', 'options' => [], 'rest_endpoint' => 'adpresso/v1/filter-options/users', ], ], [ 'key' => 'date_modified', 'label' => __( 'Last Modified', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'date_range', ], ], ], 'groups' => [ [ 'key' => 'title', 'label' => __( 'Name', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'text', ], ], [ 'key' => 'id', 'label' => __( 'ID', 'adpresso' ), 'defaultVisible' => false, 'sortable' => true, 'filter' => [ 'type' => 'text', ], ], [ 'key' => 'type', 'label' => __( 'Type', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, ], [ 'key' => 'conditions', 'label' => __( 'Conditions', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'checkbox_list', 'options' => $conditions_options, // 'selection' => 'single', ], ], [ 'key' => 'status', 'label' => __( 'Status', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, 'filter' => [ 'type' => 'checkbox_list', 'exclusiveValue' => 'trash', 'options' => $status_filter_options, // 'selection' => 'single', ], ], [ 'key' => 'shortcode', 'label' => __( 'Shortcode', 'adpresso' ), 'defaultVisible' => false, 'sortable' => false, ], [ 'key' => 'output', 'label' => __( 'Output', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false ], [ 'key' => 'ads_info', 'label' => __( 'Ads', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false ], [ 'key' => 'refresh', 'label' => __( 'Refresh', 'adpresso' ), 'defaultVisible' => false, 'sortable' => true ], [ 'key' => 'visible_ads', 'label' => __( 'Concurrent ads', 'adpresso' ), 'defaultVisible' => false, 'sortable' => false ], [ 'key' => 'author', 'label' => __( 'Author', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'searchable_list', 'options' => [], 'rest_endpoint' => 'adpresso/v1/filter-options/users', ], ], [ 'key' => 'date_modified', 'label' => __( 'Last Modified', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'date_range', ], ], ], 'placements' => [ [ 'key' => 'title', 'label' => __( 'Name', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'text', ], ], [ 'key' => 'id', 'label' => __( 'ID', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'text', ], ], [ 'key' => 'type', 'label' => __( 'Type', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, 'filter' => [ 'type' => 'checkbox_list', 'options' => $placement_type_options, 'searchable' => true, ], ], [ 'key' => 'status', 'label' => __( 'Status', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false, 'filter' => [ 'type' => 'checkbox_list', 'exclusiveValue' => 'trash', 'options' => $status_filter_options, // 'selection' => 'single', ], ], [ 'key' => 'conditions', 'label' => __( 'Conditions', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'checkbox_list', 'options' => $conditions_options, // 'selection' => 'single', ], ], [ 'key' => 'item', 'label' => __( 'Ad / Group', 'adpresso' ), 'defaultVisible' => true, 'sortable' => false ], [ 'key' => 'author', 'label' => __( 'Author', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'searchable_list', 'options' => [], 'rest_endpoint' => 'adpresso/v1/filter-options/users', ], ], [ 'key' => 'date_modified', 'label' => __( 'Last Modified', 'adpresso' ), 'defaultVisible' => true, 'sortable' => true, 'filter' => [ 'type' => 'date_range', ], ], ], ]; if ( $add_placement_shortcode ) { $columns['placements'][] = [ 'key' => 'shortcode', 'label' => __( 'Shortcode', 'adpresso' ), 'defaultVisible' => false, 'sortable' => false, ]; } $filters = []; $bulk_actions = [ 'trash' => [ 'label' => __( 'Move to Trash', 'adpresso' ), 'variant' => 'destructive', ], 'duplicate' => [ 'label' => __( 'Clone', 'adpresso' ), 'variant' => 'accent', 'disabled' => true, 'premium' => true, ], ]; return [ 'columns' => apply_filters( 'adpresso_list_table_columns', $columns ), 'filters' => apply_filters( 'adpresso_list_table_filters', $filters ), 'bulk_actions' => apply_filters( 'adpresso_list_table_bulk_actions', $bulk_actions ), 'clipboard_enabled' => defined( 'ADPRESSO_CLIPBOARD_TOOLS' ) && ADPRESSO_CLIPBOARD_TOOLS === true, ]; } }