| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | add_action( 'plugins_loaded', 'mwcode_loaded', 1 ); |
| 5 | 5 | |
| 6 | 6 | function mwcode_loaded( ) { |
| 7 | 7 | global $mwcode_core, $current_mwcode_snippet; |
| 8 | + static $last_blocked_log = []; | |
| 8 | 9 | |
| 9 | 10 | |
| 10 | 11 | if ( !isset( $mwcode_core ) ) { |
| 11 | 12 | $mwcode_core = new Meow_MWCODE_Core( ); |
| @@ -19,22 +20,55 @@ | ||
| 19 | 20 | |
| 20 | 21 | foreach ( $snippets as $snippet ) { |
| 21 | 22 | |
| 22 | 23 | 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 | + // 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 | + | |
| 24 | 56 | continue; |
| 25 | 57 | } |
| 26 | 58 | |
| 27 | 59 | $current_mwcode_snippet = $snippet; |
| 28 | - | |
| 29 | 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 | + | |
| 30 | 64 | ob_start( ); |
| 31 | 65 | |
| 32 | 66 | try { |
| 33 | - error_log( "Executing snippet: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " )" ); | |
| 67 | + $mwcode_core->log( "Executing snippet: " . $snippet['name'] . " (ID: " . $snippet['id'] . ")" ); | |
| 34 | 68 | eval( $snippet['code'] ); |
| 35 | 69 | } catch ( Throwable $e ) { |
| 36 | - $mwcode_core->log( "⚠️ Exception in snippet: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " )" ); | |
| 70 | + $mwcode_core->log( "⚠️ Exception in snippet: " . $snippet['name'] . " (ID: " . $snippet['id'] . ")" ); | |
| 37 | 71 | $mwcode_core->log( $e->getMessage( ) ); |
| 38 | 72 | $mwcode_core->update_option( 'fatal_error', "\"{$snippet["name"]}\" - " . $e->getMessage( ) ); |
| 39 | 73 | $mwcode_core->update_option( 'thrown_snippet', $snippet ); |
| 40 | 74 | } |
| @@ -56,9 +90,9 @@ | ||
| 56 | 90 | } |
| 57 | 91 | |
| 58 | 92 | |
| 59 | 93 | // 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 ) ) { | |
| 94 | +if ( is_admin( ) || MeowKit_MWCODE_Helpers::is_rest( ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
| 61 | 95 | global $mwcode_core; |
| 62 | 96 | $mwcode_core = new Meow_MWCODE_Core( ); |
| 63 | 97 | } |
| 64 | 98 | |