core = $core; add_action( 'init', [ $this, 'init' ] ); // Only add dashboard widget if module is enabled and user has permissions if ( $this->core->get_option( 'module_advisor', false ) ) { add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] ); } } public function init() { // Handle manual refresh request if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['refresh_advisor_nonce'] ) ) { if ( wp_verify_nonce( $_POST['refresh_advisor_nonce'], 'refresh_advisor_action' ) ) { $this->run_advisor(); wp_safe_redirect( remove_query_arg( 'refresh_advisor' ) ); exit; } } // Always register the task handler (in case task exists from before) add_filter( 'mwai_task_advisor_daily', [ $this, 'run_advisor_task' ], 10, 2 ); // Only ensure the task exists if module is enabled if ( $this->core->get_option( 'module_advisor', false ) ) { // Ensure the advisor task exists $this->ensure_advisor_task(); } } /** * Ensure the advisor task exists in the Tasks system */ private function ensure_advisor_task() { if ( !$this->core->tasks ) { return; } $this->core->tasks->ensure( [ 'name' => 'advisor_daily', 'description' => 'Analyze WordPress setup and provide recommendations.', 'category' => 'system', 'schedule' => '0 2 * * *', // Daily at 2 AM 'deletable' => 0, // System task, not deletable ] ); } /** * Handle advisor task execution */ public function run_advisor_task( $result, $job ) { // Check if module is enabled if ( !$this->core->get_option( 'module_advisor', false ) ) { return [ 'ok' => false, 'message' => 'Advisor module is disabled.' ]; } try { $this->run_advisor(); return [ 'ok' => true, 'message' => 'Advisor analysis completed successfully.' ]; } catch ( Exception $e ) { return [ 'ok' => false, 'message' => 'Advisor analysis failed: ' . $e->getMessage() ]; } } private function check_and_run_advisor() { $last_run_data = get_option( 'mwai_advisor_data', [] ); $last_run_time = $last_run_data['date'] ?? 0; $current_time = time(); if ( $current_time - $last_run_time > $this->update_interval ) { $this->run_advisor(); } } public function run_advisor() { try { global $mwai; if ( !$mwai->hasAI() ) { throw new Exception( 'There are no AI environments with an API key yet.' ); } $plugins = $this->get_all_installed_plugins(); $finalPrompt = $this->prompt; foreach ( $plugins as $plugin ) { $finalPrompt .= 'Plugin: ' . $plugin['title'] . "\n"; $finalPrompt .= '- Version: ' . $plugin['version'] . "\n"; $finalPrompt .= '- Description: ' . $plugin['description'] . "\n"; $finalPrompt .= '- Enabled: ' . ( $plugin['enabled'] ? 'Yes' : 'No' ) . "\n"; } $finalPrompt .= "\n"; $finalPrompt .= "WordPress Information:\n"; $finalPrompt .= '- Number of Plugins: ' . count( $plugins ) . "\n"; $finalPrompt .= '- Site URL: ' . get_site_url() . "\n"; $finalPrompt .= '- PHP Version: ' . phpversion() . "\n"; $finalPrompt .= '- WordPress Version: ' . get_bloginfo( 'version' ) . "\n"; $finalPrompt .= '- Theme: ' . wp_get_theme()->get( 'Name' ) . "\n"; $finalPrompt .= "\n"; $finalPrompt .= "General Information:\n"; $finalPrompt .= "- Today's Date: " . date( 'Y-m-d' ) . "\n"; $finalPrompt .= "\n"; $errors = MeowKit_MWAI_Helpers::php_error_logs(); $errors = array_slice( $errors, -10 ); if ( !empty( $errors ) ) { $finalPrompt .= "PHP Errors: \n"; foreach ( $errors as $error ) { $finalPrompt .= $error['date'] . ' - ' . $error['type'] . ' - ' . $error['content'] . "\n"; } } $answer = $mwai->simpleTextQuery( $finalPrompt, [ 'scope' => 'advisor' ] ); $recommendations = json_decode( $answer, true ); update_option( 'mwai_advisor_data', [ 'date' => time(), 'data' => $recommendations ], false ); } catch ( Exception $e ) { error_log( 'AI Engine: ' . $e->getMessage() ); } } public function get_all_installed_plugins() { if ( !function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $all_plugins = get_plugins(); $active_plugins = get_option( 'active_plugins', [] ); $plugins_info = []; foreach ( $all_plugins as $plugin_path => $plugin_data ) { $plugin_info = [ 'title' => $plugin_data['Name'], 'version' => $plugin_data['Version'], 'description' => $plugin_data['Description'], 'enabled' => in_array( $plugin_path, $active_plugins ) ? true : false ]; $plugins_info[] = $plugin_info; } return $plugins_info; } public function add_dashboard_widget() { wp_add_dashboard_widget( 'mwai_advisor_widget', 'AI Engine Advisor', [ $this, 'advisor_metabox' ] ); } public function advisor_metabox() { $data = get_option( 'mwai_advisor_data', [] ); $recommendations = $data['data'] ?? []; if ( empty( $recommendations ) ) { echo '

No recommendations yet.

'; } else { echo '

Everyday, AI Engine will analyze your WordPress setup and provide you with recommendations to improve it.

'; echo ''; } echo '
'; wp_nonce_field( 'refresh_advisor_action', 'refresh_advisor_nonce' ); echo '
'; echo ''; echo '
'; echo '
'; } public function generate_badge( $level ) { $color = ( $level === 'success' ) ? '#00ba37' : ( ( $level === 'warning' ) ? '#dba617' : '#e65054' ); return '
'; } }