| @@ -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; |
| @@ -136,9 +132,9 @@ | ||
| 136 | 132 | |
| 137 | 133 | $options = $this->sanitize_options( $options ); |
| 138 | 134 | |
| 139 | 135 | if ( !update_option( $this->option_name, $options, false ) ) { |
| 140 | - //$this->log( '💾 There was an issue updating the options.' ); | |
| 136 | + $this->log( '💾 There was an issue updating the options.' ); | |
| 141 | 137 | } |
| 142 | 138 | |
| 143 | 139 | return $options; |
| 144 | 140 | } |
| @@ -171,9 +167,9 @@ | ||
| 171 | 167 | $options_modified = true; |
| 172 | 168 | } |
| 173 | 169 | |
| 174 | 170 | // Update AI Engine status |
| 175 | - $options = $this->updateAIEngineStatus( $options ); | |
| 171 | + $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified; | |
| 176 | 172 | |
| 177 | 173 | // Disable AI related features if AI Engine is not available |
| 178 | 174 | if ( ! $options['ai_engine_status'] ) { |
| 179 | 175 | if ( $options['ai_suggestions'] !== false ) { |
| @@ -189,13 +185,31 @@ | ||
| 189 | 185 | |
| 190 | 186 | private function updateAIEngineStatus( &$options ) { |
| 191 | 187 | global $mwai; |
| 192 | 188 | |
| 193 | - $options['mwai_has_ai'] = !empty( $mwai ) && method_exists( $mwai, 'hasAI' ) && $mwai->hasAI(); | |
| 194 | - // Legacy | |
| 195 | - $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 | + } | |
| 196 | 194 | |
| 197 | - 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; | |
| 198 | 212 | } |
| 199 | 213 | |
| 200 | 214 | #endregion |
| 201 | 215 | |
| @@ -273,9 +287,9 @@ | ||
| 273 | 287 | |
| 274 | 288 | return [ $name, $value ]; |
| 275 | 289 | } |
| 276 | 290 | |
| 277 | - function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) { | |
| 291 | + function run_non_fn_snippet( $id, $code = null, $test = false ) { | |
| 278 | 292 | // Retrieve the snippet code from the provided code or via the snippet ID. |
| 279 | 293 | if ( $code ) { |
| 280 | 294 | $snippet = [ 'code' => $code ]; |
| 281 | 295 | } else { |
| @@ -287,12 +301,8 @@ | ||
| 287 | 301 | |
| 288 | 302 | if ( $test ) { |
| 289 | 303 | $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] ); |
| 290 | 304 | } |
| 291 | - | |
| 292 | - if( $prefix ) { | |
| 293 | - $snippet['code'] = $prefix . "\n" . $snippet['code']; | |
| 294 | - } | |
| 295 | 305 | |
| 296 | 306 | $error = null; |
| 297 | 307 | $output = null; |
| 298 | 308 | |
| @@ -386,18 +396,8 @@ | ||
| 386 | 396 | |
| 387 | 397 | $this->log( '⚡ Arguments provided: ' . json_encode( $args ) ); |
| 388 | 398 | } |
| 389 | 399 | |
| 390 | - // Global snippets are meant to be always accessible. On non-whitelisted REST routes | |
| 391 | - // (Workflow Engine, MCP, AI function-calling) the plugins_loaded pass blocks them, so | |
| 392 | - // make sure their helper library is loaded before we run a function that may call it. | |
| 393 | - $this->load_global_snippets(); | |
| 394 | - | |
| 395 | - // Make every *other* active PHP function snippet available so this function can | |
| 396 | - // call its siblings. We pass the current name as the exception so the target is | |
| 397 | - // still defined below (with the edited/test code when testing), not pre-defined here. | |
| 398 | - $this->define_all_functions( $params['name'] ); | |
| 399 | - | |
| 400 | 400 | // Check if the function has already been defined |
| 401 | 401 | if ( !in_array( $params['name'], $defined_functions ) ) { |
| 402 | 402 | |
| 403 | 403 | // If not, proceed with modification and definition |
| @@ -511,132 +511,8 @@ | ||
| 511 | 511 | |
| 512 | 512 | return null; |
| 513 | 513 | } |
| 514 | 514 | |
| 515 | - /** | |
| 516 | - * Load the active global snippets (persistent + backend/frontend for this context) | |
| 517 | - * that haven't already run this request, so on-demand function execution has the same | |
| 518 | - * always-available helper library a normal page load would. Callable functions are | |
| 519 | - * typically small wrappers around these globals. | |
| 520 | - * | |
| 521 | - * On non-whitelisted REST routes (Workflow Engine, MCP, AI function-calling) the | |
| 522 | - * plugins_loaded pass blocks global snippets for safety; this restores them for the | |
| 523 | - * deliberate, authorized act of executing a snippet. The loaded-id registry guarantees | |
| 524 | - * each global runs at most once per request, so nothing is ever re-declared. | |
| 525 | - */ | |
| 526 | - function load_global_snippets() { | |
| 527 | - global $current_mwcode_snippet; | |
| 528 | - static $done = false; | |
| 529 | - if ( $done ) { | |
| 530 | - return; | |
| 531 | - } | |
| 532 | - $done = true; | |
| 533 | - | |
| 534 | - if ( empty( $this->snippet ) ) { | |
| 535 | - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this ); | |
| 536 | - } | |
| 537 | - | |
| 538 | - $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ]; | |
| 539 | - | |
| 540 | - $snippets = $this->snippet->select( | |
| 541 | - null, // offset | |
| 542 | - -1, // limit (all) | |
| 543 | - [ | |
| 544 | - [ 'accessor' => 'active', 'value' => 1 ], | |
| 545 | - [ 'accessor' => 'scope', 'value' => $scope ], | |
| 546 | - ], | |
| 547 | - [ 'accessor' => 'priority', 'by' => 'DESC' ] | |
| 548 | - )['data'] ?? []; | |
| 549 | - | |
| 550 | - foreach ( $snippets as $snippet ) { | |
| 551 | - // Skip globals already executed this request (e.g. by the plugins_loaded pass). | |
| 552 | - if ( in_array( $snippet['id'], $this->loaded_global_ids ) ) { | |
| 553 | - continue; | |
| 554 | - } | |
| 555 | - $this->loaded_global_ids[] = $snippet['id']; | |
| 556 | - | |
| 557 | - $code = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 558 | - $current_mwcode_snippet = $snippet; | |
| 559 | - try { | |
| 560 | - ob_start(); | |
| 561 | - eval( $code ); | |
| 562 | - ob_end_clean(); | |
| 563 | - } catch ( Throwable $e ) { | |
| 564 | - ob_end_clean(); | |
| 565 | - $this->log( "⚠️ Code Engine: Failed to load global snippet \"{$snippet['name']}\": " . $e->getMessage() ); | |
| 566 | - } | |
| 567 | - } | |
| 568 | - $current_mwcode_snippet = null; | |
| 569 | - } | |
| 570 | - | |
| 571 | - /** | |
| 572 | - * Declare every active PHP function snippet in the current request, without | |
| 573 | - * invoking any of them, so function snippets can call one another. | |
| 574 | - * | |
| 575 | - * Function snippets are not auto-loaded on every request (unlike global/backend/ | |
| 576 | - * frontend scopes) — they are meant to run on demand. This is the PHP counterpart | |
| 577 | - * to get_js_functions_to_push(): it makes the whole library of functions callable | |
| 578 | - * before a function is executed (via REST, MCP, AI function-calling, Workflow Engine). | |
| 579 | - * | |
| 580 | - * Idempotent: a static guard runs the full pass only once per request, and each | |
| 581 | - * definition is wrapped in function_exists() so nothing is ever redefined. | |
| 582 | - * | |
| 583 | - * @param string|null $except Function name to skip (the one run_snippet is about to | |
| 584 | - * define itself, so edited/test code keeps priority). | |
| 585 | - */ | |
| 586 | - function define_all_functions( $except = null ) { | |
| 587 | - static $loaded = false; | |
| 588 | - if ( $loaded ) { | |
| 589 | - return; | |
| 590 | - } | |
| 591 | - $loaded = true; | |
| 592 | - | |
| 593 | - if ( empty( $this->snippet ) ) { | |
| 594 | - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this ); | |
| 595 | - } | |
| 596 | - | |
| 597 | - // One query for every active function snippet (code included), then enrich with | |
| 598 | - // the function metadata (name + target) the same way run_snippet does. | |
| 599 | - $snippets = $this->snippet->select( | |
| 600 | - null, // offset | |
| 601 | - -1, // limit (all) | |
| 602 | - [ | |
| 603 | - [ 'accessor' => 'active', 'value' => 1 ], | |
| 604 | - [ 'accessor' => 'scope', 'value' => 'function' ], | |
| 605 | - ], | |
| 606 | - [] // sort | |
| 607 | - )['data'] ?? []; | |
| 608 | - | |
| 609 | - if ( empty( $snippets ) ) { | |
| 610 | - return; | |
| 611 | - } | |
| 612 | - | |
| 613 | - $this->snippet->get_function_snippets_data( $snippets ); | |
| 614 | - | |
| 615 | - foreach ( $snippets as $snippet ) { | |
| 616 | - $name = $snippet['functionName'] ?? ''; | |
| 617 | - $target = strtolower( $snippet['functionTarget'] ?? 'php' ); | |
| 618 | - | |
| 619 | - // Skip JS functions (pushed to the front-end separately), the function the | |
| 620 | - // caller will define itself, and anything already declared in this request. | |
| 621 | - if ( $name === '' || $target === 'js' || $name === $except || function_exists( $name ) ) { | |
| 622 | - continue; | |
| 623 | - } | |
| 624 | - | |
| 625 | - // Mirror run_snippet()'s non-test handling: drop echo statements, then declare | |
| 626 | - // (never call) the function, guarded so a later run_snippet() call is a no-op. | |
| 627 | - $code = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 628 | - $code = preg_replace( '/echo\s+(.+?);/s', '', $code ); | |
| 629 | - $code = "if (!function_exists('{$name}')) {\n{$code}\n}\n"; | |
| 630 | - | |
| 631 | - try { | |
| 632 | - eval( $code ); | |
| 633 | - } catch ( Throwable $e ) { | |
| 634 | - $this->log( "⚠️ Code Engine: Failed to pre-define function \"{$name}\": " . $e->getMessage() ); | |
| 635 | - } | |
| 636 | - } | |
| 637 | - } | |
| 638 | - | |
| 639 | 515 | public function get_js_functions_to_push() { |
| 640 | 516 | $functions = $this->snippet->get_functions(); |
| 641 | 517 | $js_functions = []; |
| 642 | 518 | foreach ( $functions as &$function ) { |
| @@ -768,26 +644,16 @@ | ||
| 768 | 644 | |
| 769 | 645 | #endregion |
| 770 | 646 | |
| 771 | 647 | #region Shortcodes |
| 772 | - function separate_mwcode_atts( $atts ) { | |
| 773 | 648 | |
| 774 | - if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] ); | |
| 775 | - if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] ); | |
| 776 | - if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] ); | |
| 777 | - | |
| 778 | - return $atts; | |
| 779 | - } | |
| 780 | - | |
| 781 | 649 | function content_shortcode( $atts ) { |
| 782 | 650 | |
| 783 | - $user_atts = $this->separate_mwcode_atts( $atts ); | |
| 784 | - | |
| 785 | 651 | $atts = shortcode_atts( array( |
| 786 | - 'id' => null, | |
| 787 | - 'target' => null, // js or php | |
| 788 | - 'code' => null, // For Guttenberg block usage | |
| 789 | - ), $atts, 'code-engine' ); | |
| 652 | + 'id' => null, | |
| 653 | + 'target' => null, | |
| 654 | + 'code' => null, | |
| 655 | + ), $atts ); | |
| 790 | 656 | |
| 791 | 657 | $id = $atts['id']; |
| 792 | 658 | $target = $atts['target']; |
| 793 | 659 | $code = $atts['code']; |
| @@ -870,10 +736,9 @@ | ||
| 870 | 736 | $output = '<script>' . $snippet['code'] . '</script>'; |
| 871 | 737 | } |
| 872 | 738 | |
| 873 | 739 | if ( $is_content_php ) { |
| 874 | - $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );"; | |
| 875 | - $output = $this->run_non_fn_snippet( $id, null, false, $prefix ); | |
| 740 | + $output = $this->run_non_fn_snippet( $id ); | |
| 876 | 741 | } |
| 877 | 742 | |
| 878 | 743 | return $output; |
| 879 | 744 | } |