PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.0
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.0
0.5.7 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 All 33 releases
← All changes | classes/core.php +32 -173 0.5.20.4.0 View file →
@@ -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
@@ -279,9 +287,9 @@
279 287
280 288 return [ $name, $value ];
281 289 }
282 290
283 - function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
291 + function run_non_fn_snippet( $id, $code = null, $test = false ) {
284 292 // Retrieve the snippet code from the provided code or via the snippet ID.
285 293 if ( $code ) {
286 294 $snippet = [ 'code' => $code ];
287 295 } else {
@@ -288,17 +296,13 @@
288 296 $snippet = $this->get_snippet( $id );
289 297 }
290 298
291 299 // Remove any PHP opening tag.
292 - $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
300 + $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
293 301
294 302 if ( $test ) {
295 303 $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
296 304 }
297 -
298 - if( $prefix ) {
299 - $snippet['code'] = $prefix . "\n" . $snippet['code'];
300 - }
301 305
302 306 $error = null;
303 307 $output = null;
304 308
@@ -392,18 +396,8 @@
392 396
393 397 $this->log( '⚡ Arguments provided: ' . json_encode( $args ) );
394 398 }
395 399
396 - // Global snippets are meant to be always accessible. On non-whitelisted REST routes
397 - // (Workflow Engine, MCP, AI function-calling) the plugins_loaded pass blocks them, so
398 - // make sure their helper library is loaded before we run a function that may call it.
399 - $this->load_global_snippets();
400 -
401 - // Make every *other* active PHP function snippet available so this function can
402 - // call its siblings. We pass the current name as the exception so the target is
403 - // still defined below (with the edited/test code when testing), not pre-defined here.
404 - $this->define_all_functions( $params['name'] );
405 -
406 400 // Check if the function has already been defined
407 401 if ( !in_array( $params['name'], $defined_functions ) ) {
408 402
409 403 // If not, proceed with modification and definition
@@ -517,132 +511,8 @@
517 511
518 512 return null;
519 513 }
520 514
521 - /**
522 - * Load the active global snippets (persistent + backend/frontend for this context)
523 - * that haven't already run this request, so on-demand function execution has the same
524 - * always-available helper library a normal page load would. Callable functions are
525 - * typically small wrappers around these globals.
526 - *
527 - * On non-whitelisted REST routes (Workflow Engine, MCP, AI function-calling) the
528 - * plugins_loaded pass blocks global snippets for safety; this restores them for the
529 - * deliberate, authorized act of executing a snippet. The loaded-id registry guarantees
530 - * each global runs at most once per request, so nothing is ever re-declared.
531 - */
532 - function load_global_snippets() {
533 - global $current_mwcode_snippet;
534 - static $done = false;
535 - if ( $done ) {
536 - return;
537 - }
538 - $done = true;
539 -
540 - if ( empty( $this->snippet ) ) {
541 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
542 - }
543 -
544 - $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ];
545 -
546 - $snippets = $this->snippet->select(
547 - null, // offset
548 - -1, // limit (all)
549 - [
550 - [ 'accessor' => 'active', 'value' => 1 ],
551 - [ 'accessor' => 'scope', 'value' => $scope ],
552 - ],
553 - [ 'accessor' => 'priority', 'by' => 'DESC' ]
554 - )['data'] ?? [];
555 -
556 - foreach ( $snippets as $snippet ) {
557 - // Skip globals already executed this request (e.g. by the plugins_loaded pass).
558 - if ( in_array( $snippet['id'], $this->loaded_global_ids ) ) {
559 - continue;
560 - }
561 - $this->loaded_global_ids[] = $snippet['id'];
562 -
563 - $code = $this->snippet->sanitize_code( $snippet['code'] );
564 - $current_mwcode_snippet = $snippet;
565 - try {
566 - ob_start();
567 - eval( $code );
568 - ob_end_clean();
569 - } catch ( Throwable $e ) {
570 - ob_end_clean();
571 - $this->log( "⚠️ Code Engine: Failed to load global snippet \"{$snippet['name']}\": " . $e->getMessage() );
572 - }
573 - }
574 - $current_mwcode_snippet = null;
575 - }
576 -
577 - /**
578 - * Declare every active PHP function snippet in the current request, without
579 - * invoking any of them, so function snippets can call one another.
580 - *
581 - * Function snippets are not auto-loaded on every request (unlike global/backend/
582 - * frontend scopes) — they are meant to run on demand. This is the PHP counterpart
583 - * to get_js_functions_to_push(): it makes the whole library of functions callable
584 - * before a function is executed (via REST, MCP, AI function-calling, Workflow Engine).
585 - *
586 - * Idempotent: a static guard runs the full pass only once per request, and each
587 - * definition is wrapped in function_exists() so nothing is ever redefined.
588 - *
589 - * @param string|null $except Function name to skip (the one run_snippet is about to
590 - * define itself, so edited/test code keeps priority).
591 - */
592 - function define_all_functions( $except = null ) {
593 - static $loaded = false;
594 - if ( $loaded ) {
595 - return;
596 - }
597 - $loaded = true;
598 -
599 - if ( empty( $this->snippet ) ) {
600 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
601 - }
602 -
603 - // One query for every active function snippet (code included), then enrich with
604 - // the function metadata (name + target) the same way run_snippet does.
605 - $snippets = $this->snippet->select(
606 - null, // offset
607 - -1, // limit (all)
608 - [
609 - [ 'accessor' => 'active', 'value' => 1 ],
610 - [ 'accessor' => 'scope', 'value' => 'function' ],
611 - ],
612 - [] // sort
613 - )['data'] ?? [];
614 -
615 - if ( empty( $snippets ) ) {
616 - return;
617 - }
618 -
619 - $this->snippet->get_function_snippets_data( $snippets );
620 -
621 - foreach ( $snippets as $snippet ) {
622 - $name = $snippet['functionName'] ?? '';
623 - $target = strtolower( $snippet['functionTarget'] ?? 'php' );
624 -
625 - // Skip JS functions (pushed to the front-end separately), the function the
626 - // caller will define itself, and anything already declared in this request.
627 - if ( $name === '' || $target === 'js' || $name === $except || function_exists( $name ) ) {
628 - continue;
629 - }
630 -
631 - // Mirror run_snippet()'s non-test handling: drop echo statements, then declare
632 - // (never call) the function, guarded so a later run_snippet() call is a no-op.
633 - $code = $this->snippet->sanitize_code( $snippet['code'] );
634 - $code = preg_replace( '/echo\s+(.+?);/s', '', $code );
635 - $code = "if (!function_exists('{$name}')) {\n{$code}\n}\n";
636 -
637 - try {
638 - eval( $code );
639 - } catch ( Throwable $e ) {
640 - $this->log( "⚠️ Code Engine: Failed to pre-define function \"{$name}\": " . $e->getMessage() );
641 - }
642 - }
643 - }
644 -
645 515 public function get_js_functions_to_push() {
646 516 $functions = $this->snippet->get_functions();
647 517 $js_functions = [];
648 518 foreach ( $functions as &$function ) {
@@ -756,9 +626,9 @@
756 626 return;
757 627 }
758 628
759 629 $snippets = array_map( function ( $snippet ) use ( $blocked ) {
760 - $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
630 + $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
761 631 $snippet['blocked'] = $blocked;
762 632
763 633 // If the snippet must be executed only in the frontend, we bypass the block
764 634 if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
@@ -774,26 +644,16 @@
774 644
775 645 #endregion
776 646
777 647 #region Shortcodes
778 - function separate_mwcode_atts( $atts ) {
779 648
780 - if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] );
781 - if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] );
782 - if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] );
783 -
784 - return $atts;
785 - }
786 -
787 649 function content_shortcode( $atts ) {
788 650
789 - $user_atts = $this->separate_mwcode_atts( $atts );
790 -
791 651 $atts = shortcode_atts( array(
792 - 'id' => null,
793 - 'target' => null, // js or php
794 - 'code' => null, // For Guttenberg block usage
795 - ), $atts, 'code-engine' );
652 + 'id' => null,
653 + 'target' => null,
654 + 'code' => null,
655 + ), $atts );
796 656
797 657 $id = $atts['id'];
798 658 $target = $atts['target'];
799 659 $code = $atts['code'];
@@ -876,10 +736,9 @@
876 736 $output = '<script>' . $snippet['code'] . '</script>';
877 737 }
878 738
879 739 if ( $is_content_php ) {
880 - $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );";
881 - $output = $this->run_non_fn_snippet( $id, null, false, $prefix );
740 + $output = $this->run_non_fn_snippet( $id );
882 741 }
883 742
884 743 return $output;
885 744 }