admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aica_admin_nonce' ), 'i18n' => array( 'urlRequired' => __( 'URL is required.', 'ai-content-autopilot' ), 'feedAdded' => __( 'Feed added.', 'ai-content-autopilot' ), 'errorAdding' => __( 'Error adding feed.', 'ai-content-autopilot' ), 'networkError' => __( 'Network error.', 'ai-content-autopilot' ), 'confirmDeleteFeed' => __( 'Are you sure you want to delete this feed?', 'ai-content-autopilot' ), 'noFeeds' => __( 'No feeds added yet.', 'ai-content-autopilot' ), 'active' => __( 'Active', 'ai-content-autopilot' ), 'inactive' => __( 'Inactive', 'ai-content-autopilot' ), 'activate' => __( 'Activate', 'ai-content-autopilot' ), 'deactivate' => __( 'Deactivate', 'ai-content-autopilot' ), 'delete' => __( 'Delete', 'ai-content-autopilot' ), 'runningAutopilot' => __( 'Running autopilot... This may take a few minutes.', 'ai-content-autopilot' ), 'errorRunning' => __( 'Error during run.', 'ai-content-autopilot' ), 'networkOrTimeout' => __( 'Network error or timeout.', 'ai-content-autopilot' ), 'reindexing' => __( 'Re-indexing...', 'ai-content-autopilot' ), 'errorReindexing' => __( 'Error during re-indexing.', 'ai-content-autopilot' ), 'noLogEntries' => __( 'No log entries yet.', 'ai-content-autopilot' ), ), ) ); } /** * Render Settings page. */ public function render_settings() { if ( ! current_user_can( 'manage_options' ) ) { return; } include AICA_PLUGIN_DIR . 'admin/views/settings.php'; } /** * Render Feeds page. */ public function render_feeds() { if ( ! current_user_can( 'manage_options' ) ) { return; } include AICA_PLUGIN_DIR . 'admin/views/feeds.php'; } /** * Render Status page. */ public function render_status() { if ( ! current_user_can( 'manage_options' ) ) { return; } include AICA_PLUGIN_DIR . 'admin/views/status.php'; } /** * Handle settings form submission. */ public function handle_settings_save() { if ( ! isset( $_POST['aica_settings_nonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aica_settings_nonce'] ) ), 'aica_save_settings' ) ) { return; } if ( ! current_user_can( 'manage_options' ) ) { return; } $old_interval = Settings::get( 'cron_interval' ); $old_enabled = Settings::get( 'enabled' ); // Text fields. $text_fields = array( 'openrouter_api_key', 'fal_api_key', 'ai_model', 'ai_custom_model', 'ai_language', 'ai_niche', 'ai_style', 'image_model', 'image_custom_model', 'image_style', 'keyword_include', 'keyword_exclude', ); foreach ( $text_fields as $field ) { if ( isset( $_POST[ 'aica_' . $field ] ) ) { Settings::set( $field, sanitize_text_field( wp_unslash( $_POST[ 'aica_' . $field ] ) ) ); } } // Textarea fields (preserve line breaks). $textarea_fields = array( 'site_identity' ); foreach ( $textarea_fields as $field ) { if ( isset( $_POST[ 'aica_' . $field ] ) ) { Settings::set( $field, sanitize_textarea_field( wp_unslash( $_POST[ 'aica_' . $field ] ) ) ); } } // Numeric fields. $numeric_fields = array( 'min_words', 'max_words', 'max_per_run', 'max_per_day', 'post_author', 'default_category', 'work_hours_start', 'work_hours_end', ); foreach ( $numeric_fields as $field ) { if ( isset( $_POST[ 'aica_' . $field ] ) ) { Settings::set( $field, absint( wp_unslash( $_POST[ 'aica_' . $field ] ) ) ); } } // Float fields. if ( isset( $_POST['aica_ai_temperature'] ) ) { $temp = (float) wp_unslash( $_POST['aica_ai_temperature'] ); $temp = max( 0, min( 2, $temp ) ); Settings::set( 'ai_temperature', $temp ); } // Select fields. if ( isset( $_POST['aica_post_status'] ) ) { $status = sanitize_text_field( wp_unslash( $_POST['aica_post_status'] ) ); if ( in_array( $status, array( 'publish', 'draft', 'pending' ), true ) ) { Settings::set( 'post_status', $status ); } } if ( isset( $_POST['aica_cron_interval'] ) ) { Settings::set( 'cron_interval', sanitize_text_field( wp_unslash( $_POST['aica_cron_interval'] ) ) ); } // Checkboxes. Settings::set( 'enabled', ! empty( $_POST['aica_enabled'] ) ); Settings::set( 'generate_images', ! empty( $_POST['aica_generate_images'] ) ); Settings::set( 'include_source_link', ! empty( $_POST['aica_include_source_link'] ) ); Settings::set( 'work_hours_enabled', ! empty( $_POST['aica_work_hours_enabled'] ) ); Settings::set( 'delete_data_on_uninstall', ! empty( $_POST['aica_delete_data_on_uninstall'] ) ); // Reschedule cron if interval or enabled changed. $new_interval = Settings::get( 'cron_interval' ); $new_enabled = Settings::get( 'enabled' ); if ( $old_interval !== $new_interval || $old_enabled !== $new_enabled ) { $cron = new Cron(); $cron->reschedule(); } Settings::flush_cache(); add_settings_error( 'aica_settings', 'settings_updated', __( 'Settings saved.', 'ai-content-autopilot' ), 'updated' ); } /** * AJAX: Add a new feed. */ public function ajax_add_feed() { check_ajax_referer( 'aica_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Access denied.', 'ai-content-autopilot' ) ); } $name = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ); $url = esc_url_raw( wp_unslash( $_POST['url'] ?? '' ) ); if ( empty( $url ) ) { wp_send_json_error( __( 'URL is required.', 'ai-content-autopilot' ) ); } $feeds = json_decode( Settings::get( 'feeds', '[]' ), true ); if ( ! is_array( $feeds ) ) { $feeds = array(); } if ( count( $feeds ) >= 3 ) { wp_send_json_error( __( 'Free version supports up to 3 feeds. Upgrade to Pro for unlimited feeds.', 'ai-content-autopilot' ) ); } $feeds[] = array( 'id' => wp_generate_uuid4(), 'name' => $name ?: wp_parse_url( $url, PHP_URL_HOST ), 'url' => $url, 'active' => true, ); Settings::set( 'feeds', wp_json_encode( $feeds ) ); wp_send_json_success( array( 'feeds' => $feeds ) ); } /** * AJAX: Delete a feed. */ public function ajax_delete_feed() { check_ajax_referer( 'aica_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Access denied.', 'ai-content-autopilot' ) ); } $feed_id = sanitize_text_field( wp_unslash( $_POST['feed_id'] ?? '' ) ); $feeds = json_decode( Settings::get( 'feeds', '[]' ), true ); $feeds = array_filter( $feeds, function ( $feed ) use ( $feed_id ) { return $feed['id'] !== $feed_id; } ); Settings::set( 'feeds', wp_json_encode( array_values( $feeds ) ) ); wp_send_json_success( array( 'feeds' => array_values( $feeds ) ) ); } /** * AJAX: Toggle feed active/inactive. */ public function ajax_toggle_feed() { check_ajax_referer( 'aica_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Access denied.', 'ai-content-autopilot' ) ); } $feed_id = sanitize_text_field( wp_unslash( $_POST['feed_id'] ?? '' ) ); $feeds = json_decode( Settings::get( 'feeds', '[]' ), true ); foreach ( $feeds as &$feed ) { if ( $feed['id'] === $feed_id ) { $feed['active'] = ! $feed['active']; break; } } Settings::set( 'feeds', wp_json_encode( $feeds ) ); wp_send_json_success( array( 'feeds' => $feeds ) ); } /** * AJAX: Run autopilot now. */ public function ajax_run_now() { check_ajax_referer( 'aica_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Access denied.', 'ai-content-autopilot' ) ); } define( 'AICA_MANUAL_RUN', true ); $cron = new Cron(); $cron->run(); wp_send_json_success( array( 'message' => __( 'Run completed.', 'ai-content-autopilot' ) ) ); } /** * AJAX: Re-index internal links. */ public function ajax_reindex() { check_ajax_referer( 'aica_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Access denied.', 'ai-content-autopilot' ) ); } $links = new InternalLinks(); $links->sync_existing_posts(); wp_send_json_success( array( 'message' => __( 'Re-indexing completed.', 'ai-content-autopilot' ) ) ); } /** * AJAX: Get latest log entries. */ public function ajax_get_log() { check_ajax_referer( 'aica_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Access denied.', 'ai-content-autopilot' ) ); } $logs = Logger::get_latest( 50 ); wp_send_json_success( array( 'logs' => $logs ) ); } }