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 +48 -49 0.3.60.4.5 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 ) {
@@ -184,31 +185,13 @@
184 185
185 186 private function updateAIEngineStatus( &$options ) {
186 187 global $mwai;
187 188
188 - if ( is_null( $mwai ) || ! isset( $mwai ) ) {
189 - $options['ai_engine_status'] = false;
190 - $options['ai_engine_message'] = 'AI Engine is not available.';
191 - return true;
192 - }
189 + $options['mwai_has_ai'] = !empty( $mwai ) && method_exists( $mwai, 'hasAI' ) && $mwai->hasAI();
190 + // Legacy
191 + $options['ai_engine_status'] = $options['mwai_has_ai'];
193 192
194 - try {
195 - $status = $mwai->checkStatus();
196 -
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 - }
202 - } catch ( Exception $e ) {
203 - if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) {
204 - $options['ai_engine_status'] = false;
205 - $options['ai_engine_message'] = $e->getMessage();
206 - return true;
207 - }
208 - }
209 -
210 - return false;
193 + return $options;
211 194 }
212 195
213 196 #endregion
214 197
@@ -279,16 +262,16 @@
279 262 $value = array_map( 'trim', $value );
280 263 }
281 264
282 265 if ( $type === 'array' ) {
283 - $value = json_encode( $value );
284 - $value = str_replace( '\\', '', $value );
266 + // Convert to PHP array format instead of JSON
267 + $value = var_export( $value, true );
285 268 }
286 269
287 270 return [ $name, $value ];
288 271 }
289 272
290 - function run_non_fn_snippet( $id, $code = null, $test = false ) {
273 + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
291 274 // Retrieve the snippet code from the provided code or via the snippet ID.
292 275 if ( $code ) {
293 276 $snippet = [ 'code' => $code ];
294 277 } else {
@@ -295,13 +278,17 @@
295 278 $snippet = $this->get_snippet( $id );
296 279 }
297 280
298 281 // Remove any PHP opening tag.
299 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
282 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
300 283
301 284 if ( $test ) {
302 285 $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
303 286 }
287 +
288 + if( $prefix ) {
289 + $snippet['code'] = $prefix . "\n" . $snippet['code'];
290 + }
304 291
305 292 $error = null;
306 293 $output = null;
307 294
@@ -435,13 +422,14 @@
435 422 if ( $index < count( $params['args'] ) - 1 ) {
436 423 $params['code'] .= ', ';
437 424 }
438 425 }
426 +
439 427 $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
440 428
441 429 $error = null;
442 430 $output = null;
443 -
431 +
444 432 try {
445 433 ob_start();
446 434 eval( $params['code'] );
447 435 $output = ob_get_clean();
@@ -591,9 +579,9 @@
591 579 $blocked = false;
592 580 //$blocked = true;
593 581 }
594 582 // Block REST requests that aren't whitelisted
595 - elseif ( MeowCommon_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
583 + elseif ( MeowKit_MWCODE_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
596 584 $blocked = true;
597 585 }
598 586
599 587 if ( empty( $this->snippet ) ) {
@@ -624,9 +612,9 @@
624 612 return;
625 613 }
626 614
627 615 $snippets = array_map( function ( $snippet ) use ( $blocked ) {
628 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
616 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
629 617 $snippet['blocked'] = $blocked;
630 618
631 619 // If the snippet must be executed only in the frontend, we bypass the block
632 620 if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
@@ -642,16 +630,26 @@
642 630
643 631 #endregion
644 632
645 633 #region Shortcodes
634 + function separate_mwcode_atts( $atts ) {
646 635
636 + if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] );
637 + if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] );
638 + if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] );
639 +
640 + return $atts;
641 + }
642 +
647 643 function content_shortcode( $atts ) {
648 644
645 + $user_atts = $this->separate_mwcode_atts( $atts );
646 +
649 647 $atts = shortcode_atts( array(
650 - 'id' => null,
651 - 'target' => null,
652 - 'code' => null,
653 - ), $atts );
648 + 'id' => null,
649 + 'target' => null, // js or php
650 + 'code' => null, // For Guttenberg block usage
651 + ), $atts, 'code-engine' );
654 652
655 653 $id = $atts['id'];
656 654 $target = $atts['target'];
657 655 $code = $atts['code'];
@@ -734,9 +732,10 @@
734 732 $output = '<script>' . $snippet['code'] . '</script>';
735 733 }
736 734
737 735 if ( $is_content_php ) {
738 - $output = $this->run_non_fn_snippet( $id );
736 + $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );";
737 + $output = $this->run_non_fn_snippet( $id, null, false, $prefix );
739 738 }
740 739
741 740 return $output;
742 741 }