__( 'List social boost candidates', 'ai-eshop-optimizer' ), 'description' => __( 'READ-ONLY, no AI credits. Returns the top boost-worthy content objects for social promotion, ranked by the best available demand signal: the content-vitals content_demand score and the SEO store demand scores. Each item has object_type, object_id, title, score and url. Params: limit (default 10), type (post|page|product|any). If the signal tables are absent it falls back to recently published content. Use it to pick what to promote, then act with the social publishing abilities.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => self::input_schema(), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => true, 'idempotent' => true, ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'edit_pages' ); } private static function input_schema() { return array( 'default' => array(), 'type' => 'object', 'properties' => array( 'limit' => array( 'type' => 'integer', 'default' => 10, 'minimum' => 1, 'maximum' => 100, ), 'type' => array( 'type' => 'string', 'default' => 'any', 'enum' => array( 'post', 'page', 'product', 'any' ), ), ), ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $limit = isset( $input['limit'] ) ? (int) $input['limit'] : 10; $limit = max( 1, min( 100, $limit ) ); $type = isset( $input['type'] ) ? (string) $input['type'] : 'any'; if ( ! in_array( $type, array( 'post', 'page', 'product', 'any' ), true ) ) { $type = 'any'; } $post_types = ( 'any' === $type ) ? array( 'post', 'page', 'product' ) : array( $type ); $candidates = array(); self::collect_from_vitals( $candidates, $post_types, $limit ); self::collect_from_seo( $candidates, $post_types, $limit ); if ( empty( $candidates ) ) { self::collect_recent( $candidates, $post_types, $limit ); } usort( $candidates, static function ( $a, $b ) { if ( $a['score'] === $b['score'] ) { return $b['object_id'] <=> $a['object_id']; } return ( $a['score'] < $b['score'] ) ? 1 : -1; } ); $candidates = array_slice( array_values( $candidates ), 0, $limit ); return array( 'returned' => count( $candidates ), 'candidates' => $candidates, ); } private static function upsert( &$candidates, $object_type, $object_id, $title, $score, $url ) { $object_id = (int) $object_id; if ( $object_id <= 0 ) { return; } $key = $object_type . ':' . $object_id; $score = (float) $score; if ( isset( $candidates[ $key ] ) ) { if ( $score > $candidates[ $key ]['score'] ) { $candidates[ $key ]['score'] = $score; } return; } $candidates[ $key ] = array( 'object_type' => (string) $object_type, 'object_id' => $object_id, 'title' => (string) $title, 'score' => $score, 'url' => (string) $url, ); } private static function table_exists( $table ) { global $wpdb; return (bool) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); } private static function columns( $table ) { global $wpdb; $rows = $wpdb->get_col( "SHOW COLUMNS FROM `{$table}`" ); if ( ! is_array( $rows ) ) { return array(); } return array_map( 'strtolower', $rows ); } private static function in_placeholders( array $items ) { return implode( ',', array_fill( 0, count( $items ), '%s' ) ); } private static function collect_from_vitals( &$candidates, array $post_types, $limit ) { global $wpdb; $table = $wpdb->prefix . 'aieo_core_content_vitals'; if ( ! self::table_exists( $table ) ) { return; } $cols = self::columns( $table ); if ( ! in_array( 'content_demand', $cols, true ) || ! in_array( 'content_id', $cols, true ) || ! in_array( 'post_type', $cols, true ) ) { return; } $posts = $wpdb->posts; $in = self::in_placeholders( $post_types ); $args = $post_types; $args[] = (int) ( $limit * 2 ); $sql = "SELECT v.post_type, v.content_id, p.post_title, v.content_demand AS score FROM `{$table}` v INNER JOIN `{$posts}` p ON p.ID = v.content_id AND p.post_status = 'publish' WHERE v.post_type IN ({$in}) ORDER BY v.content_demand DESC, v.content_id DESC LIMIT %d"; $rows = $wpdb->get_results( $wpdb->prepare( $sql, $args ), ARRAY_A ); if ( ! is_array( $rows ) ) { return; } foreach ( $rows as $r ) { self::upsert( $candidates, $r['post_type'], $r['content_id'], $r['post_title'], $r['score'], get_permalink( (int) $r['content_id'] ) ); } } private static function collect_from_seo( &$candidates, array $post_types, $limit ) { global $wpdb; $table = $wpdb->prefix . 'aieo_seo_posts'; if ( ! self::table_exists( $table ) ) { return; } $cols = self::columns( $table ); if ( ! in_array( 'object_type', $cols, true ) || ! in_array( 'object_id', $cols, true ) ) { return; } $score_cols = array(); foreach ( array( 'search_demand_score', 'demand_score' ) as $c ) { if ( in_array( $c, $cols, true ) ) { $score_cols[] = 's.`' . $c . '`'; } } if ( empty( $score_cols ) ) { return; } $score_expr = ( count( $score_cols ) > 1 ) ? 'GREATEST(' . implode( ',', array_map( static function ( $c ) { return 'COALESCE(' . $c . ',0)'; }, $score_cols ) ) . ')' : 'COALESCE(' . $score_cols[0] . ',0)'; $posts = $wpdb->posts; $where = 'AND s.object_type IN (' . self::in_placeholders( $post_types ) . ')'; $args = $post_types; $args[] = (int) ( $limit * 2 ); $sql = "SELECT s.object_type, s.object_id, p.post_title, {$score_expr} AS score FROM `{$table}` s INNER JOIN `{$posts}` p ON p.ID = s.object_id AND p.post_status = 'publish' WHERE 1=1 {$where} ORDER BY score DESC, s.object_id DESC LIMIT %d"; $rows = $wpdb->get_results( $wpdb->prepare( $sql, $args ), ARRAY_A ); if ( ! is_array( $rows ) ) { return; } foreach ( $rows as $r ) { self::upsert( $candidates, $r['object_type'], $r['object_id'], $r['post_title'], $r['score'], get_permalink( (int) $r['object_id'] ) ); } } private static function collect_recent( &$candidates, array $post_types, $limit ) { $query = new \WP_Query( array( 'post_type' => $post_types, 'post_status' => 'publish', 'posts_per_page' => $limit, 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); foreach ( $query->posts as $post ) { self::upsert( $candidates, $post->post_type, $post->ID, $post->post_title, 0, get_permalink( $post ) ); } } }