$keywords, 'gsc_queries' => $gsc, 'entities' => $entities, 'salient' => $salient, 'cluster' => $cluster, 'description' => '', 'has_semantic' => ( ! empty( $entities ) || ! empty( $salient ) ), 'has_gsc' => ! empty( $gsc ), ); $out['description'] = self::lexical_description( $object_type, $object_id, $out ); self::$memo[ $mk ] = $out; return $out; } public static function prompt_lines( array $cues ) { $lines = array(); if ( ! empty( $cues['gsc_queries'] ) ) { $q = array(); foreach ( array_slice( $cues['gsc_queries'], 0, 8 ) as $g ) { $q[] = $g['query'] . ' (' . (int) $g['impressions'] . ' impr, pos ' . number_format( (float) $g['position'], 1 ) . ')'; } $lines[] = 'REAL GOOGLE QUERIES THIS PAGE ALREADY RANKS FOR (Search Console — write to capture these): ' . implode( '; ', $q ) . '.'; } if ( ! empty( $cues['entities'] ) ) { $lines[] = 'KEY ENTITIES (brands/categories/named units from the catalogue — use the exact forms): ' . implode( ', ', array_slice( $cues['entities'], 0, 10 ) ) . '.'; } if ( ! empty( $cues['salient'] ) ) { $lines[] = 'SALIENT TERMS (what the page is most about, by weight): ' . implode( ', ', array_slice( $cues['salient'], 0, 15 ) ) . '.'; } if ( '' !== $cues['cluster'] ) { $lines[] = 'TOPIC CLUSTER: ' . $cues['cluster'] . '.'; } return $lines; } private static function gsc_queries( $object_type, $object_id ) { global $wpdb; $t = $wpdb->prefix . 'aieo_gsc_daily'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return array(); } $cutoff = gmdate( 'Y-m-d', time() - 180 * ( defined( 'DAY_IN_SECONDS' ) ? DAY_IN_SECONDS : 86400 ) ); $page_demand = $wpdb->get_row( $wpdb->prepare( "SELECT SUM(clicks) clicks, SUM(impressions) impressions FROM `{$t}` WHERE dim_type = 'page' AND object_type = %s AND object_id = %d AND `date` >= %s", $object_type, $object_id, $cutoff ), ARRAY_A ); if ( empty( $page_demand['impressions'] ) ) { return array(); } $title = function_exists( 'mb_strtolower' ) ? mb_strtolower( get_the_title( $object_id ), 'UTF-8' ) : strtolower( get_the_title( $object_id ) ); $tokens = array_values( array_filter( preg_split( '/\s+/u', (string) $title ), static function ( $w ) { return mb_strlen( $w ) >= 4; } ) ); if ( empty( $tokens ) ) { return array(); } $like = array(); $args = array( $cutoff ); foreach ( array_slice( $tokens, 0, 6 ) as $tok ) { $like[] = 'dim_value LIKE %s'; $args[] = '%' . $wpdb->esc_like( $tok ) . '%'; } $rows = $wpdb->get_results( $wpdb->prepare( "SELECT dim_value, SUM(clicks) clicks, SUM(impressions) impressions, SUM(position * impressions) / NULLIF(SUM(impressions),0) position FROM `{$t}` WHERE dim_type = 'query' AND `date` >= %s AND ( " . implode( ' OR ', $like ) . " ) GROUP BY dim_value ORDER BY impressions DESC LIMIT 10", $args ), ARRAY_A ); $out = array(); foreach ( (array) $rows as $r ) { $out[] = array( 'query' => (string) $r['dim_value'], 'clicks' => (int) $r['clicks'], 'impressions' => (int) $r['impressions'], 'position' => (float) $r['position'], ); } return $out; } private static function semantic_terms( $object_type, $object_id, array &$entities, array &$salient, &$cluster ) { global $wpdb; $postings = $wpdb->prefix . 'aieo_sem_postings'; $terms = $wpdb->prefix . 'aieo_sem_terms'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $postings ) ) !== $postings || (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $terms ) ) !== $terms ) { return; } $n = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$wpdb->prefix}aieo_sem_objects`" ); $n = max( 1, $n ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT t.term, t.is_entity, t.doc_count, p.weight * LN(1 + %d / GREATEST(t.doc_count,1)) AS salience FROM `{$postings}` p JOIN `{$terms}` t ON t.id = p.term_id WHERE p.object_type = %s AND p.object_id = %d AND COALESCE(p.covered,0) = 0 AND t.is_stopword = 0 AND t.is_excluded = 0 ORDER BY salience DESC LIMIT 40", $n, $object_type, $object_id ), ARRAY_A ); foreach ( (array) $rows as $r ) { $term = (string) $r['term']; if ( ! empty( $r['is_entity'] ) ) { if ( count( $entities ) < 10 ) { $entities[] = $term; } } elseif ( count( $salient ) < 20 ) { $salient[] = $term; } } $clusters = $wpdb->prefix . 'aieo_sem_clusters'; $members = $wpdb->prefix . 'aieo_sem_cluster_members'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $members ) ) === $members ) { $cluster = (string) $wpdb->get_var( $wpdb->prepare( "SELECT c.label FROM `{$members}` m JOIN `{$clusters}` c ON c.cluster_key = m.cluster_key WHERE m.member_type = %s AND m.member_id = %d ORDER BY m.weight DESC LIMIT 1", $object_type, $object_id ) ); } } private static function lexical_description( $object_type, $object_id, array $cues ) { $name = trim( wp_strip_all_tags( get_the_title( $object_id ) ) ); if ( '' === $name ) { return ''; } $bits = array(); $lead = array_slice( array_merge( $cues['entities'], $cues['salient'] ), 0, 4 ); if ( ! empty( $lead ) ) { $bits[] = implode( ', ', $lead ); } $excerpt = wp_strip_all_tags( (string) get_post_field( 'post_excerpt', $object_id ) ); if ( '' !== $excerpt ) { $bits[] = $excerpt; } $desc = $name; if ( ! empty( $bits ) ) { $desc .= ' — ' . implode( '. ', $bits ); } $desc = preg_replace( '/\s+/u', ' ', trim( $desc ) ); if ( mb_strlen( $desc ) > 158 ) { $desc = rtrim( mb_substr( $desc, 0, 155 ) ) . '…'; } return $desc; } public static function keywords_from_json( $json ) { $json = (string) $json; if ( '' === $json ) { return array(); } $data = json_decode( $json, true ); if ( ! is_array( $data ) || empty( $data['additional'] ) || ! is_array( $data['additional'] ) ) { return array(); } $out = array(); foreach ( $data['additional'] as $a ) { $kw = is_array( $a ) ? (string) ( $a['keyphrase'] ?? '' ) : (string) $a; $kw = trim( $kw ); if ( '' !== $kw ) { $out[] = $kw; } } return $out; } public static function keywords_to_json( $json, array $keywords ) { $data = json_decode( (string) $json, true ); if ( ! is_array( $data ) ) { $data = array( 'focus' => array( 'keyphrase' => '' ) ); } $additional = array(); foreach ( $keywords as $kw ) { $kw = trim( (string) $kw ); if ( '' !== $kw ) { $additional[] = array( 'keyphrase' => $kw ); } } $data['additional'] = $additional; return wp_json_encode( $data ); } public static function reset_memo() { self::$memo = array(); } }