*/ public static function facet_params(): array { /** * Filter the faceted-navigation parameters watched for index bloat. * * @param array $params Query parameter names. */ return (array) apply_filters( 'airanksuite/store/facet_params', array( 'filter_', // WooCommerce layered nav, prefix match. 'query_type_', // WooCommerce layered nav operator. 'min_price', 'max_price', 'rating_filter', 'orderby', 'product_orderby', 'product_count', 'stock_status', 'on_sale', 'swoof', // WOOF / Product Filter. 'really_curr_tax', '_pjax', ) ); } /** * Run the audit. * * @param bool $fresh Skip the cache. * @return array */ public static function run( bool $fresh = false ): array { if ( ! self::active() ) { return array( 'active' => false ); } if ( ! $fresh ) { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } } global $wpdb; // One pass over published products. Every join is on (post_id, // meta_key), which postmeta is already indexed for. // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $totals = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS products, SUM( sku.meta_value IS NULL OR sku.meta_value = '' ) AS no_sku, SUM( gtin.meta_value IS NULL OR gtin.meta_value = '' ) AS no_gtin, SUM( price.meta_value IS NULL OR price.meta_value = '' ) AS no_price, SUM( img.meta_value IS NULL ) AS no_image, SUM( stock.meta_value = 'outofstock' ) AS out_of_stock, SUM( CHAR_LENGTH( p.post_content ) < 200 ) AS thin, SUM( CHAR_LENGTH( p.post_excerpt ) < 20 ) AS no_short_desc, SUM( d.meta_value IS NULL OR d.meta_value = '' ) AS no_meta_desc FROM %i p LEFT JOIN %i sku ON sku.post_id = p.ID AND sku.meta_key = '_sku' LEFT JOIN %i gtin ON gtin.post_id = p.ID AND gtin.meta_key = '_global_unique_id' LEFT JOIN %i price ON price.post_id = p.ID AND price.meta_key = '_price' LEFT JOIN %i img ON img.post_id = p.ID AND img.meta_key = '_thumbnail_id' LEFT JOIN %i stock ON stock.post_id = p.ID AND stock.meta_key = '_stock_status' LEFT JOIN %i d ON d.post_id = p.ID AND d.meta_key = '_airanksuite_seo_desc' WHERE p.post_type = 'product' AND p.post_status = 'publish'", $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta, $wpdb->postmeta, $wpdb->postmeta, $wpdb->postmeta, $wpdb->postmeta ), ARRAY_A ); // Out-of-stock products that are still indexable — the dead ends. $stranded = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i p INNER JOIN %i stock ON stock.post_id = p.ID AND stock.meta_key = '_stock_status' LEFT JOIN %i r ON r.post_id = p.ID AND r.meta_key = '_airanksuite_robots' WHERE p.post_type = 'product' AND p.post_status = 'publish' AND stock.meta_value = 'outofstock' AND ( r.meta_value IS NULL OR r.meta_value NOT LIKE %s )", $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta, '%' . $wpdb->esc_like( 'noindex' ) . '%' ) ); // The worst offenders, so the numbers can be turned into a work list. $samples = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_title, CHAR_LENGTH( p.post_content ) AS body, sku.meta_value AS sku, gtin.meta_value AS gtin FROM %i p LEFT JOIN %i sku ON sku.post_id = p.ID AND sku.meta_key = '_sku' LEFT JOIN %i gtin ON gtin.post_id = p.ID AND gtin.meta_key = '_global_unique_id' WHERE p.post_type = 'product' AND p.post_status = 'publish' AND CHAR_LENGTH( p.post_content ) < 200 ORDER BY body ASC LIMIT 20", $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta ), ARRAY_A ); // phpcs:enable $categories = self::categories(); $facets = self::facets(); $products = (int) ( $totals['products'] ?? 0 ); $result = array( 'active' => true, 'products' => $products, 'counts' => array( 'no_sku' => (int) ( $totals['no_sku'] ?? 0 ), 'no_gtin' => (int) ( $totals['no_gtin'] ?? 0 ), 'no_price' => (int) ( $totals['no_price'] ?? 0 ), 'no_image' => (int) ( $totals['no_image'] ?? 0 ), 'out_of_stock' => (int) ( $totals['out_of_stock'] ?? 0 ), 'stranded' => $stranded, 'thin' => (int) ( $totals['thin'] ?? 0 ), 'no_short_desc' => (int) ( $totals['no_short_desc'] ?? 0 ), 'no_meta_desc' => (int) ( $totals['no_meta_desc'] ?? 0 ), ), 'thin_list' => array_map( static fn( array $r ): array => array( 'id' => (int) $r['ID'], 'title' => (string) $r['post_title'], 'body' => (int) $r['body'], 'sku' => (string) ( $r['sku'] ?? '' ), ), (array) $samples ), 'categories' => $categories, 'facets' => $facets, 'issues' => self::issues( $products, $totals, $stranded, $categories, $facets ), ); set_transient( self::CACHE, $result, 15 * MINUTE_IN_SECONDS ); return $result; } /** * Product categories, and whether they have anything to rank with. * * @return array */ private static function categories(): array { $terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'number' => 500, ) ); if ( is_wp_error( $terms ) ) { return array( 'total' => 0, 'empty' => 0, 'no_text' => 0, 'rows' => array() ); } $rows = array(); $no_text = 0; $empty = 0; foreach ( $terms as $term ) { $text = trim( wp_strip_all_tags( (string) $term->description ) ); $has = mb_strlen( $text ) >= 120; if ( ! $has ) { ++$no_text; } if ( 0 === (int) $term->count ) { ++$empty; } if ( ! $has && (int) $term->count > 0 ) { $rows[] = array( 'id' => (int) $term->term_id, 'name' => (string) $term->name, 'products' => (int) $term->count, 'words' => str_word_count( $text ), 'url' => (string) get_term_link( $term ), ); } } // Biggest categories first: a description on the category with four // hundred products is worth four hundred times the one with one. usort( $rows, static fn( $a, $b ) => $b['products'] <=> $a['products'] ); return array( 'total' => count( $terms ), 'empty' => $empty, 'no_text' => $no_text, 'rows' => array_slice( $rows, 0, 20 ), ); } /** * Faceted navigation exposure. * * The count is the honest part: it is not "how many junk URLs exist", * because nobody can know that without crawling. It is how many filter * parameters are in play and whether anything is stopping them being * indexed — which is the decision the site owner has to make. * * @return array */ private static function facets(): array { $attributes = function_exists( 'wc_get_attribute_taxonomies' ) ? (array) wc_get_attribute_taxonomies() : array(); $robots = self::robots_body(); $guarded = array(); $open = array(); foreach ( self::facet_params() as $param ) { // A Disallow naming the parameter, in any group. Crude on purpose: // robots.txt has no formal grammar for query strings and every // real-world file expresses this differently. if ( '' !== $robots && ( str_contains( $robots, $param ) ) ) { $guarded[] = $param; } else { $open[] = $param; } } // The combinatorial reality, stated once rather than implied. $count = count( $attributes ); $combos = $count > 0 ? ( 2 ** min( $count, 12 ) ) - 1 : 0; return array( 'attributes' => array_map( static fn( $a ): array => array( 'name' => (string) ( $a->attribute_label ?? $a->attribute_name ?? '' ), 'slug' => (string) ( $a->attribute_name ?? '' ), ), $attributes ), 'guarded' => $guarded, 'open' => $open, 'combos' => $combos, 'has_robots' => '' !== $robots, ); } private static function robots_body(): string { $response = wp_remote_get( home_url( '/robots.txt' ), array( 'timeout' => 10 ) ); if ( is_wp_error( $response ) || (int) wp_remote_retrieve_response_code( $response ) >= 400 ) { return ''; } return (string) wp_remote_retrieve_body( $response ); } /** * The findings, ordered by what they cost. * * @param array|null $totals Product totals. * @param array $categories Category summary. * @param array $facets Facet summary. * @return array> */ private static function issues( int $products, ?array $totals, int $stranded, array $categories, array $facets ): array { $out = array(); if ( 0 === $products ) { return $out; } $share = static fn( int $n ): float => round( 100 * $n / max( 1, $products ), 1 ); if ( count( $facets['open'] ) > 0 && count( $facets['attributes'] ) > 0 ) { $out[] = array( 'level' => 'error', 'title' => __( 'Filter URLs are not blocked from crawling', 'ai-rank-suite' ), 'text' => sprintf( /* translators: 1: number of attributes, 2: number of combinations. */ __( 'You have %1$d filter attributes, which can produce more than %2$s crawlable filter URLs — every one a near-duplicate of a category page. Nothing in robots.txt is stopping them. This is the most damaging technical problem a WooCommerce site can have, and it is invisible from the admin because none of those URLs exist until a crawler follows a filter link.', 'ai-rank-suite' ), count( $facets['attributes'] ), number_format_i18n( $facets['combos'] ) ), 'fix' => 'crawl', ); } if ( $stranded > 0 ) { $out[] = array( 'level' => $share( $stranded ) > 10 ? 'error' : 'warn', 'title' => sprintf( /* translators: %d: number of products. */ _n( '%d out-of-stock product is still indexable', '%d out-of-stock products are still indexable', $stranded, 'ai-rank-suite' ), $stranded ), 'text' => __( 'A discontinued product that keeps ranking sends buyers to a dead end. Google reads that as a bad result and demotes the category with it. Either bring them back, redirect them to the replacement, or set them to noindex.', 'ai-rank-suite' ), 'fix' => 'content', ); } $no_gtin = (int) ( $totals['no_gtin'] ?? 0 ); if ( $share( $no_gtin ) > 50 ) { $out[] = array( 'level' => 'warn', 'title' => sprintf( /* translators: %s: percentage. */ __( '%s%% of products have no GTIN', 'ai-rank-suite' ), $share( $no_gtin ) ), 'text' => __( 'Without a GTIN or MPN, Google cannot match your listing to the same product on other sites — so you compete as an unknown item against sellers it can match, and you are excluded from several shopping surfaces entirely.', 'ai-rank-suite' ), 'fix' => 'products', ); } $thin = (int) ( $totals['thin'] ?? 0 ); if ( $share( $thin ) > 25 ) { $out[] = array( 'level' => 'warn', 'title' => sprintf( /* translators: 1: count, 2: percentage. */ __( '%1$d products have almost no description (%2$s%%)', 'ai-rank-suite' ), $thin, $share( $thin ) ), 'text' => __( 'A manufacturer paragraph reproduced on four thousand sites is the definition of duplicate content. It is also the highest-volume job the AI autopilot can usefully do.', 'ai-rank-suite' ), 'fix' => 'autopilot', ); } if ( ( $categories['no_text'] ?? 0 ) > 0 ) { $out[] = array( 'level' => 'warn', 'title' => sprintf( /* translators: %d: number of categories. */ _n( '%d product category has no text', '%d product categories have no text', (int) $categories['no_text'], 'ai-rank-suite' ), (int) $categories['no_text'] ), 'text' => __( 'A shop archive is often the page with the best commercial intent and the least content on the whole site. A category with four hundred products earns four hundred times the return on a description that the one-product category does.', 'ai-rank-suite' ), 'fix' => 'categories', ); } $no_image = (int) ( $totals['no_image'] ?? 0 ); if ( $no_image > 0 ) { $out[] = array( 'level' => 'warn', 'title' => sprintf( /* translators: %d: number of products. */ _n( '%d product has no image', '%d products have no image', $no_image, 'ai-rank-suite' ), $no_image ), 'text' => __( 'No image means no Google Shopping listing, no image in the rich result, and a grey box wherever the link is shared.', 'ai-rank-suite' ), 'fix' => 'products', ); } return $out; } public static function active(): bool { return class_exists( 'WooCommerce' ); } public static function flush(): void { delete_transient( self::CACHE ); } }