PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.3.6
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.3.6
0.5.6 0.5.5 0.5.4 0.5.3 0.5.2 0.5.1 0.5.0 0.4.9 0.4.8 0.4.7 0.4.6 trunk 0.0.1 0.0.2 0.2.8 0.2.9 0.3.0 0.3.1 0.3.2 0.3.3 0.3.4 0.3.5 0.3.6 0.3.7 0.3.8 All 32 releases
code-engine / classes / load.php

load.php in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.3.6, at classes/load.php

201 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Execute all active snippets
4 add_action( 'plugins_loaded', 'mwcode_loaded', 1 );
5
6 function mwcode_loaded( ) {
7 global $mwcode_core, $current_mwcode_snippet;
8 static $last_blocked_log = [];
9
10
11 if ( !isset( $mwcode_core ) ) {
12 $mwcode_core = new Meow_MWCODE_Core( );
13 }
14
15 $snippets = $mwcode_core->execute_active_snippets( );
16
17 if ( empty( $snippets ) ) {
18 return;
19 }
20
21 foreach ( $snippets as $snippet ) {
22
23 if ( $snippet['blocked'] ) {
24 // Determine the reason for blocking
25 $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
26 $is_settings_page = ( $page === 'mwcode_settings' );
27 $is_rest = MeowCommon_Helpers::is_rest();
28 $is_rest_whitelisted = Meow_MWCODE_Core::is_white_listed_rest();
29
30 $reason = '';
31 if ( $is_settings_page ) {
32 $reason = "on Code Engine settings page (disabled for safety)";
33 } elseif ( $is_rest && !$is_rest_whitelisted ) {
34 $requested_route = Meow_MWCODE_Core::get_requested_rest_route();
35 $reason = "REST route not whitelisted: " . ($requested_route ?: 'unknown');
36 } else {
37 // Add more debugging info
38 $debug_info = [
39 'is_rest' => $is_rest,
40 'is_whitelisted' => $is_rest_whitelisted,
41 'page' => $page,
42 'request_uri' => $_SERVER['REQUEST_URI'] ?? 'not set'
43 ];
44 $reason = "unknown reason (debug: " . json_encode($debug_info) . ")";
45 }
46
47 // Avoid duplicate logs within 2 seconds
48 $log_key = $snippet['id'] . '-' . $reason;
49 $current_time = time();
50
51 if ( !isset($last_blocked_log[$log_key]) || ($current_time - $last_blocked_log[$log_key]) > 2 ) {
52 $mwcode_core->log( "⚠️ Snippet blocked: " . $snippet['name'] . " (ID: " . $snippet['id'] . ") - " . $reason );
53 $last_blocked_log[$log_key] = $current_time;
54 }
55
56 continue;
57 }
58
59 $current_mwcode_snippet = $snippet;
60
61
62 ob_start( );
63
64 try {
65 $mwcode_core->log( "Executing snippet: " . $snippet['name'] . " (ID: " . $snippet['id'] . ")" );
66 eval( $snippet['code'] );
67 } catch ( Throwable $e ) {
68 $mwcode_core->log( "⚠️ Exception in snippet: " . $snippet['name'] . " (ID: " . $snippet['id'] . ")" );
69 $mwcode_core->log( $e->getMessage( ) );
70 $mwcode_core->update_option( 'fatal_error', "\"{$snippet["name"]}\" - " . $e->getMessage( ) );
71 $mwcode_core->update_option( 'thrown_snippet', $snippet );
72 }
73
74 ob_end_clean( );
75 }
76
77 $current_mwcode_snippet = null;
78 }
79
80 $file_path = MWCODE_PATH . '/classes/modules/helpers.php';
81 if ( file_exists( $file_path ) ) {
82 require_once( $file_path );
83 }
84
85 $file_path_common = MWCODE_PATH . '/common/helpers.php';
86 if ( file_exists( $file_path_common ) ) {
87 require_once( $file_path_common );
88 }
89
90
91 // In admin or Rest API request ( REQUEST URI begins with '/wp-json/' )
92 if ( is_admin( ) || MeowCommon_Helpers::is_rest( ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
93 global $mwcode_core;
94 $mwcode_core = new Meow_MWCODE_Core( );
95 }
96
97 // Define a global variable to store the currently executing snippet
98 global $current_mwcode_snippet;
99
100 /**
101 * Custom error handler
102 */
103 function mwcode_error_handler( $errno, $errstr, $errfile, $errline )
104 {
105 global $current_mwcode_snippet, $mwcode_core;
106
107 if ( !isset( $current_mwcode_snippet ) ) {
108 return false;
109 }
110
111 if ( !isset( $mwcode_core ) ) {
112 $mwcode_core = new Meow_MWCODE_Core( );
113 }
114
115 if ( !( error_reporting( ) & $errno ) ) {
116 // This error code is not included in error_reporting
117 return false;
118 }
119
120 if ( !array_key_exists( 'name', $current_mwcode_snippet ) ) {
121 return false;
122 }
123
124 $error = "$errstr in $errfile on line $errline";
125
126 $mwcode_core->log( "⚠️ Error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" );
127 $mwcode_core->log( $error );
128
129 $mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - $errstr" );
130 $mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet );
131
132 // Don't execute PHP internal error handler
133 return true;
134 }
135
136 /**
137 * Shutdown function to catch fatal errors
138 */
139 function mwcode_shutdown_function( )
140 {
141 global $current_mwcode_snippet, $mwcode_core;
142
143 if ( !isset( $current_mwcode_snippet ) ) {
144 return;
145 }
146
147 if ( !isset( $mwcode_core ) ) {
148 $mwcode_core = new Meow_MWCODE_Core( );
149 }
150
151 $error = error_get_last( );
152
153 $fatal_error = $error !== null && in_array( $error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR] );
154
155 if ( $fatal_error ) {
156
157 $mwcode_core->log( "⚠️ Fatal error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" );
158 $mwcode_core->log( $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'] );
159
160 $mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - " . $error['message'] );
161 $mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet );
162
163 }
164 }
165
166 // Set the custom error handler and shutdown function
167 set_error_handler( "mwcode_error_handler" );
168 register_shutdown_function( "mwcode_shutdown_function" );
169
170
171 // Load all the JS functions to the front & back
172 // TODO: Add some restrictions by pages
173 function enqueue_mwcode_scripts( )
174 {
175 global $mwcode_core;
176
177 if ( !isset( $mwcode_core ) ) {
178 $mwcode_core = new Meow_MWCODE_Core( );
179 }
180
181 wp_register_script( 'mwcode-scripts-js', '' );
182 wp_enqueue_script( 'mwcode-scripts-js' );
183
184 $js_code = '';
185
186 if ( is_admin( ) ) {
187 $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
188 if ( $page === 'mwai_settings' ) {
189 $js_code = $mwcode_core->get_js_functions_to_push( );
190 }
191 } else {
192 $js_code = $mwcode_core->get_js_functions_to_push( );
193 }
194
195 if ( $js_code ) {
196 wp_add_inline_script( 'mwcode-scripts-js', $js_code, 'after' );
197 }
198 }
199
200 add_action( 'wp_enqueue_scripts', 'enqueue_mwcode_scripts' );
201 add_action( 'admin_enqueue_scripts', 'enqueue_mwcode_scripts' );