id !== 'edit_page_aistma-content-editor' ) { // Also try to detect by URL parameters // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Page parameter check, nonce not required if ( ! isset( $_GET['page'] ) || sanitize_text_field( wp_unslash( $_GET['page'] ) ) !== 'aistma-content-editor' ) { return; } } $assets_loaded = true; wp_enqueue_style( 'aistma-standalone-editor-css', AISTMA_URL . 'admin/css/standalone-editor.css', [], filemtime( AISTMA_PATH . 'admin/css/standalone-editor.css' ) ); wp_enqueue_script( 'aistma-standalone-editor-js', AISTMA_URL . 'admin/js/standalone-editor.js', [ 'jquery', 'wp-util' ], filemtime( AISTMA_PATH . 'admin/js/standalone-editor.js' ), true ); // Get enhancement data for JavaScript // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Post ID from URL for asset loading, nonce not required $post_id = isset( $_GET['post_id'] ) ? absint( sanitize_text_field( wp_unslash( $_GET['post_id'] ) ) ) : 0; $enhancements_limit = 0; $enhancements_used = 0; $enhancements_remaining = 0; $enhancements_history = []; if ( $post_id > 0 ) { $enhancements_limit = (int) get_post_meta( $post_id, 'ai_story_maker_enhancements_limit', true ); $enhancements_history_json = get_post_meta( $post_id, 'ai_story_maker_enhancements_history', true ); $enhancements_history = ! empty( $enhancements_history_json ) ? json_decode( $enhancements_history_json, true ) : []; $enhancements_used = count( $enhancements_history ); $enhancements_remaining = max( 0, $enhancements_limit - $enhancements_used ); } wp_localize_script( 'aistma-standalone-editor-js', 'aistmaStandaloneEditor', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aistma_standalone_editor_nonce' ), 'post_id' => $post_id, 'enhancements_used' => $enhancements_used, 'enhancements_limit' => $enhancements_limit, 'enhancements_remaining' => $enhancements_remaining, 'enhancements_history' => $enhancements_history, 'strings' => [ 'loading' => __( 'Loading...', 'ai-story-maker' ), 'improving' => __( 'Improving content...', 'ai-story-maker' ), 'saving' => __( 'Saving...', 'ai-story-maker' ), 'error' => __( 'An error occurred. Please try again.', 'ai-story-maker' ), 'success' => __( 'Content improved! Enhancement usage tracked.', 'ai-story-maker' ), ] ] ); } /** * Render the editor page */ public function render_editor_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Post ID from URL for page rendering, nonce not required $post_id = isset( $_GET['post_id'] ) ? absint( sanitize_text_field( wp_unslash( $_GET['post_id'] ) ) ) : 0; if ( ! $post_id ) { echo '

' . esc_html__( 'AI Content Editor', 'ai-story-maker' ) . '

'; echo '

' . esc_html__( 'Please select a post to edit.', 'ai-story-maker' ) . '

'; return; } $post = get_post( $post_id ); if ( ! $post ) { echo '

' . esc_html__( 'AI Content Editor', 'ai-story-maker' ) . '

'; echo '

' . esc_html__( 'Post not found.', 'ai-story-maker' ) . '

'; return; } $post_meta = get_post_meta( $post_id ); $tags = get_the_tags( $post_id ); $categories = get_the_category( $post_id ); // Get enhancement data $enhancements_limit = get_post_meta( $post_id, 'ai_story_maker_enhancements_limit', true ); $enhancements_history_json = get_post_meta( $post_id, 'ai_story_maker_enhancements_history', true ); $enhancements_history = ! empty( $enhancements_history_json ) ? json_decode( $enhancements_history_json, true ) : []; $enhancements_used = count( $enhancements_history ); $enhancements_remaining = max( 0, (int) $enhancements_limit - $enhancements_used ); ?>

post_title ); ?> -

post_content ); ?>

0 ? esc_html( $enhancements_limit ) : '∞' ); ?>
0 || $enhancements_limit == 0 ) : ?>
0 ? esc_html( $enhancements_remaining ) : '∞' ); ?>

handle_improve_content_standalone( $content, $prompt, $operation_type, $post_id ); if ( $result['success'] ) { wp_send_json_success( $result['data'] ); } else { wp_send_json_error( $result['data'] ); } } catch ( \Exception $e ) { wp_send_json_error( 'An unexpected error occurred. Please try again.' ); } } /** * Handle AJAX request to get post data */ public function handle_get_post_data() { // Verify nonce if ( ! check_ajax_referer( 'aistma_standalone_editor_nonce', 'nonce', false ) ) { wp_send_json_error( 'Security check failed.' ); } // Check user permissions if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'You do not have permission to perform this action.' ); } $post_id = intval( $_POST['post_id'] ?? 0 ); if ( ! $post_id ) { wp_send_json_error( 'Invalid post ID.' ); } $post = get_post( $post_id ); if ( ! $post ) { wp_send_json_error( 'Post not found.' ); } $tags = get_the_tags( $post_id ); $meta_description = get_post_meta( $post_id, '_yoast_wpseo_metadesc', true ); wp_send_json_success( [ 'title' => $post->post_title, 'content' => $post->post_content, 'tags' => implode( ', ', wp_list_pluck( $tags ?: [], 'name' ) ), 'meta_description' => $meta_description, ] ); } /** * Handle get enhancement data AJAX request */ public function handle_get_enhancement_data() { // Verify nonce $nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ); if ( ! wp_verify_nonce( $nonce, 'aistma_standalone_editor_nonce' ) ) { wp_send_json_error( 'Invalid nonce.' ); } // Check user permissions if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Insufficient permissions.' ); } // Get post ID $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; if ( ! $post_id ) { wp_send_json_error( 'Post ID is required.' ); } // Get enhancement data $enhancements_limit = (int) get_post_meta( $post_id, 'ai_story_maker_enhancements_limit', true ); $enhancements_history_json = get_post_meta( $post_id, 'ai_story_maker_enhancements_history', true ); $enhancements_history = ! empty( $enhancements_history_json ) ? json_decode( $enhancements_history_json, true ) : []; $enhancements_used = count( $enhancements_history ); $enhancements_remaining = max( 0, $enhancements_limit - $enhancements_used ); wp_send_json_success( [ 'enhancements_used' => $enhancements_used, 'enhancements_limit' => $enhancements_limit, 'enhancements_remaining' => $enhancements_remaining, 'enhancements_history' => $enhancements_history ] ); } /** * Handle AJAX request to check enhancement eligibility */ public function handle_check_enhancement_eligibility() { // Verify nonce $nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ); if ( ! wp_verify_nonce( $nonce, 'aistma_check_enhancement_eligibility' ) ) { wp_send_json_error( 'Invalid nonce.' ); } // Check user permissions if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Insufficient permissions.' ); } // Get post ID $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; if ( ! $post_id ) { wp_send_json_error( 'Post ID is required.' ); } // A post is eligible if it was generated by this plugin. // 0 or empty enhancements_limit means unlimited — treat as eligible. // Covers both master API posts (have package_id) and personal API posts (have request_id only). $enhancements_limit = get_post_meta( $post_id, 'ai_story_maker_enhancements_limit', true ); $package_id = get_post_meta( $post_id, 'ai_story_maker_package_id', true ); $request_id = get_post_meta( $post_id, 'ai_story_maker_request_id', true ); $eligible = ! empty( $package_id ) || ! empty( $request_id ); wp_send_json_success( [ 'eligible' => $eligible, 'enhancements_limit' => $enhancements_limit, 'package_id' => $package_id ] ); } }