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