PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.5.3
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.5.3
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
← All changes | classes/core.php +959 -552 0.2.80.5.3 View file →
@@ -6,686 +6,1093 @@
6 6 use PhpParser\Error;
7 7
8 8 class Meow_MWCODE_Core
9 9 {
10 - public $admin = null;
11 - public $snippet = null;
12 - public $is_rest = false;
13 - public $is_cli = false;
14 - public $site_url = null;
15 - public $mwcode = null;
10 + public $admin = null;
11 + public $snippet = null;
12 + public $is_rest = false;
13 + public $is_cli = false;
14 + public $site_url = null;
15 + public $mwcode = null;
16 + public $licenser = null;
16 17
17 - private $option_name = 'mwcode_options';
18 + // IDs of global snippets already executed this request (by the plugins_loaded pass
19 + // or by load_global_snippets), so a global never runs twice and never re-declares.
20 + public $loaded_global_ids = [];
18 21
19 - public function __construct() {
20 - global $mwcode;
21 -
22 - $this->site_url = get_site_url();
23 - $this->is_rest = MeowCommon_Helpers::is_rest();
24 - $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
25 -
26 - // Snippets
27 - $snippet = new Meow_MWCODE_Modules_Snippet( $this );
28 - $this->snippet = $snippet;
22 + private $option_name = 'mwcode_options';
29 23
30 - // Create API before plugins_loaded
31 - $this->mwcode = new Meow_MWCODE_API( $this, $snippet );
32 - $mwcode = $this->mwcode;
33 -
34 - add_action( 'plugins_loaded', array( $this, 'init' ) );
35 - }
24 + public function __construct() {
25 + global $mwcode;
36 26
37 - function init() {
38 - // Part of the core, settings and stuff
39 - $this->admin = new Meow_MWCODE_Admin( $this );
27 + $this->site_url = get_site_url();
28 + $this->is_rest = MeowKit_MWCODE_Helpers::is_rest();
29 + $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
40 30
41 - // Only for REST
42 - if ( $this->is_rest ) {
43 - new Meow_MWCODE_Rest( $this, $this->admin, $this->snippet );
44 - }
45 - }
31 + // Snippets
32 + $snippet = new Meow_MWCODE_Modules_Snippet( $this );
33 + $this->snippet = $snippet;
46 34
35 + // Create API before plugins_loaded
36 + $this->mwcode = new Meow_MWCODE_API( $this, $snippet );
37 + $mwcode = $this->mwcode;
47 38
48 - /**
49 - *
50 - * Roles & Access Rights
51 - *
52 - */
53 - #region Roles & Access Rights
54 - public function can_access_settings() {
55 - return apply_filters( 'mwcode_allow_setup', current_user_can( 'manage_options' ) );
56 - }
39 + // Add the shortcode for the "content" snippets
40 + add_shortcode( 'code-engine', [ $this, 'content_shortcode' ] );
57 41
58 - public function can_access_features() {
59 - return apply_filters( 'mwcode_allow_usage', current_user_can( 'administrator' ) );
60 - }
42 + add_action( 'plugins_loaded', array( $this, 'init' ) );
43 + }
61 44
62 - public function check_rest_nonce( $request ) {
63 - $nonce = $request->get_header( 'X-WP-Nonce' );
64 - return wp_verify_nonce( $nonce, 'wp_rest' );
65 - }
66 - #endregion
45 + function init() {
46 + // Initialize the licenser for Pro version
47 + if ( class_exists( 'MeowKitPro_MWCODE_Licenser' ) ) {
48 + $this->licenser = new MeowKitPro_MWCODE_Licenser( MWCODE_PREFIX, MWCODE_ENTRY, MWCODE_DOMAIN, MWCODE_ITEM_ID, MWCODE_VERSION );
49 + }
67 50
68 - #region Options
51 + // Part of the core, settings and stuff
52 + $this->admin = new Meow_MWCODE_Admin( $this );
69 53
70 - function get_option( $option, $default = null ) {
71 - $options = $this->get_all_options();
72 - return $options[$option] ?? $default;
73 - }
54 + // Only for REST
55 + if ( $this->is_rest ) {
56 + new Meow_MWCODE_Rest( $this, $this->admin, $this->snippet );
57 + }
58 +
59 + // MCP integration - check both class and global variable
60 + if ( class_exists( 'Meow_MWAI_Core' ) || isset( $GLOBALS['mwai'] ) ) {
61 + new Meow_MWCODE_MCP( $this );
62 + }
63 + }
74 64
75 - function list_options() {
76 - return [
77 - //Safemode
78 - "safe_mode_status" => "on", // on, off, whitelist
79 - "safe_mode_whitelist" => [],
80 -
81 - //LOGS
82 - "server_debug_mode" => false,
65 + /**
66 + *
67 + * Roles & Access Rights
68 + *
69 + */
70 + #region Roles & Access Rights
71 + public function can_access_settings() {
72 + return apply_filters( 'mwcode_allow_setup', current_user_can( 'manage_options' ) );
73 + }
83 74
84 - //UI
85 - "ui_show_preview" => true,
75 + public function can_access_features() {
76 + return apply_filters( 'mwcode_allow_usage', current_user_can( 'administrator' ) );
77 + }
86 78
87 - //AI
88 - "ai_suggestions" => false,
89 - "ai_engine_status"=> false,
90 - "ai_engine_message" => "",
79 + public function check_rest_nonce( $request ) {
80 + $nonce = $request->get_header( 'X-WP-Nonce' );
81 + return wp_verify_nonce( $nonce, 'wp_rest' );
82 + }
83 + #endregion
91 84
92 - //API
93 - "api_endpoint" => false,
94 - "api_token" => md5( time() . rand() ),
95 - ];
96 - }
85 + #region Options
97 86
98 - function get_all_options( ) {
99 - $options = get_option( $this->option_name, $this->list_options( ) );
100 - $options = $this->sanitize_options( $options );
101 -
102 - return $options;
103 - }
87 + function get_option( $option, $default = null ) {
88 + $options = $this->get_all_options();
89 + return $options[$option] ?? $default;
90 + }
104 91
105 - function update_options( $options ) {
106 - $current_options = get_option($this->option_name);
107 -
108 - if ($current_options === $options) {
109 - // $this->log('💾 The options are already the expected value.');
110 - } else {
111 - if ( !update_option( $this->option_name, $options, false ) ) {
112 - $this->log( '💾 There was an issue updating the options.' );
113 - }
114 - }
115 -
116 - $options = $this->sanitize_options( $options );
117 - return $options;
118 - }
92 + function list_options() {
93 + return [
94 + //Safemode
95 + "safe_mode_status" => "on", // on, off, whitelist
96 + "safe_mode_whitelist" => [],
97 + //"disallow_block_php" => true, // Do not allow PHP code to be execute through Blocks "code" parameter
98 + "code_blocks" => false,
99 + "code_blocks_whitelist" => [], // Whitelist for code blocks, if empty, all code blocks are allowed
100 +
101 + //LOGS
102 + "server_debug_mode" => false,
119 103
120 - function update_option( $option, $value ) {
121 - $options = $this->get_all_options();
122 - $options[$option] = $value;
123 - return $this->update_options( $options );
124 - }
104 + //UI
105 + "ui_show_preview" => false,
125 106
126 - function reset_options() {
127 - if ( $this->get_all_options() === $this->list_options() ) {
128 - return true;
129 - }
130 - return $this->update_options( $this->list_options() );
131 - }
107 + //AI
108 + "ai_suggestions" => false,
109 + "ai_engine_status"=> false,
110 + "mwai_active" => false,
111 + "ai_engine_message" => "",
132 112
133 - // Validate and keep the options clean and logical.
134 - function sanitize_options( $options ) {
135 - $options_modified = false;
113 + //API
114 + "api_endpoint" => false,
115 + "api_token" => md5( time() . rand() ),
116 +
117 + //MCP
118 + "mcp_support" => false,
119 + "mcp_functions" => false,
136 120
137 - // Make sure safe mode whitelist is an array
138 - if ( ! is_array( $options['safe_mode_whitelist'] ) ) {
139 - $options['safe_mode_whitelist'] = explode( ",", $options['safe_mode_whitelist'] );
140 - $options_modified = true;
141 - }
121 + //MAINTENANCE
122 + "clean_uninstall" => false,
123 + ];
124 + }
142 125
143 - // Update AI Engine status
144 - $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified;
126 + function get_all_options( ) {
127 + $options = get_option( $this->option_name, [] );
128 + $defaults = $this->list_options();
129 +
130 + // Merge with defaults to ensure all options exist
131 + $options = array_merge( $defaults, $options );
132 +
133 + $options = $this->sanitize_options( $options );
134 + return $options;
135 + }
145 136
146 - // Disable AI related features if AI Engine is not available
147 - if ( ! $options['ai_engine_status'] && $options['ai_suggestions'] !== false ) {
148 - $options['ai_suggestions'] = false;
149 - $options_modified = true;
150 - }
137 + function update_options( $options ) {
151 138
152 - if ( $options_modified ) {
153 - update_option( $this->option_name, $options, false );
154 - }
139 + $options = $this->sanitize_options( $options );
155 140
156 - return $options;
157 - }
141 + if ( !update_option( $this->option_name, $options, false ) ) {
142 + //$this->log( '💾 There was an issue updating the options.' );
143 + }
144 +
145 + return $options;
146 + }
158 147
159 - private function updateAIEngineStatus( &$options ) {
160 - global $mwai;
148 + function update_option( $option, $value ) {
149 + $options = $this->get_all_options();
150 + $options[$option] = $value;
151 + return $this->update_options( $options );
152 + }
161 153
162 - if ( is_null( $mwai ) || ! isset( $mwai ) ) {
163 - $options['ai_engine_status'] = false;
164 - $options['ai_engine_message'] = 'AI Engine is not available.';
165 - return true;
166 - }
154 + function reset_options() {
155 + if ( $this->get_all_options() === $this->list_options() ) {
156 + return true;
157 + }
158 + return $this->update_options( $this->list_options() );
159 + }
167 160
168 - try {
169 - $status = $mwai->checkStatus();
161 + // Validate and keep the options clean and logical.
162 + function sanitize_options( $options ) {
163 + $options_modified = false;
164 +
165 + // Ensure mcp_support exists in options
166 + if ( !isset( $options['mcp_support'] ) ) {
167 + $options['mcp_support'] = false;
168 + }
170 169
171 - if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) {
172 - $options['ai_engine_status'] = true;
173 - $options['ai_engine_message'] = $status;
174 - return true;
175 - }
176 - } catch ( Exception $e ) {
177 - if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) {
178 - $options['ai_engine_status'] = false;
179 - $options['ai_engine_message'] = $e->getMessage();
180 - return true;
181 - }
182 - }
170 + // Make sure safe mode whitelist is an array
171 + if ( ! is_array( $options['safe_mode_whitelist'] ) ) {
172 + $options['safe_mode_whitelist'] = explode( ",", $options['safe_mode_whitelist'] );
173 + $options_modified = true;
174 + }
183 175
184 - return false;
185 - }
176 + // Update AI Engine status
177 + $options = $this->updateAIEngineStatus( $options );
186 178
187 - // #endregion
179 + // Disable AI related features if AI Engine is not available
180 + if ( ! $options['ai_engine_status'] ) {
181 + if ( $options['ai_suggestions'] !== false ) {
182 + $options['ai_suggestions'] = false;
183 + $options_modified = true;
184 + }
185 + // Note: We don't disable MCP support here anymore
186 + // It will be checked at runtime in the MCP class
187 + }
188 188
189 - #region Snippets
189 + return $options;
190 + }
190 191
191 - /**
192 - * Get snippet.
193 - *
194 - * @param $id
195 - * @return mixed
196 - */
197 - protected function get_snippet( $id ) {
198 - if ( $this->snippet === null ) {
199 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
200 - }
192 + private function updateAIEngineStatus( &$options ) {
193 + global $mwai;
201 194
202 - return $this->snippet->select_one( $id );
195 + // AI Engine is active (regardless of whether an API key is configured).
196 + // MCP exposure only needs AI Engine present, not a key, so the MCP toggles
197 + // gate on this rather than on mwai_has_ai.
198 + $options['mwai_active'] = !empty( $mwai );
199 + $options['mwai_has_ai'] = !empty( $mwai ) && method_exists( $mwai, 'hasAI' ) && $mwai->hasAI();
200 + // Legacy
201 + $options['ai_engine_status'] = $options['mwai_has_ai'];
202 +
203 + return $options;
204 + }
205 +
206 + #endregion
207 +
208 + #region Snippets
209 +
210 + /**
211 + * Get snippet.
212 + *
213 + * @param $id
214 + * @return mixed
215 + */
216 + protected function get_snippet( $id ) {
217 + if ( $this->snippet === null ) {
218 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
203 219 }
204 220
205 - private function sanitize_arg( $name, $value ) {
206 - if ( $name[0] !== '$' ) { $name = '$' . $name; }
207 -
208 - if ( !empty( $value ) && !is_numeric( $value ) && $value[0] !== '"' && $value[strlen( $value ) - 1] !== '"' ) {
209 - $value = '"' . esc_sql( $value ) . '"';
210 - }
221 + return $this->snippet->select_one( $id );
222 + }
211 223
212 - return [ $name, $value ];
213 - }
224 + function add_snippet( $params ) {
214 225
215 - function run_non_fn_snippet( $id ) {
216 - $snippet = $this->get_snippet( $id );
217 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
226 + $response = [
227 + "snippet" => null,
228 + "result" => false,
229 + ];
218 230
219 - $error = null;
220 - $output = null;
231 + $this->snippet->validate( $params );
221 232
222 - try {
223 - ob_start();
224 - eval( $snippet['code'] );
225 - $output = ob_get_clean();
226 - } catch ( Throwable $e ) {
227 - $error = new Exception( ' Error executing the snippet, ' . $e->getMessage() );
228 - ob_clean();
229 - } finally {
230 - restore_error_handler();
231 - }
233 + $params = $this->snippet->formatParamsForDatabase( $params );
232 234
233 - if ( $error !== null ) {
234 - throw $error;
235 - }
235 + // Route to UPDATE when an existing snippet id is provided (updateSnippet / the
236 + // MCP mwcode_update_snippet tool). This previously always insert()ed, so an
237 + // update tried to INSERT a row with an already-used primary key: that fails on
238 + // the SQLite backend (Studio/Playground) with "Could not insert the snippet",
239 + // and duplicates or errors elsewhere. The admin UI was unaffected because it
240 + // calls snippet->update() directly.
241 + $existing = !empty( $params['id'] ) ? $this->snippet->select_one( $params['id'] ) : null;
242 + if ( $existing ) {
243 + $this->snippet->update( $params );
244 + $result = $params['id'];
245 + }
246 + else {
247 + unset( $params['id'] );
248 + $result = $this->snippet->insert( $params );
249 + }
250 + $snippet = $this->snippet->select_one( $result );
236 251
237 - return $output;
238 - }
252 + if( $result ) {
253 + $params['id'] = (string)$result;
239 254
240 - function run_snippet( $id, $args = [], $params = [] )
241 - {
242 - // Static array to track defined functions
243 - static $defined_functions = array();
255 + $this->snippet->create_or_update_function_snippet( $params );
256 + $this->snippet->create_or_update_interval_snippet( $params );
244 257
245 - if ( $id ) { // If there is an ID, we get the snippet, if not we get the data from the params
246 - $snippet = $this->get_snippet( $id );
247 - $this->snippet->get_function_snippets_data( $snippet ); // adds the function data to the snippet
258 + $this->snippet->get_function_snippets_data( $snippet );
259 + }
248 260
249 - $params = [ // We set the params according to the snippet we fetched
250 - 'test' => false, // If we pass an ID to the function, we are not testing the snippet
251 - // 'test' => $params['test'] ?? false if needed we can still use ID and test at the same time (should not happen)
252 - 'code' => $snippet['code'],
253 - 'name' => $snippet['functionName'],
254 - 'args' => $snippet['functionArgs'],
255 - 'values' => $snippet['functionArgsDict'] // Contains the default values of the arguments
256 - ];
257 - }
261 + $response['snippet'] = $snippet;
262 + $response['result'] = $result;
258 263
259 - // Sanitize all the arguments if the option is enabled
260 - if ( $this->get_option( 'sanitize_arguments', true ) ) {
264 + return $response;
265 + }
261 266
262 - if ( $args ) {
263 - foreach ( $args as $name => $value ) {
264 - list( $sanitizedName, $sanitizedValue ) = $this->sanitize_arg( $name, $value );
265 - unset( $args[$name] );
267 + private function sanitize_arg( $name, $value, $type = null) {
268 + $real_type = gettype( $value );
266 269
267 - $args[$sanitizedName] = $sanitizedValue;
268 - }
269 - }
270 + if ( $name[0] !== '$' ) { $name = '$' . $name; }
270 271
271 - foreach ( $params['values'] as $name => $value ) {
272 + if ( $type == null ) {
273 + $type = $real_type;
274 + }
272 275
273 - if( array_key_exists( 'input', $value) ) {
274 - list( $sanitizedInputName, $sanitizedInputValue ) = $this->sanitize_arg( $name, $value['input'] );
275 - $params['values'][$sanitizedInputName]['input'] = $sanitizedInputValue;
276 - }
277 -
278 - if( array_key_exists( 'default', $value) ) {
279 - list( $sanitizedDefaultValueName, $sanitizedDefaultValue ) = $this->sanitize_arg( $name, $value['default'] );
280 - $params['values'][$sanitizedDefaultValueName]['default'] = $sanitizedDefaultValue;
281 - }
282 - }
276 + if ( $type != 'array' && !empty( $value ) && !is_numeric( $value ) && $value[0] !== '"' && $value[strlen( $value ) - 1] !== '"' ) {
277 + $value = '"' . esc_sql( $value ) . '"';
278 + }
283 279
284 - }
280 + if ( $type === 'array' && $real_type === 'string' ) {
281 + // We got a string like this: "["a", "b", "c"]" or "[ 1, 2, 3 ]"
282 + // We need to convert it to an array
283 + $value = str_replace( '"', '', $value );
284 + $value = str_replace( '[', '', $value );
285 + $value = str_replace( ']', '', $value );
286 + $value = explode( ',', $value );
287 + $value = array_map( 'trim', $value );
288 + }
285 289
286 -
290 + if ( $type === 'array' ) {
291 + // Convert to PHP array format instead of JSON
292 + $value = var_export( $value, true );
293 + }
287 294
288 - // Make sure the function is existing and is the one in the snippet
289 - if ( empty( $params['code'] ) ) {
290 - throw new Exception( 'Code Engine: The snippet code appears to be empty.' );
291 - }
292 -
293 - if ( empty( $params['name'] ) || ! str_contains( $params['code'], $params['name'] ) ) {
294 - throw new Exception( "Code Engine: Function name does not match. The name should be {$params['name']}." );
295 - }
295 + return [ $name, $value ];
296 + }
296 297
297 - // Overwrite the default values with the provided ones
298 - if ( $args ) {
299 - foreach ( $args as $name => $value ) {
300 - $params['values'][$name]['input'] = $value;
301 - }
298 + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
299 + // Retrieve the snippet code from the provided code or via the snippet ID.
300 + if ( $code ) {
301 + $snippet = [ 'code' => $code ];
302 + } else {
303 + $snippet = $this->get_snippet( $id );
304 + }
302 305
303 - $this->log( '⚡ Arguments provided: ' . json_encode( $args ) );
304 - }
306 + // Remove any PHP opening tag.
307 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
305 308
306 - // Check if the function has already been defined
307 - if ( !in_array( $params['name'], $defined_functions ) ) {
309 + if ( $test ) {
310 + $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
311 + }
308 312
309 - // If not, proceed with modification and definition
310 - if ( $params['test'] ) { // Make sure the echo statement uses a line break
311 - $params['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $params['code'] );
312 - } else { // Remove all echo statements
313 - $params['code'] = preg_replace( '/echo\s+(.+?);/s', '', $params['code'] );
314 - }
313 + if( $prefix ) {
314 + $snippet['code'] = $prefix . "\n" . $snippet['code'];
315 + }
316 +
317 + $error = null;
318 + $output = null;
319 +
320 + try {
321 + ob_start();
322 + eval( $snippet['code'] );
323 + $output = ob_get_clean();
324 + } catch ( Throwable $e ) {
325 + $snippet_id = $id ? " ( ID: $id )" : '(Content Gutenberg Block)';
326 + $this->log( '🔴 Error executing the snippet ' . $snippet_id . ' : ' . $e->getMessage() );
327 + ob_clean();
328 + } finally {
329 + restore_error_handler();
330 + }
331 +
332 + // If in test mode, return output as an array of lines with an 'error' key if needed.
333 + if ( $test ) {
334 + $output = explode( "\n", trim( $output ) );
335 + if ( $error !== null ) {
336 + $output['error'] = $error->getMessage();
337 + }
338 + } else {
339 + if ( $error !== null ) {
340 + throw $error;
341 + }
342 + }
343 +
344 + return $output;
345 + }
315 346
316 - $params['code'] = "if (!function_exists('{$params['name']}')) {\n" . $params['code'] . "\n}\n";
347 + function run_snippet( $id, $args = [], $params = [] )
348 + {
349 + // Static array to track defined functions
350 + static $defined_functions = array();
317 351
318 - // Add the function name to the array to avoid redefinition
319 - $defined_functions[] = $params['name'];
320 - } else {
321 - // If already defined, just prepare to call the function without redefining it
322 - $params['code'] = '';
323 - }
352 + if ( $id ) { // If there is an ID, we get the snippet, if not we get the data from the params
353 + $snippet = $this->get_snippet( $id );
354 + $this->snippet->get_function_snippets_data( $snippet ); // adds the function data to the snippet
324 355
325 - // Prepare the code to be executed
326 - $params['code'] .= "\n\$mwcode_result = {$params['name']}(";
327 - foreach ( $params['args'] as $index => $arg ) {
328 - $value = 'null'; // In case the argument is not provided it will be null
356 + $params = [ // We set the params according to the snippet we fetched
357 + 'test' => false, // If we pass an ID to the function, we are not testing the snippet
358 + // 'test' => $params['test'] ?? false if needed we can still use ID and test at the same time (should not happen)
359 + 'code' => $snippet['code'],
360 + 'name' => $snippet['functionName'],
361 + 'args' => $snippet['functionArgs'],
362 + 'values' => $snippet['functionArgsDict'] // Contains the default values of the arguments
363 + ];
364 + }
329 365
330 - if ( array_key_exists( $arg, $params['values'] ) ) { // Avoid warnings if the argument is not provided
366 + // Sanitize all the arguments if the option is enabled
367 + if ( $this->get_option( 'sanitize_arguments', true ) ) {
331 368
332 - // If the argument is provided, use it, if not use the default value
333 - if ( !empty( $params['values'][$arg]['input'] ) ) {
334 - $value = $params['values'][$arg]['input'];
369 + if ( $args ) {
370 + foreach ( $args as $name => $value ) {
371 + list( $sanitizedName, $sanitizedValue ) = $this->sanitize_arg( $name, $value );
372 + unset( $args[$name] );
335 373
336 - } else if ( !empty( $params['values'][$arg]['default'] ) ) {
337 - $value = $params['values'][$arg]['default'];
338 - }
339 - }
374 + $args[$sanitizedName] = $sanitizedValue;
375 + }
376 + }
340 377
341 - $params['code'] .= "{$value}";
342 - if ( $index < count( $params['args'] ) - 1 ) {
343 - $params['code'] .= ', ';
344 - }
345 - }
346 - $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
378 + foreach ( $params['values'] as $name => $value ) {
347 379
348 - $error = null;
349 - $output = null;
380 + if( array_key_exists( 'input', $value) ) {
381 + list( $sanitizedInputName, $sanitizedInputValue ) = $this->sanitize_arg( $name, $value['input'], $value['type'] );
382 + $params['values'][$sanitizedInputName]['input'] = $sanitizedInputValue;
383 + }
350 384
351 - try {
352 - ob_start();
353 - eval( $params['code'] );
354 - $output = ob_get_clean();
355 -
356 - if ( $params['test'] ){
357 - $output = explode( "\n", $output );
358 - }
359 -
360 - } catch ( Throwable $e ) {
361 - //$this->log('Code Engine: Error executing the function: ' . $e->getMessage());
362 - $error = new Exception(' Error executing the function, ' . $e->getMessage());
385 + if( array_key_exists( 'default', $value) ) {
386 + list( $sanitizedDefaultValueName, $sanitizedDefaultValue ) = $this->sanitize_arg( $name, $value['default'], $value['type'] );
387 + $params['values'][$sanitizedDefaultValueName]['default'] = $sanitizedDefaultValue;
388 + }
389 + }
363 390
364 - ob_clean();
365 - } finally {
366 - restore_error_handler();
367 - }
391 + }
368 392
369 - if ( $error !== null ) {
370 - if( $params['test'] ){
371 - $output['error'] = $error->getMessage();
372 - } else {
373 - throw $error;
374 - }
375 - }
393 + // Make sure the function is existing and is the one in the snippet
394 + if ( empty( $params['code'] ) ) {
395 + throw new Exception( 'Code Engine: The snippet code appears to be empty.' );
396 + }
376 397
377 - return $output;
378 - }
398 + if ( empty( $params['name'] ) || ! str_contains( $params['code'], $params['name'] ) ) {
399 + throw new Exception( "Code Engine: Function name does not match. The name should be {$params['name']}." );
400 + }
379 401
402 + // Overwrite the default values with the provided ones
403 + if ( $args ) {
404 + foreach ( $args as $name => $value ) {
405 + $params['values'][$name]['input'] = $value;
406 + }
380 407
381 - function parse_snippet( $code, $new_snippet = false ){
382 - $parser = ( new ParserFactory( ) )->createForNewestSupportedVersion( );
408 + $this->log( '⚡ Arguments provided: ' . json_encode( $args ) );
409 + }
383 410
384 - if( !$this->snippet ){
385 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
386 - }
411 + // Global snippets are meant to be always accessible. On non-whitelisted REST routes
412 + // (Workflow Engine, MCP, AI function-calling) the plugins_loaded pass blocks them, so
413 + // make sure their helper library is loaded before we run a function that may call it.
414 + $this->load_global_snippets();
387 415
388 - // First we check the function names are unique
389 - $fn = $this->snippet->sanitize_and_check_functions( $code, $new_snippet );
390 - if ( ! $fn['is_valid'] ) {
416 + // Make every *other* active PHP function snippet available so this function can
417 + // call its siblings. We pass the current name as the exception so the target is
418 + // still defined below (with the edited/test code when testing), not pre-defined here.
419 + $this->define_all_functions( $params['name'] );
391 420
392 - $lint = [
393 - 'line' => 1,
394 - 'attributes' => $fn['attributes'][0],
395 - 'raw_message' => implode(', ', $fn['errors'][0]),
396 - 'message' => implode(', ', $fn['errors'][0]),
397 - ];
421 + // Check if the function has already been defined
422 + if ( !in_array( $params['name'], $defined_functions ) ) {
398 423
399 - return $lint;
400 - }
424 + // If not, proceed with modification and definition
425 + if ( $params['test'] ) { // Make sure the echo statement uses a line break
426 + $params['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $params['code'] );
427 + } else { // Remove all echo statements
428 + $params['code'] = preg_replace( '/echo\s+(.+?);/s', '', $params['code'] );
429 + }
401 430
402 - try {
403 - $stmts = $parser->parse( $code );
404 - $result = $stmts;
405 - } catch ( PhpParser\Error $e ) {
431 + $params['code'] = "if (!function_exists('{$params['name']}')) {\n" . $params['code'] . "\n}\n";
406 432
407 - $lint = [
408 - 'line' => $e->getStartLine(),
409 - 'attributes' => $e->getAttributes(),
410 - 'raw_message' => $e->getRawMessage(),
411 - 'message' => $e->getMessage(),
412 - ];
433 + // Add the function name to the array to avoid redefinition
434 + $defined_functions[] = $params['name'];
435 + } else {
436 + // If already defined, just prepare to call the function without redefining it
437 + $params['code'] = '';
438 + }
413 439
414 - return $lint;
415 - }
440 + // Prepare the code to be executed
441 + $params['code'] .= "\n\$mwcode_result = {$params['name']}(";
442 + foreach ( $params['args'] as $index => $arg ) {
443 + $value = 'null'; // In case the argument is not provided it will be null
416 444
417 - return null;
418 - }
445 + if ( array_key_exists( $arg, $params['values'] ) ) { // Avoid warnings if the argument is not provided
419 446
420 - public function get_js_functions_to_push() {
421 - $functions = $this->snippet->get_functions();
422 - $js_functions = [];
423 - foreach ( $functions as &$function ) {
424 - if ( !isset( $function['target'] ) ) {
425 - $function['target'] = 'php';
426 - }
427 - if ( $function['target'] == 'js' ) {
428 - $js_functions[] = $function;
429 - }
430 - }
431 - $snippets = [];
432 - foreach ( $js_functions as $function ) {
433 - $snippet = $this->snippet->select_one( $function['snippetId'] );
434 - $snippet['function_info'] = $function; // Add function info to snippet
435 - $snippets[] = $snippet;
436 - }
437 -
438 - return $this->generate_js_functions_code( $snippets );
439 - }
440 -
441 - function generate_js_functions_code ($snippets ) {
442 - $code = "";
443 - foreach ( $snippets as $snippet ) {
444 - $function_code = $snippet['code'];
445 - $function_info = $snippet['function_info'];
446 -
447 - // Extract function name and arguments
448 - preg_match( '/(?:const|let|var)?\s*(\w+)\s*=\s*\((.*?)\)\s*=>/', $function_code, $matches );
449 - $function_name = $matches[1] ?? $function_info['name'];
450 - $function_args = $matches[2] ?? '';
451 -
452 - // Prepare default values
453 - $default_args = [];
454 - foreach ( $function_info['args'] as $arg ) {
455 - if ( isset( $arg['default'] ) && $arg['default'] !== '' ) {
456 - $default_args[$arg['name']] = $arg['default'];
457 - }
458 - }
459 -
460 - // Modify function to use default values
461 - if ( !empty( $default_args ) ) {
462 - $new_args = explode( ',', $function_args );
463 - foreach ( $new_args as &$arg ) {
464 - $arg = trim( $arg );
465 - if ( isset( $default_args[$arg] ) ) {
466 - $arg .= " = " . json_encode( $default_args[$arg] );
467 - }
468 - }
469 - $new_args_string = implode( ', ', $new_args );
470 - $function_code = preg_replace(
471 - '/(\w+)\s*=\s*\((.*?)\)\s*=>/',
472 - "$1 = ($new_args_string) =>",
473 - $function_code
474 - );
475 - }
476 -
477 - $code .= $function_code . "\n\n";
478 - }
447 + // If the argument is provided, use it, if not use the default value
448 + if ( !empty( $params['values'][$arg]['input'] ) ) {
449 + $value = $params['values'][$arg]['input'];
479 450
480 - return $code;
481 - }
451 + } else if ( !empty( $params['values'][$arg]['default'] ) ) {
452 + $value = $params['values'][$arg]['default'];
453 + }
454 + }
482 455
456 + $params['code'] .= "{$value}";
457 + if ( $index < count( $params['args'] ) - 1 ) {
458 + $params['code'] .= ', ';
459 + }
460 + }
483 461
484 - /**
485 - * [STATIC] Execute active snippets.
486 - *
487 - * @return array
488 - */
489 - public function execute_active_snippets() {
462 + $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
490 463
491 - $blocked = false;
492 - $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
493 - if ( $page === 'mwcode_settings' || !Meow_MWCODE_Core::is_white_listed_rest() ) {
494 - $blocked = true;
495 - }
464 + $error = null;
465 + $output = null;
466 +
467 + try {
468 + ob_start();
469 + eval( $params['code'] );
470 + $output = ob_get_clean();
471 +
472 + if ( $params['test'] ){
473 + $output = explode( "\n", $output );
474 + }
475 +
476 + } catch ( Throwable $e ) {
477 + //$this->log('Code Engine: Error executing the function: ' . $e->getMessage());
478 + $error = new Exception(' Error executing the function, ' . $e->getMessage());
496 479
497 - if ( empty( $this->snippet ) ) {
498 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
499 - }
480 + ob_clean();
481 + } finally {
482 + restore_error_handler();
483 + }
500 484
501 - $ts = $this->get_option( 'thrown_snippet', null );
502 - if ( !empty( $ts ) ) {
503 - $this->log( "⚠️ Your snippet \"{$ts['name']}\" has thrown a fatal error last time, so we disabled it. Please check the logs for more information." );
504 - $this->snippet->force_disable( $ts['id'] );
505 - $this->update_option( 'thrown_snippet', null );
506 - }
485 + if ( $error !== null ) {
486 + if( $params['test'] ){
487 + $output['error'] = $error->getMessage();
488 + } else {
489 + throw $error;
490 + }
491 + }
507 492
508 - $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ];
509 - // Get all active snippets
493 + return $output;
494 + }
510 495
511 -
512 496
513 - $snippets = $this->snippet->select(
514 - null, // offset
515 - -1, // limit
516 - [
517 - [ 'accessor' => 'active', 'value' => 1 ],
518 - [ 'accessor' => 'scope', 'value' => $scope ],
519 - ], // filter
520 - [ 'accessor' => 'priority', 'by' => 'DESC' ] // sort
521 - )['data'];
497 + function parse_snippet( $code, $new_snippet = false ){
498 + $parser = ( new ParserFactory( ) )->createForNewestSupportedVersion( );
522 499
500 + if( !$this->snippet ){
501 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
502 + }
523 503
524 - if ( empty( $snippets ) ) {
525 - return;
526 - }
504 + // First we check the function names are unique
505 + $fn = $this->snippet->sanitize_and_check_functions( $code, $new_snippet );
506 + if ( ! $fn['is_valid'] ) {
527 507
528 - $snippets = array_map( function ( $snippet ) use ( $blocked ) {
529 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
530 - $snippet['blocked'] = $blocked;
508 + $lint = [
509 + 'line' => 1,
510 + 'attributes' => $fn['attributes'][0],
511 + 'raw_message' => implode(', ', $fn['errors'][0]),
512 + 'message' => implode(', ', $fn['errors'][0]),
513 + ];
531 514
532 - // If the snippet must be executed only in the frontend, we bypass the block
533 - if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
534 - $snippet['blocked'] = false;
535 - }
515 + return $lint;
516 + }
536 517
537 - return $snippet;
538 - }, $snippets );
518 + try {
519 + $stmts = $parser->parse( $code );
520 + $result = $stmts;
521 + } catch ( PhpParser\Error $e ) {
539 522
540 -
523 + $lint = [
524 + 'line' => $e->getStartLine(),
525 + 'attributes' => $e->getAttributes(),
526 + 'raw_message' => $e->getRawMessage(),
527 + 'message' => $e->getMessage(),
528 + ];
541 529
542 - return $snippets;
530 + return $lint;
543 531 }
544 532
533 + return null;
534 + }
545 535
546 - #endregion
536 + /**
537 + * Load the active global snippets (persistent + backend/frontend for this context)
538 + * that haven't already run this request, so on-demand function execution has the same
539 + * always-available helper library a normal page load would. Callable functions are
540 + * typically small wrappers around these globals.
541 + *
542 + * On non-whitelisted REST routes (Workflow Engine, MCP, AI function-calling) the
543 + * plugins_loaded pass blocks global snippets for safety; this restores them for the
544 + * deliberate, authorized act of executing a snippet. The loaded-id registry guarantees
545 + * each global runs at most once per request, so nothing is ever re-declared.
546 + */
547 + function load_global_snippets() {
548 + global $current_mwcode_snippet;
549 + static $done = false;
550 + if ( $done ) {
551 + return;
552 + }
553 + $done = true;
547 554
548 - #region Logs
555 + if ( empty( $this->snippet ) ) {
556 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
557 + }
549 558
550 - function get_logs() {
551 - $log_file_path = $this->get_logs_path();
559 + $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ];
552 560
553 - if ( !file_exists( $log_file_path ) ) {
554 - return "Empty log file.";
555 - }
561 + $snippets = $this->snippet->select(
562 + null, // offset
563 + -1, // limit (all)
564 + [
565 + [ 'accessor' => 'active', 'value' => 1 ],
566 + [ 'accessor' => 'scope', 'value' => $scope ],
567 + ],
568 + [ 'accessor' => 'priority', 'by' => 'DESC' ]
569 + )['data'] ?? [];
556 570
557 - $content = file_get_contents( $log_file_path );
558 - $lines = explode( "\n", $content );
559 - $lines = array_filter( $lines );
560 - $lines = array_reverse( $lines );
561 - $content = implode( "\n", $lines );
562 - return $content;
563 - }
571 + foreach ( $snippets as $snippet ) {
572 + // Skip globals already executed this request (e.g. by the plugins_loaded pass).
573 + if ( in_array( $snippet['id'], $this->loaded_global_ids ) ) {
574 + continue;
575 + }
576 + $this->loaded_global_ids[] = $snippet['id'];
564 577
565 - function clear_logs() {
566 - $logPath = $this->get_logs_path();
567 - if ( file_exists( $logPath ) ) {
568 - unlink( $logPath );
569 - }
578 + $code = $this->snippet->sanitize_code( $snippet['code'] );
579 + $current_mwcode_snippet = $snippet;
580 + try {
581 + ob_start();
582 + eval( $code );
583 + ob_end_clean();
584 + } catch ( Throwable $e ) {
585 + ob_end_clean();
586 + $this->log( "⚠️ Code Engine: Failed to load global snippet \"{$snippet['name']}\": " . $e->getMessage() );
587 + }
588 + }
589 + $current_mwcode_snippet = null;
590 + }
570 591
571 - $options = $this->get_all_options();
572 - $options['logs_path'] = null;
573 - $this->update_options( $options );
574 - }
592 + /**
593 + * Declare every active PHP function snippet in the current request, without
594 + * invoking any of them, so function snippets can call one another.
595 + *
596 + * Function snippets are not auto-loaded on every request (unlike global/backend/
597 + * frontend scopes) — they are meant to run on demand. This is the PHP counterpart
598 + * to get_js_functions_to_push(): it makes the whole library of functions callable
599 + * before a function is executed (via REST, MCP, AI function-calling, Workflow Engine).
600 + *
601 + * Idempotent: a static guard runs the full pass only once per request, and each
602 + * definition is wrapped in function_exists() so nothing is ever redefined.
603 + *
604 + * @param string|null $except Function name to skip (the one run_snippet is about to
605 + * define itself, so edited/test code keeps priority).
606 + */
607 + function define_all_functions( $except = null ) {
608 + static $loaded = false;
609 + if ( $loaded ) {
610 + return;
611 + }
612 + $loaded = true;
575 613
576 - function get_logs_path() {
577 - $uploads_dir = wp_upload_dir();
578 - $uploads_dir_path = trailingslashit( $uploads_dir['basedir'] );
614 + if ( empty( $this->snippet ) ) {
615 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
616 + }
579 617
580 - $path = $this->get_option( 'logs_path' );
618 + // One query for every active function snippet (code included), then enrich with
619 + // the function metadata (name + target) the same way run_snippet does.
620 + $snippets = $this->snippet->select(
621 + null, // offset
622 + -1, // limit (all)
623 + [
624 + [ 'accessor' => 'active', 'value' => 1 ],
625 + [ 'accessor' => 'scope', 'value' => 'function' ],
626 + ],
627 + [] // sort
628 + )['data'] ?? [];
581 629
582 - if ( $path && file_exists( $path ) ) {
583 - // make sure the path is legal (within the uploads directory with the MWCODE_PREFIX and log extension)
584 - if ( strpos( $path, $uploads_dir_path ) !== 0 || strpos( $path, MWCODE_PREFIX ) === false || substr( $path, -4 ) !== '.log' ) {
585 - $path = null;
586 - } else {
587 - return $path;
588 - }
589 - }
630 + if ( empty( $snippets ) ) {
631 + return;
632 + }
590 633
591 - if ( !$path ) {
592 - $path = $uploads_dir_path . MWCODE_PREFIX . "_" . $this->random_ascii_chars() . ".log";
593 - if ( !file_exists( $path ) ) {
594 - touch( $path );
595 - }
596 - $options = $this->get_all_options();
597 - $options['logs_path'] = $path;
598 - $this->update_options( $options );
599 - }
634 + $this->snippet->get_function_snippets_data( $snippets );
600 635
601 - return $path;
602 - }
636 + foreach ( $snippets as $snippet ) {
637 + $name = $snippet['functionName'] ?? '';
638 + $target = strtolower( $snippet['functionTarget'] ?? 'php' );
603 639
604 - function log( $data = null ) {
605 - if ( !$this->get_option( 'server_debug_mode', false ) ) { return false; }
606 - $log_file_path = $this->get_logs_path();
607 - $fh = @fopen( $log_file_path, 'a' );
608 - if ( !$fh ) { return false; }
609 - $date = date( "Y-m-d H:i:s" );
610 - if ( is_null( $data ) ) {
611 - fwrite( $fh, "\n" );
612 - }
613 - else {
614 - fwrite( $fh, "$date: {$data}\n" );
615 - //$this->log( "[MWCODE] $data" );
616 - }
617 - fclose( $fh );
618 - return true;
619 - }
640 + // Skip JS functions (pushed to the front-end separately), the function the
641 + // caller will define itself, and anything already declared in this request.
642 + if ( $name === '' || $target === 'js' || $name === $except || function_exists( $name ) ) {
643 + continue;
644 + }
620 645
621 - private function random_ascii_chars( $length = 8 ) {
622 - $characters = array_merge( range( 'A', 'Z' ), range( 'a', 'z' ), range( '0', '9' ) );
623 - $characters_length = count( $characters );
624 - $random_string = '';
646 + // Mirror run_snippet()'s non-test handling: drop echo statements, then declare
647 + // (never call) the function, guarded so a later run_snippet() call is a no-op.
648 + $code = $this->snippet->sanitize_code( $snippet['code'] );
649 + $code = preg_replace( '/echo\s+(.+?);/s', '', $code );
650 + $code = "if (!function_exists('{$name}')) {\n{$code}\n}\n";
625 651
626 - for ( $i = 0; $i < $length; $i++ ) {
627 - $random_string .= $characters[rand(0, $characters_length - 1)];
628 - }
652 + try {
653 + eval( $code );
654 + } catch ( Throwable $e ) {
655 + $this->log( "⚠️ Code Engine: Failed to pre-define function \"{$name}\": " . $e->getMessage() );
656 + }
657 + }
658 + }
629 659
630 - return $random_string;
631 - }
660 + public function get_js_functions_to_push() {
661 + $functions = $this->snippet->get_functions();
662 + $js_functions = [];
663 + foreach ( $functions as &$function ) {
664 + if ( !isset( $function['target'] ) ) {
665 + $function['target'] = 'php';
666 + }
667 + if ( $function['target'] == 'js' ) {
668 + $js_functions[] = $function;
669 + }
670 + }
671 + $snippets = [];
672 + foreach ( $js_functions as $function ) {
673 + $snippet = $this->snippet->select_one( $function['snippetId'] );
674 + $snippet['function_info'] = $function; // Add function info to snippet
675 + $snippets[] = $snippet;
676 + }
632 677
633 - #endregion
678 + return $this->generate_js_functions_code( $snippets );
679 + }
680 +
681 + function generate_js_functions_code ($snippets ) {
682 + $code = "";
683 + foreach ( $snippets as $snippet ) {
684 + $function_code = $snippet['code'];
685 + $function_info = $snippet['function_info'];
634 686
635 - #region Helpers
687 + // Extract function name and arguments
688 + preg_match( '/(?:const|let|var)?\s*(\w+)\s*=\s*\((.*?)\)\s*=>/', $function_code, $matches );
689 + $function_name = $matches[1] ?? $function_info['name'];
690 + $function_args = $matches[2] ?? '';
636 691
637 - /**
638 - * Check if the request is from a white-listed REST route.
639 - *
640 - * @return bool
641 - */
642 - public static function is_white_listed_rest() {
643 - $authorized = false;
644 - $white_listed = array(
645 - 'mwai/v1',
646 - 'mwai-ui/v1',
647 - 'media-file-renamer/v1',
648 - 'media-cleaner/v1',
649 - 'wplr/v1',
650 - 'code-engine/v1',
651 - 'wp/v2',
652 - 'meow-gallery/v1',
692 + // Prepare default values
693 + $default_args = [];
694 + foreach ( $function_info['args'] as $arg ) {
695 + if ( isset( $arg['default'] ) && $arg['default'] !== '' ) {
696 + $default_args[$arg['name']] = $arg['default'];
697 + }
698 + }
699 +
700 + // Modify function to use default values
701 + if ( !empty( $default_args ) ) {
702 + $new_args = explode( ',', $function_args );
703 + foreach ( $new_args as &$arg ) {
704 + $arg = trim( $arg );
705 + if ( isset( $default_args[$arg] ) ) {
706 + $arg .= " = " . json_encode( $default_args[$arg] );
707 + }
708 + }
709 + $new_args_string = implode( ', ', $new_args );
710 + $function_code = preg_replace(
711 + '/(\w+)\s*=\s*\((.*?)\)\s*=>/',
712 + "$1 = ($new_args_string) =>",
713 + $function_code
653 714 );
715 + }
654 716
655 - $white_listed = apply_filters( 'meow_mwcode_white_listed_rest', $white_listed );
717 + $code .= $function_code . "\n\n";
718 + }
656 719
657 - $route = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : null;
658 - $requested_route = null;
659 -
660 - if ( $route ) {
661 - $route_parts = explode( '/wp-json/', $route );
662 -
663 - if ( isset( $route_parts[1] ) ) {
664 - $requested_route = trim( $route_parts[1], '/' );
665 - foreach ( $white_listed as $white_listed_route ) {
666 - if ( strpos( $requested_route, $white_listed_route ) === 0 ) {
667 - $authorized = true;
668 - $authorized = apply_filters( 'meow_mwcode_white_listed_rest_authorized', $authorized, $requested_route );
669 - return $authorized;
670 - }
671 - }
672 - }
673 -
674 - if ( is_admin() ) {
675 - $authorized = true;
720 + return $code;
721 + }
676 722
677 - $authorized = apply_filters( 'meow_mwcode_white_listed_rest_authorized', $authorized, $requested_route );
678 - return $authorized;
679 - }
680 723
724 + /**
725 + * [STATIC] Execute active snippets.
726 + *
727 + * @return array
728 + */
729 + public function execute_active_snippets() {
681 730
731 + $blocked = false;
732 + $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
733 +
734 +
735 + if ( $page === 'mwcode_settings' ) {
736 + // If we blocks global snippets like nonce_life filter, we would block the settings page so let's remove the block for this page
737 +
738 + $blocked = false;
739 + //$blocked = true;
740 + }
741 + // Block REST requests that aren't whitelisted
742 + elseif ( MeowKit_MWCODE_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
743 + $blocked = true;
744 + }
745 +
746 + if ( empty( $this->snippet ) ) {
747 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
748 + }
749 +
750 + $ts = $this->get_option( 'thrown_snippet', null );
751 + if ( !empty( $ts ) ) {
752 + $this->log( "⚠️ Your snippet \"{$ts['name']}\" has thrown a fatal error last time, so we disabled it. Please check the logs for more information." );
753 + $this->snippet->force_disable( $ts['id'] );
754 + $this->update_option( 'thrown_snippet', null );
755 + }
756 +
757 + $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ];
758 + // Get all active snippets
759 +
760 + $snippets = $this->snippet->select(
761 + null, // offset
762 + -1, // limit
763 + [
764 + [ 'accessor' => 'active', 'value' => 1 ],
765 + [ 'accessor' => 'scope', 'value' => $scope ],
766 + ], // filter
767 + [ 'accessor' => 'priority', 'by' => 'DESC' ] // sort
768 + )['data'];
769 +
770 + if ( empty( $snippets ) ) {
771 + return;
772 + }
773 +
774 + $snippets = array_map( function ( $snippet ) use ( $blocked ) {
775 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
776 + $snippet['blocked'] = $blocked;
777 +
778 + // If the snippet must be executed only in the frontend, we bypass the block
779 + if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
780 + $snippet['blocked'] = false;
781 + }
782 +
783 + return $snippet;
784 + }, $snippets );
785 +
786 + return $snippets;
787 + }
788 +
789 +
790 + #endregion
791 +
792 + #region Shortcodes
793 + function separate_mwcode_atts( $atts ) {
794 +
795 + if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] );
796 + if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] );
797 + if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] );
798 +
799 + return $atts;
800 + }
801 +
802 + function content_shortcode( $atts ) {
803 +
804 + $user_atts = $this->separate_mwcode_atts( $atts );
805 +
806 + $atts = shortcode_atts( array(
807 + 'id' => null,
808 + 'target' => null, // js or php
809 + 'code' => null, // For Guttenberg block usage
810 + ), $atts, 'code-engine' );
811 +
812 + $id = $atts['id'];
813 + $target = $atts['target'];
814 + $code = $atts['code'];
815 + $current_post = get_post();
816 +
817 + $no_js = defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML;
818 + $allow_php = $this->get_option( 'code_blocks', false );
819 + $allow_php_whitelist = $this->get_option( 'code_blocks_whitelist', [] );
820 +
821 + // If the ID is null, it means it comes from a Guttenberg block
822 + $is_block = empty( $id ) && !empty( $code );
823 +
824 + if( $is_block ) {
825 +
826 + if( $target !== 'js' && $target !== 'php' ) {
827 + return '<b>Code Engine:</b> Please provide a valid target (js or php).';
828 + }
829 +
830 + if ( $no_js && $target === 'js' ) {
831 + return '<b>Code Engine:</b> Code Block JS are disabled because unfiltered HTML is not allowed on your server.';
832 + }
833 +
834 + if ( $target === 'php' ) {
835 +
836 + if ( !$allow_php ) {
837 + return '<b>Code Engine:</b> Code Block PHP are disabled. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
682 838 }
683 839
684 - $authorized = apply_filters( 'meow_mwcode_white_listed_rest_authorized', $authorized, $requested_route );
685 - return $authorized;
840 + if ( !empty( $allow_php_whitelist ) && !in_array( $current_post->ID, $allow_php_whitelist ) ) {
841 + return '<b>Code Engine:</b> Code Block PHP are disabled for this post. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
842 + }
843 + }
844 +
845 + // Because the code from Blocks are sanitized, we need to replace the &quot; with "
846 + $code = str_replace( '&quot;', '"', $code );
847 +
848 + if ( $target === 'js' ) {
849 + $output = '<script>' . $code . '</script>';
850 + }
851 +
852 + if ( $target === 'php' ) {
853 + $output = $this->run_non_fn_snippet( null, $code );
854 + }
855 +
856 + return $output;
686 857 }
687 858
688 - #endregion
859 + // If not a block, we get the snippet by ID
860 + // If the ID is not null, it means it comes from a shortcode
861 + if ( empty( $id ) && empty( $code ) ) {
862 + return '<b>Code Engine:</b> Please provide a snippet ID.';
863 + }
864 +
865 + $snippet = $this->get_snippet( $id );
866 +
867 + if ( empty( $snippet ) ) {
868 + return '<b>Code Engine:</b> The snippet does not exist.';
869 + }
870 +
871 + //Check if the snippet scope is either content_php or content_js
872 + $is_content_php = $snippet['scope'] === 'content_php';
873 + $is_content_js = $snippet['scope'] === 'content_js';
874 +
875 + if ( !$is_content_php && !$is_content_js ) {
876 + return '<b>Code Engine:</b> The snippet is not a content snippet.';
877 + }
878 +
879 + if( $no_js && $is_content_js ) {
880 + return '<b>Code Engine:</b> Code Engine JS snippets are disabled because unfiltered HTML is not allowed on your server.';
881 + }
882 +
883 + //Check if the snippet is active
884 + if ( !$snippet['active'] ) {
885 + return '<b>Code Engine:</b> The snippet is not active.';
886 + }
887 +
888 + $output = '<b>Code Engine:</b> No output.';
889 +
890 + if ( $is_content_js ) {
891 + $output = '<script>' . $snippet['code'] . '</script>';
892 + }
893 +
894 + if ( $is_content_php ) {
895 + $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );";
896 + $output = $this->run_non_fn_snippet( $id, null, false, $prefix );
897 + }
898 +
899 + return $output;
900 + }
901 +
902 + #endregion
903 +
904 + #region Logs
905 +
906 + function get_logs() {
907 + $log_file_path = $this->get_logs_path();
908 +
909 + if ( !file_exists( $log_file_path ) ) {
910 + return "Empty log file.";
911 + }
912 +
913 + $content = file_get_contents( $log_file_path );
914 + $lines = explode( "\n", $content );
915 + $lines = array_filter( $lines );
916 + $lines = array_reverse( $lines );
917 + $content = implode( "\n", $lines );
918 + return $content;
919 + }
920 +
921 + function clear_logs() {
922 + $logPath = $this->get_logs_path();
923 + if ( file_exists( $logPath ) ) {
924 + unlink( $logPath );
925 + }
926 +
927 + $options = $this->get_all_options();
928 + $options['logs_path'] = null;
929 + $this->update_options( $options );
930 + }
931 +
932 + function get_logs_path() {
933 + $uploads_dir = wp_upload_dir();
934 + $uploads_dir_path = trailingslashit( $uploads_dir['basedir'] );
935 +
936 + $path = $this->get_option( 'logs_path' );
937 +
938 + if ( $path && file_exists( $path ) ) {
939 + // make sure the path is legal (within the uploads directory with the MWCODE_PREFIX and log extension)
940 + if ( strpos( $path, $uploads_dir_path ) !== 0 || strpos( $path, MWCODE_PREFIX ) === false || substr( $path, -4 ) !== '.log' ) {
941 + $path = null;
942 + } else {
943 + return $path;
944 + }
945 + }
946 +
947 + if ( !$path ) {
948 + $path = $uploads_dir_path . MWCODE_PREFIX . "_" . $this->random_ascii_chars() . ".log";
949 + if ( !file_exists( $path ) ) {
950 + touch( $path );
951 + }
952 + $options = $this->get_all_options();
953 + $options['logs_path'] = $path;
954 + $this->update_options( $options );
955 + }
956 +
957 + return $path;
958 + }
959 +
960 + function log( $data = null ) {
961 + if ( !$this->get_option( 'server_debug_mode', false ) ) { return false; }
962 + $log_file_path = $this->get_logs_path();
963 + $fh = @fopen( $log_file_path, 'a' );
964 + if ( !$fh ) { return false; }
965 + $date = date( "Y-m-d H:i:s" );
966 + if ( is_null( $data ) ) {
967 + fwrite( $fh, "\n" );
968 + }
969 + else {
970 + fwrite( $fh, "$date: {$data}\n" );
971 + //$this->log( "[MWCODE] $data" );
972 + }
973 + fclose( $fh );
974 + return true;
975 + }
976 +
977 + private function random_ascii_chars( $length = 8 ) {
978 + $characters = array_merge( range( 'A', 'Z' ), range( 'a', 'z' ), range( '0', '9' ) );
979 + $characters_length = count( $characters );
980 + $random_string = '';
981 +
982 + for ( $i = 0; $i < $length; $i++ ) {
983 + $random_string .= $characters[rand(0, $characters_length - 1)];
984 + }
985 +
986 + return $random_string;
987 + }
988 +
989 + #endregion
990 +
991 + #region Helpers
992 +
993 + /**
994 + * Check if the request is from a white-listed REST route.
995 + *
996 + * @return bool
997 + */
998 + public static function is_white_listed_rest() {
999 + $options = get_option( 'mwcode_snippet_vault_options', array() );
1000 +
1001 + // Early return if bypass is enabled
1002 + if ( !empty( $options['bypass_rest_security'] ) ) {
1003 + return true;
1004 + }
1005 +
1006 + // Early return for admin requests
1007 + if ( is_admin() ) {
1008 + return apply_filters( 'mwcode_rest_authorized', true, null );
1009 + }
1010 +
1011 + // Get the requested route
1012 + $requested_route = self::get_requested_rest_route();
1013 + if ( !$requested_route ) {
1014 + return apply_filters( 'mwcode_rest_authorized', false, null );
1015 + }
1016 +
1017 + // Check against whitelist
1018 + $white_listed = apply_filters( 'mwcode_rest_whitelist', array(
1019 + 'mwai/v1',
1020 + 'mwai-ui/v1',
1021 + 'media-file-renamer/v1',
1022 + 'media-cleaner/v1',
1023 + 'wplr/v1',
1024 + 'code-engine/v1',
1025 + 'wp/v2',
1026 + 'meow-gallery/v1',
1027 + 'mcp/v1',
1028 + ));
1029 +
1030 + $authorized = self::is_route_whitelisted( $requested_route, $white_listed );
1031 +
1032 + // Log if debug mode is enabled
1033 + if ( !empty( $options['server_debug_mode'] ) ) {
1034 + self::log_route_status( $requested_route, $authorized );
1035 + }
1036 +
1037 + return apply_filters( 'mwcode_rest_authorized', $authorized, $requested_route );
1038 + }
1039 +
1040 + /**
1041 + * Extract the REST route from the request URI.
1042 + *
1043 + * @return string|null
1044 + */
1045 + public static function get_requested_rest_route() {
1046 + if ( !isset( $_SERVER['REQUEST_URI'] ) ) {
1047 + return null;
1048 + }
1049 +
1050 + $route_parts = explode( '/wp-json/', $_SERVER['REQUEST_URI'] );
1051 +
1052 + if ( isset( $route_parts[1] ) ) {
1053 + return trim( $route_parts[1], '/' );
1054 + }
1055 +
1056 + return null;
1057 + }
1058 +
1059 + /**
1060 + * Check if a route is in the whitelist.
1061 + *
1062 + * @param string $route The route to check
1063 + * @param array $white_listed The whitelist array
1064 + * @return bool
1065 + */
1066 + private static function is_route_whitelisted( $route, $white_listed ) {
1067 + foreach ( $white_listed as $white_listed_route ) {
1068 + if ( strpos( $route, $white_listed_route ) === 0 ) {
1069 + return true;
1070 + }
1071 + }
1072 + return false;
1073 + }
1074 +
1075 + /**
1076 + * Log the route authorization status.
1077 + *
1078 + * @param string $route The route being checked
1079 + * @param bool $authorized Whether the route is authorized
1080 + */
1081 + private static function log_route_status( $route, $authorized ) {
1082 + global $mwcode_core;
1083 +
1084 + $message = $authorized
1085 + ? "✅ REST route authorized: " . $route
1086 + : "❌ REST route rejected (not whitelisted): " . $route;
1087 +
1088 + if ( isset( $mwcode_core ) ) {
1089 + $mwcode_core->log( $message );
1090 + } else {
1091 + error_log( "[Code Engine] " . $message );
1092 + }
1093 + }
1094 +
1095 + #endregion
689 1096 }
690 1097
691 1098 ?>