logger = $logger; } /** * Return related post IDs + scores for a source post. * * @return array list of ['post_id'=>int,'score'=>float] */ public function get_related( $source_post_id, $max_items = 3 ) { global $wpdb; $t_index = esc_sql( AIT_AIRCE_DB::table( 'index_item' ) ); $t_embed = esc_sql( AIT_AIRCE_DB::table( 'embedding' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table names from trusted source $source_row = $wpdb->get_row( $wpdb->prepare( "SELECT i.id as index_item_id, i.post_type, e.vector_json, e.vector_norm FROM {$t_index} i INNER JOIN {$t_embed} e ON e.index_item_id = i.id WHERE i.post_id = %d AND i.status = 'active'", $source_post_id ), ARRAY_A ); if ( ! $source_row ) { return []; } $source_vec = json_decode( $source_row['vector_json'], true ); if ( ! is_array( $source_vec ) ) { return []; } $source_norm = floatval( $source_row['vector_norm'] ); $post_type = $source_row['post_type']; // Allow Pro to broaden (or restrict) which post types are eligible. $allowed_post_types = apply_filters( 'ait_airce/recommender_allowed_post_types', [ $post_type ], $source_post_id, $post_type ); $allowed_post_types = is_array( $allowed_post_types ) ? array_values( array_filter( array_map( 'sanitize_key', $allowed_post_types ) ) ) : [ $post_type ]; if ( empty( $allowed_post_types ) ) { $allowed_post_types = [ $post_type ]; } // Build dynamic IN() clause for eligible post types. $placeholders = implode( ',', array_fill( 0, count( $allowed_post_types ), '%s' ) ); $sql = "SELECT i.post_id, i.post_type, e.vector_json, e.vector_norm FROM {$t_index} i INNER JOIN {$t_embed} e ON e.index_item_id = i.id WHERE i.status = 'active' AND i.post_type IN ({$placeholders}) AND i.post_id <> %d"; $params = array_merge( $allowed_post_types, [ $source_post_id ] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe dynamic IN() clause with placeholders $candidates = $wpdb->get_results( $wpdb->prepare( $sql, $params ), ARRAY_A ); if ( empty( $candidates ) ) { return []; } $prefer_cat = (bool) AIT_AIRCE_Settings::get( 'prefer_same_category' ); $source_cats = []; if ( $prefer_cat && $post_type === 'post' ) { $source_cats = wp_get_post_categories( $source_post_id ); $source_cats = array_map( 'intval', (array) $source_cats ); } // Minimum similarity score (Pro can set via filter or settings). $min_score = floatval( apply_filters( 'ait_airce/recommender_min_score', 0.0, $source_post_id, $post_type, $allowed_post_types ) ); // Allow integrations/Pro to filter candidate IDs before scoring. $candidate_ids = array_map( function( $row ) { return isset( $row['post_id'] ) ? (int) $row['post_id'] : 0; }, (array) $candidates ); $candidate_ids = array_values( array_filter( array_map( 'intval', $candidate_ids ) ) ); $candidate_ids = apply_filters( 'ait_airce/recommender_candidate_filter', $candidate_ids, $source_post_id, $post_type, $allowed_post_types ); $candidate_ids = is_array( $candidate_ids ) ? array_values( array_filter( array_map( 'intval', $candidate_ids ) ) ) : []; if ( empty( $candidate_ids ) ) { return []; } $candidate_lookup = array_fill_keys( $candidate_ids, true ); $scored = []; foreach ( $candidates as $c ) { $cid = isset( $c['post_id'] ) ? (int) $c['post_id'] : 0; if ( $cid <= 0 || empty( $candidate_lookup[ $cid ] ) ) { continue; } // Allow Pro/integrations to exclude candidates (exclusions, taxonomy locks, etc.). $skip = (bool) apply_filters( 'ait_airce/recommender_skip_candidate', false, $cid, $source_post_id, $post_type, ( isset( $c['post_type'] ) ? sanitize_key( $c['post_type'] ) : '' ) ); if ( $skip ) { continue; } $vec = json_decode( $c['vector_json'], true ); if ( ! is_array( $vec ) ) { continue; } $score = $this->cosine_similarity( $source_vec, $source_norm, $vec, floatval( $c['vector_norm'] ) ); // Allow Pro/integrations to adjust scores (e.g. prefer same variation). $score = (float) apply_filters( 'ait_airce/recommender_score_adjust', $score, $source_post_id, $cid, $post_type, ( isset( $c['post_type'] ) ? sanitize_key( $c['post_type'] ) : '' ) ); // Soft category preference: small boost if share a category. if ( $prefer_cat && $post_type === 'post' && ! empty( $source_cats ) ) { $cats = wp_get_post_categories( intval( $c['post_id'] ) ); if ( array_intersect( $source_cats, array_map( 'intval', (array) $cats ) ) ) { $score += 0.02; } } if ( $min_score > 0 && $score < $min_score ) { continue; } $scored[] = [ 'post_id' => $cid, 'score' => round( $score, 6 ), 'post_type' => isset( $c['post_type'] ) ? sanitize_key( $c['post_type'] ) : '', ]; } usort( $scored, function( $a, $b ) { if ( $a['score'] === $b['score'] ) { return 0; } return ( $a['score'] > $b['score'] ) ? -1 : 1; } ); $max_items = max( 0, intval( $max_items ) ); $result = array_slice( $scored, 0, $max_items ); // Allow Pro to fill in missing items (smart fallback). if ( $max_items > 0 && count( $result ) < $max_items ) { $result = apply_filters( 'ait_airce/recommender_fallback', $result, $source_post_id, $max_items, $post_type, $allowed_post_types ); if ( ! is_array( $result ) ) { $result = []; } $result = array_slice( array_values( $result ), 0, $max_items ); } return $result; } protected function cosine_similarity( $a, $a_norm, $b, $b_norm ) { if ( empty( $a_norm ) || empty( $b_norm ) ) { return 0.0; } $dot = 0.0; $len = min( count( $a ), count( $b ) ); for ( $i = 0; $i < $len; $i++ ) { $dot += floatval( $a[$i] ) * floatval( $b[$i] ); } return $dot / ( $a_norm * $b_norm ); } }