array( 'label' => __( 'Overview', 'ai-seo-tools' ), 'icon' => 'grid', ), 'write' => array( 'label' => __( 'Write', 'ai-seo-tools' ), 'icon' => 'pen', 'module' => 'enable_post_generator_module', ), 'alt-text' => array( 'label' => __( 'Alt text', 'ai-seo-tools' ), 'icon' => 'image', 'module' => 'enable_alt_text_module', ), 'tags' => array( 'label' => __( 'Tags', 'ai-seo-tools' ), 'icon' => 'tag', 'module' => 'enable_auto_tagging_module', ), 'snippets' => array( 'label' => __( 'Snippets', 'ai-seo-tools' ), 'icon' => 'search', 'module' => 'enable_meta_module', ), 'schema' => array( 'label' => __( 'Structured data', 'ai-seo-tools' ), 'icon' => 'code', 'module' => 'enable_schema_module', ), 'activity' => array( 'label' => __( 'Activity', 'ai-seo-tools' ), 'icon' => 'activity', ), 'settings' => array( 'label' => __( 'Settings', 'ai-seo-tools' ), 'icon' => 'settings', ), ); } public function __construct() { add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); add_filter( 'admin_body_class', array( $this, 'body_class' ) ); add_action( 'wp_ajax_ai_seo_queue_status', array( $this, 'ajax_queue_status' ) ); add_action( 'wp_ajax_ai_seo_queue_stop', array( $this, 'ajax_queue_stop' ) ); add_action( 'wp_ajax_ai_seo_queue_reset', array( $this, 'ajax_queue_reset' ) ); add_action( 'wp_ajax_ai_seo_refresh_models', array( $this, 'ajax_refresh_models' ) ); add_action( 'wp_ajax_ai_seo_test_key', array( $this, 'ajax_test_key' ) ); add_action( 'wp_ajax_ai_seo_activity', array( $this, 'ajax_activity' ) ); } /* --------------------------------------------------------------------- */ /* Menu */ /* --------------------------------------------------------------------- */ public function register_menu(): void { add_menu_page( esc_html__( 'AI SEO Tools', 'ai-seo-tools' ), esc_html__( 'AI SEO Tools', 'ai-seo-tools' ), 'manage_options', self::SLUG, array( $this, 'render_page' ), $this->menu_icon(), 76 ); } /** * Menu mark for the admin sidebar. * * WordPress runs svg-painter.js over base64 menu icons and rewrites every * fill to a single colour from the active admin scheme, so this variant is * deliberately flat: no gradients, no strokes and no background plate. The * full-colour brand icon is used on the plugin's own screen instead. */ private function menu_icon(): string { $path = AI_SEO_TOOLS_PATH . 'assets/images/menu-icon.svg'; if ( ! is_readable( $path ) ) { return 'dashicons-search'; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Bundled asset. $svg = file_get_contents( $path ); if ( false === $svg ) { return 'dashicons-search'; } $svg = trim( (string) preg_replace( '/\s+/S', ' ', $svg ) ); return 'data:image/svg+xml;base64,' . base64_encode( $svg ); } /** * URL of the full-colour brand icon. */ public static function brand_icon_url(): string { return AI_SEO_TOOLS_URL . 'assets/images/icon.svg'; } public function body_class( string $classes ): string { if ( $this->is_plugin_screen() ) { $classes .= ' ai-seo-admin '; } return $classes; } private function is_plugin_screen(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen check. return isset( $_GET['page'] ) && self::SLUG === sanitize_key( wp_unslash( $_GET['page'] ) ); } public static function current_tab(): string { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen check. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'overview'; return array_key_exists( $tab, self::tabs() ) ? $tab : 'overview'; } public static function tab_url( string $tab ): string { return add_query_arg( array( 'page' => self::SLUG, 'tab' => $tab, ), admin_url( 'admin.php' ) ); } /* --------------------------------------------------------------------- */ /* Assets */ /* --------------------------------------------------------------------- */ public function enqueue( string $hook ): void { $on_plugin_screen = $this->is_plugin_screen(); $on_list_table = in_array( $hook, array( 'upload.php', 'edit.php' ), true ); $on_editor = in_array( $hook, array( 'post.php', 'post-new.php' ), true ); if ( ! $on_plugin_screen && ! $on_list_table && ! $on_editor ) { return; } wp_enqueue_style( 'ai-seo-admin', AI_SEO_TOOLS_URL . 'admin/assets/css/ai-seo-admin.css', array(), AI_SEO_TOOLS_VERSION ); wp_enqueue_script( 'ai-seo-admin', AI_SEO_TOOLS_URL . 'admin/assets/js/ai-seo-admin.js', array(), AI_SEO_TOOLS_VERSION, true ); wp_localize_script( 'ai-seo-admin', 'aiSeo', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => AI_SEO_Tools_Ajax::nonce(), 'ready' => AI_SEO_Tools_Client::is_ready(), 'settings' => self::tab_url( 'settings' ), 'i18n' => array( 'working' => __( 'Working…', 'ai-seo-tools' ), 'generating' => __( 'Generating…', 'ai-seo-tools' ), 'saved' => __( 'Saved', 'ai-seo-tools' ), 'failed' => __( 'Failed', 'ai-seo-tools' ), 'noKey' => __( 'Add an API key first.', 'ai-seo-tools' ), 'confirmStop' => __( 'Stop this run? Anything already processed is kept.', 'ai-seo-tools' ), 'undo' => __( 'Undo', 'ai-seo-tools' ), 'undone' => __( 'Reverted', 'ai-seo-tools' ), 'nothing' => __( 'Nothing to show yet.', 'ai-seo-tools' ), 'apply' => __( 'Apply', 'ai-seo-tools' ), 'noLinks' => __( 'No worthwhile internal links found for this post.', 'ai-seo-tools' ), 'added' => __( 'Added', 'ai-seo-tools' ), 'generate' => __( 'Generate', 'ai-seo-tools' ), 'save' => __( 'Save', 'ai-seo-tools' ), 'review' => __( 'Review and save', 'ai-seo-tools' ), 'drafted' => __( 'Draft created', 'ai-seo-tools' ), 'openDraft' => __( 'Open draft', 'ai-seo-tools' ), 'noTopic' => __( 'Type a topic first.', 'ai-seo-tools' ), 'regenerate' => __( 'Regenerate', 'ai-seo-tools' ), 'removed' => __( 'Removed', 'ai-seo-tools' ), ), ) ); } /* --------------------------------------------------------------------- */ /* Page shell */ /* --------------------------------------------------------------------- */ public function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to view this page.', 'ai-seo-tools' ) ); } $tab = self::current_tab(); $options = get_option( AI_SEO_Tools_Client::OPTION, array() ); ?>

