| @@ -14,12 +14,8 @@ | ||
| 14 | 14 | public $site_url = null; |
| 15 | 15 | public $mwcode = null; |
| 16 | 16 | public $licenser = null; |
| 17 | 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 | - | |
| 22 | 18 | private $option_name = 'mwcode_options'; |
| 23 | 19 | |
| 24 | 20 | public function __construct() { |
| 25 | 21 | global $mwcode; |
| @@ -106,9 +102,8 @@ | ||
| 106 | 102 | |
| 107 | 103 | //AI |
| 108 | 104 | "ai_suggestions" => false, |
| 109 | 105 | "ai_engine_status"=> false, |
| 110 | - "mwai_active" => false, | |
| 111 | 106 | "ai_engine_message" => "", |
| 112 | 107 | |
| 113 | 108 | //API |
| 114 | 109 | "api_endpoint" => false, |
| @@ -115,9 +110,8 @@ | ||
| 115 | 110 | "api_token" => md5( time() . rand() ), |
| 116 | 111 | |
| 117 | 112 | //MCP |
| 118 | 113 | "mcp_support" => false, |
| 119 | - "mcp_functions" => false, | |
| 120 | 114 | |
| 121 | 115 | //MAINTENANCE |
| 122 | 116 | "clean_uninstall" => false, |
| 123 | 117 | ]; |
| @@ -138,9 +132,9 @@ | ||
| 138 | 132 | |
| 139 | 133 | $options = $this->sanitize_options( $options ); |
| 140 | 134 | |
| 141 | 135 | if ( !update_option( $this->option_name, $options, false ) ) { |
| 142 | - //$this->log( '💾 There was an issue updating the options.' ); | |
| 136 | + $this->log( '💾 There was an issue updating the options.' ); | |
| 143 | 137 | } |
| 144 | 138 | |
| 145 | 139 | return $options; |
| 146 | 140 | } |
| @@ -173,9 +167,9 @@ | ||
| 173 | 167 | $options_modified = true; |
| 174 | 168 | } |
| 175 | 169 | |
| 176 | 170 | // Update AI Engine status |
| 177 | - $options = $this->updateAIEngineStatus( $options ); | |
| 171 | + $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified; | |
| 178 | 172 | |
| 179 | 173 | // Disable AI related features if AI Engine is not available |
| 180 | 174 | if ( ! $options['ai_engine_status'] ) { |
| 181 | 175 | if ( $options['ai_suggestions'] !== false ) { |
| @@ -191,17 +185,31 @@ | ||
| 191 | 185 | |
| 192 | 186 | private function updateAIEngineStatus( &$options ) { |
| 193 | 187 | global $mwai; |
| 194 | 188 | |
| 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']; | |
| 189 | + if ( is_null( $mwai ) || ! isset( $mwai ) ) { | |
| 190 | + $options['ai_engine_status'] = false; | |
| 191 | + $options['ai_engine_message'] = 'AI Engine is not available.'; | |
| 192 | + return true; | |
| 193 | + } | |
| 202 | 194 | |
| 203 | - return $options; | |
| 195 | + try { | |
| 196 | + $status = $mwai->checkStatus(); | |
| 197 | + | |
| 198 | + if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) { | |
| 199 | + $options['ai_engine_status'] = true; | |
| 200 | + $options['ai_engine_message'] = $status; | |
| 201 | + return true; | |
| 202 | + } | |
| 203 | + } catch ( Exception $e ) { | |
| 204 | + if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) { | |
| 205 | + $options['ai_engine_status'] = false; | |
| 206 | + $options['ai_engine_message'] = $e->getMessage(); | |
| 207 | + return true; | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | + return false; | |
| 204 | 212 | } |
| 205 | 213 | |
| 206 | 214 | #endregion |
| 207 | 215 | |
| @@ -230,24 +238,9 @@ | ||
| 230 | 238 | |
| 231 | 239 | $this->snippet->validate( $params ); |
| 232 | 240 | |
| 233 | 241 | $params = $this->snippet->formatParamsForDatabase( $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 | - } | |
| 242 | + $result = $this->snippet->insert( $params ); | |
| 250 | 243 | $snippet = $this->snippet->select_one( $result ); |
| 251 | 244 | |
| 252 | 245 | if( $result ) { |
| 253 | 246 | $params['id'] = (string)$result; |
| @@ -294,9 +287,9 @@ | ||
| 294 | 287 | |
| 295 | 288 | return [ $name, $value ]; |
| 296 | 289 | } |
| 297 | 290 | |
| 298 | - function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) { | |
| 291 | + function run_non_fn_snippet( $id, $code = null, $test = false ) { | |
| 299 | 292 | // Retrieve the snippet code from the provided code or via the snippet ID. |
| 300 | 293 | if ( $code ) { |
| 301 | 294 | $snippet = [ 'code' => $code ]; |
| 302 | 295 | } else { |
| @@ -303,17 +296,13 @@ | ||
| 303 | 296 | $snippet = $this->get_snippet( $id ); |
| 304 | 297 | } |
| 305 | 298 | |
| 306 | 299 | // Remove any PHP opening tag. |
| 307 | - $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 300 | + $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 ); | |
| 308 | 301 | |
| 309 | 302 | if ( $test ) { |
| 310 | 303 | $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] ); |
| 311 | 304 | } |
| 312 | - | |
| 313 | - if( $prefix ) { | |
| 314 | - $snippet['code'] = $prefix . "\n" . $snippet['code']; | |
| 315 | - } | |
| 316 | 305 | |
| 317 | 306 | $error = null; |
| 318 | 307 | $output = null; |
| 319 | 308 | |
| @@ -407,18 +396,8 @@ | ||
| 407 | 396 | |
| 408 | 397 | $this->log( '⚡ Arguments provided: ' . json_encode( $args ) ); |
| 409 | 398 | } |
| 410 | 399 | |
| 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(); | |
| 415 | - | |
| 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'] ); | |
| 420 | - | |
| 421 | 400 | // Check if the function has already been defined |
| 422 | 401 | if ( !in_array( $params['name'], $defined_functions ) ) { |
| 423 | 402 | |
| 424 | 403 | // If not, proceed with modification and definition |
| @@ -532,132 +511,8 @@ | ||
| 532 | 511 | |
| 533 | 512 | return null; |
| 534 | 513 | } |
| 535 | 514 | |
| 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; | |
| 554 | - | |
| 555 | - if ( empty( $this->snippet ) ) { | |
| 556 | - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this ); | |
| 557 | - } | |
| 558 | - | |
| 559 | - $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ]; | |
| 560 | - | |
| 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'] ?? []; | |
| 570 | - | |
| 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']; | |
| 577 | - | |
| 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 | - } | |
| 591 | - | |
| 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; | |
| 613 | - | |
| 614 | - if ( empty( $this->snippet ) ) { | |
| 615 | - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this ); | |
| 616 | - } | |
| 617 | - | |
| 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'] ?? []; | |
| 629 | - | |
| 630 | - if ( empty( $snippets ) ) { | |
| 631 | - return; | |
| 632 | - } | |
| 633 | - | |
| 634 | - $this->snippet->get_function_snippets_data( $snippets ); | |
| 635 | - | |
| 636 | - foreach ( $snippets as $snippet ) { | |
| 637 | - $name = $snippet['functionName'] ?? ''; | |
| 638 | - $target = strtolower( $snippet['functionTarget'] ?? 'php' ); | |
| 639 | - | |
| 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 | - } | |
| 645 | - | |
| 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"; | |
| 651 | - | |
| 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 | - } | |
| 659 | - | |
| 660 | 515 | public function get_js_functions_to_push() { |
| 661 | 516 | $functions = $this->snippet->get_functions(); |
| 662 | 517 | $js_functions = []; |
| 663 | 518 | foreach ( $functions as &$function ) { |
| @@ -771,9 +626,9 @@ | ||
| 771 | 626 | return; |
| 772 | 627 | } |
| 773 | 628 | |
| 774 | 629 | $snippets = array_map( function ( $snippet ) use ( $blocked ) { |
| 775 | - $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 630 | + $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 ); | |
| 776 | 631 | $snippet['blocked'] = $blocked; |
| 777 | 632 | |
| 778 | 633 | // If the snippet must be executed only in the frontend, we bypass the block |
| 779 | 634 | if ( !is_admin() && $snippet['scope'] === 'frontend' ) { |
| @@ -789,26 +644,16 @@ | ||
| 789 | 644 | |
| 790 | 645 | #endregion |
| 791 | 646 | |
| 792 | 647 | #region Shortcodes |
| 793 | - function separate_mwcode_atts( $atts ) { | |
| 794 | 648 | |
| 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 | 649 | function content_shortcode( $atts ) { |
| 803 | 650 | |
| 804 | - $user_atts = $this->separate_mwcode_atts( $atts ); | |
| 805 | - | |
| 806 | 651 | $atts = shortcode_atts( array( |
| 807 | - 'id' => null, | |
| 808 | - 'target' => null, // js or php | |
| 809 | - 'code' => null, // For Guttenberg block usage | |
| 810 | - ), $atts, 'code-engine' ); | |
| 652 | + 'id' => null, | |
| 653 | + 'target' => null, | |
| 654 | + 'code' => null, | |
| 655 | + ), $atts ); | |
| 811 | 656 | |
| 812 | 657 | $id = $atts['id']; |
| 813 | 658 | $target = $atts['target']; |
| 814 | 659 | $code = $atts['code']; |
| @@ -891,10 +736,9 @@ | ||
| 891 | 736 | $output = '<script>' . $snippet['code'] . '</script>'; |
| 892 | 737 | } |
| 893 | 738 | |
| 894 | 739 | if ( $is_content_php ) { |
| 895 | - $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );"; | |
| 896 | - $output = $this->run_non_fn_snippet( $id, null, false, $prefix ); | |
| 740 | + $output = $this->run_non_fn_snippet( $id ); | |
| 897 | 741 | } |
| 898 | 742 | |
| 899 | 743 | return $output; |
| 900 | 744 | } |