| @@ -132,9 +132,9 @@ | ||
| 132 | 132 | |
| 133 | 133 | $options = $this->sanitize_options( $options ); |
| 134 | 134 | |
| 135 | 135 | if ( !update_option( $this->option_name, $options, false ) ) { |
| 136 | - //$this->log( '💾 There was an issue updating the options.' ); | |
| 136 | + $this->log( '💾 There was an issue updating the options.' ); | |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | return $options; |
| 140 | 140 | } |
| @@ -167,9 +167,9 @@ | ||
| 167 | 167 | $options_modified = true; |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | // Update AI Engine status |
| 171 | - $options = $this->updateAIEngineStatus( $options ); | |
| 171 | + $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified; | |
| 172 | 172 | |
| 173 | 173 | // Disable AI related features if AI Engine is not available |
| 174 | 174 | if ( ! $options['ai_engine_status'] ) { |
| 175 | 175 | if ( $options['ai_suggestions'] !== false ) { |
| @@ -185,13 +185,31 @@ | ||
| 185 | 185 | |
| 186 | 186 | private function updateAIEngineStatus( &$options ) { |
| 187 | 187 | global $mwai; |
| 188 | 188 | |
| 189 | - $options['mwai_has_ai'] = !empty( $mwai ) && method_exists( $mwai, 'hasAI' ) && $mwai->hasAI(); | |
| 190 | - // Legacy | |
| 191 | - $options['ai_engine_status'] = $options['mwai_has_ai']; | |
| 189 | + if ( is_null( $mwai ) || ! isset( $mwai ) ) { | |
| 190 | + $options['ai_engine_status'] = false; | |
| 191 | + $options['ai_engine_message'] = 'AI Engine is not available.'; | |
| 192 | + return true; | |
| 193 | + } | |
| 192 | 194 | |
| 193 | - return $options; | |
| 195 | + try { | |
| 196 | + $status = $mwai->checkStatus(); | |
| 197 | + | |
| 198 | + if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) { | |
| 199 | + $options['ai_engine_status'] = true; | |
| 200 | + $options['ai_engine_message'] = $status; | |
| 201 | + return true; | |
| 202 | + } | |
| 203 | + } catch ( Exception $e ) { | |
| 204 | + if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) { | |
| 205 | + $options['ai_engine_status'] = false; | |
| 206 | + $options['ai_engine_message'] = $e->getMessage(); | |
| 207 | + return true; | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | + return false; | |
| 194 | 212 | } |
| 195 | 213 | |
| 196 | 214 | #endregion |
| 197 | 215 | |
| @@ -269,9 +287,9 @@ | ||
| 269 | 287 | |
| 270 | 288 | return [ $name, $value ]; |
| 271 | 289 | } |
| 272 | 290 | |
| 273 | - function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) { | |
| 291 | + function run_non_fn_snippet( $id, $code = null, $test = false ) { | |
| 274 | 292 | // Retrieve the snippet code from the provided code or via the snippet ID. |
| 275 | 293 | if ( $code ) { |
| 276 | 294 | $snippet = [ 'code' => $code ]; |
| 277 | 295 | } else { |
| @@ -278,17 +296,13 @@ | ||
| 278 | 296 | $snippet = $this->get_snippet( $id ); |
| 279 | 297 | } |
| 280 | 298 | |
| 281 | 299 | // Remove any PHP opening tag. |
| 282 | - $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 300 | + $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 ); | |
| 283 | 301 | |
| 284 | 302 | if ( $test ) { |
| 285 | 303 | $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] ); |
| 286 | 304 | } |
| 287 | - | |
| 288 | - if( $prefix ) { | |
| 289 | - $snippet['code'] = $prefix . "\n" . $snippet['code']; | |
| 290 | - } | |
| 291 | 305 | |
| 292 | 306 | $error = null; |
| 293 | 307 | $output = null; |
| 294 | 308 | |
| @@ -612,9 +626,9 @@ | ||
| 612 | 626 | return; |
| 613 | 627 | } |
| 614 | 628 | |
| 615 | 629 | $snippets = array_map( function ( $snippet ) use ( $blocked ) { |
| 616 | - $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] ); | |
| 630 | + $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 ); | |
| 617 | 631 | $snippet['blocked'] = $blocked; |
| 618 | 632 | |
| 619 | 633 | // If the snippet must be executed only in the frontend, we bypass the block |
| 620 | 634 | if ( !is_admin() && $snippet['scope'] === 'frontend' ) { |
| @@ -630,26 +644,16 @@ | ||
| 630 | 644 | |
| 631 | 645 | #endregion |
| 632 | 646 | |
| 633 | 647 | #region Shortcodes |
| 634 | - function separate_mwcode_atts( $atts ) { | |
| 635 | 648 | |
| 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 | - | |
| 643 | 649 | function content_shortcode( $atts ) { |
| 644 | 650 | |
| 645 | - $user_atts = $this->separate_mwcode_atts( $atts ); | |
| 646 | - | |
| 647 | 651 | $atts = shortcode_atts( array( |
| 648 | - 'id' => null, | |
| 649 | - 'target' => null, // js or php | |
| 650 | - 'code' => null, // For Guttenberg block usage | |
| 651 | - ), $atts, 'code-engine' ); | |
| 652 | + 'id' => null, | |
| 653 | + 'target' => null, | |
| 654 | + 'code' => null, | |
| 655 | + ), $atts ); | |
| 652 | 656 | |
| 653 | 657 | $id = $atts['id']; |
| 654 | 658 | $target = $atts['target']; |
| 655 | 659 | $code = $atts['code']; |
| @@ -732,10 +736,9 @@ | ||
| 732 | 736 | $output = '<script>' . $snippet['code'] . '</script>'; |
| 733 | 737 | } |
| 734 | 738 | |
| 735 | 739 | if ( $is_content_php ) { |
| 736 | - $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );"; | |
| 737 | - $output = $this->run_non_fn_snippet( $id, null, false, $prefix ); | |
| 740 | + $output = $this->run_non_fn_snippet( $id ); | |
| 738 | 741 | } |
| 739 | 742 | |
| 740 | 743 | return $output; |
| 741 | 744 | } |