render_status_pill(); ?>
0 ) : ?> / $
'', 'image' => '', 'tag' => '', 'search' => '', 'code' => '', 'activity' => '', 'settings' => '', 'link' => '', 'pen' => '', 'check' => '', 'alert' => '', ); $path = $paths[ $name ] ?? $paths['grid']; return ''; } /* --------------------------------------------------------------------- */ /* Shared AJAX */ /* --------------------------------------------------------------------- */ public function ajax_queue_status(): void { AI_SEO_Tools_Ajax::verify( 'edit_posts' ); $slug = AI_SEO_Tools_Ajax::text( 'queue' ); if ( ! in_array( $slug, AI_SEO_Tools_Queue::registered(), true ) ) { wp_send_json_error( array( 'message' => __( 'Unknown queue.', 'ai-seo-tools' ) ), 400 ); } wp_send_json_success( array( 'state' => AI_SEO_Tools_Queue::status( $slug ) ) ); } public function ajax_queue_stop(): void { AI_SEO_Tools_Ajax::verify( 'manage_options' ); $slug = AI_SEO_Tools_Ajax::text( 'queue' ); if ( ! in_array( $slug, AI_SEO_Tools_Queue::registered(), true ) ) { wp_send_json_error( array( 'message' => __( 'Unknown queue.', 'ai-seo-tools' ) ), 400 ); } wp_send_json_success( array( 'state' => AI_SEO_Tools_Queue::stop( $slug ) ) ); } public function ajax_queue_reset(): void { AI_SEO_Tools_Ajax::verify( 'manage_options' ); $slug = AI_SEO_Tools_Ajax::text( 'queue' ); if ( ! in_array( $slug, AI_SEO_Tools_Queue::registered(), true ) ) { wp_send_json_error( array( 'message' => __( 'Unknown queue.', 'ai-seo-tools' ) ), 400 ); } AI_SEO_Tools_Queue::reset( $slug ); wp_send_json_success( array( 'state' => AI_SEO_Tools_Queue::status( $slug ) ) ); } public function ajax_refresh_models(): void { AI_SEO_Tools_Ajax::verify( 'manage_options' ); $provider = AI_SEO_Tools_Ajax::text( 'provider' ); $models = AI_SEO_Tools_Client::fetch_models( $provider, true ); if ( is_wp_error( $models ) ) { wp_send_json_error( array( 'message' => $models->get_error_message() ), 400 ); } wp_send_json_success( array( 'models' => $models ) ); } /** * Sends the cheapest possible request to confirm the key works. */ public function ajax_test_key(): void { AI_SEO_Tools_Ajax::verify( 'manage_options' ); $result = AI_SEO_Tools_Client::request( array( 'module' => 'core', 'action' => 'test', 'system' => 'You are a connection test.', 'prompt' => 'Reply with the single word: ok', // Reasoning models spend tokens before they answer, so a tight cap // would make a perfectly good key look broken. 'max_tokens' => 500, 'retry' => true, ) ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ), 400 ); } wp_send_json_success( array( 'message' => sprintf( /* translators: 1: provider label, 2: model id. */ __( 'Connected to %1$s using %2$s.', 'ai-seo-tools' ), AI_SEO_Tools_Client::providers()[ $result['provider'] ]['label'] ?? $result['provider'], $result['model'] ), ) ); } public function ajax_activity(): void { AI_SEO_Tools_Ajax::verify( 'edit_posts' ); $module = AI_SEO_Tools_Ajax::text( 'module' ); $rows = AI_SEO_Tools_Log::recent( 40, $module ); $items = array(); foreach ( $rows as $row ) { $items[] = array( 'id' => (int) $row['id'], 'module' => $row['module'], 'action' => $row['action'], 'status' => $row['status'], 'message' => $row['message'], 'object' => $row['object_id'] ? get_the_title( (int) $row['object_id'] ) : '', 'edit' => $row['object_id'] ? get_edit_post_link( (int) $row['object_id'], 'raw' ) : '', 'cost' => (float) $row['cost'], 'tokens' => (int) $row['input_tokens'] + (int) $row['output_tokens'], 'time' => mysql2date( 'M j, H:i', $row['created_at'] ), 'undoable' => ! empty( $row['undo_meta'] ) && 'undone' !== $row['status'], ); } wp_send_json_success( array( 'items' => $items ) ); } } }