PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.3
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.3
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 +51 -37 0.3.60.4.3 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
@@ -104,8 +110,11 @@
104 110 "api_token" => md5( time() . rand() ),
105 111
106 112 //MCP
107 113 "mcp_support" => false,
114 +
115 + //MAINTENANCE
116 + "clean_uninstall" => false,
108 117 ];
109 118 }
110 119
111 120 function get_all_options( ) {
@@ -119,19 +128,15 @@
119 128 return $options;
120 129 }
121 130
122 131 function update_options( $options ) {
123 - $current_options = get_option($this->option_name);
124 132
125 - if ($current_options === $options) {
126 - // $this->log('💾 The options are already the expected value.');
127 - } else {
128 - if ( !update_option( $this->option_name, $options, false ) ) {
129 - $this->log( '💾 There was an issue updating the options.' );
130 - }
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.' );
131 137 }
132 -
133 - $options = $this->sanitize_options( $options );
138 +
134 139 return $options;
135 140 }
136 141
137 142 function update_option( $option, $value ) {
@@ -162,9 +167,9 @@
162 167 $options_modified = true;
163 168 }
164 169
165 170 // Update AI Engine status
166 - $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified;
171 + $options = $this->updateAIEngineStatus( $options );
167 172
168 173 // Disable AI related features if AI Engine is not available
169 174 if ( ! $options['ai_engine_status'] ) {
170 175 if ( $options['ai_suggestions'] !== false ) {
@@ -174,12 +179,8 @@
174 179 // Note: We don't disable MCP support here anymore
175 180 // It will be checked at runtime in the MCP class
176 181 }
177 182
178 - if ( $options_modified ) {
179 - update_option( $this->option_name, $options, false );
180 - }
181 -
182 183 return $options;
183 184 }
184 185
185 186 private function updateAIEngineStatus( &$options ) {
@@ -187,28 +188,25 @@
187 188
188 189 if ( is_null( $mwai ) || ! isset( $mwai ) ) {
189 190 $options['ai_engine_status'] = false;
190 191 $options['ai_engine_message'] = 'AI Engine is not available.';
191 - return true;
192 +
193 + return $options;
192 194 }
193 195
194 196 try {
195 197 $status = $mwai->checkStatus();
196 198
197 - if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) {
198 - $options['ai_engine_status'] = true;
199 - $options['ai_engine_message'] = $status;
200 - return true;
201 - }
199 + $options['ai_engine_status'] = true;
200 + $options['ai_engine_message'] = is_array( $status ) ? "Environments: " . implode( ', ', $status ) : $status;
201 +
202 202 } catch ( Exception $e ) {
203 - if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) {
203 +
204 204 $options['ai_engine_status'] = false;
205 205 $options['ai_engine_message'] = $e->getMessage();
206 - return true;
207 - }
208 206 }
209 207
210 - return false;
208 + return $options;
211 209 }
212 210
213 211 #endregion
214 212
@@ -279,16 +277,16 @@
279 277 $value = array_map( 'trim', $value );
280 278 }
281 279
282 280 if ( $type === 'array' ) {
283 - $value = json_encode( $value );
284 - $value = str_replace( '\\', '', $value );
281 + // Convert to PHP array format instead of JSON
282 + $value = var_export( $value, true );
285 283 }
286 284
287 285 return [ $name, $value ];
288 286 }
289 287
290 - function run_non_fn_snippet( $id, $code = null, $test = false ) {
288 + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
291 289 // Retrieve the snippet code from the provided code or via the snippet ID.
292 290 if ( $code ) {
293 291 $snippet = [ 'code' => $code ];
294 292 } else {
@@ -295,13 +293,17 @@
295 293 $snippet = $this->get_snippet( $id );
296 294 }
297 295
298 296 // Remove any PHP opening tag.
299 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
297 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
300 298
301 299 if ( $test ) {
302 300 $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
303 301 }
302 +
303 + if( $prefix ) {
304 + $snippet['code'] = $prefix . "\n" . $snippet['code'];
305 + }
304 306
305 307 $error = null;
306 308 $output = null;
307 309
@@ -435,13 +437,14 @@
435 437 if ( $index < count( $params['args'] ) - 1 ) {
436 438 $params['code'] .= ', ';
437 439 }
438 440 }
441 +
439 442 $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
440 443
441 444 $error = null;
442 445 $output = null;
443 -
446 +
444 447 try {
445 448 ob_start();
446 449 eval( $params['code'] );
447 450 $output = ob_get_clean();
@@ -591,9 +594,9 @@
591 594 $blocked = false;
592 595 //$blocked = true;
593 596 }
594 597 // Block REST requests that aren't whitelisted
595 - elseif ( MeowCommon_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
598 + elseif ( MeowKit_MWCODE_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
596 599 $blocked = true;
597 600 }
598 601
599 602 if ( empty( $this->snippet ) ) {
@@ -624,9 +627,9 @@
624 627 return;
625 628 }
626 629
627 630 $snippets = array_map( function ( $snippet ) use ( $blocked ) {
628 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
631 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
629 632 $snippet['blocked'] = $blocked;
630 633
631 634 // If the snippet must be executed only in the frontend, we bypass the block
632 635 if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
@@ -642,16 +645,26 @@
642 645
643 646 #endregion
644 647
645 648 #region Shortcodes
649 + function separate_mwcode_atts( $atts ) {
646 650
651 + if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] );
652 + if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] );
653 + if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] );
654 +
655 + return $atts;
656 + }
657 +
647 658 function content_shortcode( $atts ) {
648 659
660 + $user_atts = $this->separate_mwcode_atts( $atts );
661 +
649 662 $atts = shortcode_atts( array(
650 - 'id' => null,
651 - 'target' => null,
652 - 'code' => null,
653 - ), $atts );
663 + 'id' => null,
664 + 'target' => null, // js or php
665 + 'code' => null, // For Guttenberg block usage
666 + ), $atts, 'code-engine' );
654 667
655 668 $id = $atts['id'];
656 669 $target = $atts['target'];
657 670 $code = $atts['code'];
@@ -734,9 +747,10 @@
734 747 $output = '<script>' . $snippet['code'] . '</script>';
735 748 }
736 749
737 750 if ( $is_content_php ) {
738 - $output = $this->run_non_fn_snippet( $id );
751 + $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );";
752 + $output = $this->run_non_fn_snippet( $id, null, false, $prefix );
739 753 }
740 754
741 755 return $output;
742 756 }