* @since 1.48.2 */ namespace AdvancedAds\Admin\Ads; use AdvancedAds\Abstracts\Ad; use AdvancedAds\Abstracts\Admin_List_Table; use AdvancedAds\Constants; use AdvancedAds\Framework\Utilities\Params; use AdvancedAds\Options; use AdvancedAds\Interfaces\Ad_Type; use WP_Query; defined( 'ABSPATH' ) || exit; /** * Admin Ad List Table. */ class List_Table extends Admin_List_Table { /** * Object being shown on the row. * * @var Ad|null */ protected $object = null; /** * Object type. * * @var string */ protected $object_type = 'ad'; /** * Post type. * * @var string */ protected $list_table_type = Constants::POST_TYPE_AD; /** * Hook into WordPress. * * @return void */ public function hooks(): void { parent::hooks(); add_action( 'pre_get_posts', [ $this, 'posts_ordering' ] ); add_filter( 'views_edit-' . $this->list_table_type, [ $this, 'add_views' ] ); add_filter( 'disable_months_dropdown', [ $this, 'disable_months_dropdown' ], 10, 2 ); } /** * Disable the months dropdown on the ads list table only. * * @param bool $disable Whether to disable the dropdown. * @param string $post_type Current post type. * * @return bool */ public function disable_months_dropdown( $disable, $post_type ): bool { return Constants::POST_TYPE_AD === $post_type ? true : (bool) $disable; } /** * Define which columns to show on this screen. * * @param array $columns Existing columns. * * @return array */ public function define_columns( $columns ): array { // Remove the group taxonomy column as we have custom 'Used' column. unset( $columns[ 'taxonomy-' . Constants::TAXONOMY_GROUP ] ); $new_columns = []; foreach ( $columns as $key => $value ) { $new_columns[ $key ] = $value; if ( 'cb' === $key ) { $new_columns['ad_type'] = __( 'Type', 'advanced-ads' ); } if ( 'title' === $key ) { $new_columns['title'] = __( 'Name', 'advanced-ads' ); $new_columns['ad_size'] = __( 'Size', 'advanced-ads' ); $new_columns['ad_timing'] = __( 'Ad Planning', 'advanced-ads' ); $new_columns['ad_shortcode'] = __( 'Ad Shortcode', 'advanced-ads' ); $new_columns['ad_adsense_id'] = __( 'AdSense ID', 'advanced-ads' ); $new_columns['ad_date'] = __( 'Date', 'advanced-ads' ); $new_columns['ad_description'] = __( 'Notes', 'advanced-ads' ); $new_columns['ad_preview'] = __( 'Preview', 'advanced-ads' ); $new_columns['ad_used'] = __( 'Used', 'advanced-ads' ); $new_columns['ad_debugmode'] = __( 'Debug Mode', 'advanced-ads' ); // show only when privacy setting is enabled. if ( Options::instance()->get( 'privacy.enabled' ) ) { $new_columns['ad_privacyignore'] = __( 'Privacy Ignore', 'advanced-ads' ); } } } unset( $new_columns['date'] ); return apply_filters( 'advanced-ads-ad-columns', $new_columns ); } /** * Define which columns are sortable. * * @param array> $columns Existing columns. * * @return array> */ public function define_sortable_columns( $columns ): array { $columns['ad_date'] = 'ad_date'; return $columns; } /** * Define hidden columns. * * @return array */ protected function define_hidden_columns(): array { return [ 'ad_description', 'author', 'ad_size', 'ad_shortcode', 'ad_date', 'ad_preview', 'ad_adsense_id', 'ad_debugmode', 'ad_privacyignore', ]; } /** * Render any custom filters and search inputs for the list table. * * @return void */ protected function render_filters(): void { include_once ADVADS_ABSPATH . 'views/admin/ads/filters.php'; } /** * Add expiring and expired ads view. * * @param array $views Available list table views. * * @return array */ public function add_views( $views ): array { global $wp_list_table; $counts = wp_count_posts( $this->list_table_type, 'readable' ); $expired = $counts->{Constants::AD_STATUS_EXPIRED}; $expiring = $counts->{Constants::AD_STATUS_EXPIRING}; // Exclude expired and expiring ads all. $total_ads = array_sum( (array) $counts ) - $counts->{'auto-draft'}; $all_ads = $total_ads - $expired - $expiring; $views['all'] = str_replace( $total_ads, $all_ads, $views['all'] ); if ( $expiring > 0 ) { $views[ Constants::AD_STATUS_EXPIRING ] = sprintf( '%s (%d)', add_query_arg( [ 'post_type' => Constants::POST_TYPE_AD, 'addate' => 'advads-filter-expiring', 'orderby' => 'expiry_date', 'order' => 'ASC', ], 'edit.php' ), 'advads-filter-expiring' === Params::request( 'addate' ) ? 'class="current" aria-current="page"' : '', esc_attr_x( 'Expiring', 'Post list header for ads expiring in the future.', 'advanced-ads' ), $expiring ); } if ( $expired > 0 ) { $views[ Constants::AD_STATUS_EXPIRED ] = sprintf( '%s (%d)', add_query_arg( [ 'post_type' => Constants::POST_TYPE_AD, 'addate' => 'advads-filter-expired', 'orderby' => 'expiry_date', 'order' => 'DESC', ], 'edit.php' ), 'advads-filter-expired' === Params::request( 'addate' ) ? 'class="current" aria-current="page"' : '', esc_attr_x( 'Expired', 'Post list header for expired ads.', 'advanced-ads' ), $expired ); } // Render views. $wp_list_table->screen->render_screen_reader_content( 'heading_views' ); $is_all = $this->is_all_filters_applied(); include ADVADS_ABSPATH . 'views/admin/table-views-list.php'; return []; } /** * Query filters. * * @param array $query_vars Query vars. * * @return array */ protected function query_filters( $query_vars ): array { // Early bail!! if ( wp_doing_ajax() ) { return $query_vars; } if ( 'expiry_date' === $query_vars['orderby'] ) { $query_vars['orderby'] = 'meta_value'; $query_vars['meta_key'] = Constants::AD_META_EXPIRATION_TIME; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key $query_vars['order'] = strtoupper( $query_vars['order'] ) === 'DESC' ? 'DESC' : 'ASC'; if ( 'advads-filter-expired' === Params::get( 'addate' ) ) { $query_vars['post_status'] = Constants::AD_STATUS_EXPIRED; } } $filter_author = Params::get( 'ad_author' ); if ( $filter_author ) { $query_vars['author'] = $filter_author; } return $query_vars; } /** * Modify the post listing order in the admin panel for a specific custom post type. * * @param WP_Query $query The WP_Query object. * * @return void */ public function posts_ordering( $query ): void { global $typenow; if ( ! $query->is_main_query() || $this->list_table_type !== $typenow ) { return; } $orderby = Params::get( 'orderby', 'title' ); // Expiry views are remapped to meta sorting in query_filters; do not overwrite. if ( 'expiry_date' === $orderby || ( 'meta_value' === $query->get( 'orderby' ) && Constants::AD_META_EXPIRATION_TIME === $query->get( 'meta_key' ) ) ) { return; } if ( 'ad_date' === $orderby ) { $orderby = 'post_modified'; } $order = strtoupper( Params::get( 'order', 'ASC' ) ); $query->set( 'orderby', $orderby ); $query->set( 'order', 'DESC' === $order ? 'DESC' : 'ASC' ); } /** * Pre-fetch any data for the row each column has access to it. * * @param int $post_id Post ID being shown. * * @return void */ protected function prepare_row_data( $post_id ): void { if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) { $this->object = wp_advads_get_ad( $post_id ); } } /** * Display the ad type icon in the ads list. * * @return void */ protected function render_ad_type_column(): void { $ad = $this->object; $size_string = $this->get_ad_size_string(); include ADVADS_ABSPATH . 'views/admin/tables/ads/column-type.php'; } /** * Display the ad description in the ads list * * @return void */ protected function render_ad_description_column(): void { $description = wp_trim_words( $this->object->get_description(), 50 ); include ADVADS_ABSPATH . 'views/admin/tables/ads/column-description.php'; } /** * Display an ad preview in ads list. * * @return void */ protected function render_ad_preview_column(): void { $type_object = $this->object->get_type_object(); // Early bail!! if ( ! $type_object ) { return; } if ( is_callable( [ $type_object, 'render_preview' ] ) ) { $type_object->render_preview( $this->object ); } do_action( 'advanced-ads-ad-list-details-column-after', $this->object, $type_object ); } /** * Display the ad size in the ads list * * @return void */ protected function render_ad_size_column(): void { $size = $this->get_ad_size_string(); // Early bail!! if ( empty( $size ) ) { return; } include ADVADS_ABSPATH . 'views/admin/tables/ads/column-size.php'; } /** * Display ad timing in ads list * * @return void */ protected function render_ad_timing_column(): void { list( 'status_strings' => $status_strings, 'html_classes' => $html_classes, ) = $this->object->get_ad_schedule_details(); ob_start(); do_action_ref_array( 'advanced-ads-ad-list-timing-column-after', [ $this->object, &$html_classes, ] ); $content_after = ob_get_clean(); include ADVADS_ABSPATH . 'views/admin/tables/ads/column-timing.php'; } /** * Display ad shortcode in ads list * * @return void */ protected function render_ad_shortcode_column(): void { $ad_id = $this->object->get_id(); include ADVADS_ABSPATH . 'views/admin/tables/ads/column-shortcode.php'; } /** * Display an ad date in ads list. * * @return void */ protected function render_ad_date_column(): void { $id = $this->object->get_id(); if ( ! $id ) { return; } $datetime_regex = get_option( 'date_format' ) . ' \\a\\t ' . get_option( 'time_format' ); $published_date = get_the_date( $datetime_regex, $id ); $modified_date = get_the_modified_date( $datetime_regex, $id ); include ADVADS_ABSPATH . 'views/admin/tables/ads/column-date.php'; } /** * Display the adsense id. * * @return void */ protected function render_ad_adsense_id_column(): void { if ( ! $this->object->is_type( 'adsense' ) ) { return; } $raw = $this->object->get_content(); if ( null === $raw || '' === $raw ) { return; } $content = json_decode( $raw ); if ( ! is_object( $content ) ) { return; } // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $slotid = $content->slotId ?? null; include ADVADS_ABSPATH . 'views/admin/tables/ads/column-adsense.php'; } /** * Cached group/placement usage maps (once per request). * * @var array{groups_by_ad: array>, placements_by_item: array>}|null */ private static $ad_usage_maps = null; /** * Build group and placement usage maps once per list table request. * * @return array{groups_by_ad: array>, placements_by_item: array>} */ private function get_ad_usage_maps(): array { if ( null !== self::$ad_usage_maps ) { return self::$ad_usage_maps; } $placements_by_item = []; foreach ( wp_advads_get_placement_summaries() as $id => $summary ) { if ( '' === $summary['item'] ) { continue; } $placements_by_item[ $summary['item'] ][] = [ 'id' => $id, 'title' => $summary['title'], 'edit_link' => add_query_arg( [ 'post_type' => Constants::POST_TYPE_PLACEMENT ], admin_url( 'edit.php#modal-placement-edit-' . $id ) ), ]; } $groups_by_ad = []; foreach ( wp_advads_get_group_summaries() as $group_id => $summary ) { foreach ( array_keys( $summary['ad_weights'] ?? [] ) as $ad_id ) { $groups_by_ad[ (int) $ad_id ][] = [ 'id' => $group_id, 'title' => $summary['title'], 'edit_link' => add_query_arg( [ 'page' => 'advanced-ads-groups', 'advads-last-edited-group' => $group_id, ], admin_url( 'admin.php' ) . '#modal-group-edit-' . $group_id ), ]; } } self::$ad_usage_maps = compact( 'groups_by_ad', 'placements_by_item' ); return self::$ad_usage_maps; } /** * Display ad usage in groups & placements. * * @return void */ protected function render_ad_used_column(): void { $ad_id = $this->object->get_id(); if ( ! $ad_id ) { return; } $maps = $this->get_ad_usage_maps(); $groups = $maps['groups_by_ad'][ $ad_id ] ?? []; $placements = $maps['placements_by_item'][ 'ad_' . $ad_id ] ?? []; include ADVADS_ABSPATH . 'views/admin/tables/ads/column-used.php'; } /** * Display the debug mode in the ads list. * * @return void */ protected function render_ad_debugmode_column(): void { $debug_mode = $this->object->is_debug_mode(); include ADVADS_ABSPATH . 'views/admin/tables/ads/column-debug.php'; } /** * Display the privacy ignore status in the ads list. * * @return void */ protected function render_ad_privacyignore_column(): void { $privacyignore = $this->object->get_prop( 'privacy.ignore-consent' ) ?? false; include ADVADS_ABSPATH . 'views/admin/tables/ads/column-privacyignore.php'; } /** * Get the ad size string to display in post list. * * @return string */ private function get_ad_size_string(): string { $size = ''; if ( ! empty( $this->object->get_width() ) || ! empty( $this->object->get_height() ) ) { $size = sprintf( '%d × %d', $this->object->get_width(), $this->object->get_height() ); } /** * Filter the ad size string to display in the ads post list. * * @param string $size Size string. * @param Ad $ad Ad instance. */ return (string) apply_filters( 'advanced-ads-list-ad-size', $size, $this->object ); } }