plugin_config = $detected; $this->meta_keys = $detected['meta_keys'] ?? []; } /** * Check for duplicate/similar meta content for a single product * * Compares provided meta title and description against existing published products * and returns top 5 most similar matches above configured thresholds. * * @param string $meta_title The meta title to check. * @param string $meta_description The meta description to check. * @param int $exclude_product_id Optional product ID to exclude from comparison. * * @return array Array of similar products with similarity scores. */ public function check_duplicates( string $meta_title, string $meta_description, int $exclude_product_id = 0 ): array { if ( empty( $meta_title ) && empty( $meta_description ) ) { return []; } if ( empty( $this->meta_keys ) ) { return []; } $candidates = $this->load_fuzzy_candidates(); $results = $this->find_matches( $meta_title, $meta_description, $exclude_product_id, $candidates ); usort( $results, function ( $a, $b ) { return $b['similarity_score'] <=> $a['similarity_score']; } ); return array_slice( $results, 0, self::MAX_RESULTS_PER_ITEM ); } /** * Check for duplicates across multiple products in batch * * Efficiently processes multiple products and returns all duplicates found. * Useful for bulk operations where multiple products need duplicate checking. * * @param array $items Array of items with 'product_id', 'title', 'description' keys. * * @return array Associative array with product_id as key and duplicate results as value. */ public function check_duplicates_batch( array $items ): array { if ( empty( $items ) ) { return []; } if ( count( $items ) > self::MAX_ITEMS_PER_BATCH ) { $items = array_slice( $items, 0, self::MAX_ITEMS_PER_BATCH ); } if ( empty( $this->meta_keys ) ) { return []; } $candidates = $this->load_fuzzy_candidates(); $batch_results = []; foreach ( $items as $item ) { $exclude_product_id = $item['product_id'] ?? 0; $meta_title = $item['title'] ?? ''; $meta_description = $item['description'] ?? ''; if ( $exclude_product_id <= 0 ) { continue; } if ( empty( $meta_title ) && empty( $meta_description ) ) { continue; } $results = $this->find_matches( $meta_title, $meta_description, (int) $exclude_product_id, $candidates ); if ( ! empty( $results ) ) { usort( $results, function ( $a, $b ) { return $b['similarity_score'] <=> $a['similarity_score']; } ); $batch_results[ $exclude_product_id ] = array_slice( $results, 0, self::MAX_RESULTS_PER_ITEM ); } } return $batch_results; } /** * Compare product meta against existing values and collect similar matches * * Extracted helper to reduce duplication between check_duplicates and check_duplicates_batch. * Compares provided meta title and description against a product's existing meta values. * * @param int $product_id The product ID to compare against. * @param string $meta_title The meta title to check. * @param string $meta_description The meta description to check. * @param array $existing Existing 'title' and 'description' values of the product. * @param int|null $exclude_product_id Optional product ID to exclude from comparison. * * @return array Array of similar matches found for this product. */ private function compareProductMeta( int $product_id, string $meta_title, string $meta_description, array $existing, ?int $exclude_product_id = null ): array { $results = []; if ( $exclude_product_id > 0 && (int) $product_id === $exclude_product_id ) { return $results; } $existing_title = $existing['title'] ?? ''; $existing_description = $existing['description'] ?? ''; if ( ! empty( $meta_title ) && ! empty( $existing_title ) ) { $title_similarity = $this->calculate_similarity( $meta_title, $existing_title ); if ( $title_similarity >= self::TITLE_THRESHOLD ) { $results[] = [ 'product_id' => $product_id, 'product_name' => get_the_title( $product_id ), 'similarity_score' => round( $title_similarity, 1 ), 'field' => 'title', 'existing_value' => $existing_title, ]; } } if ( ! empty( $meta_description ) && ! empty( $existing_description ) ) { $description_similarity = $this->calculate_similarity( $meta_description, $existing_description ); if ( $description_similarity >= self::DESCRIPTION_THRESHOLD ) { $results[] = [ 'product_id' => $product_id, 'product_name' => get_the_title( $product_id ), 'similarity_score' => round( $description_similarity, 1 ), 'field' => 'description', 'existing_value' => $existing_description, ]; } } return $results; } /** * Calculate similarity percentage between two strings * * Uses PHP's similar_text() function with percentage mode. * Normalizes strings before comparison for better accuracy. * * @param string $str1 First string to compare. * @param string $str2 Second string to compare. * * @return float Similarity percentage (0-100). */ private function calculate_similarity( string $str1, string $str2 ): float { $has_mb = function_exists( 'mb_strtolower' ); $str1 = $has_mb ? mb_strtolower( trim( $str1 ), 'UTF-8' ) : strtolower( trim( $str1 ) ); $str2 = $has_mb ? mb_strtolower( trim( $str2 ), 'UTF-8' ) : strtolower( trim( $str2 ) ); if ( $str1 === $str2 ) { return 100.0; } $len1 = $has_mb ? mb_strlen( $str1, 'UTF-8' ) : strlen( $str1 ); $len2 = $has_mb ? mb_strlen( $str2, 'UTF-8' ) : strlen( $str2 ); if ( $len1 > 0 && $len2 > 0 ) { $length_ratio = min( $len1, $len2 ) / max( $len1, $len2 ); if ( $length_ratio < 0.5 ) { return $length_ratio * 100; } } if ( $len1 > self::MAX_SIMILARITY_LENGTH || $len2 > self::MAX_SIMILARITY_LENGTH ) { $trunc1 = $has_mb ? mb_substr( $str1, 0, self::MAX_SIMILARITY_LENGTH, 'UTF-8' ) : substr( $str1, 0, self::MAX_SIMILARITY_LENGTH ); $trunc2 = $has_mb ? mb_substr( $str2, 0, self::MAX_SIMILARITY_LENGTH, 'UTF-8' ) : substr( $str2, 0, self::MAX_SIMILARITY_LENGTH ); $similarity = 0.0; similar_text( $trunc1, $trunc2, $similarity ); return $similarity; } $similarity = 0.0; similar_text( $str1, $str2, $similarity ); return $similarity; } /** * Collect exact and similar matches for one title and description * * Exact matches come from the whole catalog via SQL. The fuzzy pass only * covers the bounded candidate set. A product and field pair is reported once. * * @param string $meta_title The meta title to check. * @param string $meta_description The meta description to check. * @param int $exclude_product_id Product ID to exclude from comparison. * @param array $candidates Map of product ID to 'title' and 'description'. * * @return array Matches, unsorted. */ private function find_matches( string $meta_title, string $meta_description, int $exclude_product_id, array $candidates ): array { $results = []; $seen = []; foreach ( $this->find_exact_matches( $meta_title, $meta_description, $exclude_product_id ) as $match ) { $seen[ $match['product_id'] . ':' . $match['field'] ] = true; $results[] = $match; } foreach ( $candidates as $product_id => $existing ) { foreach ( $this->compareProductMeta( (int) $product_id, $meta_title, $meta_description, $existing, $exclude_product_id ) as $match ) { if ( isset( $seen[ $match['product_id'] . ':' . $match['field'] ] ) ) { continue; } $results[] = $match; } } return $results; } /** * Find published products whose stored value equals the given one * * The database answers value equality, so the whole catalog is covered * without loading rows into PHP. Plugins that keep values in post meta are * matched on their meta keys; All in One SEO is matched in its own table. * * @param string $meta_title The meta title to check. * @param string $meta_description The meta description to check. * @param int $exclude_product_id Product ID to exclude. * * @return array Exact matches with a similarity score of 100. */ private function find_exact_matches( string $meta_title, string $meta_description, int $exclude_product_id ): array { global $wpdb; $fields = [ 'title' => trim( $meta_title ), 'description' => trim( $meta_description ), ]; $is_aioseo = 'aioseo' === ( $this->plugin_config['plugin_slug'] ?? '' ); $results = []; foreach ( $fields as $field => $value ) { if ( '' === $value ) { continue; } if ( $is_aioseo ) { $column = 'title' === $field ? 'title' : 'description'; $query = $wpdb->prepare( "SELECT p.ID AS product_id, a.{$column} AS value FROM {$wpdb->posts} p INNER JOIN {$wpdb->prefix}aioseo_posts a ON a.post_id = p.ID WHERE p.post_type = 'product' AND p.post_status = 'publish' AND p.ID <> %d AND TRIM(a.{$column}) = %s ORDER BY p.ID ASC LIMIT %d", $exclude_product_id, $value, self::MAX_RESULTS_PER_ITEM ); } else { $meta_key = $this->meta_keys[ $field . '_key' ] ?? ''; if ( '' === $meta_key ) { continue; } $query = $wpdb->prepare( "SELECT p.ID AS product_id, pm.meta_value AS value FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID WHERE p.post_type = 'product' AND p.post_status = 'publish' AND p.ID <> %d AND pm.meta_key = %s AND TRIM(pm.meta_value) = %s ORDER BY p.ID ASC LIMIT %d", $exclude_product_id, $meta_key, $value, self::MAX_RESULTS_PER_ITEM ); } $rows = $wpdb->get_results( $query, 'ARRAY_A' ); foreach ( is_array( $rows ) ? $rows : [] as $row ) { $results[] = [ 'product_id' => (int) $row['product_id'], 'product_name' => get_the_title( (int) $row['product_id'] ), 'similarity_score' => 100.0, 'field' => $field, 'existing_value' => (string) $row['value'], ]; } } return $results; } /** * Load the SEO title and description of the most recently modified products * * One query reads just the two values per product for a deterministic, * bounded set, so the similar_text() pass cannot grow with the catalog. * Post meta plugins are read from post meta; All in One SEO from its table. * * @return array Map of product ID to array with 'title' and 'description'. */ private function load_fuzzy_candidates(): array { global $wpdb; if ( 'aioseo' === ( $this->plugin_config['plugin_slug'] ?? '' ) ) { $query = $wpdb->prepare( "SELECT p.ID AS product_id, a.title AS title, a.description AS description FROM {$wpdb->posts} p INNER JOIN {$wpdb->prefix}aioseo_posts a ON a.post_id = p.ID WHERE p.post_type = 'product' AND p.post_status = 'publish' AND ( COALESCE(a.title, '') <> '' OR COALESCE(a.description, '') <> '' ) ORDER BY p.post_modified_gmt DESC, p.ID DESC LIMIT %d", self::MAX_FUZZY_CANDIDATES ); } else { $title_key = $this->meta_keys['title_key'] ?? ''; $description_key = $this->meta_keys['description_key'] ?? ''; if ( '' === $title_key || '' === $description_key ) { return []; } $query = $wpdb->prepare( "SELECT p.ID AS product_id, t.meta_value AS title, d.meta_value AS description FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} t ON t.post_id = p.ID AND t.meta_key = %s LEFT JOIN {$wpdb->postmeta} d ON d.post_id = p.ID AND d.meta_key = %s WHERE p.post_type = 'product' AND p.post_status = 'publish' AND ( COALESCE(t.meta_value, '') <> '' OR COALESCE(d.meta_value, '') <> '' ) ORDER BY p.post_modified_gmt DESC, p.ID DESC LIMIT %d", $title_key, $description_key, self::MAX_FUZZY_CANDIDATES ); } $rows = $wpdb->get_results( $query, 'ARRAY_A' ); $candidates = []; foreach ( is_array( $rows ) ? $rows : [] as $row ) { $product_id = (int) $row['product_id']; if ( isset( $candidates[ $product_id ] ) ) { continue; } $candidates[ $product_id ] = [ 'title' => (string) ( $row['title'] ?? '' ), 'description' => (string) ( $row['description'] ?? '' ), ]; } return $candidates; } }