PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.2
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.2
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 +819 -553 0.2.80.4.2 View file →
@@ -6,686 +6,952 @@
6 6 use PhpParser\Error;
7 7
8 8 class Meow_MWCODE_Core
9 9 {
10 - public $admin = null;
11 - public $snippet = null;
12 - public $is_rest = false;
13 - public $is_cli = false;
14 - public $site_url = null;
15 - public $mwcode = null;
10 + public $admin = null;
11 + public $snippet = null;
12 + public $is_rest = false;
13 + public $is_cli = false;
14 + public $site_url = null;
15 + public $mwcode = null;
16 + public $licenser = null;
16 17
17 - private $option_name = 'mwcode_options';
18 + private $option_name = 'mwcode_options';
18 19
19 - public function __construct() {
20 - global $mwcode;
21 -
22 - $this->site_url = get_site_url();
23 - $this->is_rest = MeowCommon_Helpers::is_rest();
24 - $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
25 -
26 - // Snippets
27 - $snippet = new Meow_MWCODE_Modules_Snippet( $this );
28 - $this->snippet = $snippet;
20 + public function __construct() {
21 + global $mwcode;
29 22
30 - // Create API before plugins_loaded
31 - $this->mwcode = new Meow_MWCODE_API( $this, $snippet );
32 - $mwcode = $this->mwcode;
33 -
34 - add_action( 'plugins_loaded', array( $this, 'init' ) );
35 - }
23 + $this->site_url = get_site_url();
24 + $this->is_rest = MeowKit_MWCODE_Helpers::is_rest();
25 + $this->is_cli = defined( 'WP_CLI' ) && WP_CLI;
36 26
37 - function init() {
38 - // Part of the core, settings and stuff
39 - $this->admin = new Meow_MWCODE_Admin( $this );
27 + // Snippets
28 + $snippet = new Meow_MWCODE_Modules_Snippet( $this );
29 + $this->snippet = $snippet;
40 30
41 - // Only for REST
42 - if ( $this->is_rest ) {
43 - new Meow_MWCODE_Rest( $this, $this->admin, $this->snippet );
44 - }
45 - }
31 + // Create API before plugins_loaded
32 + $this->mwcode = new Meow_MWCODE_API( $this, $snippet );
33 + $mwcode = $this->mwcode;
46 34
35 + // Add the shortcode for the "content" snippets
36 + add_shortcode( 'code-engine', [ $this, 'content_shortcode' ] );
47 37
48 - /**
49 - *
50 - * Roles & Access Rights
51 - *
52 - */
53 - #region Roles & Access Rights
54 - public function can_access_settings() {
55 - return apply_filters( 'mwcode_allow_setup', current_user_can( 'manage_options' ) );
56 - }
38 + add_action( 'plugins_loaded', array( $this, 'init' ) );
39 + }
57 40
58 - public function can_access_features() {
59 - return apply_filters( 'mwcode_allow_usage', current_user_can( 'administrator' ) );
60 - }
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 + }
61 46
62 - public function check_rest_nonce( $request ) {
63 - $nonce = $request->get_header( 'X-WP-Nonce' );
64 - return wp_verify_nonce( $nonce, 'wp_rest' );
65 - }
66 - #endregion
47 + // Part of the core, settings and stuff
48 + $this->admin = new Meow_MWCODE_Admin( $this );
67 49
68 - #region Options
50 + // Only for REST
51 + if ( $this->is_rest ) {
52 + new Meow_MWCODE_Rest( $this, $this->admin, $this->snippet );
53 + }
54 +
55 + // MCP integration - check both class and global variable
56 + if ( class_exists( 'Meow_MWAI_Core' ) || isset( $GLOBALS['mwai'] ) ) {
57 + new Meow_MWCODE_MCP( $this );
58 + }
59 + }
69 60
70 - function get_option( $option, $default = null ) {
71 - $options = $this->get_all_options();
72 - return $options[$option] ?? $default;
73 - }
61 + /**
62 + *
63 + * Roles & Access Rights
64 + *
65 + */
66 + #region Roles & Access Rights
67 + public function can_access_settings() {
68 + return apply_filters( 'mwcode_allow_setup', current_user_can( 'manage_options' ) );
69 + }
74 70
75 - function list_options() {
76 - return [
77 - //Safemode
78 - "safe_mode_status" => "on", // on, off, whitelist
79 - "safe_mode_whitelist" => [],
80 -
81 - //LOGS
82 - "server_debug_mode" => false,
71 + public function can_access_features() {
72 + return apply_filters( 'mwcode_allow_usage', current_user_can( 'administrator' ) );
73 + }
83 74
84 - //UI
85 - "ui_show_preview" => true,
75 + public function check_rest_nonce( $request ) {
76 + $nonce = $request->get_header( 'X-WP-Nonce' );
77 + return wp_verify_nonce( $nonce, 'wp_rest' );
78 + }
79 + #endregion
86 80
87 - //AI
88 - "ai_suggestions" => false,
89 - "ai_engine_status"=> false,
90 - "ai_engine_message" => "",
81 + #region Options
91 82
92 - //API
93 - "api_endpoint" => false,
94 - "api_token" => md5( time() . rand() ),
95 - ];
96 - }
83 + function get_option( $option, $default = null ) {
84 + $options = $this->get_all_options();
85 + return $options[$option] ?? $default;
86 + }
97 87
98 - function get_all_options( ) {
99 - $options = get_option( $this->option_name, $this->list_options( ) );
100 - $options = $this->sanitize_options( $options );
101 -
102 - return $options;
103 - }
88 + function list_options() {
89 + return [
90 + //Safemode
91 + "safe_mode_status" => "on", // on, off, whitelist
92 + "safe_mode_whitelist" => [],
93 + //"disallow_block_php" => true, // Do not allow PHP code to be execute through Blocks "code" parameter
94 + "code_blocks" => false,
95 + "code_blocks_whitelist" => [], // Whitelist for code blocks, if empty, all code blocks are allowed
96 +
97 + //LOGS
98 + "server_debug_mode" => false,
104 99
105 - function update_options( $options ) {
106 - $current_options = get_option($this->option_name);
107 -
108 - if ($current_options === $options) {
109 - // $this->log('💾 The options are already the expected value.');
110 - } else {
111 - if ( !update_option( $this->option_name, $options, false ) ) {
112 - $this->log( '💾 There was an issue updating the options.' );
113 - }
114 - }
115 -
116 - $options = $this->sanitize_options( $options );
117 - return $options;
118 - }
100 + //UI
101 + "ui_show_preview" => false,
119 102
120 - function update_option( $option, $value ) {
121 - $options = $this->get_all_options();
122 - $options[$option] = $value;
123 - return $this->update_options( $options );
124 - }
103 + //AI
104 + "ai_suggestions" => false,
105 + "ai_engine_status"=> false,
106 + "ai_engine_message" => "",
125 107
126 - function reset_options() {
127 - if ( $this->get_all_options() === $this->list_options() ) {
128 - return true;
129 - }
130 - return $this->update_options( $this->list_options() );
131 - }
108 + //API
109 + "api_endpoint" => false,
110 + "api_token" => md5( time() . rand() ),
111 +
112 + //MCP
113 + "mcp_support" => false,
132 114
133 - // Validate and keep the options clean and logical.
134 - function sanitize_options( $options ) {
135 - $options_modified = false;
115 + //MAINTENANCE
116 + "clean_uninstall" => false,
117 + ];
118 + }
136 119
137 - // Make sure safe mode whitelist is an array
138 - if ( ! is_array( $options['safe_mode_whitelist'] ) ) {
139 - $options['safe_mode_whitelist'] = explode( ",", $options['safe_mode_whitelist'] );
140 - $options_modified = true;
141 - }
120 + function get_all_options( ) {
121 + $options = get_option( $this->option_name, [] );
122 + $defaults = $this->list_options();
123 +
124 + // Merge with defaults to ensure all options exist
125 + $options = array_merge( $defaults, $options );
126 +
127 + $options = $this->sanitize_options( $options );
128 + return $options;
129 + }
142 130
143 - // Update AI Engine status
144 - $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified;
131 + function update_options( $options ) {
145 132
146 - // Disable AI related features if AI Engine is not available
147 - if ( ! $options['ai_engine_status'] && $options['ai_suggestions'] !== false ) {
148 - $options['ai_suggestions'] = false;
149 - $options_modified = true;
150 - }
133 + $options = $this->sanitize_options( $options );
151 134
152 - if ( $options_modified ) {
153 - update_option( $this->option_name, $options, false );
154 - }
135 + if ( !update_option( $this->option_name, $options, false ) ) {
136 + $this->log( '💾 There was an issue updating the options.' );
137 + }
138 +
139 + return $options;
140 + }
155 141
156 - return $options;
157 - }
142 + function update_option( $option, $value ) {
143 + $options = $this->get_all_options();
144 + $options[$option] = $value;
145 + return $this->update_options( $options );
146 + }
158 147
159 - private function updateAIEngineStatus( &$options ) {
160 - global $mwai;
148 + function reset_options() {
149 + if ( $this->get_all_options() === $this->list_options() ) {
150 + return true;
151 + }
152 + return $this->update_options( $this->list_options() );
153 + }
161 154
162 - if ( is_null( $mwai ) || ! isset( $mwai ) ) {
163 - $options['ai_engine_status'] = false;
164 - $options['ai_engine_message'] = 'AI Engine is not available.';
165 - return true;
166 - }
155 + // Validate and keep the options clean and logical.
156 + function sanitize_options( $options ) {
157 + $options_modified = false;
158 +
159 + // Ensure mcp_support exists in options
160 + if ( !isset( $options['mcp_support'] ) ) {
161 + $options['mcp_support'] = false;
162 + }
167 163
168 - try {
169 - $status = $mwai->checkStatus();
164 + // Make sure safe mode whitelist is an array
165 + if ( ! is_array( $options['safe_mode_whitelist'] ) ) {
166 + $options['safe_mode_whitelist'] = explode( ",", $options['safe_mode_whitelist'] );
167 + $options_modified = true;
168 + }
170 169
171 - if ( $options['ai_engine_status'] != true || $options['ai_engine_message'] != $status ) {
172 - $options['ai_engine_status'] = true;
173 - $options['ai_engine_message'] = $status;
174 - return true;
175 - }
176 - } catch ( Exception $e ) {
177 - if ( $options['ai_engine_status'] != false || $options['ai_engine_message'] != $e->getMessage() ) {
178 - $options['ai_engine_status'] = false;
179 - $options['ai_engine_message'] = $e->getMessage();
180 - return true;
181 - }
182 - }
170 + // Update AI Engine status
171 + $options_modified = $this->updateAIEngineStatus( $options ) || $options_modified;
183 172
184 - return false;
185 - }
173 + // Disable AI related features if AI Engine is not available
174 + if ( ! $options['ai_engine_status'] ) {
175 + if ( $options['ai_suggestions'] !== false ) {
176 + $options['ai_suggestions'] = false;
177 + $options_modified = true;
178 + }
179 + // Note: We don't disable MCP support here anymore
180 + // It will be checked at runtime in the MCP class
181 + }
186 182
187 - // #endregion
183 + return $options;
184 + }
188 185
189 - #region Snippets
186 + private function updateAIEngineStatus( &$options ) {
187 + global $mwai;
190 188
191 - /**
192 - * Get snippet.
193 - *
194 - * @param $id
195 - * @return mixed
196 - */
197 - protected function get_snippet( $id ) {
198 - if ( $this->snippet === null ) {
199 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
200 - }
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 + }
201 194
202 - return $this->snippet->select_one( $id );
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 + }
203 209 }
204 210
205 - private function sanitize_arg( $name, $value ) {
206 - if ( $name[0] !== '$' ) { $name = '$' . $name; }
207 -
208 - if ( !empty( $value ) && !is_numeric( $value ) && $value[0] !== '"' && $value[strlen( $value ) - 1] !== '"' ) {
209 - $value = '"' . esc_sql( $value ) . '"';
210 - }
211 + return false;
212 + }
211 213
212 - return [ $name, $value ];
213 - }
214 + #endregion
214 215
215 - function run_non_fn_snippet( $id ) {
216 - $snippet = $this->get_snippet( $id );
217 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
216 + #region Snippets
218 217
219 - $error = null;
220 - $output = null;
218 + /**
219 + * Get snippet.
220 + *
221 + * @param $id
222 + * @return mixed
223 + */
224 + protected function get_snippet( $id ) {
225 + if ( $this->snippet === null ) {
226 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
227 + }
221 228
222 - try {
223 - ob_start();
224 - eval( $snippet['code'] );
225 - $output = ob_get_clean();
226 - } catch ( Throwable $e ) {
227 - $error = new Exception( ' Error executing the snippet, ' . $e->getMessage() );
228 - ob_clean();
229 - } finally {
230 - restore_error_handler();
231 - }
229 + return $this->snippet->select_one( $id );
230 + }
232 231
233 - if ( $error !== null ) {
234 - throw $error;
235 - }
232 + function add_snippet( $params ) {
236 233
237 - return $output;
238 - }
234 + $response = [
235 + "snippet" => null,
236 + "result" => false,
237 + ];
239 238
240 - function run_snippet( $id, $args = [], $params = [] )
241 - {
242 - // Static array to track defined functions
243 - static $defined_functions = array();
239 + $this->snippet->validate( $params );
244 240
245 - if ( $id ) { // If there is an ID, we get the snippet, if not we get the data from the params
246 - $snippet = $this->get_snippet( $id );
247 - $this->snippet->get_function_snippets_data( $snippet ); // adds the function data to the snippet
241 + $params = $this->snippet->formatParamsForDatabase( $params );
242 + $result = $this->snippet->insert( $params );
243 + $snippet = $this->snippet->select_one( $result );
248 244
249 - $params = [ // We set the params according to the snippet we fetched
250 - 'test' => false, // If we pass an ID to the function, we are not testing the snippet
251 - // 'test' => $params['test'] ?? false if needed we can still use ID and test at the same time (should not happen)
252 - 'code' => $snippet['code'],
253 - 'name' => $snippet['functionName'],
254 - 'args' => $snippet['functionArgs'],
255 - 'values' => $snippet['functionArgsDict'] // Contains the default values of the arguments
256 - ];
257 - }
245 + if( $result ) {
246 + $params['id'] = (string)$result;
258 247
259 - // Sanitize all the arguments if the option is enabled
260 - if ( $this->get_option( 'sanitize_arguments', true ) ) {
248 + $this->snippet->create_or_update_function_snippet( $params );
249 + $this->snippet->create_or_update_interval_snippet( $params );
261 250
262 - if ( $args ) {
263 - foreach ( $args as $name => $value ) {
264 - list( $sanitizedName, $sanitizedValue ) = $this->sanitize_arg( $name, $value );
265 - unset( $args[$name] );
251 + $this->snippet->get_function_snippets_data( $snippet );
252 + }
266 253
267 - $args[$sanitizedName] = $sanitizedValue;
268 - }
269 - }
254 + $response['snippet'] = $snippet;
255 + $response['result'] = $result;
270 256
271 - foreach ( $params['values'] as $name => $value ) {
257 + return $response;
258 + }
272 259
273 - if( array_key_exists( 'input', $value) ) {
274 - list( $sanitizedInputName, $sanitizedInputValue ) = $this->sanitize_arg( $name, $value['input'] );
275 - $params['values'][$sanitizedInputName]['input'] = $sanitizedInputValue;
276 - }
277 -
278 - if( array_key_exists( 'default', $value) ) {
279 - list( $sanitizedDefaultValueName, $sanitizedDefaultValue ) = $this->sanitize_arg( $name, $value['default'] );
280 - $params['values'][$sanitizedDefaultValueName]['default'] = $sanitizedDefaultValue;
281 - }
282 - }
260 + private function sanitize_arg( $name, $value, $type = null) {
261 + $real_type = gettype( $value );
283 262
284 - }
263 + if ( $name[0] !== '$' ) { $name = '$' . $name; }
285 264
286 -
265 + if ( $type == null ) {
266 + $type = $real_type;
267 + }
287 268
288 - // Make sure the function is existing and is the one in the snippet
289 - if ( empty( $params['code'] ) ) {
290 - throw new Exception( 'Code Engine: The snippet code appears to be empty.' );
291 - }
292 -
293 - if ( empty( $params['name'] ) || ! str_contains( $params['code'], $params['name'] ) ) {
294 - throw new Exception( "Code Engine: Function name does not match. The name should be {$params['name']}." );
295 - }
269 + if ( $type != 'array' && !empty( $value ) && !is_numeric( $value ) && $value[0] !== '"' && $value[strlen( $value ) - 1] !== '"' ) {
270 + $value = '"' . esc_sql( $value ) . '"';
271 + }
296 272
297 - // Overwrite the default values with the provided ones
298 - if ( $args ) {
299 - foreach ( $args as $name => $value ) {
300 - $params['values'][$name]['input'] = $value;
301 - }
273 + if ( $type === 'array' && $real_type === 'string' ) {
274 + // We got a string like this: "["a", "b", "c"]" or "[ 1, 2, 3 ]"
275 + // We need to convert it to an array
276 + $value = str_replace( '"', '', $value );
277 + $value = str_replace( '[', '', $value );
278 + $value = str_replace( ']', '', $value );
279 + $value = explode( ',', $value );
280 + $value = array_map( 'trim', $value );
281 + }
302 282
303 - $this->log( '⚡ Arguments provided: ' . json_encode( $args ) );
304 - }
283 + if ( $type === 'array' ) {
284 + // Convert to PHP array format instead of JSON
285 + $value = var_export( $value, true );
286 + }
305 287
306 - // Check if the function has already been defined
307 - if ( !in_array( $params['name'], $defined_functions ) ) {
288 + return [ $name, $value ];
289 + }
308 290
309 - // If not, proceed with modification and definition
310 - if ( $params['test'] ) { // Make sure the echo statement uses a line break
311 - $params['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $params['code'] );
312 - } else { // Remove all echo statements
313 - $params['code'] = preg_replace( '/echo\s+(.+?);/s', '', $params['code'] );
314 - }
291 + function run_non_fn_snippet( $id, $code = null, $test = false, $prefix = '' ) {
292 + // Retrieve the snippet code from the provided code or via the snippet ID.
293 + if ( $code ) {
294 + $snippet = [ 'code' => $code ];
295 + } else {
296 + $snippet = $this->get_snippet( $id );
297 + }
315 298
316 - $params['code'] = "if (!function_exists('{$params['name']}')) {\n" . $params['code'] . "\n}\n";
299 + // Remove any PHP opening tag.
300 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
317 301
318 - // Add the function name to the array to avoid redefinition
319 - $defined_functions[] = $params['name'];
320 - } else {
321 - // If already defined, just prepare to call the function without redefining it
322 - $params['code'] = '';
323 - }
302 + if ( $test ) {
303 + $snippet['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $snippet['code'] );
304 + }
324 305
325 - // Prepare the code to be executed
326 - $params['code'] .= "\n\$mwcode_result = {$params['name']}(";
327 - foreach ( $params['args'] as $index => $arg ) {
328 - $value = 'null'; // In case the argument is not provided it will be null
306 + if( $prefix ) {
307 + $snippet['code'] = $prefix . "\n" . $snippet['code'];
308 + }
309 +
310 + $error = null;
311 + $output = null;
312 +
313 + try {
314 + ob_start();
315 + eval( $snippet['code'] );
316 + $output = ob_get_clean();
317 + } catch ( Throwable $e ) {
318 + $snippet_id = $id ? " ( ID: $id )" : '(Content Gutenberg Block)';
319 + $this->log( '🔴 Error executing the snippet ' . $snippet_id . ' : ' . $e->getMessage() );
320 + ob_clean();
321 + } finally {
322 + restore_error_handler();
323 + }
324 +
325 + // If in test mode, return output as an array of lines with an 'error' key if needed.
326 + if ( $test ) {
327 + $output = explode( "\n", trim( $output ) );
328 + if ( $error !== null ) {
329 + $output['error'] = $error->getMessage();
330 + }
331 + } else {
332 + if ( $error !== null ) {
333 + throw $error;
334 + }
335 + }
336 +
337 + return $output;
338 + }
329 339
330 - if ( array_key_exists( $arg, $params['values'] ) ) { // Avoid warnings if the argument is not provided
340 + function run_snippet( $id, $args = [], $params = [] )
341 + {
342 + // Static array to track defined functions
343 + static $defined_functions = array();
331 344
332 - // If the argument is provided, use it, if not use the default value
333 - if ( !empty( $params['values'][$arg]['input'] ) ) {
334 - $value = $params['values'][$arg]['input'];
345 + if ( $id ) { // If there is an ID, we get the snippet, if not we get the data from the params
346 + $snippet = $this->get_snippet( $id );
347 + $this->snippet->get_function_snippets_data( $snippet ); // adds the function data to the snippet
335 348
336 - } else if ( !empty( $params['values'][$arg]['default'] ) ) {
337 - $value = $params['values'][$arg]['default'];
338 - }
339 - }
349 + $params = [ // We set the params according to the snippet we fetched
350 + 'test' => false, // If we pass an ID to the function, we are not testing the snippet
351 + // 'test' => $params['test'] ?? false if needed we can still use ID and test at the same time (should not happen)
352 + 'code' => $snippet['code'],
353 + 'name' => $snippet['functionName'],
354 + 'args' => $snippet['functionArgs'],
355 + 'values' => $snippet['functionArgsDict'] // Contains the default values of the arguments
356 + ];
357 + }
340 358
341 - $params['code'] .= "{$value}";
342 - if ( $index < count( $params['args'] ) - 1 ) {
343 - $params['code'] .= ', ';
344 - }
345 - }
346 - $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
359 + // Sanitize all the arguments if the option is enabled
360 + if ( $this->get_option( 'sanitize_arguments', true ) ) {
347 361
348 - $error = null;
349 - $output = null;
362 + if ( $args ) {
363 + foreach ( $args as $name => $value ) {
364 + list( $sanitizedName, $sanitizedValue ) = $this->sanitize_arg( $name, $value );
365 + unset( $args[$name] );
350 366
351 - try {
352 - ob_start();
353 - eval( $params['code'] );
354 - $output = ob_get_clean();
355 -
356 - if ( $params['test'] ){
357 - $output = explode( "\n", $output );
358 - }
359 -
360 - } catch ( Throwable $e ) {
361 - //$this->log('Code Engine: Error executing the function: ' . $e->getMessage());
362 - $error = new Exception(' Error executing the function, ' . $e->getMessage());
367 + $args[$sanitizedName] = $sanitizedValue;
368 + }
369 + }
363 370
364 - ob_clean();
365 - } finally {
366 - restore_error_handler();
367 - }
371 + foreach ( $params['values'] as $name => $value ) {
368 372
369 - if ( $error !== null ) {
370 - if( $params['test'] ){
371 - $output['error'] = $error->getMessage();
372 - } else {
373 - throw $error;
374 - }
375 - }
373 + if( array_key_exists( 'input', $value) ) {
374 + list( $sanitizedInputName, $sanitizedInputValue ) = $this->sanitize_arg( $name, $value['input'], $value['type'] );
375 + $params['values'][$sanitizedInputName]['input'] = $sanitizedInputValue;
376 + }
376 377
377 - return $output;
378 - }
378 + if( array_key_exists( 'default', $value) ) {
379 + list( $sanitizedDefaultValueName, $sanitizedDefaultValue ) = $this->sanitize_arg( $name, $value['default'], $value['type'] );
380 + $params['values'][$sanitizedDefaultValueName]['default'] = $sanitizedDefaultValue;
381 + }
382 + }
379 383
384 + }
380 385
381 - function parse_snippet( $code, $new_snippet = false ){
382 - $parser = ( new ParserFactory( ) )->createForNewestSupportedVersion( );
386 + // Make sure the function is existing and is the one in the snippet
387 + if ( empty( $params['code'] ) ) {
388 + throw new Exception( 'Code Engine: The snippet code appears to be empty.' );
389 + }
383 390
384 - if( !$this->snippet ){
385 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
386 - }
391 + if ( empty( $params['name'] ) || ! str_contains( $params['code'], $params['name'] ) ) {
392 + throw new Exception( "Code Engine: Function name does not match. The name should be {$params['name']}." );
393 + }
387 394
388 - // First we check the function names are unique
389 - $fn = $this->snippet->sanitize_and_check_functions( $code, $new_snippet );
390 - if ( ! $fn['is_valid'] ) {
395 + // Overwrite the default values with the provided ones
396 + if ( $args ) {
397 + foreach ( $args as $name => $value ) {
398 + $params['values'][$name]['input'] = $value;
399 + }
391 400
392 - $lint = [
393 - 'line' => 1,
394 - 'attributes' => $fn['attributes'][0],
395 - 'raw_message' => implode(', ', $fn['errors'][0]),
396 - 'message' => implode(', ', $fn['errors'][0]),
397 - ];
401 + $this->log( '⚡ Arguments provided: ' . json_encode( $args ) );
402 + }
398 403
399 - return $lint;
400 - }
404 + // Check if the function has already been defined
405 + if ( !in_array( $params['name'], $defined_functions ) ) {
401 406
402 - try {
403 - $stmts = $parser->parse( $code );
404 - $result = $stmts;
405 - } catch ( PhpParser\Error $e ) {
407 + // If not, proceed with modification and definition
408 + if ( $params['test'] ) { // Make sure the echo statement uses a line break
409 + $params['code'] = preg_replace( '/echo\s+(.+?);/s', 'echo $1 . "\n";', $params['code'] );
410 + } else { // Remove all echo statements
411 + $params['code'] = preg_replace( '/echo\s+(.+?);/s', '', $params['code'] );
412 + }
406 413
407 - $lint = [
408 - 'line' => $e->getStartLine(),
409 - 'attributes' => $e->getAttributes(),
410 - 'raw_message' => $e->getRawMessage(),
411 - 'message' => $e->getMessage(),
412 - ];
414 + $params['code'] = "if (!function_exists('{$params['name']}')) {\n" . $params['code'] . "\n}\n";
413 415
414 - return $lint;
415 - }
416 + // Add the function name to the array to avoid redefinition
417 + $defined_functions[] = $params['name'];
418 + } else {
419 + // If already defined, just prepare to call the function without redefining it
420 + $params['code'] = '';
421 + }
416 422
417 - return null;
418 - }
423 + // Prepare the code to be executed
424 + $params['code'] .= "\n\$mwcode_result = {$params['name']}(";
425 + foreach ( $params['args'] as $index => $arg ) {
426 + $value = 'null'; // In case the argument is not provided it will be null
419 427
420 - public function get_js_functions_to_push() {
421 - $functions = $this->snippet->get_functions();
422 - $js_functions = [];
423 - foreach ( $functions as &$function ) {
424 - if ( !isset( $function['target'] ) ) {
425 - $function['target'] = 'php';
426 - }
427 - if ( $function['target'] == 'js' ) {
428 - $js_functions[] = $function;
429 - }
430 - }
431 - $snippets = [];
432 - foreach ( $js_functions as $function ) {
433 - $snippet = $this->snippet->select_one( $function['snippetId'] );
434 - $snippet['function_info'] = $function; // Add function info to snippet
435 - $snippets[] = $snippet;
436 - }
437 -
438 - return $this->generate_js_functions_code( $snippets );
439 - }
440 -
441 - function generate_js_functions_code ($snippets ) {
442 - $code = "";
443 - foreach ( $snippets as $snippet ) {
444 - $function_code = $snippet['code'];
445 - $function_info = $snippet['function_info'];
446 -
447 - // Extract function name and arguments
448 - preg_match( '/(?:const|let|var)?\s*(\w+)\s*=\s*\((.*?)\)\s*=>/', $function_code, $matches );
449 - $function_name = $matches[1] ?? $function_info['name'];
450 - $function_args = $matches[2] ?? '';
451 -
452 - // Prepare default values
453 - $default_args = [];
454 - foreach ( $function_info['args'] as $arg ) {
455 - if ( isset( $arg['default'] ) && $arg['default'] !== '' ) {
456 - $default_args[$arg['name']] = $arg['default'];
457 - }
458 - }
459 -
460 - // Modify function to use default values
461 - if ( !empty( $default_args ) ) {
462 - $new_args = explode( ',', $function_args );
463 - foreach ( $new_args as &$arg ) {
464 - $arg = trim( $arg );
465 - if ( isset( $default_args[$arg] ) ) {
466 - $arg .= " = " . json_encode( $default_args[$arg] );
467 - }
468 - }
469 - $new_args_string = implode( ', ', $new_args );
470 - $function_code = preg_replace(
471 - '/(\w+)\s*=\s*\((.*?)\)\s*=>/',
472 - "$1 = ($new_args_string) =>",
473 - $function_code
474 - );
475 - }
476 -
477 - $code .= $function_code . "\n\n";
478 - }
428 + if ( array_key_exists( $arg, $params['values'] ) ) { // Avoid warnings if the argument is not provided
479 429
480 - return $code;
481 - }
430 + // If the argument is provided, use it, if not use the default value
431 + if ( !empty( $params['values'][$arg]['input'] ) ) {
432 + $value = $params['values'][$arg]['input'];
482 433
434 + } else if ( !empty( $params['values'][$arg]['default'] ) ) {
435 + $value = $params['values'][$arg]['default'];
436 + }
437 + }
483 438
484 - /**
485 - * [STATIC] Execute active snippets.
486 - *
487 - * @return array
488 - */
489 - public function execute_active_snippets() {
439 + $params['code'] .= "{$value}";
440 + if ( $index < count( $params['args'] ) - 1 ) {
441 + $params['code'] .= ', ';
442 + }
443 + }
490 444
491 - $blocked = false;
492 - $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
493 - if ( $page === 'mwcode_settings' || !Meow_MWCODE_Core::is_white_listed_rest() ) {
494 - $blocked = true;
495 - }
445 + $params['code'] .= ");\necho print_r(\$mwcode_result, true);";
496 446
497 - if ( empty( $this->snippet ) ) {
498 - $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
499 - }
447 + $error = null;
448 + $output = null;
449 +
450 + try {
451 + ob_start();
452 + eval( $params['code'] );
453 + $output = ob_get_clean();
454 +
455 + if ( $params['test'] ){
456 + $output = explode( "\n", $output );
457 + }
458 +
459 + } catch ( Throwable $e ) {
460 + //$this->log('Code Engine: Error executing the function: ' . $e->getMessage());
461 + $error = new Exception(' Error executing the function, ' . $e->getMessage());
500 462
501 - $ts = $this->get_option( 'thrown_snippet', null );
502 - if ( !empty( $ts ) ) {
503 - $this->log( "⚠️ Your snippet \"{$ts['name']}\" has thrown a fatal error last time, so we disabled it. Please check the logs for more information." );
504 - $this->snippet->force_disable( $ts['id'] );
505 - $this->update_option( 'thrown_snippet', null );
506 - }
463 + ob_clean();
464 + } finally {
465 + restore_error_handler();
466 + }
507 467
508 - $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ];
509 - // Get all active snippets
468 + if ( $error !== null ) {
469 + if( $params['test'] ){
470 + $output['error'] = $error->getMessage();
471 + } else {
472 + throw $error;
473 + }
474 + }
510 475
511 -
476 + return $output;
477 + }
512 478
513 - $snippets = $this->snippet->select(
514 - null, // offset
515 - -1, // limit
516 - [
517 - [ 'accessor' => 'active', 'value' => 1 ],
518 - [ 'accessor' => 'scope', 'value' => $scope ],
519 - ], // filter
520 - [ 'accessor' => 'priority', 'by' => 'DESC' ] // sort
521 - )['data'];
522 479
480 + function parse_snippet( $code, $new_snippet = false ){
481 + $parser = ( new ParserFactory( ) )->createForNewestSupportedVersion( );
523 482
524 - if ( empty( $snippets ) ) {
525 - return;
483 + if( !$this->snippet ){
484 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
485 + }
486 +
487 + // First we check the function names are unique
488 + $fn = $this->snippet->sanitize_and_check_functions( $code, $new_snippet );
489 + if ( ! $fn['is_valid'] ) {
490 +
491 + $lint = [
492 + 'line' => 1,
493 + 'attributes' => $fn['attributes'][0],
494 + 'raw_message' => implode(', ', $fn['errors'][0]),
495 + 'message' => implode(', ', $fn['errors'][0]),
496 + ];
497 +
498 + return $lint;
499 + }
500 +
501 + try {
502 + $stmts = $parser->parse( $code );
503 + $result = $stmts;
504 + } catch ( PhpParser\Error $e ) {
505 +
506 + $lint = [
507 + 'line' => $e->getStartLine(),
508 + 'attributes' => $e->getAttributes(),
509 + 'raw_message' => $e->getRawMessage(),
510 + 'message' => $e->getMessage(),
511 + ];
512 +
513 + return $lint;
514 + }
515 +
516 + return null;
517 + }
518 +
519 + public function get_js_functions_to_push() {
520 + $functions = $this->snippet->get_functions();
521 + $js_functions = [];
522 + foreach ( $functions as &$function ) {
523 + if ( !isset( $function['target'] ) ) {
524 + $function['target'] = 'php';
525 + }
526 + if ( $function['target'] == 'js' ) {
527 + $js_functions[] = $function;
528 + }
529 + }
530 + $snippets = [];
531 + foreach ( $js_functions as $function ) {
532 + $snippet = $this->snippet->select_one( $function['snippetId'] );
533 + $snippet['function_info'] = $function; // Add function info to snippet
534 + $snippets[] = $snippet;
535 + }
536 +
537 + return $this->generate_js_functions_code( $snippets );
538 + }
539 +
540 + function generate_js_functions_code ($snippets ) {
541 + $code = "";
542 + foreach ( $snippets as $snippet ) {
543 + $function_code = $snippet['code'];
544 + $function_info = $snippet['function_info'];
545 +
546 + // Extract function name and arguments
547 + preg_match( '/(?:const|let|var)?\s*(\w+)\s*=\s*\((.*?)\)\s*=>/', $function_code, $matches );
548 + $function_name = $matches[1] ?? $function_info['name'];
549 + $function_args = $matches[2] ?? '';
550 +
551 + // Prepare default values
552 + $default_args = [];
553 + foreach ( $function_info['args'] as $arg ) {
554 + if ( isset( $arg['default'] ) && $arg['default'] !== '' ) {
555 + $default_args[$arg['name']] = $arg['default'];
526 556 }
557 + }
527 558
528 - $snippets = array_map( function ( $snippet ) use ( $blocked ) {
529 - $snippet['code'] = preg_replace( '/<\?php/', '', $snippet['code'], 1 );
530 - $snippet['blocked'] = $blocked;
559 + // Modify function to use default values
560 + if ( !empty( $default_args ) ) {
561 + $new_args = explode( ',', $function_args );
562 + foreach ( $new_args as &$arg ) {
563 + $arg = trim( $arg );
564 + if ( isset( $default_args[$arg] ) ) {
565 + $arg .= " = " . json_encode( $default_args[$arg] );
566 + }
567 + }
568 + $new_args_string = implode( ', ', $new_args );
569 + $function_code = preg_replace(
570 + '/(\w+)\s*=\s*\((.*?)\)\s*=>/',
571 + "$1 = ($new_args_string) =>",
572 + $function_code
573 + );
574 + }
531 575
532 - // If the snippet must be executed only in the frontend, we bypass the block
533 - if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
534 - $snippet['blocked'] = false;
535 - }
576 + $code .= $function_code . "\n\n";
577 + }
536 578
537 - return $snippet;
538 - }, $snippets );
579 + return $code;
580 + }
539 581
540 -
541 582
542 - return $snippets;
583 + /**
584 + * [STATIC] Execute active snippets.
585 + *
586 + * @return array
587 + */
588 + public function execute_active_snippets() {
589 +
590 + $blocked = false;
591 + $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null;
592 +
593 +
594 + if ( $page === 'mwcode_settings' ) {
595 + // If we blocks global snippets like nonce_life filter, we would block the settings page so let's remove the block for this page
596 +
597 + $blocked = false;
598 + //$blocked = true;
543 599 }
600 + // Block REST requests that aren't whitelisted
601 + elseif ( MeowKit_MWCODE_Helpers::is_rest() && !Meow_MWCODE_Core::is_white_listed_rest() ) {
602 + $blocked = true;
603 + }
544 604
605 + if ( empty( $this->snippet ) ) {
606 + $this->snippet = new Meow_MWCODE_Modules_Snippet( $this );
607 + }
545 608
546 - #endregion
609 + $ts = $this->get_option( 'thrown_snippet', null );
610 + if ( !empty( $ts ) ) {
611 + $this->log( "⚠️ Your snippet \"{$ts['name']}\" has thrown a fatal error last time, so we disabled it. Please check the logs for more information." );
612 + $this->snippet->force_disable( $ts['id'] );
613 + $this->update_option( 'thrown_snippet', null );
614 + }
547 615
548 - #region Logs
616 + $scope = is_admin() ? [ 'backend', 'persistent' ] : [ 'frontend', 'persistent' ];
617 + // Get all active snippets
549 618
550 - function get_logs() {
551 - $log_file_path = $this->get_logs_path();
619 + $snippets = $this->snippet->select(
620 + null, // offset
621 + -1, // limit
622 + [
623 + [ 'accessor' => 'active', 'value' => 1 ],
624 + [ 'accessor' => 'scope', 'value' => $scope ],
625 + ], // filter
626 + [ 'accessor' => 'priority', 'by' => 'DESC' ] // sort
627 + )['data'];
552 628
553 - if ( !file_exists( $log_file_path ) ) {
554 - return "Empty log file.";
555 - }
629 + if ( empty( $snippets ) ) {
630 + return;
631 + }
556 632
557 - $content = file_get_contents( $log_file_path );
558 - $lines = explode( "\n", $content );
559 - $lines = array_filter( $lines );
560 - $lines = array_reverse( $lines );
561 - $content = implode( "\n", $lines );
562 - return $content;
563 - }
633 + $snippets = array_map( function ( $snippet ) use ( $blocked ) {
634 + $snippet['code'] = $this->snippet->sanitize_code( $snippet['code'] );
635 + $snippet['blocked'] = $blocked;
564 636
565 - function clear_logs() {
566 - $logPath = $this->get_logs_path();
567 - if ( file_exists( $logPath ) ) {
568 - unlink( $logPath );
569 - }
637 + // If the snippet must be executed only in the frontend, we bypass the block
638 + if ( !is_admin() && $snippet['scope'] === 'frontend' ) {
639 + $snippet['blocked'] = false;
640 + }
570 641
571 - $options = $this->get_all_options();
572 - $options['logs_path'] = null;
573 - $this->update_options( $options );
574 - }
642 + return $snippet;
643 + }, $snippets );
575 644
576 - function get_logs_path() {
577 - $uploads_dir = wp_upload_dir();
578 - $uploads_dir_path = trailingslashit( $uploads_dir['basedir'] );
645 + return $snippets;
646 + }
579 647
580 - $path = $this->get_option( 'logs_path' );
581 648
582 - if ( $path && file_exists( $path ) ) {
583 - // make sure the path is legal (within the uploads directory with the MWCODE_PREFIX and log extension)
584 - if ( strpos( $path, $uploads_dir_path ) !== 0 || strpos( $path, MWCODE_PREFIX ) === false || substr( $path, -4 ) !== '.log' ) {
585 - $path = null;
586 - } else {
587 - return $path;
588 - }
589 - }
649 + #endregion
590 650
591 - if ( !$path ) {
592 - $path = $uploads_dir_path . MWCODE_PREFIX . "_" . $this->random_ascii_chars() . ".log";
593 - if ( !file_exists( $path ) ) {
594 - touch( $path );
595 - }
596 - $options = $this->get_all_options();
597 - $options['logs_path'] = $path;
598 - $this->update_options( $options );
599 - }
651 + #region Shortcodes
652 + function separate_mwcode_atts( $atts ) {
600 653
601 - return $path;
602 - }
654 + if( array_key_exists( 'id', $atts ) ) unset( $atts['id'] );
655 + if( array_key_exists( 'target', $atts ) ) unset( $atts['target'] );
656 + if( array_key_exists( 'code', $atts ) ) unset( $atts['code'] );
603 657
604 - function log( $data = null ) {
605 - if ( !$this->get_option( 'server_debug_mode', false ) ) { return false; }
606 - $log_file_path = $this->get_logs_path();
607 - $fh = @fopen( $log_file_path, 'a' );
608 - if ( !$fh ) { return false; }
609 - $date = date( "Y-m-d H:i:s" );
610 - if ( is_null( $data ) ) {
611 - fwrite( $fh, "\n" );
612 - }
613 - else {
614 - fwrite( $fh, "$date: {$data}\n" );
615 - //$this->log( "[MWCODE] $data" );
616 - }
617 - fclose( $fh );
618 - return true;
619 - }
658 + return $atts;
659 + }
620 660
621 - private function random_ascii_chars( $length = 8 ) {
622 - $characters = array_merge( range( 'A', 'Z' ), range( 'a', 'z' ), range( '0', '9' ) );
623 - $characters_length = count( $characters );
624 - $random_string = '';
661 + function content_shortcode( $atts ) {
625 662
626 - for ( $i = 0; $i < $length; $i++ ) {
627 - $random_string .= $characters[rand(0, $characters_length - 1)];
628 - }
663 + $user_atts = $this->separate_mwcode_atts( $atts );
629 664
630 - return $random_string;
631 - }
665 + $atts = shortcode_atts( array(
666 + 'id' => null,
667 + 'target' => null, // js or php
668 + 'code' => null, // For Guttenberg block usage
669 + ), $atts, 'code-engine' );
632 670
633 - #endregion
671 + $id = $atts['id'];
672 + $target = $atts['target'];
673 + $code = $atts['code'];
674 + $current_post = get_post();
675 +
676 + $no_js = defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML;
677 + $allow_php = $this->get_option( 'code_blocks', false );
678 + $allow_php_whitelist = $this->get_option( 'code_blocks_whitelist', [] );
679 +
680 + // If the ID is null, it means it comes from a Guttenberg block
681 + $is_block = empty( $id ) && !empty( $code );
634 682
635 - #region Helpers
683 + if( $is_block ) {
636 684
637 - /**
638 - * Check if the request is from a white-listed REST route.
639 - *
640 - * @return bool
641 - */
642 - public static function is_white_listed_rest() {
643 - $authorized = false;
644 - $white_listed = array(
645 - 'mwai/v1',
646 - 'mwai-ui/v1',
647 - 'media-file-renamer/v1',
648 - 'media-cleaner/v1',
649 - 'wplr/v1',
650 - 'code-engine/v1',
651 - 'wp/v2',
652 - 'meow-gallery/v1',
653 - );
685 + if( $target !== 'js' && $target !== 'php' ) {
686 + return '<b>Code Engine:</b> Please provide a valid target (js or php).';
687 + }
654 688
655 - $white_listed = apply_filters( 'meow_mwcode_white_listed_rest', $white_listed );
689 + if ( $no_js && $target === 'js' ) {
690 + return '<b>Code Engine:</b> Code Block JS are disabled because unfiltered HTML is not allowed on your server.';
691 + }
656 692
657 - $route = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : null;
658 - $requested_route = null;
659 -
660 - if ( $route ) {
661 - $route_parts = explode( '/wp-json/', $route );
662 -
663 - if ( isset( $route_parts[1] ) ) {
664 - $requested_route = trim( $route_parts[1], '/' );
665 - foreach ( $white_listed as $white_listed_route ) {
666 - if ( strpos( $requested_route, $white_listed_route ) === 0 ) {
667 - $authorized = true;
668 - $authorized = apply_filters( 'meow_mwcode_white_listed_rest_authorized', $authorized, $requested_route );
669 - return $authorized;
670 - }
671 - }
672 - }
673 -
674 - if ( is_admin() ) {
675 - $authorized = true;
693 + if ( $target === 'php' ) {
676 694
677 - $authorized = apply_filters( 'meow_mwcode_white_listed_rest_authorized', $authorized, $requested_route );
678 - return $authorized;
679 - }
695 + if ( !$allow_php ) {
696 + return '<b>Code Engine:</b> Code Block PHP are disabled. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
697 + }
680 698
699 + if ( !empty( $allow_php_whitelist ) && !in_array( $current_post->ID, $allow_php_whitelist ) ) {
700 + return '<b>Code Engine:</b> Code Block PHP are disabled for this post. If you are an administrator, you can enable it in the settings, this is not recommended. Please use a Content Snippet ( PHP ) instead.';
701 + }
702 + }
681 703
682 - }
704 + // Because the code from Blocks are sanitized, we need to replace the &quot; with "
705 + $code = str_replace( '&quot;', '"', $code );
683 706
684 - $authorized = apply_filters( 'meow_mwcode_white_listed_rest_authorized', $authorized, $requested_route );
685 - return $authorized;
707 + if ( $target === 'js' ) {
708 + $output = '<script>' . $code . '</script>';
709 + }
710 +
711 + if ( $target === 'php' ) {
712 + $output = $this->run_non_fn_snippet( null, $code );
713 + }
714 +
715 + return $output;
686 716 }
687 717
688 - #endregion
718 + // If not a block, we get the snippet by ID
719 + // If the ID is not null, it means it comes from a shortcode
720 + if ( empty( $id ) && empty( $code ) ) {
721 + return '<b>Code Engine:</b> Please provide a snippet ID.';
722 + }
723 +
724 + $snippet = $this->get_snippet( $id );
725 +
726 + if ( empty( $snippet ) ) {
727 + return '<b>Code Engine:</b> The snippet does not exist.';
728 + }
729 +
730 + //Check if the snippet scope is either content_php or content_js
731 + $is_content_php = $snippet['scope'] === 'content_php';
732 + $is_content_js = $snippet['scope'] === 'content_js';
733 +
734 + if ( !$is_content_php && !$is_content_js ) {
735 + return '<b>Code Engine:</b> The snippet is not a content snippet.';
736 + }
737 +
738 + if( $no_js && $is_content_js ) {
739 + return '<b>Code Engine:</b> Code Engine JS snippets are disabled because unfiltered HTML is not allowed on your server.';
740 + }
741 +
742 + //Check if the snippet is active
743 + if ( !$snippet['active'] ) {
744 + return '<b>Code Engine:</b> The snippet is not active.';
745 + }
746 +
747 + $output = '<b>Code Engine:</b> No output.';
748 +
749 + if ( $is_content_js ) {
750 + $output = '<script>' . $snippet['code'] . '</script>';
751 + }
752 +
753 + if ( $is_content_php ) {
754 + $prefix = "\$mwcode_atts = unserialize( '" . serialize( $user_atts ) . "' );";
755 + $output = $this->run_non_fn_snippet( $id, null, false, $prefix );
756 + }
757 +
758 + return $output;
759 + }
760 +
761 + #endregion
762 +
763 + #region Logs
764 +
765 + function get_logs() {
766 + $log_file_path = $this->get_logs_path();
767 +
768 + if ( !file_exists( $log_file_path ) ) {
769 + return "Empty log file.";
770 + }
771 +
772 + $content = file_get_contents( $log_file_path );
773 + $lines = explode( "\n", $content );
774 + $lines = array_filter( $lines );
775 + $lines = array_reverse( $lines );
776 + $content = implode( "\n", $lines );
777 + return $content;
778 + }
779 +
780 + function clear_logs() {
781 + $logPath = $this->get_logs_path();
782 + if ( file_exists( $logPath ) ) {
783 + unlink( $logPath );
784 + }
785 +
786 + $options = $this->get_all_options();
787 + $options['logs_path'] = null;
788 + $this->update_options( $options );
789 + }
790 +
791 + function get_logs_path() {
792 + $uploads_dir = wp_upload_dir();
793 + $uploads_dir_path = trailingslashit( $uploads_dir['basedir'] );
794 +
795 + $path = $this->get_option( 'logs_path' );
796 +
797 + if ( $path && file_exists( $path ) ) {
798 + // make sure the path is legal (within the uploads directory with the MWCODE_PREFIX and log extension)
799 + if ( strpos( $path, $uploads_dir_path ) !== 0 || strpos( $path, MWCODE_PREFIX ) === false || substr( $path, -4 ) !== '.log' ) {
800 + $path = null;
801 + } else {
802 + return $path;
803 + }
804 + }
805 +
806 + if ( !$path ) {
807 + $path = $uploads_dir_path . MWCODE_PREFIX . "_" . $this->random_ascii_chars() . ".log";
808 + if ( !file_exists( $path ) ) {
809 + touch( $path );
810 + }
811 + $options = $this->get_all_options();
812 + $options['logs_path'] = $path;
813 + $this->update_options( $options );
814 + }
815 +
816 + return $path;
817 + }
818 +
819 + function log( $data = null ) {
820 + if ( !$this->get_option( 'server_debug_mode', false ) ) { return false; }
821 + $log_file_path = $this->get_logs_path();
822 + $fh = @fopen( $log_file_path, 'a' );
823 + if ( !$fh ) { return false; }
824 + $date = date( "Y-m-d H:i:s" );
825 + if ( is_null( $data ) ) {
826 + fwrite( $fh, "\n" );
827 + }
828 + else {
829 + fwrite( $fh, "$date: {$data}\n" );
830 + //$this->log( "[MWCODE] $data" );
831 + }
832 + fclose( $fh );
833 + return true;
834 + }
835 +
836 + private function random_ascii_chars( $length = 8 ) {
837 + $characters = array_merge( range( 'A', 'Z' ), range( 'a', 'z' ), range( '0', '9' ) );
838 + $characters_length = count( $characters );
839 + $random_string = '';
840 +
841 + for ( $i = 0; $i < $length; $i++ ) {
842 + $random_string .= $characters[rand(0, $characters_length - 1)];
843 + }
844 +
845 + return $random_string;
846 + }
847 +
848 + #endregion
849 +
850 + #region Helpers
851 +
852 + /**
853 + * Check if the request is from a white-listed REST route.
854 + *
855 + * @return bool
856 + */
857 + public static function is_white_listed_rest() {
858 + $options = get_option( 'mwcode_snippet_vault_options', array() );
859 +
860 + // Early return if bypass is enabled
861 + if ( !empty( $options['bypass_rest_security'] ) ) {
862 + return true;
863 + }
864 +
865 + // Early return for admin requests
866 + if ( is_admin() ) {
867 + return apply_filters( 'mwcode_rest_authorized', true, null );
868 + }
869 +
870 + // Get the requested route
871 + $requested_route = self::get_requested_rest_route();
872 + if ( !$requested_route ) {
873 + return apply_filters( 'mwcode_rest_authorized', false, null );
874 + }
875 +
876 + // Check against whitelist
877 + $white_listed = apply_filters( 'mwcode_rest_whitelist', array(
878 + 'mwai/v1',
879 + 'mwai-ui/v1',
880 + 'media-file-renamer/v1',
881 + 'media-cleaner/v1',
882 + 'wplr/v1',
883 + 'code-engine/v1',
884 + 'wp/v2',
885 + 'meow-gallery/v1',
886 + 'mcp/v1',
887 + ));
888 +
889 + $authorized = self::is_route_whitelisted( $requested_route, $white_listed );
890 +
891 + // Log if debug mode is enabled
892 + if ( !empty( $options['server_debug_mode'] ) ) {
893 + self::log_route_status( $requested_route, $authorized );
894 + }
895 +
896 + return apply_filters( 'mwcode_rest_authorized', $authorized, $requested_route );
897 + }
898 +
899 + /**
900 + * Extract the REST route from the request URI.
901 + *
902 + * @return string|null
903 + */
904 + public static function get_requested_rest_route() {
905 + if ( !isset( $_SERVER['REQUEST_URI'] ) ) {
906 + return null;
907 + }
908 +
909 + $route_parts = explode( '/wp-json/', $_SERVER['REQUEST_URI'] );
910 +
911 + if ( isset( $route_parts[1] ) ) {
912 + return trim( $route_parts[1], '/' );
913 + }
914 +
915 + return null;
916 + }
917 +
918 + /**
919 + * Check if a route is in the whitelist.
920 + *
921 + * @param string $route The route to check
922 + * @param array $white_listed The whitelist array
923 + * @return bool
924 + */
925 + private static function is_route_whitelisted( $route, $white_listed ) {
926 + foreach ( $white_listed as $white_listed_route ) {
927 + if ( strpos( $route, $white_listed_route ) === 0 ) {
928 + return true;
929 + }
930 + }
931 + return false;
932 + }
933 +
934 + /**
935 + * Log the route authorization status.
936 + *
937 + * @param string $route The route being checked
938 + * @param bool $authorized Whether the route is authorized
939 + */
940 + private static function log_route_status( $route, $authorized ) {
941 + global $mwcode_core;
942 +
943 + $message = $authorized
944 + ? "✅ REST route authorized: " . $route
945 + : "❌ REST route rejected (not whitelisted): " . $route;
946 +
947 + if ( isset( $mwcode_core ) ) {
948 + $mwcode_core->log( $message );
949 + } else {
950 + error_log( "[Code Engine] " . $message );
951 + }
952 + }
953 +
954 + #endregion
689 955 }
690 956
691 957 ?>