| @@ -12,9 +12,14 @@ | ||
| 12 | 12 | public $is_rest = false; |
| 13 | 13 | public $is_cli = false; |
| 14 | 14 | public $site_url = null; |
| 15 | 15 | public $mwcode = null; |
| 16 | + public $licenser = null; | |
| 16 | 17 | |
| 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 = []; | |
| 21 | + | |
| 17 | 22 | private $option_name = 'mwcode_options'; |
| 18 | 23 | |
| 19 | 24 | public function __construct() { |
| 20 | 25 | global $mwcode; |
| @@ -19,9 +24,9 @@ | ||
| 19 | 24 | public function __construct() { |
| 20 | 25 | global $mwcode; |
| 21 | 26 | |
| 22 | 27 | $this->site_url = get_site_url(); |
| 23 | - $this->is_rest = MeowCommon_Helpers::is_rest(); | |
| 28 | + $this->is_rest = MeowKit_MWCODE_Helpers::is_rest(); | |
| 24 | 29 | $this->is_cli = defined( 'WP_CLI' ) && WP_CLI; |
| 25 | 30 | |
| 26 | 31 | // Snippets |
| 27 | 32 | $snippet = new Meow_MWCODE_Modules_Snippet( $this ); |
| @@ -37,8 +42,13 @@ | ||
| 37 | 42 | add_action( 'plugins_loaded', array( $this, 'init' ) ); |
| 38 | 43 | } |
| 39 | 44 | |
| 40 | 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 | + } | |
| 50 | + | |
| 41 | 51 | // Part of the core, settings and stuff |
| 42 | 52 | $this->admin = new Meow_MWCODE_Admin( $this ); |
| 43 | 53 | |
| 44 | 54 | // Only for REST |
| @@ -83,9 +93,11 @@ | ||
| 83 | 93 | return [ |
| 84 | 94 | //Safemode |
| 85 | 95 | "safe_mode_status" => "on", // on, off, whitelist |
| 86 | 96 | "safe_mode_whitelist" => [], |
| 87 | - "disallow_block_php" => true, // Do not allow PHP code to be execute through Blocks "code" parameter | |
| 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 | |
| 88 | 100 | |
| 89 | 101 | //LOGS |
| 90 | 102 | "server_debug_mode" => false, |
| 91 | 103 | |
| @@ -94,8 +106,9 @@ | ||
| 94 | 106 | |
| 95 | 107 | //AI |
| 96 | 108 | "ai_suggestions" => false, |
| 97 | 109 | "ai_engine_status"=> false, |
| 110 | + "mwai_active" => false, | |
| 98 | 111 | "ai_engine_message" => "", |
| 99 | 112 | |
| 100 | 113 | //API |
| 101 | 114 | "api_endpoint" => false, |
| @@ -102,8 +115,12 @@ | ||
| 102 | 115 | "api_token" => md5( time() . rand() ), |
| 103 | 116 | |
| 104 | 117 | //MCP |
| 105 | 118 | "mcp_support" => false, |
| 119 | + "mcp_functions" => false, | |
| 120 | + | |
| 121 | + //MAINTENANCE | |
| 122 | + "clean_uninstall" => false, | |
| 106 | 123 | ]; |
| 107 | 124 | } |
| 108 | 125 | |
| 109 | 126 | function get_all_options( ) { |
| @@ -117,19 +134,15 @@ | ||
| 117 | 134 | return $options; |
| 118 | 135 | } |
| 119 | 136 | |
| 120 | 137 | function update_options( $options ) { |
| 121 | - $current_options = get_option($this->option_name); | |
| 122 | 138 | |
| 123 | - if ($current_options === $options) { | |
| 124 | - // $this->log('💾 The options are already the expected value.'); | |
| 125 | - } else { | |
| 126 | - if ( !update_option( $this->option_name, $options, false ) ) { | |
| 127 | - $this->log( '💾 There was an issue updating the options.' ); | |
| 128 | - } | |
| 139 | + $options = $this->sanitize_options( $options ); | |
| 140 | + | |
| 141 | + if ( !update_option( $this->option_name, $options, false ) ) { | |
| 142 | + //$this->log( '💾 There was an issue updating the options.' ); | |
| 129 | 143 | } |
| 130 | - | |
| 131 | - $options = $this->sanitize_options( $options ); | |
| 144 | + | |
| 132 | 145 | return $options; |
| 133 | 146 | } |
| 134 | 147 | |
| 135 | 148 | function update_option( $option, $value ) { |
| @@ -160,9 +173,9 @@ | ||
| 160 | 173 | $options_modified = true; |
| 161 | 174 | } |
| 162 | 175 | |
| 163 | 176 | // Update AI Engine status |
| 164 | - $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified; | |
| 177 | + $options = $this->updateAIEngineStatus( $options ); | |
| 165 | 178 | |
| 166 | 179 | // Disable AI related features if AI Engine is not available |
| 167 | 180 | if ( ! $options['ai_engine_status'] ) { |
| 168 | 181 | if ( $options['ai_suggestions'] !== false ) { |
| @@ -172,12 +185,8 @@ | ||
| 172 | 185 | // Note: We don't disable MCP support here anymore |
| 173 | 186 | // It will be checked at runtime in the MCP class |
| 174 | 187 | } |
| 175 | 188 | |
| 176 | - if ( $options_modified ) { | |
| 177 | - update_option( $this->option_name, $options, false ); | |
| 178 | - } | |
| 179 | - | |
| 180 | 189 | return $options; |
| 181 | 190 | } |
| 182 | 191 | |
| 183 | 192 | private function updateAIEngineStatus( &$options ) { |
| @@ -182,31 +191,17 @@ | ||
| 182 | 191 | |
| 183 | 192 | private function updateAIEngineStatus( &$options ) { |
| 184 | 193 | global $mwai; |
| 185 | 194 | |
| 186 | - if ( is_null( $mwai ) || ! isset( $mwai ) ) { | |
| 187 | - $options['ai_engine_status'] = false; | |
| 188 | - $options['ai_engine_message'] = 'AI Engine is not available.'; | |
| 189 | - return true; | |
| 190 | - } | |
| 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']; | |
| 191 | 202 | |
| 192 | - try { | |
| 193 | - $status = $mwai->checkStatus(); | |
| 194 | - | |
| 195 | - if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) { | |
| 196 | - $options['ai_engine_status'] = true; | |
| 197 | - $options['ai_engine_message'] = $status; | |
| 198 | - return true; | |
| 199 | - } | |
| 200 | - } catch ( Exception $e ) { | |
| 201 | - if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) { | |
| 202 | - $options['ai_engine_status'] = false; | |
| 203 | - $options['ai_engine_message'] = $e->getMessage(); | |
| 204 | - return true; | |
| 205 | - } | |
| 206 | - } | |
| 207 | - | |
| 208 | - return false; | |
| 203 | + return $options; | |
| 209 | 204 | } |
| 210 | 205 | |
| 211 | 206 | #endregion |
| 212 | 207 | |
| @@ -235,9 +230,24 @@ | ||
| 235 | 230 | |
| 236 | 231 | $this->snippet->validate( $params ); |
| 237 | 232 | |
| 238 | 233 | $params = $this->snippet->formatParamsForDatabase( $params ); |
| 239 | - $result = $this->snippet->insert( $params ); | |
| 234 | + | |
| 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 | + } | |
| 240 | 250 | $snippet = $this->snippet->select_one( $result ); |
| 241 | 251 | |
| 242 | 252 | if( $result ) { |
| 243 | 253 | $params['id'] = (string)$result; |
| @@ -277,16 +287,16 @@ | ||
| 277 | 287 | $value = array_map( 'trim', $value ); |
| 278 | 288 | } |
| 279 | 289 | |
| 280 | 290 | if ( $type === 'array' ) { |
| 281 | - $value = json_encode( $value ); | |
| 282 | - $value = str_replace( '\\', '', $value ); | |
| 291 | + // Convert to PHP array format instead of JSON | |
| 292 | + $value = var_export( $value, true ); | |
| 283 | 293 | } |
| 284 | 294 | |
| 285 | 295 | return [ $name, $value ]; |
| 286 | 296 | } |
| 287 | 297 | |
| 288 | - function run_non_fn_snippet( $id, $code = null, $test = false ) { | |
| 298 | + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) { | |
| 289 | 299 | // Retrieve the snippet code from the provided code or via the snippet ID. |
| 290 | 300 | if ( $code ) { |
| 291 | 301 | $snippet = [ 'code' => $code ]; |
| 292 | 302 | } else { |
| @@ -293,13 +303,17 @@ | ||
| 293 | 303 | $snippet = $this->get_snippet( $id ); |
| 294 | 304 | } |
| 295 | 305 | |
| 296 | 306 | // Remove any PHP opening tag. |
| 297 | - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 ); | |
| 307 | + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 298 | 308 | |
| 299 | 309 | if ( $test ) { |
| 300 | 310 | $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] ); |
| 301 | 311 | } |
| 312 | + | |
| 313 | + if( $prefix ) { | |
| 314 | + $snippet['code'] = $prefix . "\n" . $snippet['code']; | |
| 315 | + } | |
| 302 | 316 | |
| 303 | 317 | $error = null; |
| 304 | 318 | $output = null; |
| 305 | 319 | |
| @@ -348,35 +362,17 @@ | ||
| 348 | 362 | 'values' => $snippet['functionArgsDict'] // Contains the default values of the arguments |
| 349 | 363 | ]; |
| 350 | 364 | } |
| 351 | 365 | |
| 352 | - // Sanitize all the arguments if the option is enabled | |
| 353 | - if ( $this->get_option( 'sanitize_arguments', true ) ) { | |
| 366 | + // Arguments used to be sanitized into PHP-literal strings here (quoting, | |
| 367 | + // esc_sql, var_export) so they could be concatenated into a string of PHP and | |
| 368 | + // eval-ed. That is gone: the function is now called with call_user_func_array | |
| 369 | + // (see below), so values are passed as data and need no literal-formatting. | |
| 370 | + // The old formatting also prefixed argument keys with "$" via sanitize_arg, | |
| 371 | + // which stored the provided value under "$name" while the call read "name", so | |
| 372 | + // provided arguments never reached the function. Passing the raw values through | |
| 373 | + // fixes both issues at once. | |
| 354 | 374 | |
| 355 | - if ( $args ) { | |
| 356 | - foreach ( $args as $name => $value ) { | |
| 357 | - list( $sanitizedName, $sanitizedValue ) = $this->sanitize_arg( $name, $value ); | |
| 358 | - unset( $args[$name] ); | |
| 359 | - | |
| 360 | - $args[$sanitizedName] = $sanitizedValue; | |
| 361 | - } | |
| 362 | - } | |
| 363 | - | |
| 364 | - foreach ( $params['values'] as $name => $value ) { | |
| 365 | - | |
| 366 | - if( array_key_exists( 'input', $value) ) { | |
| 367 | - list( $sanitizedInputName, $sanitizedInputValue ) = $this->sanitize_arg( $name, $value['input'], $value['type'] ); | |
| 368 | - $params['values'][$sanitizedInputName]['input'] = $sanitizedInputValue; | |
| 369 | - } | |
| 370 | - | |
| 371 | - if( array_key_exists( 'default', $value) ) { | |
| 372 | - list( $sanitizedDefaultValueName, $sanitizedDefaultValue ) = $this->sanitize_arg( $name, $value['default'], $value['type'] ); | |
| 373 | - $params['values'][$sanitizedDefaultValueName]['default'] = $sanitizedDefaultValue; | |
| 374 | - } | |
| 375 | - } | |
| 376 | - | |
| 377 | - } | |
| 378 | - | |
| 379 | 375 | // Make sure the function is existing and is the one in the snippet |
| 380 | 376 | if ( empty( $params['code'] ) ) { |
| 381 | 377 | throw new Exception( 'Code Engine: The snippet code appears to be empty.' ); |
| 382 | 378 | } |
| @@ -384,17 +380,32 @@ | ||
| 384 | 380 | if ( empty( $params['name'] ) || ! str_contains( $params['code'], $params['name'] ) ) { |
| 385 | 381 | throw new Exception( "Code Engine: Function name does not match. The name should be {$params['name']}." ); |
| 386 | 382 | } |
| 387 | 383 | |
| 388 | - // Overwrite the default values with the provided ones | |
| 384 | + // Collect the provided values, keyed by their normalized (dollar-less) name. | |
| 385 | + // Incoming keys come from the AI/MCP schema, where register_function_tools() | |
| 386 | + // strips a leading "$" from the declared name. The stored arg names can still | |
| 387 | + // carry the "$", so we normalize both sides before matching below. Without this | |
| 388 | + // a value provided as "style" never binds to an argument declared "$style". | |
| 389 | + $provided = []; | |
| 389 | 390 | if ( $args ) { |
| 390 | 391 | foreach ( $args as $name => $value ) { |
| 391 | - $params['values'][$name]['input'] = $value; | |
| 392 | + $provided[ ltrim( $name, '$' ) ] = $value; | |
| 392 | 393 | } |
| 393 | 394 | |
| 394 | 395 | $this->log( '⚡ Arguments provided: ' . json_encode( $args ) ); |
| 395 | 396 | } |
| 396 | 397 | |
| 398 | + // Global snippets are meant to be always accessible. On non-whitelisted REST routes | |
| 399 | + // (Workflow Engine, MCP, AI function-calling) the plugins_loaded pass blocks them, so | |
| 400 | + // make sure their helper library is loaded before we run a function that may call it. | |
| 401 | + $this->load_global_snippets(); | |
| 402 | + | |
| 403 | + // Make every *other* active PHP function snippet available so this function can | |
| 404 | + // call its siblings. We pass the current name as the exception so the target is | |
| 405 | + // still defined below (with the edited/test code when testing), not pre-defined here. | |
| 406 | + $this->define_all_functions( $params['name'] ); | |
| 407 | + | |
| 397 | 408 | // Check if the function has already been defined |
| 398 | 409 | if ( !in_array( $params['name'], $defined_functions ) ) { |
| 399 | 410 | |
| 400 | 411 | // If not, proceed with modification and definition |
| @@ -412,30 +423,32 @@ | ||
| 412 | 423 | // If already defined, just prepare to call the function without redefining it |
| 413 | 424 | $params['code'] = ''; |
| 414 | 425 | } |
| 415 | 426 | |
| 416 | - // Prepare the code to be executed | |
| 417 | - $params['code'] .= "\n\$mwcode_result = {$params['name']}("; | |
| 418 | - foreach ( $params['args'] as $index => $arg ) { | |
| 419 | - $value = 'null'; // In case the argument is not provided it will be null | |
| 420 | - | |
| 421 | - if ( array_key_exists( $arg, $params['values'] ) ) { // Avoid warnings if the argument is not provided | |
| 422 | - | |
| 423 | - // If the argument is provided, use it, if not use the default value | |
| 424 | - if ( !empty( $params['values'][$arg]['input'] ) ) { | |
| 425 | - $value = $params['values'][$arg]['input']; | |
| 426 | - | |
| 427 | - } else if ( !empty( $params['values'][$arg]['default'] ) ) { | |
| 428 | - $value = $params['values'][$arg]['default']; | |
| 429 | - } | |
| 427 | + // Resolve the arguments as REAL PHP values, in the function's declared order. | |
| 428 | + // The previous version concatenated each value into a string of PHP and eval-ed | |
| 429 | + // the call, which broke on any string or edge-case value with a parse error | |
| 430 | + // ("syntax error, unexpected token ')'"). call_user_func_array passes them as | |
| 431 | + // data, so no value can ever corrupt the call syntax. | |
| 432 | + $callArgs = []; | |
| 433 | + foreach ( $params['args'] as $arg ) { | |
| 434 | + $key = ltrim( $arg, '$' ); // Match the normalized name the caller sent. | |
| 435 | + $value = null; // Not provided and no default -> null. | |
| 436 | + // array_key_exists, not !empty: a legitimately provided 0, "0", "" or false | |
| 437 | + // must reach the function instead of silently falling back to the default. | |
| 438 | + if ( array_key_exists( $key, $provided ) ) { | |
| 439 | + $value = $provided[ $key ]; | |
| 440 | + } else if ( isset( $params['values'][$arg]['default'] ) && $params['values'][$arg]['default'] !== '' ) { | |
| 441 | + $value = $params['values'][$arg]['default']; | |
| 430 | 442 | } |
| 431 | - | |
| 432 | - $params['code'] .= "{$value}"; | |
| 433 | - if ( $index < count( $params['args'] ) - 1 ) { | |
| 434 | - $params['code'] .= ', '; | |
| 443 | + // An array-typed argument can arrive as a string like "[1, 2, 3]"; turn it | |
| 444 | + // into a real array so the function receives what its signature expects. | |
| 445 | + if ( ( $params['values'][$arg]['type'] ?? null ) === 'array' && is_string( $value ) ) { | |
| 446 | + $decoded = json_decode( $value, true ); | |
| 447 | + $value = is_array( $decoded ) ? $decoded : array_map( 'trim', explode( ',', trim( $value, "[] \t\n\r" ) ) ); | |
| 435 | 448 | } |
| 449 | + $callArgs[] = $value; | |
| 436 | 450 | } |
| 437 | - $params['code'] .= ");\necho print_r(\$mwcode_result, true);"; | |
| 438 | 451 | |
| 439 | 452 | $error = null; |
| 440 | 453 | $output = null; |
| 441 | 454 | |
| @@ -440,20 +453,28 @@ | ||
| 440 | 453 | $output = null; |
| 441 | 454 | |
| 442 | 455 | try { |
| 443 | 456 | ob_start(); |
| 444 | - eval( $params['code'] ); | |
| 457 | + // $params['code'] holds the function definition (empty if it was already | |
| 458 | + // defined earlier this request). Declare it, then invoke it as data. | |
| 459 | + if ( $params['code'] !== '' ) { | |
| 460 | + eval( $params['code'] ); | |
| 461 | + } | |
| 462 | + $mwcode_result = call_user_func_array( $params['name'], $callArgs ); | |
| 463 | + echo print_r( $mwcode_result, true ); | |
| 445 | 464 | $output = ob_get_clean(); |
| 446 | - | |
| 447 | - if ( $params['test'] ){ | |
| 465 | + | |
| 466 | + if ( $params['test'] ) { | |
| 448 | 467 | $output = explode( "\n", $output ); |
| 449 | 468 | } |
| 450 | - | |
| 469 | + | |
| 451 | 470 | } catch ( Throwable $e ) { |
| 452 | 471 | //$this->log('Code Engine: Error executing the function: ' . $e->getMessage()); |
| 453 | 472 | $error = new Exception(' Error executing the function, ' . $e->getMessage()); |
| 454 | 473 | |
| 455 | - ob_clean(); | |
| 474 | + if ( ob_get_level() > 0 ) { | |
| 475 | + ob_end_clean(); | |
| 476 | + } | |
| 456 | 477 | } finally { |
| 457 | 478 | restore_error_handler(); |
| 458 | 479 | } |
| 459 | 480 | |
| @@ -507,8 +528,132 @@ | ||
| 507 | 528 | |
| 508 | 529 | return null; |
| 509 | 530 | } |
| 510 | 531 | |
| 532 | + /** | |
| 533 | + * Load the active global snippets (persistent + backend/frontend for this context) | |
| 534 | + * that haven't already run this request, so on-demand function execution has the same | |
| 535 | + * always-available helper library a normal page load would. Callable functions are | |
| 536 | + * typically small wrappers around these globals. | |
| 537 | + * | |
| 538 | + * On non-whitelisted REST routes (Workflow Engine, MCP, AI function-calling) the | |
| 539 | + * plugins_loaded pass blocks global snippets for safety; this restores them for the | |
| 540 | + * deliberate, authorized act of executing a snippet. The loaded-id registry guarantees | |
| 541 | + * each global runs at most once per request, so nothing is ever re-declared. | |
| 542 | + */ | |
| 543 | + function load_global_snippets() { | |
| 544 | + global $current_mwcode_snippet; | |
| 545 | + static $done = false; | |
| 546 | + if ( $done ) { | |
| 547 | + return; | |
| 548 | + } | |
| 549 | + $done = true; | |
| 550 | + | |
| 551 | + if ( empty( $this->snippet ) ) { | |
| 552 | + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this ); | |
| 553 | + } | |
| 554 | + | |
| 555 | + $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ]; | |
| 556 | + | |
| 557 | + $snippets = $this->snippet->select( | |
| 558 | + null, // offset | |
| 559 | + -1, // limit (all) | |
| 560 | + [ | |
| 561 | + [ 'accessor' => 'active', 'value' => 1 ], | |
| 562 | + [ 'accessor' => 'scope', 'value' => $scope ], | |
| 563 | + ], | |
| 564 | + [ 'accessor' => 'priority', 'by' => 'DESC' ] | |
| 565 | + )['data'] ?? []; | |
| 566 | + | |
| 567 | + foreach ( $snippets as $snippet ) { | |
| 568 | + // Skip globals already executed this request (e.g. by the plugins_loaded pass). | |
| 569 | + if ( in_array( $snippet['id'], $this->loaded_global_ids ) ) { | |
| 570 | + continue; | |
| 571 | + } | |
| 572 | + $this->loaded_global_ids[] = $snippet['id']; | |
| 573 | + | |
| 574 | + $code = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 575 | + $current_mwcode_snippet = $snippet; | |
| 576 | + try { | |
| 577 | + ob_start(); | |
| 578 | + eval( $code ); | |
| 579 | + ob_end_clean(); | |
| 580 | + } catch ( Throwable $e ) { | |
| 581 | + ob_end_clean(); | |
| 582 | + $this->log( "⚠️ Code Engine: Failed to load global snippet \"{$snippet['name']}\": " . $e->getMessage() ); | |
| 583 | + } | |
| 584 | + } | |
| 585 | + $current_mwcode_snippet = null; | |
| 586 | + } | |
| 587 | + | |
| 588 | + /** | |
| 589 | + * Declare every active PHP function snippet in the current request, without | |
| 590 | + * invoking any of them, so function snippets can call one another. | |
| 591 | + * | |
| 592 | + * Function snippets are not auto-loaded on every request (unlike global/backend/ | |
| 593 | + * frontend scopes) — they are meant to run on demand. This is the PHP counterpart | |
| 594 | + * to get_js_functions_to_push(): it makes the whole library of functions callable | |
| 595 | + * before a function is executed (via REST, MCP, AI function-calling, Workflow Engine). | |
| 596 | + * | |
| 597 | + * Idempotent: a static guard runs the full pass only once per request, and each | |
| 598 | + * definition is wrapped in function_exists() so nothing is ever redefined. | |
| 599 | + * | |
| 600 | + * @param string|null $except Function name to skip (the one run_snippet is about to | |
| 601 | + * define itself, so edited/test code keeps priority). | |
| 602 | + */ | |
| 603 | + function define_all_functions( $except = null ) { | |
| 604 | + static $loaded = false; | |
| 605 | + if ( $loaded ) { | |
| 606 | + return; | |
| 607 | + } | |
| 608 | + $loaded = true; | |
| 609 | + | |
| 610 | + if ( empty( $this->snippet ) ) { | |
| 611 | + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this ); | |
| 612 | + } | |
| 613 | + | |
| 614 | + // One query for every active function snippet (code included), then enrich with | |
| 615 | + // the function metadata (name + target) the same way run_snippet does. | |
| 616 | + $snippets = $this->snippet->select( | |
| 617 | + null, // offset | |
| 618 | + -1, // limit (all) | |
| 619 | + [ | |
| 620 | + [ 'accessor' => 'active', 'value' => 1 ], | |
| 621 | + [ 'accessor' => 'scope', 'value' => 'function' ], | |
| 622 | + ], | |
| 623 | + [] // sort | |
| 624 | + )['data'] ?? []; | |
| 625 | + | |
| 626 | + if ( empty( $snippets ) ) { | |
| 627 | + return; | |
| 628 | + } | |
| 629 | + | |
| 630 | + $this->snippet->get_function_snippets_data( $snippets ); | |
| 631 | + | |
| 632 | + foreach ( $snippets as $snippet ) { | |
| 633 | + $name = $snippet['functionName'] ?? ''; | |
| 634 | + $target = strtolower( $snippet['functionTarget'] ?? 'php' ); | |
| 635 | + | |
| 636 | + // Skip JS functions (pushed to the front-end separately), the function the | |
| 637 | + // caller will define itself, and anything already declared in this request. | |
| 638 | + if ( $name === '' || $target === 'js' || $name === $except || function_exists( $name ) ) { | |
| 639 | + continue; | |
| 640 | + } | |
| 641 | + | |
| 642 | + // Mirror run_snippet()'s non-test handling: drop echo statements, then declare | |
| 643 | + // (never call) the function, guarded so a later run_snippet() call is a no-op. | |
| 644 | + $code = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 645 | + $code = preg_replace( '/echo\s+(.+?);/s', '', $code ); | |
| 646 | + $code = "if (!function_exists('{$name}')) {\n{$code}\n}\n"; | |
| 647 | + | |
| 648 | + try { | |
| 649 | + eval( $code ); | |
| 650 | + } catch ( Throwable $e ) { | |
| 651 | + $this->log( "⚠️ Code Engine: Failed to pre-define function \"{$name}\": " . $e->getMessage() ); | |
| 652 | + } | |
| 653 | + } | |
| 654 | + } | |
| 655 | + | |
| 511 | 656 | public function get_js_functions_to_push() { |
| 512 | 657 | $functions = $this->snippet->get_functions(); |
| 513 | 658 | $js_functions = []; |
| 514 | 659 | foreach ( $functions as &$function ) { |
| @@ -589,9 +734,9 @@ | ||
| 589 | 734 | $blocked = false; |
| 590 | 735 | //$blocked = true; |
| 591 | 736 | } |
| 592 | 737 | // Block REST requests that aren't whitelisted |
| 593 | - elseif ( MeowCommon_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) { | |
| 738 | + elseif ( MeowKit_MWCODE_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) { | |
| 594 | 739 | $blocked = true; |
| 595 | 740 | } |
| 596 | 741 | |
| 597 | 742 | if ( empty( $this->snippet ) ) { |
| @@ -622,9 +767,9 @@ | ||
| 622 | 767 | return; |
| 623 | 768 | } |
| 624 | 769 | |
| 625 | 770 | $snippets = array_map( function ( $snippet ) use ( $blocked ) { |
| 626 | - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 ); | |
| 771 | + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 627 | 772 | $snippet['blocked'] = $blocked; |
| 628 | 773 | |
| 629 | 774 | // If the snippet must be executed only in the frontend, we bypass the block |
| 630 | 775 | if ( !is_admin() && $snippet['scope'] === 'frontend' ) { |
| @@ -640,23 +785,35 @@ | ||
| 640 | 785 | |
| 641 | 786 | #endregion |
| 642 | 787 | |
| 643 | 788 | #region Shortcodes |
| 789 | + function separate_mwcode_atts( $atts ) { | |
| 644 | 790 | |
| 791 | + if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] ); | |
| 792 | + if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] ); | |
| 793 | + if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] ); | |
| 794 | + | |
| 795 | + return $atts; | |
| 796 | + } | |
| 797 | + | |
| 645 | 798 | function content_shortcode( $atts ) { |
| 646 | 799 | |
| 800 | + $user_atts = $this->separate_mwcode_atts( $atts ); | |
| 801 | + | |
| 647 | 802 | $atts = shortcode_atts( array( |
| 648 | - 'id' => null, | |
| 649 | - 'target' => null, | |
| 650 | - 'code' => null, | |
| 651 | - ), $atts ); | |
| 803 | + 'id' => null, | |
| 804 | + 'target' => null, // js or php | |
| 805 | + 'code' => null, // For Guttenberg block usage | |
| 806 | + ), $atts, 'code-engine' ); | |
| 652 | 807 | |
| 653 | 808 | $id = $atts['id']; |
| 654 | 809 | $target = $atts['target']; |
| 655 | 810 | $code = $atts['code']; |
| 811 | + $current_post = get_post(); | |
| 656 | 812 | |
| 657 | 813 | $no_js = defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML; |
| 658 | - $no_php = $this->get_option( 'disallow_block_php', true ); | |
| 814 | + $allow_php = $this->get_option( 'code_blocks', false ); | |
| 815 | + $allow_php_whitelist = $this->get_option( 'code_blocks_whitelist', [] ); | |
| 659 | 816 | |
| 660 | 817 | // If the ID is null, it means it comes from a Guttenberg block |
| 661 | 818 | $is_block = empty( $id ) && !empty( $code ); |
| 662 | 819 | |
| @@ -669,10 +826,17 @@ | ||
| 669 | 826 | if ( $no_js && $target === 'js' ) { |
| 670 | 827 | return '<b>Code Engine:</b> Code Block JS are disabled because unfiltered HTML is not allowed on your server.'; |
| 671 | 828 | } |
| 672 | 829 | |
| 673 | - if ( $no_php && $target === 'php' ) { | |
| 674 | - 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.'; | |
| 830 | + if ( $target === 'php' ) { | |
| 831 | + | |
| 832 | + if ( !$allow_php ) { | |
| 833 | + 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.'; | |
| 834 | + } | |
| 835 | + | |
| 836 | + if ( !empty( $allow_php_whitelist ) && !in_array( $current_post->ID, $allow_php_whitelist ) ) { | |
| 837 | + 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.'; | |
| 838 | + } | |
| 675 | 839 | } |
| 676 | 840 | |
| 677 | 841 | // Because the code from Blocks are sanitized, we need to replace the " with " |
| 678 | 842 | $code = str_replace( '"', '"', $code ); |
| @@ -723,9 +887,10 @@ | ||
| 723 | 887 | $output = '<script>' . $snippet['code'] . '</script>'; |
| 724 | 888 | } |
| 725 | 889 | |
| 726 | 890 | if ( $is_content_php ) { |
| 727 | - $output = $this->run_non_fn_snippet( $id ); | |
| 891 | + $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );"; | |
| 892 | + $output = $this->run_non_fn_snippet( $id, null, false, $prefix ); | |
| 728 | 893 | } |
| 729 | 894 | |
| 730 | 895 | return $output; |
| 731 | 896 | } |