*/ public $additional_columns = array( 'name' => array( 1, 'Name' ), ); /** * Set up taxonomy table configuration. * * @param string $taxonomy Taxonomy slug. * @param string $screen_slug Screen slug. */ public function __construct( string $taxonomy, string $screen_slug ) { parent::__construct( array( 'singular' => $taxonomy, 'plural' => $taxonomy . 's', 'ajax' => false, ) ); $this->taxonomy = $taxonomy; $this->screen_slug = $screen_slug; } /** * Handle bulk optimize action (delegated to JS). * * @return void */ public function process_bulk_action() { if ( $this->current_action() !== 'optimize' ) { return; } if ( ! $this->has_valid_bulk_action_nonce() ) { return; } $term_ids = array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by has_valid_bulk_action_nonce(). if ( isset( $_POST['term'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized via absint below. $raw = wp_unslash( $_POST['term'] ); $term_ids = is_array( $raw ) ? array_map( 'absint', $raw ) : array(); } $ids = $term_ids; // JS handles optimization. } /** * Checkbox column renderer. * * @param \WP_Term $item Term row. * @return string */ protected function column_cb( $item ) { return sprintf( '', $item->term_id ); } /** * Render a single row with optimization metadata for client-side eligibility checks. * * @param \WP_Term $item Term row. * @return void */ public function single_row( $item ) { $item_id = isset( $item->term_id ) ? (int) $item->term_id : 0; $optimized = $this->is_term_optimized( $item ) ? '1' : '0'; printf( '', esc_attr( (string) $item_id ), esc_attr( $optimized ) ); $this->single_row_columns( $item ); echo ''; } /** * Render trimmed description. * * @param \WP_Term $item Term row. * @return string */ protected function column_long_desc( $item ) { $desc = wp_trim_words( $item->description, 5, '…' ); return esc_html( '' !== $desc ? $desc : '—' ); } /** * Render meta title column. * * @param \WP_Term|\WP_Post $item Row data. * @return string */ protected function column_meta_title( $item ) { if ( is_object( $item ) && isset( $item->term_id ) ) { $meta_title = get_term_meta( $item->term_id, '_parallax_meta_title', true ); } else { $meta_title = get_post_meta( $item->ID, '_parallax_meta_title', true ); } return esc_html( '' !== $meta_title ? $meta_title : '—' ); } /** * Render meta description column. * * @param \WP_Term|\WP_Post $item Row data. * @return string */ protected function column_meta_desc( $item ) { if ( is_object( $item ) && isset( $item->term_id ) ) { $meta_desc = get_term_meta( $item->term_id, '_parallax_meta_desc', true ); } else { $meta_desc = get_post_meta( $item->ID, '_parallax_meta_desc', true ); } return esc_html( '' !== $meta_desc ? $meta_desc : '—' ); } /** * Render term name with edit link. * * @param \WP_Term $item Term row. * @return string */ protected function column_name( $item ) { $edit_link = get_edit_term_link( $item->term_id, $this->taxonomy ); return sprintf( '%s', esc_url( $edit_link ), esc_html( $item->name ) ); } /** * Render term slug. * * @param \WP_Term $item Term row. * @return string */ protected function column_slug( $item ) { return esc_html( $item->slug ); } /** * Render term count column. * * @param \WP_Term $item Term row. * @return int */ protected function column_count( $item ) { return intval( $item->count ); } /** * Render optimized yes/no column. * * @param \WP_Term $item Term row. * @return string */ protected function column_optimized( $item ) { $optimized = $this->is_term_optimized( $item ); return $optimized ? esc_html( Messages::get( 'filtered.yes' ) ) : esc_html( Messages::get( 'filtered.no' ) ); } /** * Render status column with link when optimized. * * @param \WP_Term $item Term row. * @return string */ protected function column_status( $item ) { if ( $this->is_term_optimized( $item ) ) { $edit_link = get_edit_term_link( $item->term_id, $this->taxonomy ); $label = esc_html( Messages::get( 'filtered.optimized' ) ); return $edit_link ? sprintf( '%s', esc_url( $edit_link ), $label ) : $label; } return esc_html( Messages::get( 'filtered.not_optimized' ) ); } /** * Prepare term list table items. * * @return void */ public function prepare_items() { $requested_page = max( 1, $this->get_request_int( 'paged', 1 ) ); $current_page = $requested_page; $hide_optimized = $this->is_hide_optimized_enabled(); $total_count = $this->resolve_total_term_count(); $terms = $this->query_terms_for_page( $current_page ); $visible_terms = $hide_optimized ? $this->filter_out_optimized_terms( $terms ) : $terms; if ( $hide_optimized && empty( $visible_terms ) ) { $max_pages = $total_count > 0 ? max( 1, (int) ceil( $total_count / $this->per_page ) ) : 1; $fallback = $this->find_next_page_with_unoptimized_terms( $current_page, $max_pages ); if ( null !== $fallback ) { $current_page = $fallback['page']; $visible_terms = $fallback['items']; } } $_REQUEST['paged'] = (string) $current_page; $_GET['paged'] = (string) $current_page; $this->items = $visible_terms; $this->_column_headers = array( $this->get_columns(), array(), array() ); $total_pages = $total_count > 0 ? (int) ceil( $total_count / $this->per_page ) : 1; $this->set_pagination_args( array( 'total_items' => $total_count, 'per_page' => $this->per_page, 'total_pages' => max( 1, $total_pages ), ) ); $this->process_bulk_action(); } /** * Output message when no items are found. * * @return void */ public function no_items() { if ( $this->is_hide_optimized_enabled() ) { echo esc_html( Messages::get( 'filtered.all_optimized' ) ); return; } echo esc_html( Messages::get( 'filtered.no_items' ) ); } /** * Fetch terms for the requested page while honouring active filters. * * @param int $page Page number. * @return array */ private function query_terms_for_page( int $page ): array { $query = new WP_Term_Query( $this->build_term_query_args( $page ) ); $terms = $query->get_terms(); return is_array( $terms ) ? $terms : array(); } /** * Build the term query arguments for a specific page. * * @param int $page Page number. * @return array */ private function build_term_query_args( int $page ): array { $args = array( 'taxonomy' => $this->taxonomy, 'hide_empty' => false, 'number' => $this->per_page, 'offset' => ( $page - 1 ) * $this->per_page, ); if ( 'product_cat' === $this->taxonomy ) { $excluded_ids = Settings::get_excluded_category_ids(); if ( ! empty( $excluded_ids ) ) { // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Exclusions are admin-configured and required to enforce skip lists. $args['exclude'] = $excluded_ids; } } $search = $this->get_request_string( 's' ); if ( null !== $search && '' !== $search ) { $args['search'] = $search; } $orderby = $this->get_request_string( 'orderby' ); if ( null !== $orderby && '' !== $orderby ) { $args['orderby'] = $orderby; $order = $this->get_request_string( 'order' ); if ( null !== $order && in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ) { $args['order'] = strtoupper( $order ); } } return $args; } /** * Remove optimized terms from the provided collection. * * @param array $terms List of terms to filter. * @return array */ private function filter_out_optimized_terms( array $terms ): array { return array_values( array_filter( $terms, function ( $item ) { return ! $this->is_term_optimized( $item ); } ) ); } /** * Locate the next page that contains unoptimized terms. * * @param int $current_page Current page number. * @param int $max_pages Maximum number of pages. * @return array{page:int, items:array}|null */ private function find_next_page_with_unoptimized_terms( int $current_page, int $max_pages ): ?array { $page = $current_page; $max_attempts = 10; while ( $page < $max_pages && ( $page - $current_page ) < $max_attempts ) { ++$page; $terms = $this->filter_out_optimized_terms( $this->query_terms_for_page( $page ) ); if ( ! empty( $terms ) ) { return array( 'page' => $page, 'items' => $terms, ); } } return null; } /** * Determine total term count for the active filters. * * @return int */ private function resolve_total_term_count(): int { $args = array( 'taxonomy' => $this->taxonomy, 'hide_empty' => false, 'fields' => 'count', ); if ( 'product_cat' === $this->taxonomy ) { $excluded_ids = Settings::get_excluded_category_ids(); if ( ! empty( $excluded_ids ) ) { // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Exclusions are admin-configured and required to enforce skip lists. $args['exclude'] = $excluded_ids; } } $search = $this->get_request_string( 's' ); if ( null !== $search && '' !== $search ) { $args['search'] = $search; } $query = new WP_Term_Query( $args ); $count = $query->get_terms(); if ( is_wp_error( $count ) ) { return 0; } return is_numeric( $count ) ? (int) $count : 0; } /** * Determines if a term has been optimized. * * Checks the term's snapshot metadata and compares the original values (name, description, meta fields) * with the current values. Returns true if any of the current values differ from the original snapshot, * or if new values have been added where none existed before. * * @param object $item WP_Term object or similar, representing the term to check. * @return bool True if the term is optimized, false otherwise. */ private function is_term_optimized( $item ): bool { $term_id = isset( $item->term_id ) ? (int) $item->term_id : 0; if ( $term_id <= 0 ) { return false; } $snapshot_raw = get_term_meta( $term_id, OriginalSnapshot::TERM_META_KEY, true ); $snapshot = maybe_unserialize( $snapshot_raw ); $snapshot = is_array( $snapshot ) ? $snapshot : array(); $original_name = (string) ( $snapshot['name'] ?? '' ); $original_desc = (string) ( $snapshot['description'] ?? '' ); $original_meta = isset( $snapshot['parallax_meta'] ) && is_array( $snapshot['parallax_meta'] ) ? $snapshot['parallax_meta'] : array(); $original_meta_title = (string) ( $original_meta['title'] ?? '' ); $original_meta_desc = (string) ( $original_meta['description'] ?? '' ); $current_name = (string) ( $item->name ?? '' ); $current_desc = (string) ( $item->description ?? '' ); $current_meta_title = (string) get_term_meta( $term_id, '_parallax_meta_title', true ); $current_meta_desc = (string) get_term_meta( $term_id, '_parallax_meta_desc', true ); if ( null !== $original_name && $original_name !== $current_name ) { return true; } if ( null !== $original_desc && $original_desc !== $current_desc ) { return true; } if ( array() !== $snapshot ) { if ( '' === $original_desc && '' !== $current_desc ) { return true; } if ( '' === $original_meta_title && '' !== $current_meta_title ) { return true; } if ( '' === $original_meta_desc && '' !== $current_meta_desc ) { return true; } } if ( '' !== $current_meta_title && $current_meta_title !== $original_meta_title ) { return true; } if ( '' !== $current_meta_desc && $current_meta_desc !== $original_meta_desc ) { return true; } if ( array() === $snapshot && ( '' !== $current_meta_title || '' !== $current_meta_desc ) ) { return true; } return false; } }