__( 'List pages with open SEO fixes', 'ai-eshop-optimizer' ), 'description' => __( 'READ-ONLY, no AI credits. Returns the highest-ROI pages that have open SEO fixes (fix_count > 0), ranked by real demand DESC then SEO score ASC — a high-demand page with a poor score sits first. Each item includes object_type, object_id, title, url, demand, seo_score, open_fixes, is_pillar, and has_pending_draft (whether an AI title/description draft is already awaiting approval). Also returns a store-wide summary (pages needing work, total open fixes, pillar count). Use this FIRST to answer "which pages need fixing" / "fix my top N pages" before calling aieo/seo-fix-page.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'properties' => array( 'limit' => array( 'type' => 'integer', 'default' => 20, 'minimum' => 1, 'maximum' => 100 ), 'pillars_only' => array( 'type' => 'boolean', 'default' => false, 'description' => 'Only cornerstone/pillar pages.' ), 'min_fixes' => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1, 'description' => 'Minimum open fixes per page.' ), ), ), '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( 'manage_woocommerce' ); } public static function execute( $input ) { global $wpdb; $input = is_array( $input ) ? $input : array(); $limit = min( 100, max( 1, (int) ( $input['limit'] ?? 20 ) ) ); $min_fixes = max( 1, (int) ( $input['min_fixes'] ?? 1 ) ); $pillars_only = ! empty( $input['pillars_only'] ); $sc = $wpdb->prefix . 'aieo_seo_scores'; $sp = $wpdb->prefix . 'aieo_seo_posts'; if ( (string) $wpdb->get_var( "SHOW TABLES LIKE '{$sc}'" ) !== $sc ) { return new \WP_Error( 'aieo_seo_off', __( 'The SEO engine has not scored this site yet (no wp_aieo_seo_scores table). Enable it in SEO → General.', 'ai-eshop-optimizer' ) ); } $pillars = $pillars_only ? ' AND s.is_pillar = 1' : ''; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT s.object_type, s.object_id, s.score, s.fix_count, s.is_pillar, COALESCE( p.demand_score, 0 ) AS demand, COALESCE( p.ai_state, 'none' ) AS ai_state, wp.post_title FROM `{$sc}` s INNER JOIN {$wpdb->posts} wp ON wp.ID = s.object_id AND wp.post_type = s.object_type LEFT JOIN `{$sp}` p ON p.object_type = s.object_type AND p.object_id = s.object_id WHERE wp.post_status = 'publish' AND s.fix_count >= %d{$pillars} ORDER BY demand DESC, s.score ASC LIMIT %d", $min_fixes, $limit ), ARRAY_A ); $rows = is_array( $rows ) ? $rows : array(); $items = array(); foreach ( $rows as $r ) { $oid = (int) $r['object_id']; $items[] = array( 'object_type' => (string) $r['object_type'], 'object_id' => $oid, 'title' => (string) $r['post_title'], 'url' => (string) get_permalink( $oid ), 'demand' => round( (float) $r['demand'], 2 ), 'seo_score' => (int) $r['score'], 'open_fixes' => (int) $r['fix_count'], 'is_pillar' => (bool) (int) $r['is_pillar'], 'has_pending_draft' => 'suggested' === (string) $r['ai_state'], ); } $summary = $wpdb->get_row( "SELECT COUNT(*) AS pages_needing_work, COALESCE( SUM( s.fix_count ), 0 ) AS total_open_fixes, COALESCE( SUM( s.is_pillar = 1 ), 0 ) AS pillar_pages FROM `{$sc}` s INNER JOIN {$wpdb->posts} wp ON wp.ID = s.object_id AND wp.post_type = s.object_type WHERE wp.post_status = 'publish' AND s.fix_count > 0", ARRAY_A ); return array( 'summary' => is_array( $summary ) ? array_map( 'intval', $summary ) : array(), 'returned' => count( $items ), 'items' => $items, 'next_action' => __( 'To fix one, call aieo/seo-fix-page with its object_type + object_id. That step generates SERP-aware copy through the AI service and consumes credits — confirm with the merchant before running a batch.', 'ai-eshop-optimizer' ), ); } } class SeoFixPageAbility { const ABILITY_NAME = 'aieo/seo-fix-page'; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Fix one page’s SEO title & description (AI)', 'ai-eshop-optimizer' ), 'description' => __( 'GENERATIVE — CONSUMES AI CREDITS. For ONE page, writes an SEO title + meta description tuned to the live Google SERP for the page’s target keyword and its conversion goal, via the AI service. By default (apply=false) it DRAFTS the copy for the merchant to review (returns before/after); pass apply=true to publish it live immediately. Returns status ("drafted" | "applied" | "skipped"), the before/after title & description, and the target keyphrase. Because each call costs credits, confirm with the merchant before fixing more than a few pages. Get candidates from aieo/seo-list-fixes.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'required' => array( 'object_type', 'object_id' ), 'properties' => array( 'object_type' => array( 'type' => 'string', 'description' => 'e.g. product, post, page.' ), 'object_id' => array( 'type' => 'integer', 'minimum' => 1 ), 'apply' => array( 'type' => 'boolean', 'default' => false, 'description' => 'false = draft for review (default); true = publish the fix live.' ), ), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => false, 'idempotent' => false, 'destructive' => false ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $type = isset( $input['object_type'] ) ? sanitize_key( (string) $input['object_type'] ) : ''; $id = isset( $input['object_id'] ) ? (int) $input['object_id'] : 0; $apply = ! empty( $input['apply'] ); if ( '' === $type || $id <= 0 ) { return new \WP_Error( 'aieo_bad_input', __( 'object_type and object_id are required.', 'ai-eshop-optimizer' ) ); } $post = get_post( $id ); if ( ! $post || $post->post_type !== $type || 'publish' !== $post->post_status ) { return new \WP_Error( 'aieo_no_post', __( 'No published page found for that object_type / object_id.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_SEO_Write_Queue' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-seo-write-queue.php'; } if ( ! class_exists( 'AIEO_SEO_Write_Queue' ) || ! \AIEO_SEO_Write_Queue::available() ) { return new \WP_Error( 'aieo_unavailable', __( 'The AI write queue is unavailable — enable the SEO engine (SEO → General) and configure the Content Connector token in the Rooster One Pro.', 'ai-eshop-optimizer' ) ); } $before = class_exists( 'AIEO_SEO_Store' ) ? (array) \AIEO_SEO_Store::get( $type, $id ) : array(); if ( ! empty( $before['ai_locked'] ) ) { return new \WP_Error( 'aieo_ai_locked', __( 'This page is locked against AI writes, so its SEO title and description were left unchanged and no AI credits were used.', 'ai-eshop-optimizer' ) ); } $result = \AIEO_SEO_Write_Queue::process( $type, $id ); if ( 0 === strpos( $result, 'skipped:' ) ) { return array( 'status' => 'skipped', 'reason' => substr( $result, 8 ), 'object_type' => $type, 'object_id' => $id, ); } if ( $apply && 'drafted' === $result ) { $current = class_exists( 'AIEO_SEO_Store' ) ? (array) \AIEO_SEO_Store::get( $type, $id ) : array(); if ( empty( $current['ai_locked'] ) && \AIEO_SEO_Write_Queue::approve( $type, $id ) ) { $result = 'applied'; } } $after = class_exists( 'AIEO_SEO_Store' ) ? (array) \AIEO_SEO_Store::get( $type, $id ) : array(); $live = ( 'applied' === $result ); return array( 'status' => $result, 'object_type' => $type, 'object_id' => $id, 'title' => get_the_title( $id ), 'url' => (string) get_permalink( $id ), 'before' => array( 'title' => (string) ( $before['title'] ?? '' ), 'description' => (string) ( $before['description'] ?? '' ), ), 'after' => array( 'title' => (string) ( $live ? ( $after['title'] ?? '' ) : ( $after['suggested_title'] ?? '' ) ), 'description' => (string) ( $live ? ( $after['description'] ?? '' ) : ( $after['suggested_description'] ?? '' ) ), ), 'keyphrase' => (string) ( $after['suggested_keyphrase'] ?? $after['focus_keyphrase'] ?? '' ), 'credits_used'=> true, 'note' => $live ? __( 'Published live (used AI credits). The page head was re-materialised.', 'ai-eshop-optimizer' ) : __( 'Draft created for review (used AI credits). Approve it in SEO → Health, or call aieo/seo-fix-page again with apply=true, to publish.', 'ai-eshop-optimizer' ), ); } } class SeoApplyFreeFixesAbility { const ABILITY_NAME = 'aieo/seo-apply-free-fixes'; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Apply free (no-AI) SEO fixes to a page', 'ai-eshop-optimizer' ), 'description' => __( 'DETERMINISTIC — NO AI CREDITS. For ONE page, applies the structural fixes that need no copywriting: (1) canonical — materialise the canonical URL; (2) keyphrase — set the focus keyphrase from the page’s OWN top-ranked Google keyword when none is set. Applies immediately and re-scores, so the returned fix_count already reflects the change. Free and safe — always run this FIRST, before the metered generative fixes (aieo/seo-fix-page for title/description, aieo/seo-fix-image-alt for images). A THIRD fix, "indexable" (clear an explicit noindex so the page can rank), is EXPLICIT-ONLY: it is NEVER applied unless you pass fixes:["indexable"], because many pages are noindexed on purpose (thank-you pages, thin archives) — only apply it after confirming the noindex was accidental. Returns the applied fixes + the new fix_count/score.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'required' => array( 'object_type', 'object_id' ), 'properties' => array( 'object_type' => array( 'type' => 'string', 'description' => 'e.g. product, post, page.' ), 'object_id' => array( 'type' => 'integer', 'minimum' => 1 ), 'fixes' => array( 'type' => 'array', 'items' => array( 'type' => 'string', 'enum' => array( 'canonical', 'keyphrase', 'indexable' ) ), 'description' => 'Optional subset to apply. Omit to apply the safe fixes (canonical + keyphrase). "indexable" only ever applies when you name it explicitly here — it un-noindexes the page.', ), ), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => false, 'idempotent' => true, 'destructive' => false ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $type = isset( $input['object_type'] ) ? sanitize_key( (string) $input['object_type'] ) : ''; $id = isset( $input['object_id'] ) ? (int) $input['object_id'] : 0; $fixes = isset( $input['fixes'] ) && is_array( $input['fixes'] ) ? array_map( 'sanitize_key', $input['fixes'] ) : array(); if ( '' === $type || $id <= 0 ) { return new \WP_Error( 'aieo_bad_input', __( 'object_type and object_id are required.', 'ai-eshop-optimizer' ) ); } $post = get_post( $id ); if ( ! $post || $post->post_type !== $type || 'publish' !== $post->post_status ) { return new \WP_Error( 'aieo_no_post', __( 'No published page found for that object_type / object_id.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_SEO_Fixer' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-seo-fixer.php'; } if ( ! class_exists( 'AIEO_SEO_Fixer' ) ) { return new \WP_Error( 'aieo_unavailable', __( 'The deterministic fix engine is unavailable on this site.', 'ai-eshop-optimizer' ) ); } $applicable = \AIEO_SEO_Fixer::applicable( $type, $id ); if ( empty( $applicable ) ) { return array( 'status' => 'nothing_to_do', 'object_type' => $type, 'object_id' => $id, 'applied' => array(), 'applicable' => array(), 'note' => __( 'No free structural fixes are applicable (canonical + keyphrase are already set, or no ranked keyword is known). The remaining fixes are generative — use aieo/seo-fix-page / aieo/seo-fix-image-alt.', 'ai-eshop-optimizer' ), ); } $res = \AIEO_SEO_Fixer::apply( $type, $id, $fixes ); $note = __( 'Applied free of charge (no AI credits). To rewrite the title/description use aieo/seo-fix-page; for image alt text use aieo/seo-fix-image-alt (both metered).', 'ai-eshop-optimizer' ); if ( empty( $res['applied'] ) && in_array( 'indexable', $applicable, true ) && ! in_array( 'indexable', $fixes, true ) ) { $note = __( 'This page is set to noindex. Making it indexable is not applied automatically — if that noindex is accidental, call again with fixes:["indexable"] to clear it.', 'ai-eshop-optimizer' ); } return array( 'status' => empty( $res['applied'] ) ? 'nothing_to_do' : 'applied', 'object_type' => $type, 'object_id' => $id, 'title' => get_the_title( $id ), 'url' => (string) get_permalink( $id ), 'applied' => $res['applied'], 'applicable' => $applicable, 'fix_count' => $res['fix_count'], 'seo_score' => $res['seo_score'], 'credits_used' => false, 'note' => $note, ); } } class SeoFixImageAltAbility { const ABILITY_NAME = 'aieo/seo-fix-image-alt'; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Write image alt text for a page (AI)', 'ai-eshop-optimizer' ), 'description' => __( 'GENERATIVE — CONSUMES AI CREDITS. For ONE page, finds the images with NO alt text (featured image, product gallery, attached images) and writes a concise, descriptive, keyword-aware alt for each via the AI service, in the page’s language. By default (apply=false) it PREVIEWS the drafted alts; pass apply=true to write them to the media library. Returns per-image {id, filename, alt} and how many images were updated. Because it costs credits, confirm before running across many pages. Get candidates from aieo/seo-list-fixes.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'required' => array( 'object_type', 'object_id' ), 'properties' => array( 'object_type' => array( 'type' => 'string', 'description' => 'e.g. product, post, page.' ), 'object_id' => array( 'type' => 'integer', 'minimum' => 1 ), 'apply' => array( 'type' => 'boolean', 'default' => false, 'description' => 'false = preview drafted alts (default); true = write them to the media library.' ), ), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => false, 'idempotent' => false, 'destructive' => false ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $type = isset( $input['object_type'] ) ? sanitize_key( (string) $input['object_type'] ) : ''; $id = isset( $input['object_id'] ) ? (int) $input['object_id'] : 0; $apply = ! empty( $input['apply'] ); if ( '' === $type || $id <= 0 ) { return new \WP_Error( 'aieo_bad_input', __( 'object_type and object_id are required.', 'ai-eshop-optimizer' ) ); } $post = get_post( $id ); if ( ! $post || $post->post_type !== $type || 'publish' !== $post->post_status ) { return new \WP_Error( 'aieo_no_post', __( 'No published page found for that object_type / object_id.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-seo-ai-assist.php'; } if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) || ! \AIEO_SEO_AI_Assist::available() ) { return new \WP_Error( 'aieo_unavailable', __( 'AI is unavailable — configure the Content Connector token in the Rooster One Pro.', 'ai-eshop-optimizer' ) ); } $images = self::images_missing_alt( $post ); if ( empty( $images ) ) { return array( 'status' => 'nothing_to_do', 'object_type' => $type, 'object_id' => $id, 'updated' => 0, 'credits_used' => false, 'note' => __( 'Every image on this page already has alt text. No credits used.', 'ai-eshop-optimizer' ), ); } $alts = \AIEO_SEO_AI_Assist::suggest_image_alts( $post, $images ); if ( is_wp_error( $alts ) ) { return $alts; } $by_id = array(); foreach ( $images as $im ) { $by_id[ (int) $im['id'] ] = $im; } $items = array(); $updated = 0; foreach ( $alts as $aid => $alt ) { if ( ! isset( $by_id[ $aid ] ) || '' === $alt ) { continue; } if ( $apply ) { update_post_meta( (int) $aid, '_wp_attachment_image_alt', $alt ); $updated++; } $items[] = array( 'id' => (int) $aid, 'role' => (string) $by_id[ $aid ]['role'], 'filename' => (string) $by_id[ $aid ]['filename'], 'alt' => $alt, ); } $fix_count = null; if ( $apply && $updated > 0 && class_exists( 'AIEO_SEO_Score' ) && method_exists( 'AIEO_SEO_Score', 'build' ) ) { $score = (array) \AIEO_SEO_Score::build( $type, $id ); $fix_count = isset( $score['fix_count'] ) ? (int) $score['fix_count'] : null; } return array( 'status' => $apply ? 'applied' : 'drafted', 'object_type' => $type, 'object_id' => $id, 'title' => get_the_title( $id ), 'url' => (string) get_permalink( $id ), 'images' => $items, 'candidates' => count( $images ), 'updated' => $updated, 'fix_count' => $fix_count, 'credits_used' => true, 'note' => $apply ? __( 'Alt text written to the media library (used AI credits).', 'ai-eshop-optimizer' ) : __( 'Drafted alt text for review (used AI credits). Call again with apply=true to write it to the media library.', 'ai-eshop-optimizer' ), ); } private static function images_missing_alt( $post ) { $ids = array(); $thumb = (int) get_post_thumbnail_id( $post->ID ); if ( $thumb > 0 ) { $ids[ $thumb ] = 'featured'; } if ( 'product' === $post->post_type && function_exists( 'wc_get_product' ) ) { $product = wc_get_product( $post->ID ); if ( $product ) { foreach ( (array) $product->get_gallery_image_ids() as $gid ) { $gid = (int) $gid; if ( $gid > 0 && ! isset( $ids[ $gid ] ) ) { $ids[ $gid ] = 'gallery'; } } } } foreach ( (array) get_attached_media( 'image', $post->ID ) as $att ) { $aid = (int) $att->ID; if ( $aid > 0 && ! isset( $ids[ $aid ] ) ) { $ids[ $aid ] = 'content'; } } $out = array(); foreach ( $ids as $aid => $role ) { $alt = trim( (string) get_post_meta( $aid, '_wp_attachment_image_alt', true ) ); if ( '' === $alt ) { $out[] = array( 'id' => $aid, 'role' => $role, 'filename' => basename( (string) get_post_meta( $aid, '_wp_attached_file', true ) ), ); } } return $out; } } class SeoUndoFixAbility { const ABILITY_NAME = 'aieo/seo-undo-fix'; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Undo an auto-applied SEO fix', 'ai-eshop-optimizer' ), 'description' => __( 'FREE. Reverts a focus keyphrase that was auto-set by aieo/seo-apply-free-fixes (it is source-stamped, so a manually edited keyphrase is never touched). Re-scores after reverting. Canonical is not undone because it is the correct value. Returns what was reverted.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'required' => array( 'object_type', 'object_id' ), 'properties' => array( 'object_type' => array( 'type' => 'string' ), 'object_id' => array( 'type' => 'integer', 'minimum' => 1 ), ), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => false, 'idempotent' => true, 'destructive' => false ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $type = isset( $input['object_type'] ) ? sanitize_key( (string) $input['object_type'] ) : ''; $id = isset( $input['object_id'] ) ? (int) $input['object_id'] : 0; if ( '' === $type || $id <= 0 ) { return new \WP_Error( 'aieo_bad_input', __( 'object_type and object_id are required.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_SEO_Fixer' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-seo-fixer.php'; } if ( ! class_exists( 'AIEO_SEO_Fixer' ) ) { return new \WP_Error( 'aieo_unavailable', __( 'The fix engine is unavailable on this site.', 'ai-eshop-optimizer' ) ); } $reverted = \AIEO_SEO_Fixer::undo( $type, $id ); return array( 'status' => empty( $reverted ) ? 'nothing_to_undo' : 'reverted', 'object_type' => $type, 'object_id' => $id, 'reverted' => $reverted, 'note' => empty( $reverted ) ? __( 'Nothing to undo — no auto-applied keyphrase carrying our stamp was found (a manual edit is never touched).', 'ai-eshop-optimizer' ) : __( 'Reverted the auto-applied keyphrase and re-scored the page.', 'ai-eshop-optimizer' ), ); } } class SeoFixBodyAbility { const ABILITY_NAME = 'aieo/seo-fix-body'; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Draft improved body content for a page (AI)', 'ai-eshop-optimizer' ), 'description' => __( 'GENERATIVE — CONSUMES AI CREDITS. For ONE page, writes richer, better-structured body copy that naturally includes the focus keyphrase (clears the "keyphrase in content" check and fixes thin content), via the AI service, in the page’s language. SAFE BY DEFAULT: apply=false stores a DRAFT and returns a before/after excerpt — it does NOT change the live page. Pass apply=true to publish it to post_content; WordPress saves a revision automatically, so it can be rolled back from the page’s Revisions screen. Never invents facts. Confirm credit cost before doing several pages. Get candidates from aieo/seo-list-fixes or aieo/seo-content-gaps.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'required' => array( 'object_type', 'object_id' ), 'properties' => array( 'object_type' => array( 'type' => 'string', 'description' => 'e.g. product, post, page.' ), 'object_id' => array( 'type' => 'integer', 'minimum' => 1 ), 'apply' => array( 'type' => 'boolean', 'default' => false, 'description' => 'false = draft for review (default); true = publish to post_content (a revision is saved).' ), ), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => false, 'idempotent' => false, 'destructive' => false ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ) && current_user_can( 'edit_posts' ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $type = isset( $input['object_type'] ) ? sanitize_key( (string) $input['object_type'] ) : ''; $id = isset( $input['object_id'] ) ? (int) $input['object_id'] : 0; $apply = ! empty( $input['apply'] ); if ( '' === $type || $id <= 0 ) { return new \WP_Error( 'aieo_bad_input', __( 'object_type and object_id are required.', 'ai-eshop-optimizer' ) ); } $post = get_post( $id ); if ( ! $post || $post->post_type !== $type || 'publish' !== $post->post_status ) { return new \WP_Error( 'aieo_no_post', __( 'No published page found for that object_type / object_id.', 'ai-eshop-optimizer' ) ); } if ( ! current_user_can( 'edit_post', $id ) ) { return new \WP_Error( 'aieo_forbidden', __( 'You cannot edit that page.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_SEO_Write_Queue' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-seo-write-queue.php'; } if ( ! class_exists( 'AIEO_SEO_Write_Queue' ) || ! \AIEO_SEO_Write_Queue::body_available() ) { return new \WP_Error( 'aieo_unavailable', __( 'AI is unavailable — configure the Content Connector token in the Rooster One Pro.', 'ai-eshop-optimizer' ) ); } $before_excerpt = self::excerpt( (string) $post->post_content ); $result = \AIEO_SEO_Write_Queue::process_body( $type, $id ); if ( 0 === strpos( $result, 'skipped:' ) ) { return array( 'status' => 'skipped', 'reason' => substr( $result, 8 ), 'object_type' => $type, 'object_id' => $id, ); } $row = class_exists( 'AIEO_SEO_Store' ) ? (array) \AIEO_SEO_Store::get( $type, $id ) : array(); $draft = (string) ( $row['suggested_body'] ?? '' ); if ( $apply ) { $ok = \AIEO_SEO_Write_Queue::approve_body( $type, $id ); if ( is_wp_error( $ok ) ) { return $ok; } $fresh = get_post( $id ); return array( 'status' => 'applied', 'object_type' => $type, 'object_id' => $id, 'title' => get_the_title( $id ), 'url' => (string) get_permalink( $id ), 'before' => $before_excerpt, 'after' => self::excerpt( $fresh ? (string) $fresh->post_content : $draft ), 'credits_used' => true, 'note' => __( 'Published new body content (used AI credits). WordPress saved a revision — restore the previous version from the page’s Revisions screen if needed.', 'ai-eshop-optimizer' ), ); } return array( 'status' => 'drafted', 'object_type' => $type, 'object_id' => $id, 'title' => get_the_title( $id ), 'url' => (string) get_permalink( $id ), 'before' => $before_excerpt, 'after' => self::excerpt( $draft ), 'credits_used' => true, 'note' => __( 'Body draft created for review (used AI credits). It has NOT changed the live page. Approve it on SEO → Health, or call again with apply=true to publish.', 'ai-eshop-optimizer' ), ); } private static function excerpt( $html ) { $t = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( (string) $html ) ) ); if ( function_exists( 'mb_strlen' ) && mb_strlen( $t ) > 400 ) { $t = mb_substr( $t, 0, 400 ) . '…'; } return $t; } }