'boolean', 'default' => true, 'sanitize_callback' => 'rest_sanitize_boolean', ) ); register_setting( $group, 'wtofe_review_summary_min_reviews', array( 'type' => 'integer', 'default' => 10, 'sanitize_callback' => function ( $val ) { $val = absint( $val ); return $val < 1 ? 10 : $val; }, ) ); register_setting( $group, 'wtofe_review_summary_regen_interval', array( 'type' => 'integer', 'default' => 5, 'sanitize_callback' => function ( $val ) { $val = absint( $val ); return $val < 1 ? 5 : $val; }, ) ); register_setting( $group, 'wtofe_review_summary_max_reviews', array( 'type' => 'integer', 'default' => 30, 'sanitize_callback' => function ( $val ) { $val = absint( $val ); return $val < 5 ? 30 : $val; }, ) ); register_setting( $group, 'wtofe_review_summary_max_words', array( 'type' => 'integer', 'default' => 120, 'sanitize_callback' => function ( $val ) { $val = absint( $val ); return $val < 50 ? 120 : $val; }, ) ); register_setting( $group, 'wtofe_ai_model_review_summary', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); } /** * Adds the "AI Review Summary" page to the settings app, with the values * and strings it renders (see app/src/pages/review-summary). * * @param array $sections * @return array */ public static function add_settings_app_section( $sections ) { $sections['review-summary'] = array( 'order' => 30, 'group' => 'general', 'values' => array( 'wtofe_review_summary_enabled' => (bool) get_option( 'wtofe_review_summary_enabled', true ), 'wtofe_review_summary_min_reviews' => (int) get_option( 'wtofe_review_summary_min_reviews', 10 ), 'wtofe_review_summary_regen_interval' => (int) get_option( 'wtofe_review_summary_regen_interval', 5 ), 'wtofe_review_summary_max_reviews' => (int) get_option( 'wtofe_review_summary_max_reviews', 30 ), 'wtofe_review_summary_max_words' => (int) get_option( 'wtofe_review_summary_max_words', 120 ), 'wtofe_ai_model_review_summary' => WTOFE_Settings_App::selectable_model( get_option( 'wtofe_ai_model_review_summary', '' ) ), ), 'data' => array(), ); return $sections; } // ------------------------------------------------------------------------- // Meta Box // ------------------------------------------------------------------------- /** * Registers the "AI Review Summary" meta box on the product edit screen. */ public static function add_meta_box() { add_meta_box( 'wtofe_review_summary_box', __( 'AI Review Summary', 'ai-content-generator-for-woocommerce' ), array( __CLASS__, 'render_meta_box' ), 'product', 'normal', 'default' ); } /** * Renders the review summary meta box. * * @param WP_Post $post */ public static function render_meta_box( $post ) { $product_id = absint( $post->ID ); $meta = WTOFE_Review_Summary_Generator::get_all_meta( $product_id ); $is_enabled = WTOFE_Review_Summary_Generator::is_enabled(); $min_reviews = WTOFE_Review_Summary_Generator::get_min_reviews(); $review_count = (int) get_comments( array( 'post_id' => $product_id, 'status' => 'approve', 'type' => 'review', 'count' => true, ) ); require WTOFE_AI_PLUGIN_DIR . 'admin/modules/review-summary/partials/meta-box-review-summary.php'; } // ------------------------------------------------------------------------- // Asset Enqueueing // ------------------------------------------------------------------------- /** * Enqueues admin CSS and JS on product edit screens only. * * @param string $hook Current admin page hook suffix. */ public static function enqueue_scripts( $hook ) { if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = get_current_screen(); if ( ! $screen || 'product' !== $screen->post_type ) { return; } wp_enqueue_style( 'wtofe-review-summary-admin', WTOFE_AI_PLUGIN_URL . 'admin/modules/review-summary/assets/css/review-summary-admin.css', array(), WTOFE_AI_VERSION ); wp_enqueue_script( 'wtofe-review-summary-admin', WTOFE_AI_PLUGIN_URL . 'admin/modules/review-summary/assets/js/review-summary-admin.js', array( 'jquery' ), WTOFE_AI_VERSION, true ); wp_localize_script( 'wtofe-review-summary-admin', 'wtofeRS', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wtofe_review_summary_nonce' ), 'postId' => get_the_ID(), 'i18n' => array( 'generating' => __( 'Generating summary…', 'ai-content-generator-for-woocommerce' ), 'regenerate' => __( 'Generate Now', 'ai-content-generator-for-woocommerce' ), 'error' => __( 'Generation failed. Please try again.', 'ai-content-generator-for-woocommerce' ), 'noReviews' => __( 'Not enough reviews yet.', 'ai-content-generator-for-woocommerce' ), 'positive' => __( 'Positive', 'ai-content-generator-for-woocommerce' ), 'negative' => __( 'Negative', 'ai-content-generator-for-woocommerce' ), 'mixed' => __( 'Mixed', 'ai-content-generator-for-woocommerce' ), 'neutral' => __( 'Neutral', 'ai-content-generator-for-woocommerce' ), ), ) ); } // ------------------------------------------------------------------------- // AJAX // ------------------------------------------------------------------------- /** * Handles the AJAX "Generate Now" / auto-generate request. * * Expects POST fields: * nonce – wtofe_review_summary_nonce * post_id – the product ID * * Returns JSON: { success, data: { summary, pros, cons, overall_sentiment, * generated_at, review_count, model } } */ public static function ajax_generate() { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wtofe_review_summary_nonce' ) ) { wp_send_json_error( array( 'message' => __( 'Invalid nonce.', 'ai-content-generator-for-woocommerce' ) ) ); } if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'ai-content-generator-for-woocommerce' ) ) ); } $product_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; if ( ! $product_id ) { wp_send_json_error( array( 'message' => __( 'Invalid product ID.', 'ai-content-generator-for-woocommerce' ) ) ); } $result = WTOFE_Review_Summary_Generator::generate( $product_id ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } // Return the full summary payload alongside the stored meta for UI update. $meta = WTOFE_Review_Summary_Generator::get_all_meta( $product_id ); wp_send_json_success( array( 'summary' => $result['summary'], 'pros' => $result['pros'], 'cons' => $result['cons'], 'overall_sentiment' => $result['overall_sentiment'], 'generated_at' => $meta['generated_at'], 'review_count' => $meta['review_count'], 'model' => $meta['model'], ) ); } }