PluginProbe ʕ •ᴥ•ʔ
AI Engine – The Chatbot, AI Framework & MCP for WordPress / trunk
AI Engine – The Chatbot, AI Framework & MCP for WordPress vtrunk
3.5.7 3.5.6 3.5.5 3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.4.9 3.4.8 3.4.7 0.2.1 1.6.91 0.2.2 1.6.92 0.2.3 1.6.93 0.2.4 1.6.94 0.2.5 1.6.95 0.2.6 1.6.96 0.2.7 1.6.97 0.2.8 1.6.98 0.2.9 1.6.99 0.3.0 1.7.0 0.3.1 1.7.1 0.3.2 1.7.2 0.3.3 1.7.3 0.3.4 1.7.4 0.3.5 1.7.5 0.3.6 1.7.6 0.4.0 1.7.7 0.4.1 1.7.8 0.4.2 1.7.9 0.4.3 1.8.0 0.4.4 1.8.1 0.4.5 1.8.2 0.4.6 1.8.3 0.4.7 1.8.4 0.4.8 1.8.5 0.4.9 1.8.6 0.5.0 1.8.7 0.5.1 1.8.8 0.5.2 1.8.9 0.5.3 1.9.0 0.5.4 1.9.1 0.5.5 1.9.2 0.5.6 1.9.3 0.5.7 1.9.4 0.5.8 1.9.5 0.5.9 1.9.6 0.6.0 1.9.7 0.6.1 1.9.8 0.6.2 1.9.81 0.6.3 1.9.82 0.6.4 1.9.83 0.6.5 1.9.84 0.6.6 1.9.85 0.6.7 1.9.86 0.6.8 1.9.87 0.6.9 1.9.88 0.7.0 1.9.89 0.7.1 1.9.90 0.7.2 1.9.91 0.7.3 1.9.92 0.7.4 1.9.93 0.7.5 1.9.94 0.7.6 1.9.95 0.7.7 1.9.96 0.7.8 1.9.97 0.7.9 1.9.98 0.8.0 1.9.99 0.8.1 2.0.0 0.8.2 2.0.1 0.8.3 2.0.2 0.8.4 2.0.3 0.8.5 2.0.4 0.8.6 2.0.5 0.8.7 2.0.6 0.8.8 2.0.7 0.8.9 2.0.8 0.9.0 2.0.9 0.9.2 2.1.0 0.9.3 2.1.1 0.9.4 2.1.2 0.9.5 2.1.3 0.9.6 2.1.4 0.9.7 2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 1.6.86 0.1.7 1.6.87 0.1.8 1.6.88 0.1.9 1.6.89 0.2.0 1.6.90
ai-engine / classes / modules / advisor.php
ai-engine / classes / modules Last commit date
advisor.php 3 months ago chatbot.php 3 weeks ago discussions.php 1 day ago editor-assistant.php 3 months ago files.php 3 months ago forms-manager.php 3 months ago gdpr.php 4 months ago search.php 3 months ago security.php 1 year ago tasks-examples.php 6 months ago tasks.php 1 month ago wand.php 3 months ago
advisor.php
243 lines
1 <?php
2
3 class Meow_MWAI_Modules_Advisor {
4 private $core = null;
5 private $update_interval = 24 * 60 * 60;
6 private $prompt = "Based on all the plugins I have installed and my WordPress information, can you give me general advice and recommendations about my WordPress setup? Aim for a concise list of 6-8 recommendations total. Consider factors such as whether certain types of plugins are still needed, if some might be deprecated, their overall impact on performance, if there are redundancies or conflicts between plugins, and if some functionality is no longer required with recent versions of WordPress. Provide the recommendations in a JSON format with 'level', 'severity', 'title', and 'description' fields:
7
8 'level' can be 'success', 'warning', or 'danger'. Use 'success' to highlight areas that are working well and should be maintained to keep things running optimally. This motivates the user to continue good practices. Use 'warning' for issues that are not critical but could be improved. Use 'danger' for critical issues that should be addressed immediately.
9 'severity' should be an integer from 0 to 100, with 0 being the least severe and 100 being the most severe.
10 'title' should be a concise description of the recommendation.
11 'description' should provide more details about the recommendation.
12 Here is a simple JSON example of the format to use, without any actual data:
13 [
14 {
15 \"level\": \"success\",
16 \"severity\": 0,
17 \"title\": \"Example Success\",
18 \"description\": \"This is an example of a successful recommendation.\"
19 },
20 {
21
22 \"level\": \"warning\",
23 \"severity\": 50,
24 \"title\": \"Example Warning\",
25 \"description\": \"This is an example of a non-critical recommendation.\"
26 },
27 {
28 \"level\": \"danger\",
29 \"severity\": 100,
30 \"title\": \"Example Danger\",
31 \"description\": \"This is an example of a critical recommendation.\"
32 }
33 ]
34
35 Instead of focusing on individual plugins, provide more general and holistic recommendations based on the overall WordPress system and the installed plugins as a whole. Feel free to use titles like \"Useless Plugins\" or \"Performance Considerations\" to group related recommendations. Avoid repeating the same or very similar recommendations. Only output the JSON - do not include any other text or formatting like markdown. If you don't have any recommendations, output an empty array [].
36
37 Here are the plugins I have installed, along with my WordPress information:";
38
39 public function __construct( $core ) {
40 $this->core = $core;
41 add_action( 'init', [ $this, 'init' ] );
42
43 // Only add dashboard widget if module is enabled and user has permissions
44 if ( $this->core->get_option( 'module_advisor', false ) ) {
45 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] );
46 }
47 }
48
49 public function init() {
50 // Handle manual refresh request
51 if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['refresh_advisor_nonce'] ) ) {
52 if ( wp_verify_nonce( $_POST['refresh_advisor_nonce'], 'refresh_advisor_action' ) ) {
53 $this->run_advisor();
54 wp_safe_redirect( remove_query_arg( 'refresh_advisor' ) );
55 exit;
56 }
57 }
58
59 // Always register the task handler (in case task exists from before)
60 add_filter( 'mwai_task_advisor_daily', [ $this, 'run_advisor_task' ], 10, 2 );
61
62 // Only ensure the task exists if module is enabled
63 if ( $this->core->get_option( 'module_advisor', false ) ) {
64 // Ensure the advisor task exists
65 $this->ensure_advisor_task();
66 }
67 }
68
69 /**
70 * Ensure the advisor task exists in the Tasks system
71 */
72 private function ensure_advisor_task() {
73 if ( !$this->core->tasks ) {
74 return;
75 }
76
77 $this->core->tasks->ensure( [
78 'name' => 'advisor_daily',
79 'description' => 'Analyze WordPress setup and provide recommendations.',
80 'category' => 'system',
81 'schedule' => '0 2 * * *', // Daily at 2 AM
82 'deletable' => 0, // System task, not deletable
83 ] );
84 }
85
86 /**
87 * Handle advisor task execution
88 */
89 public function run_advisor_task( $result, $job ) {
90 // Check if module is enabled
91 if ( !$this->core->get_option( 'module_advisor', false ) ) {
92 return [
93 'ok' => false,
94 'message' => 'Advisor module is disabled.'
95 ];
96 }
97
98 try {
99 $this->run_advisor();
100 return [
101 'ok' => true,
102 'message' => 'Advisor analysis completed successfully.'
103 ];
104 }
105 catch ( Exception $e ) {
106 return [
107 'ok' => false,
108 'message' => 'Advisor analysis failed: ' . $e->getMessage()
109 ];
110 }
111 }
112
113 private function check_and_run_advisor() {
114 $last_run_data = get_option( 'mwai_advisor_data', [] );
115 $last_run_time = $last_run_data['date'] ?? 0;
116 $current_time = time();
117 if ( $current_time - $last_run_time > $this->update_interval ) {
118 $this->run_advisor();
119 }
120 }
121
122 public function run_advisor() {
123 try {
124 global $mwai;
125 if ( !$mwai->hasAI() ) {
126 throw new Exception( 'There are no AI environments with an API key yet.' );
127 }
128 $plugins = $this->get_all_installed_plugins();
129
130 $finalPrompt = $this->prompt;
131 foreach ( $plugins as $plugin ) {
132 $finalPrompt .= 'Plugin: ' . $plugin['title'] . "\n";
133 $finalPrompt .= '- Version: ' . $plugin['version'] . "\n";
134 $finalPrompt .= '- Description: ' . $plugin['description'] . "\n";
135 $finalPrompt .= '- Enabled: ' . ( $plugin['enabled'] ? 'Yes' : 'No' ) . "\n";
136 }
137 $finalPrompt .= "\n";
138
139 $finalPrompt .= "WordPress Information:\n";
140 $finalPrompt .= '- Number of Plugins: ' . count( $plugins ) . "\n";
141 $finalPrompt .= '- Site URL: ' . get_site_url() . "\n";
142 $finalPrompt .= '- PHP Version: ' . phpversion() . "\n";
143 $finalPrompt .= '- WordPress Version: ' . get_bloginfo( 'version' ) . "\n";
144 $finalPrompt .= '- Theme: ' . wp_get_theme()->get( 'Name' ) . "\n";
145 $finalPrompt .= "\n";
146
147 $finalPrompt .= "General Information:\n";
148 $finalPrompt .= "- Today's Date: " . date( 'Y-m-d' ) . "\n";
149 $finalPrompt .= "\n";
150
151 $errors = MeowKit_MWAI_Helpers::php_error_logs();
152 $errors = array_slice( $errors, -10 );
153 if ( !empty( $errors ) ) {
154 $finalPrompt .= "PHP Errors: \n";
155 foreach ( $errors as $error ) {
156 $finalPrompt .= $error['date'] . ' - ' . $error['type'] . ' - ' . $error['content'] . "\n";
157 }
158 }
159
160 $answer = $mwai->simpleTextQuery( $finalPrompt, [ 'scope' => 'advisor' ] );
161 $recommendations = json_decode( $answer, true );
162 update_option( 'mwai_advisor_data', [
163 'date' => time(),
164 'data' => $recommendations
165 ], false );
166
167 }
168 catch ( Exception $e ) {
169 error_log( 'AI Engine: ' . $e->getMessage() );
170 }
171 }
172
173 public function get_all_installed_plugins() {
174 if ( !function_exists( 'get_plugins' ) ) {
175 require_once ABSPATH . 'wp-admin/includes/plugin.php';
176 }
177 $all_plugins = get_plugins();
178 $active_plugins = get_option( 'active_plugins', [] );
179 $plugins_info = [];
180 foreach ( $all_plugins as $plugin_path => $plugin_data ) {
181 $plugin_info = [
182 'title' => $plugin_data['Name'],
183 'version' => $plugin_data['Version'],
184 'description' => $plugin_data['Description'],
185 'enabled' => in_array( $plugin_path, $active_plugins ) ? true : false
186 ];
187 $plugins_info[] = $plugin_info;
188 }
189 return $plugins_info;
190 }
191
192 public function add_dashboard_widget() {
193 wp_add_dashboard_widget(
194 'mwai_advisor_widget',
195 'AI Engine Advisor',
196 [ $this, 'advisor_metabox' ]
197 );
198 }
199
200 public function advisor_metabox() {
201 $data = get_option( 'mwai_advisor_data', [] );
202 $recommendations = $data['data'] ?? [];
203 if ( empty( $recommendations ) ) {
204 echo '<p>No recommendations yet.</p>';
205 }
206 else {
207 echo '<p>Everyday, AI Engine will analyze your WordPress setup and provide you with recommendations to improve it.</p>';
208 echo '<ul>';
209 foreach ( $recommendations as $recommendation ) {
210 $title = isset( $recommendation['title'] ) ? $recommendation['title'] : 'Miscellaneous';
211 $description = isset( $recommendation['description'] ) ? $recommendation['description'] : 'No information available.';
212 echo '<li style="display: inline;">';
213 echo '<div style="display: flex; margin-bottom: 10px;">';
214 echo $this->generate_badge( $recommendation['level'] );
215 echo '<div>';
216 echo '<strong>' . $title . '</strong> - ' . $description;
217 echo '</div>';
218 echo '</div>';
219 echo '</li>';
220 }
221 echo '</ul>';
222 }
223 echo '<form method="POST">';
224 wp_nonce_field( 'refresh_advisor_action', 'refresh_advisor_nonce' );
225 echo '<div style="display: flex; justify-content: end;">';
226 echo '<input type="submit" class="button" value="Refresh Recommendations">';
227 echo '</div>';
228 echo '</form>';
229 }
230
231 public function generate_badge( $level ) {
232 $color = ( $level === 'success' ) ? '#00ba37' : ( ( $level === 'warning' ) ? '#dba617' : '#e65054' );
233 return '<div style="display: flex; align-items: center; margin-bottom: 5px;">
234 <div style="width: 20px; height: 20px; margin-right: 10px;">
235 <svg aria-hidden="true" focusable="false" width="100%" height="100%" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
236 <circle r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0" style="stroke: #e2e2e2;"></circle>
237 <circle id="bar" r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0" style="stroke-width: 15px; stroke: ' . $color . ';"></circle>
238 </svg>
239 </div>
240 </div>';
241 }
242 }
243