PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.5
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.5
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 +1 -140 0.5.10.4.5 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 }
@@ -387,18 +382,8 @@
387 382
388 383 $this->log( '⚡ Arguments provided: ' . json_encode( $args ) );
389 384 }
390 385
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 386 // Check if the function has already been defined
402 387 if ( !in_array( $params['name'], $defined_functions ) ) {
403 388
404 389 // If not, proceed with modification and definition
@@ -510,132 +495,8 @@
510 495 return $lint;
511 496 }
512 497
513 498 return null;
514 - }
515 -
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 499 }
639 500
640 501 public function get_js_functions_to_push() {
641 502 $functions = $this->snippet->get_functions();