PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.4
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.4
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 +44 -51 0.3.70.4.4 View file →
@@ -20,9 +20,9 @@
20 20 public function __construct() {
21 21 global $mwcode;
22 22
23 23 $this->site_url = get_site_url();
24 - $this->is_rest = MeowCommon_Helpers::is_rest();
24 + $this->is_rest = MeowKit_MWCODE_Helpers::is_rest();
25 25 $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
26 26
27 27 // Snippets
28 28 $snippet = new Meow_MWCODE_Modules_Snippet( $this );
@@ -39,10 +39,10 @@
39 39 }
40 40
41 41 function init() {
42 42 // Initialize the licenser for Pro version
43 - if ( class_exists( 'MeowCommonPro_Licenser' ) ) {
44 - $this->licenser = new MeowCommonPro_Licenser( MWCODE_PREFIX, MWCODE_ENTRY, MWCODE_DOMAIN, MWCODE_ITEM_ID, MWCODE_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 45 }
46 46
47 47 // Part of the core, settings and stuff
48 48 $this->admin = new Meow_MWCODE_Admin( $this );
@@ -110,8 +110,11 @@
110 110 "api_token" => md5( time() . rand() ),
111 111
112 112 //MCP
113 113 "mcp_support" => false,
114 +
115 + //MAINTENANCE
116 + "clean_uninstall" => false,
114 117 ];
115 118 }
116 119
117 120 function get_all_options( ) {
@@ -125,19 +128,15 @@
125 128 return $options;
126 129 }
127 130
128 131 function update_options( $options ) {
129 - $current_options = get_option($this->option_name);
130 132
131 - if ($current_options === $options) {
132 - // $this->log('💾 The options are already the expected value.');
133 - } else {
134 - if ( !update_option( $this->option_name, $options, false ) ) {
135 - $this->log( '💾 There was an issue updating the options.' );
136 - }
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.' );
137 137 }
138 -
139 - $options = $this->sanitize_options( $options );
138 +
140 139 return $options;
141 140 }
142 141
143 142 function update_option( $option, $value ) {
@@ -168,9 +167,9 @@
168 167 $options_modified = true;
169 168 }
170 169
171 170 // Update AI Engine status
172 - $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified;
171 + $options = $this->updateAIEngineStatus( $options );
173 172
174 173 // Disable AI related features if AI Engine is not available
175 174 if ( ! $options['ai_engine_status'] ) {
176 175 if ( $options['ai_suggestions'] !== false ) {
@@ -180,12 +179,8 @@
180 179 // Note: We don't disable MCP support here anymore
181 180 // It will be checked at runtime in the MCP class
182 181 }
183 182
184 - if ( $options_modified ) {
185 - update_option( $this->option_name, $options, false );
186 - }
187 -
188 183 return $options;
189 184 }
190 185
191 186 private function updateAIEngineStatus( &$options ) {
@@ -190,31 +185,13 @@
190 185
191 186 private function updateAIEngineStatus( &$options ) {
192 187 global $mwai;
193 188
194 - if ( is_null( $mwai ) || ! isset( $mwai ) ) {
195 - $options['ai_engine_status'] = false;
196 - $options['ai_engine_message'] = 'AI Engine is not available.';
197 - return true;
198 - }
189 + $options['mwai_has_ai'] = !empty( $mwai ) && $mwai->hasAI();
190 + // Legacy
191 + $options['ai_engine_status'] = $options['mwai_has_ai'];
199 192
200 - try {
201 - $status = $mwai->checkStatus();
202 -
203 - if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) {
204 - $options['ai_engine_status'] = true;
205 - $options['ai_engine_message'] = $status;
206 - return true;
207 - }
208 - } catch ( Exception $e ) {
209 - if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) {
210 - $options['ai_engine_status'] = false;
211 - $options['ai_engine_message'] = $e->getMessage();
212 - return true;
213 - }
214 - }
215 -
216 - return false;
193 + return $options;
217 194 }
218 195
219 196 #endregion
220 197
@@ -285,16 +262,16 @@
285 262 $value = array_map( 'trim', $value );
286 263 }
287 264
288 265 if ( $type === 'array' ) {
289 - $value = json_encode( $value );
290 - $value = str_replace( '\\', '', $value );
266 + // Convert to PHP array format instead of JSON
267 + $value = var_export( $value, true );
291 268 }
292 269
293 270 return [ $name, $value ];
294 271 }
295 272
296 - function run_non_fn_snippet( $id, $code = null, $test = false ) {
273 + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
297 274 // Retrieve the snippet code from the provided code or via the snippet ID.
298 275 if ( $code ) {
299 276 $snippet = [ 'code' => $code ];
300 277 } else {
@@ -301,13 +278,17 @@
301 278 $snippet = $this->get_snippet( $id );
302 279 }
303 280
304 281 // Remove any PHP opening tag.
305 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
282 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
306 283
307 284 if ( $test ) {
308 285 $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
309 286 }
287 +
288 + if( $prefix ) {
289 + $snippet['code'] = $prefix . "\n" . $snippet['code'];
290 + }
310 291
311 292 $error = null;
312 293 $output = null;
313 294
@@ -441,13 +422,14 @@
441 422 if ( $index < count( $params['args'] ) - 1 ) {
442 423 $params['code'] .= ', ';
443 424 }
444 425 }
426 +
445 427 $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
446 428
447 429 $error = null;
448 430 $output = null;
449 -
431 +
450 432 try {
451 433 ob_start();
452 434 eval( $params['code'] );
453 435 $output = ob_get_clean();
@@ -597,9 +579,9 @@
597 579 $blocked = false;
598 580 //$blocked = true;
599 581 }
600 582 // Block REST requests that aren't whitelisted
601 - 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() ) {
602 584 $blocked = true;
603 585 }
604 586
605 587 if ( empty( $this->snippet ) ) {
@@ -630,9 +612,9 @@
630 612 return;
631 613 }
632 614
633 615 $snippets = array_map( function ( $snippet ) use ( $blocked ) {
634 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
616 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
635 617 $snippet['blocked'] = $blocked;
636 618
637 619 // If the snippet must be executed only in the frontend, we bypass the block
638 620 if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
@@ -648,16 +630,26 @@
648 630
649 631 #endregion
650 632
651 633 #region Shortcodes
634 + function separate_mwcode_atts( $atts ) {
652 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 +
653 643 function content_shortcode( $atts ) {
654 644
645 + $user_atts = $this->separate_mwcode_atts( $atts );
646 +
655 647 $atts = shortcode_atts( array(
656 - 'id' => null,
657 - 'target' => null,
658 - 'code' => null,
659 - ), $atts );
648 + 'id' => null,
649 + 'target' => null, // js or php
650 + 'code' => null, // For Guttenberg block usage
651 + ), $atts, 'code-engine' );
660 652
661 653 $id = $atts['id'];
662 654 $target = $atts['target'];
663 655 $code = $atts['code'];
@@ -740,9 +732,10 @@
740 732 $output = '<script>' . $snippet['code'] . '</script>';
741 733 }
742 734
743 735 if ( $is_content_php ) {
744 - $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 );
745 738 }
746 739
747 740 return $output;
748 741 }