PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.2
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.2
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 0.3.8 All 32 releases
← All changes | classes/core.php +58 -30 0.3.50.4.2 View file →
@@ -12,8 +12,9 @@
12 12 public $is_rest = false;
13 13 public $is_cli = false;
14 14 public $site_url = null;
15 15 public $mwcode = null;
16 + public $licenser = null;
16 17
17 18 private $option_name = 'mwcode_options';
18 19
19 20 public function __construct() {
@@ -19,9 +20,9 @@
19 20 public function __construct() {
20 21 global $mwcode;
21 22
22 23 $this->site_url = get_site_url();
23 - $this->is_rest = MeowCommon_Helpers::is_rest();
24 + $this->is_rest = MeowKit_MWCODE_Helpers::is_rest();
24 25 $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
25 26
26 27 // Snippets
27 28 $snippet = new Meow_MWCODE_Modules_Snippet( $this );
@@ -37,8 +38,13 @@
37 38 add_action( 'plugins_loaded', array( $this, 'init' ) );
38 39 }
39 40
40 41 function init() {
42 + // Initialize the licenser for Pro version
43 + if ( class_exists( 'MeowKitPro_MWCODE_Licenser' ) ) {
44 + $this->licenser = new MeowKitPro_MWCODE_Licenser( MWCODE_PREFIX, MWCODE_ENTRY, MWCODE_DOMAIN, MWCODE_ITEM_ID, MWCODE_VERSION );
45 + }
46 +
41 47 // Part of the core, settings and stuff
42 48 $this->admin = new Meow_MWCODE_Admin( $this );
43 49
44 50 // Only for REST
@@ -83,9 +89,11 @@
83 89 return [
84 90 //Safemode
85 91 "safe_mode_status" => "on", // on, off, whitelist
86 92 "safe_mode_whitelist" => [],
87 - "disallow_block_php" => true, // Do not allow PHP code to be execute through Blocks "code" parameter
93 + //"disallow_block_php" => true, // Do not allow PHP code to be execute through Blocks "code" parameter
94 + "code_blocks" => false,
95 + "code_blocks_whitelist" => [], // Whitelist for code blocks, if empty, all code blocks are allowed
88 96
89 97 //LOGS
90 98 "server_debug_mode" => false,
91 99
@@ -102,8 +110,11 @@
102 110 "api_token" => md5( time() . rand() ),
103 111
104 112 //MCP
105 113 "mcp_support" => false,
114 +
115 + //MAINTENANCE
116 + "clean_uninstall" => false,
106 117 ];
107 118 }
108 119
109 120 function get_all_options( ) {
@@ -117,19 +128,15 @@
117 128 return $options;
118 129 }
119 130
120 131 function update_options( $options ) {
121 - $current_options = get_option($this->option_name);
122 132
123 - if ($current_options === $options) {
124 - // $this->log('💾 The options are already the expected value.');
125 - } else {
126 - if ( !update_option( $this->option_name, $options, false ) ) {
127 - $this->log( '💾 There was an issue updating the options.' );
128 - }
133 + $options = $this->sanitize_options( $options );
134 +
135 + if ( !update_option( $this->option_name, $options, false ) ) {
136 + $this->log( '💾 There was an issue updating the options.' );
129 137 }
130 -
131 - $options = $this->sanitize_options( $options );
138 +
132 139 return $options;
133 140 }
134 141
135 142 function update_option( $option, $value ) {
@@ -172,12 +179,8 @@
172 179 // Note: We don't disable MCP support here anymore
173 180 // It will be checked at runtime in the MCP class
174 181 }
175 182
176 - if ( $options_modified ) {
177 - update_option( $this->option_name, $options, false );
178 - }
179 -
180 183 return $options;
181 184 }
182 185
183 186 private function updateAIEngineStatus( &$options ) {
@@ -277,16 +280,16 @@
277 280 $value = array_map( 'trim', $value );
278 281 }
279 282
280 283 if ( $type === 'array' ) {
281 - $value = json_encode( $value );
282 - $value = str_replace( '\\', '', $value );
284 + // Convert to PHP array format instead of JSON
285 + $value = var_export( $value, true );
283 286 }
284 287
285 288 return [ $name, $value ];
286 289 }
287 290
288 - function run_non_fn_snippet( $id, $code = null, $test = false ) {
291 + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
289 292 // Retrieve the snippet code from the provided code or via the snippet ID.
290 293 if ( $code ) {
291 294 $snippet = [ 'code' => $code ];
292 295 } else {
@@ -293,13 +296,17 @@
293 296 $snippet = $this->get_snippet( $id );
294 297 }
295 298
296 299 // Remove any PHP opening tag.
297 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
300 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
298 301
299 302 if ( $test ) {
300 303 $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
301 304 }
305 +
306 + if( $prefix ) {
307 + $snippet['code'] = $prefix . "\n" . $snippet['code'];
308 + }
302 309
303 310 $error = null;
304 311 $output = null;
305 312
@@ -433,13 +440,14 @@
433 440 if ( $index < count( $params['args'] ) - 1 ) {
434 441 $params['code'] .= ', ';
435 442 }
436 443 }
444 +
437 445 $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
438 446
439 447 $error = null;
440 448 $output = null;
441 -
449 +
442 450 try {
443 451 ob_start();
444 452 eval( $params['code'] );
445 453 $output = ob_get_clean();
@@ -589,9 +597,9 @@
589 597 $blocked = false;
590 598 //$blocked = true;
591 599 }
592 600 // Block REST requests that aren't whitelisted
593 - elseif ( MeowCommon_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
601 + elseif ( MeowKit_MWCODE_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
594 602 $blocked = true;
595 603 }
596 604
597 605 if ( empty( $this->snippet ) ) {
@@ -622,9 +630,9 @@
622 630 return;
623 631 }
624 632
625 633 $snippets = array_map( function ( $snippet ) use ( $blocked ) {
626 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
634 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
627 635 $snippet['blocked'] = $blocked;
628 636
629 637 // If the snippet must be executed only in the frontend, we bypass the block
630 638 if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
@@ -640,23 +648,35 @@
640 648
641 649 #endregion
642 650
643 651 #region Shortcodes
652 + function separate_mwcode_atts( $atts ) {
644 653
654 + if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] );
655 + if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] );
656 + if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] );
657 +
658 + return $atts;
659 + }
660 +
645 661 function content_shortcode( $atts ) {
646 662
663 + $user_atts = $this->separate_mwcode_atts( $atts );
664 +
647 665 $atts = shortcode_atts( array(
648 - 'id' => null,
649 - 'target' => null,
650 - 'code' => null,
651 - ), $atts );
666 + 'id' => null,
667 + 'target' => null, // js or php
668 + 'code' => null, // For Guttenberg block usage
669 + ), $atts, 'code-engine' );
652 670
653 671 $id = $atts['id'];
654 672 $target = $atts['target'];
655 673 $code = $atts['code'];
674 + $current_post = get_post();
656 675
657 676 $no_js = defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML;
658 - $no_php = $this->get_option( 'disallow_block_php', true );
677 + $allow_php = $this->get_option( 'code_blocks', false );
678 + $allow_php_whitelist = $this->get_option( 'code_blocks_whitelist', [] );
659 679
660 680 // If the ID is null, it means it comes from a Guttenberg block
661 681 $is_block = empty( $id ) && !empty( $code );
662 682
@@ -669,10 +689,17 @@
669 689 if ( $no_js && $target === 'js' ) {
670 690 return '<b>Code Engine:</b> Code Block JS are disabled because unfiltered HTML is not allowed on your server.';
671 691 }
672 692
673 - if ( $no_php && $target === 'php' ) {
674 - return '<b>Code Engine:</b> Code Block PHP are disabled. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
693 + if ( $target === 'php' ) {
694 +
695 + if ( !$allow_php ) {
696 + return '<b>Code Engine:</b> Code Block PHP are disabled. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
697 + }
698 +
699 + if ( !empty( $allow_php_whitelist ) && !in_array( $current_post->ID, $allow_php_whitelist ) ) {
700 + return '<b>Code Engine:</b> Code Block PHP are disabled for this post. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
701 + }
675 702 }
676 703
677 704 // Because the code from Blocks are sanitized, we need to replace the &quot; with "
678 705 $code = str_replace( '&quot;', '"', $code );
@@ -723,9 +750,10 @@
723 750 $output = '<script>' . $snippet['code'] . '</script>';
724 751 }
725 752
726 753 if ( $is_content_php ) {
727 - $output = $this->run_non_fn_snippet( $id );
754 + $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );";
755 + $output = $this->run_non_fn_snippet( $id, null, false, $prefix );
728 756 }
729 757
730 758 return $output;
731 759 }