' . esc_html__( 'Settings', 'aeo-content-ai-studio' ) . ''; array_unshift( $links, $settings_link ); return $links; } public function add_menu() { add_menu_page( __( 'AEO Content AI Studio', 'aeo-content-ai-studio' ), __( 'AEO Content', 'aeo-content-ai-studio' ), 'manage_options', 'aeocas-audit-report', array( $this, 'render_audit_report' ), AEOCAS_PLUGIN_URL . 'admin/images/icon.png', 30 ); add_submenu_page( 'aeocas-audit-report', __( 'AEO Audit Report', 'aeo-content-ai-studio' ), __( 'Audit Report', 'aeo-content-ai-studio' ), 'manage_options', 'aeocas-audit-report', array( $this, 'render_audit_report' ) ); add_submenu_page( 'aeocas-audit-report', __( 'AEO Content AI Studio', 'aeo-content-ai-studio' ), __( 'Settings', 'aeo-content-ai-studio' ), 'manage_options', 'aeo-content-ai-studio', array( $this, 'render_page' ) ); add_submenu_page( 'aeocas-audit-report', __( 'AEO Activity Log', 'aeo-content-ai-studio' ), __( 'Activity Log', 'aeo-content-ai-studio' ), 'manage_options', 'aeocas-activity-log', array( $this, 'render_activity_log' ) ); } public function register_settings() { register_setting( 'aeocas_settings', 'aeocas_site_token', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_and_register_api_key' ), ) ); register_setting( 'aeocas_settings', 'aeocas_enabled_features', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_features' ), ) ); } /** * Sanitize API key and attempt registration with the platform. * * Generates a plugin_token (if not already present) and sends it * to the platform during registration. The platform uses this token * to authenticate its requests to the plugin's REST API. */ public function sanitize_and_register_api_key( $input ) { $api_key = sanitize_text_field( $input ); if ( empty( $api_key ) ) { delete_option( 'aeocas_connection_verified' ); return ''; } // Generate plugin token if not already present. $plugin_token = get_option( 'aeocas_plugin_token', '' ); if ( empty( $plugin_token ) ) { $plugin_token = AEOCAS_Auth::generate_plugin_token(); } $response = wp_remote_post( trailingslashit( AEOCAS_PLATFORM_URL ) . 'api/v1/plugin/register', array( 'body' => wp_json_encode( array( 'site_url' => get_site_url(), 'plugin_token' => $plugin_token, ) ), 'headers' => array( 'Content-Type' => 'application/json', 'x-api-key' => $api_key, ), 'timeout' => 15, ) ); if ( is_wp_error( $response ) ) { delete_option( 'aeocas_connection_verified' ); add_settings_error( 'aeocas_site_token', 'aeocas_register_failed', __( 'Could not connect to AEO Content platform. Please try again later.', 'aeo-content-ai-studio' ), 'error' ); return $api_key; } $status = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 200 === $status && ! empty( $body['ok'] ) ) { // Save plugin token only after successful registration. update_option( 'aeocas_plugin_token', $plugin_token, false ); update_option( 'aeocas_connection_verified', true ); add_settings_error( 'aeocas_site_token', 'aeocas_register_success', __( 'Successfully connected to AEO Content platform.', 'aeo-content-ai-studio' ), 'success' ); } else { delete_option( 'aeocas_connection_verified' ); $message = ! empty( $body['error'] ) ? sanitize_text_field( $body['error'] ) : __( 'Registration failed.', 'aeo-content-ai-studio' ); add_settings_error( 'aeocas_site_token', 'aeocas_register_failed', $message, 'error' ); } return $api_key; } public function sanitize_features( $input ) { if ( ! is_array( $input ) ) { return array(); } $available = aeocas_plugin()->get_available_modules(); return array_values( array_intersect( $input, $available ) ); } public function enqueue_styles( $hook ) { $aeocas_pages = array( 'toplevel_page_aeocas-audit-report', 'aeo-content_page_aeo-content-ai-studio', 'aeo-content_page_aeocas-activity-log', ); if ( ! in_array( $hook, $aeocas_pages, true ) ) { return; } $asset_ver = AEOCAS_VERSION . '.' . filemtime( AEOCAS_PLUGIN_DIR . 'admin/css/admin.css' ); wp_enqueue_style( 'aeocas-admin', AEOCAS_PLUGIN_URL . 'admin/css/admin.css', array(), $asset_ver ); // Audit page JS. if ( 'toplevel_page_aeocas-audit-report' === $hook ) { $js_ver = AEOCAS_VERSION . '.' . filemtime( AEOCAS_PLUGIN_DIR . 'admin/js/audit.js' ); wp_enqueue_script( 'aeocas-audit', AEOCAS_PLUGIN_URL . 'admin/js/audit.js', array(), $js_ver, true ); wp_localize_script( 'aeocas-audit', 'aeocasAudit', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aeocas_audit_nonce' ), 'favicon' => get_site_icon_url( 48, '' ), ) ); } } public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } include AEOCAS_PLUGIN_DIR . 'admin/views/settings-page.php'; } public function render_activity_log() { if ( ! current_user_can( 'manage_options' ) ) { return; } include AEOCAS_PLUGIN_DIR . 'admin/views/activity-log-page.php'; } public function render_audit_report() { if ( ! current_user_can( 'manage_options' ) ) { return; } include AEOCAS_PLUGIN_DIR . 'admin/views/audit-page.php'; } }