PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.2.9
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.2.9
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
code-engine / classes / load.php

load.php in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.2.9, at classes/load.php

169 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Execute all active snippets
4 add_action( 'plugins_loaded', 'mwcode_loaded', 1 );
5
6 function mwcode_loaded( ) {
7 global $mwcode_core, $current_mwcode_snippet;
8
9
10 if ( !isset( $mwcode_core ) ) {
11 $mwcode_core = new Meow_MWCODE_Core( );
12 }
13
14 $snippets = $mwcode_core->execute_active_snippets( );
15
16 if ( empty( $snippets ) ) {
17 return;
18 }
19
20 foreach ( $snippets as $snippet ) {
21
22 if ( $snippet['blocked'] ) {
23 $mwcode_core->log( "⚠️ Snippet is blocked: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " ) Because it's not a frontend request, on a non authorized page." );
24 continue;
25 }
26
27 $current_mwcode_snippet = $snippet;
28
29
30 ob_start( );
31
32 try {
33 error_log( "Executing snippet: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " )" );
34 eval( $snippet['code'] );
35 } catch ( Throwable $e ) {
36 $mwcode_core->log( "⚠️ Exception in snippet: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " )" );
37 $mwcode_core->log( $e->getMessage( ) );
38 $mwcode_core->update_option( 'fatal_error', "\"{$snippet["name"]}\" - " . $e->getMessage( ) );
39 $mwcode_core->update_option( 'thrown_snippet', $snippet );
40 }
41
42 ob_end_clean( );
43 }
44
45 $current_mwcode_snippet = null;
46 }
47
48 $file_path = MWCODE_PATH . '/classes/modules/helpers.php';
49 if ( file_exists( $file_path ) ) {
50 require_once( $file_path );
51 }
52
53 $file_path_common = MWCODE_PATH . '/common/helpers.php';
54 if ( file_exists( $file_path_common ) ) {
55 require_once( $file_path_common );
56 }
57
58
59 // In admin or Rest API request ( REQUEST URI begins with '/wp-json/' )
60 if ( is_admin( ) || MeowCommon_Helpers::is_rest( ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
61 global $mwcode_core;
62 $mwcode_core = new Meow_MWCODE_Core( );
63 }
64
65 // Define a global variable to store the currently executing snippet
66 global $current_mwcode_snippet;
67
68 /**
69 * Custom error handler
70 */
71 function mwcode_error_handler( $errno, $errstr, $errfile, $errline )
72 {
73 global $current_mwcode_snippet, $mwcode_core;
74
75 if ( !isset( $current_mwcode_snippet ) ) {
76 return false;
77 }
78
79 if ( !isset( $mwcode_core ) ) {
80 $mwcode_core = new Meow_MWCODE_Core( );
81 }
82
83 if ( !( error_reporting( ) & $errno ) ) {
84 // This error code is not included in error_reporting
85 return false;
86 }
87
88 if ( !array_key_exists( 'name', $current_mwcode_snippet ) ) {
89 return false;
90 }
91
92 $error = "$errstr in $errfile on line $errline";
93
94 $mwcode_core->log( "⚠️ Error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" );
95 $mwcode_core->log( $error );
96
97 $mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - $errstr" );
98 $mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet );
99
100 // Don't execute PHP internal error handler
101 return true;
102 }
103
104 /**
105 * Shutdown function to catch fatal errors
106 */
107 function mwcode_shutdown_function( )
108 {
109 global $current_mwcode_snippet, $mwcode_core;
110
111 if ( !isset( $current_mwcode_snippet ) ) {
112 return;
113 }
114
115 if ( !isset( $mwcode_core ) ) {
116 $mwcode_core = new Meow_MWCODE_Core( );
117 }
118
119 $error = error_get_last( );
120
121 $fatal_error = $error !== null && in_array( $error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR] );
122
123 if ( $fatal_error ) {
124
125 $mwcode_core->log( "⚠️ Fatal error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" );
126 $mwcode_core->log( $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'] );
127
128 $mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - " . $error['message'] );
129 $mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet );
130
131 }
132 }
133
134 // Set the custom error handler and shutdown function
135 set_error_handler( "mwcode_error_handler" );
136 register_shutdown_function( "mwcode_shutdown_function" );
137
138
139 // Load all the JS functions to the front & back
140 // TODO: Add some restrictions by pages
141 function enqueue_mwcode_scripts( )
142 {
143 global $mwcode_core;
144
145 if ( !isset( $mwcode_core ) ) {
146 $mwcode_core = new Meow_MWCODE_Core( );
147 }
148
149 wp_register_script( 'mwcode-scripts-js', '' );
150 wp_enqueue_script( 'mwcode-scripts-js' );
151
152 $js_code = '';
153
154 if ( is_admin( ) ) {
155 $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
156 if ( $page === 'mwai_settings' ) {
157 $js_code = $mwcode_core->get_js_functions_to_push( );
158 }
159 } else {
160 $js_code = $mwcode_core->get_js_functions_to_push( );
161 }
162
163 if ( $js_code ) {
164 wp_add_inline_script( 'mwcode-scripts-js', $js_code, 'after' );
165 }
166 }
167
168 add_action( 'wp_enqueue_scripts', 'enqueue_mwcode_scripts' );
169 add_action( 'admin_enqueue_scripts', 'enqueue_mwcode_scripts' );