| 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 |
static $last_blocked_log = []; |
| 9 |
|
| 10 |
|
| 11 |
if ( !isset( $mwcode_core ) ) { |
| 12 |
$mwcode_core = new Meow_MWCODE_Core( ); |
| 13 |
} |
| 14 |
|
| 15 |
$snippets = $mwcode_core->execute_active_snippets( ); |
| 16 |
|
| 17 |
if ( empty( $snippets ) ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
foreach ( $snippets as $snippet ) { |
| 22 |
|
| 23 |
if ( $snippet['blocked'] ) { |
| 24 |
// Determine the reason for blocking |
| 25 |
$page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null; |
| 26 |
$is_settings_page = ( $page === 'mwcode_settings' ); |
| 27 |
$is_rest = MeowKit_MWCODE_Helpers::is_rest(); |
| 28 |
$is_rest_whitelisted = Meow_MWCODE_Core::is_white_listed_rest(); |
| 29 |
|
| 30 |
$reason = ''; |
| 31 |
if ( $is_settings_page ) { |
| 32 |
$reason = "on Code Engine settings page (disabled for safety)"; |
| 33 |
} elseif ( $is_rest && !$is_rest_whitelisted ) { |
| 34 |
$requested_route = Meow_MWCODE_Core::get_requested_rest_route(); |
| 35 |
$reason = "REST route not whitelisted: " . ($requested_route ?: 'unknown'); |
| 36 |
} else { |
| 37 |
// Add more debugging info |
| 38 |
$debug_info = [ |
| 39 |
'is_rest' => $is_rest, |
| 40 |
'is_whitelisted' => $is_rest_whitelisted, |
| 41 |
'page' => $page, |
| 42 |
'request_uri' => $_SERVER['REQUEST_URI'] ?? 'not set' |
| 43 |
]; |
| 44 |
$reason = "unknown reason (debug: " . json_encode($debug_info) . ")"; |
| 45 |
} |
| 46 |
|
| 47 |
// Avoid duplicate logs within 2 seconds |
| 48 |
$log_key = $snippet['id'] . '-' . $reason; |
| 49 |
$current_time = time(); |
| 50 |
|
| 51 |
if ( !isset($last_blocked_log[$log_key]) || ($current_time - $last_blocked_log[$log_key]) > 2 ) { |
| 52 |
$mwcode_core->log( "⚠️ Snippet blocked: " . $snippet['name'] . " (ID: " . $snippet['id'] . ") - " . $reason ); |
| 53 |
$last_blocked_log[$log_key] = $current_time; |
| 54 |
} |
| 55 |
|
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$current_mwcode_snippet = $snippet; |
| 60 |
|
| 61 |
// Remember we ran this global so load_global_snippets() won't run it a second time. |
| 62 |
$mwcode_core->loaded_global_ids[] = $snippet['id']; |
| 63 |
|
| 64 |
ob_start( ); |
| 65 |
|
| 66 |
try { |
| 67 |
$mwcode_core->log( "Executing snippet: " . $snippet['name'] . " (ID: " . $snippet['id'] . ")" ); |
| 68 |
eval( $snippet['code'] ); |
| 69 |
} catch ( Throwable $e ) { |
| 70 |
$mwcode_core->log( "⚠️ Exception in snippet: " . $snippet['name'] . " (ID: " . $snippet['id'] . ")" ); |
| 71 |
$mwcode_core->log( $e->getMessage( ) ); |
| 72 |
$mwcode_core->update_option( 'fatal_error', "\"{$snippet["name"]}\" - " . $e->getMessage( ) ); |
| 73 |
$mwcode_core->update_option( 'thrown_snippet', $snippet ); |
| 74 |
} |
| 75 |
|
| 76 |
ob_end_clean( ); |
| 77 |
} |
| 78 |
|
| 79 |
$current_mwcode_snippet = null; |
| 80 |
} |
| 81 |
|
| 82 |
$file_path = MWCODE_PATH . '/classes/modules/helpers.php'; |
| 83 |
if ( file_exists( $file_path ) ) { |
| 84 |
require_once( $file_path ); |
| 85 |
} |
| 86 |
|
| 87 |
$file_path_common = MWCODE_PATH . '/common/helpers.php'; |
| 88 |
if ( file_exists( $file_path_common ) ) { |
| 89 |
require_once( $file_path_common ); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
// In admin or Rest API request ( REQUEST URI begins with '/wp-json/' ) |
| 94 |
if ( is_admin( ) || MeowKit_MWCODE_Helpers::is_rest( ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 95 |
global $mwcode_core; |
| 96 |
$mwcode_core = new Meow_MWCODE_Core( ); |
| 97 |
} |
| 98 |
|
| 99 |
// Define a global variable to store the currently executing snippet |
| 100 |
global $current_mwcode_snippet; |
| 101 |
|
| 102 |
/** |
| 103 |
* Custom error handler |
| 104 |
*/ |
| 105 |
function mwcode_error_handler( $errno, $errstr, $errfile, $errline ) |
| 106 |
{ |
| 107 |
global $current_mwcode_snippet, $mwcode_core; |
| 108 |
|
| 109 |
if ( !isset( $current_mwcode_snippet ) ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
if ( !isset( $mwcode_core ) ) { |
| 114 |
$mwcode_core = new Meow_MWCODE_Core( ); |
| 115 |
} |
| 116 |
|
| 117 |
if ( !( error_reporting( ) & $errno ) ) { |
| 118 |
// This error code is not included in error_reporting |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if ( !array_key_exists( 'name', $current_mwcode_snippet ) ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
$error = "$errstr in $errfile on line $errline"; |
| 127 |
|
| 128 |
$mwcode_core->log( "⚠️ Error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" ); |
| 129 |
$mwcode_core->log( $error ); |
| 130 |
|
| 131 |
$mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - $errstr" ); |
| 132 |
$mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet ); |
| 133 |
|
| 134 |
// Don't execute PHP internal error handler |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Shutdown function to catch fatal errors |
| 140 |
*/ |
| 141 |
function mwcode_shutdown_function( ) |
| 142 |
{ |
| 143 |
global $current_mwcode_snippet, $mwcode_core; |
| 144 |
|
| 145 |
if ( !isset( $current_mwcode_snippet ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if ( !isset( $mwcode_core ) ) { |
| 150 |
$mwcode_core = new Meow_MWCODE_Core( ); |
| 151 |
} |
| 152 |
|
| 153 |
$error = error_get_last( ); |
| 154 |
|
| 155 |
$fatal_error = $error !== null && in_array( $error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR] ); |
| 156 |
|
| 157 |
if ( $fatal_error ) { |
| 158 |
|
| 159 |
$mwcode_core->log( "⚠️ Fatal error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" ); |
| 160 |
$mwcode_core->log( $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'] ); |
| 161 |
|
| 162 |
$mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - " . $error['message'] ); |
| 163 |
$mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet ); |
| 164 |
|
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Set the custom error handler and shutdown function |
| 169 |
set_error_handler( "mwcode_error_handler" ); |
| 170 |
register_shutdown_function( "mwcode_shutdown_function" ); |
| 171 |
|
| 172 |
|
| 173 |
// Load all the JS functions to the front & back |
| 174 |
// TODO: Add some restrictions by pages |
| 175 |
function enqueue_mwcode_scripts( ) |
| 176 |
{ |
| 177 |
global $mwcode_core; |
| 178 |
|
| 179 |
if ( !isset( $mwcode_core ) ) { |
| 180 |
$mwcode_core = new Meow_MWCODE_Core( ); |
| 181 |
} |
| 182 |
|
| 183 |
wp_register_script( 'mwcode-scripts-js', '' ); |
| 184 |
wp_enqueue_script( 'mwcode-scripts-js' ); |
| 185 |
|
| 186 |
$js_code = ''; |
| 187 |
|
| 188 |
if ( is_admin( ) ) { |
| 189 |
$page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null; |
| 190 |
if ( $page === 'mwai_settings' ) { |
| 191 |
$js_code = $mwcode_core->get_js_functions_to_push( ); |
| 192 |
} |
| 193 |
} else { |
| 194 |
$js_code = $mwcode_core->get_js_functions_to_push( ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( $js_code ) { |
| 198 |
wp_add_inline_script( 'mwcode-scripts-js', $js_code, 'after' ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
add_action( 'wp_enqueue_scripts', 'enqueue_mwcode_scripts' ); |
| 203 |
add_action( 'admin_enqueue_scripts', 'enqueue_mwcode_scripts' ); |