__( 'Generate social posts for a page (AI)', 'ai-eshop-optimizer' ), 'description' => __( 'GENERATIVE — CONSUMES AI CREDITS. For ONE published object (product, post, page…), drafts platform-native social copy for the requested networks (facebook, instagram, x, linkedin, tiktok) via the AI service, in the page’s language. It only DRAFTS — nothing is published and no account tokens are used. Returns posts[], each with {platform, text, hashtags[], cta, variant_label}. Because it writes copy through the AI service, each call spends the merchant’s AI credits — confirm before generating across many pages.', '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' => 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' ); } private static function input_schema() { return array( 'default' => array(), 'type' => 'object', 'required' => array( 'object_id', 'platforms' ), 'properties' => array( 'object_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'ID of the published post/product/page to write social copy for.', ), 'platforms' => array( 'type' => 'array', 'minItems' => 1, 'items' => array( 'type' => 'string', 'enum' => array( 'facebook', 'instagram', 'x', 'linkedin', 'tiktok' ), ), 'description' => 'Which networks to draft copy for.', ), 'object_type' => array( 'type' => 'string', 'description' => 'Optional post type hint (e.g. product, post, page). Defaults to the object’s actual post_type.', ), ), ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); $id = isset( $input['object_id'] ) ? (int) $input['object_id'] : 0; $platforms = array(); if ( isset( $input['platforms'] ) && is_array( $input['platforms'] ) ) { $allowed = array( 'facebook', 'instagram', 'x', 'linkedin', 'tiktok' ); foreach ( $input['platforms'] as $p ) { $p = sanitize_key( (string) $p ); if ( in_array( $p, $allowed, true ) && ! in_array( $p, $platforms, true ) ) { $platforms[] = $p; } } } if ( $id <= 0 || empty( $platforms ) ) { return array( 'error' => __( 'object_id and at least one valid platform are required.', 'ai-eshop-optimizer' ) ); } $post = get_post( $id ); if ( ! $post || 'publish' !== $post->post_status ) { return array( 'error' => __( 'No published object found for that object_id.', 'ai-eshop-optimizer' ) ); } if ( ! class_exists( 'AIEO_Social_AI_Writer' ) ) { return array( 'error' => __( 'The social AI writer is unavailable on this site.', 'ai-eshop-optimizer' ) ); } $requested = $id; $id = \AIEO_Social_AI_Writer::promotable_object_id( $id ); if ( $id !== $requested ) { $post = get_post( $id ); if ( ! $post || 'publish' !== $post->post_status ) { return array( 'error' => __( 'That variation belongs to a product that is not published.', 'ai-eshop-optimizer' ) ); } } $type = isset( $input['object_type'] ) && '' !== trim( (string) $input['object_type'] ) ? sanitize_key( (string) $input['object_type'] ) : (string) $post->post_type; if ( 'product_variation' === $type ) { $type = 'product'; } $ctx = array( 'platforms' => $platforms, 'title' => (string) get_the_title( $id ), 'excerpt' => self::build_excerpt( $post ), 'url' => (string) get_permalink( $id ), 'type' => $type, 'object_id' => $id, ); $result = \AIEO_Social_AI_Writer::generate( $ctx ); if ( is_wp_error( $result ) ) { return array( 'error' => $result->get_error_message() ); } $posts = ( is_array( $result ) && isset( $result['posts'] ) && is_array( $result['posts'] ) ) ? $result['posts'] : array(); $out = array( 'object_type' => $type, 'object_id' => $id, 'title' => $ctx['title'], 'url' => $ctx['url'], 'posts' => $posts, 'credits_used' => true, 'note' => __( 'Drafted social copy (used AI credits). Nothing was published — publish or schedule the drafts from the Social scheduler.', 'ai-eshop-optimizer' ), ); if ( $id !== $requested ) { $out['requested_object_id'] = $requested; $out['folded_to_parent'] = true; $out['note'] .= ' ' . sprintf( __( 'Note: %1$d is a product variation, which has no page of its own and cannot be targeted by a campaign — the copy was written for its parent product %2$d. Say so when reporting back.', 'ai-eshop-optimizer' ), $requested, $id ); } return $out; } private static function build_excerpt( $post ) { $raw = trim( (string) $post->post_excerpt ); if ( '' === $raw ) { $raw = (string) $post->post_content; $raw = class_exists( 'AIEO_DMM_Offer_Live_Facts' ) ? AIEO_DMM_Offer_Live_Facts::strip( $raw ) : $raw; } $text = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( $raw ) ) ); if ( function_exists( 'mb_strlen' ) && mb_strlen( $text ) > 500 ) { $text = mb_substr( $text, 0, 500 ) . '…'; } return $text; } }