wp-staging
Last commit date
Backend
1 day ago
Backup
1 day ago
Basic
1 day ago
Component
1 day ago
Core
1 day ago
Framework
1 day ago
Frontend
1 day ago
Notifications
1 day ago
Staging
1 day ago
assets
1 day ago
languages
1 day ago
resources
1 day ago
vendor_wpstg
1 day ago
views
1 day ago
CONTRIBUTING.md
2 years ago
Deactivate.php
1 day ago
README.md
5 months ago
SECURITY.md
3 weeks ago
autoloader.php
1 day ago
bootstrap.php
1 day ago
commonBootstrap.php
1 day ago
constantsFree.php
1 day ago
freeBootstrap.php
1 day ago
install.php
1 day ago
opcacheBootstrap.php
1 day ago
readme.txt
1 day ago
runtimeRequirements.php
1 day ago
uninstall.php
1 day ago
wp-staging-error-handler.php
1 day ago
wp-staging.php
1 day ago
wp-staging-error-handler.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | namespace WPStaging\functions; |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | function debug_log($message, $logType = 'info', $logInDebugLog = true) |
| 17 | { |
| 18 | |
| 19 | static $fileHandler; |
| 20 | |
| 21 | if ($logType === 'debug' && !defined('WPSTG_DEBUG') || defined('WPSTG_DEBUG') && !WPSTG_DEBUG) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | if ($logInDebugLog && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) { |
| 26 | error_log('[' . $logType . '] WP Staging - ' . $message, 0); |
| 27 | } |
| 28 | |
| 29 | if (!defined('WPSTG_DEBUG_LOG_FILE')) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if (is_null($fileHandler)) { |
| 34 | |
| 35 | $fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a'); |
| 36 | |
| 37 | |
| 38 | |
| 39 | if (stripos(PHP_OS, "WIN") !== 0 && is_resource($fileHandler)) { |
| 40 | flock($fileHandler, LOCK_SH | LOCK_NB); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $message = sprintf( |
| 45 | "[WP STAGING Manual Logging][%s][%s] %s\n", |
| 46 | $logType, |
| 47 | current_time('mysql'), |
| 48 | $message |
| 49 | ); |
| 50 | |
| 51 | if (is_resource($fileHandler)) { |
| 52 | try { |
| 53 | fwrite($fileHandler, $message, 5 * MB_IN_BYTES); |
| 54 | } catch (\Throwable $ex) { |
| 55 | |
| 56 | error_log('WP Staging - Error writing to the debug log file: ' . $ex->getMessage()); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | function shutdown_function() |
| 65 | { |
| 66 | if (!defined('WPSTG_DEBUG_LOG_FILE') || !defined('WPSTG_PLUGIN_SLUG')) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | $error = error_get_last(); |
| 71 | |
| 72 | if (!is_array($error)) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | $fatalErrorTypes = [ |
| 78 | E_ERROR => 'E_ERROR', |
| 79 | E_PARSE => 'E_PARSE', |
| 80 | E_USER_ERROR => 'E_USER_ERROR', |
| 81 | E_COMPILE_ERROR => 'E_COMPILE_ERROR', |
| 82 | E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', |
| 83 | ]; |
| 84 | |
| 85 | |
| 86 | $allErrorTypes = [ |
| 87 | E_ERROR => "E_ERROR", |
| 88 | E_WARNING => "E_WARNING", |
| 89 | E_PARSE => "E_PARSE", |
| 90 | E_NOTICE => "E_NOTICE", |
| 91 | E_CORE_ERROR => "E_CORE_ERROR", |
| 92 | E_CORE_WARNING => "E_CORE_WARNING", |
| 93 | E_COMPILE_ERROR => "E_COMPILE_ERROR", |
| 94 | E_COMPILE_WARNING => "E_COMPILE_WARNING", |
| 95 | E_USER_ERROR => "E_USER_ERROR", |
| 96 | E_USER_WARNING => "E_USER_WARNING", |
| 97 | E_USER_NOTICE => "E_USER_NOTICE", |
| 98 | E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR", |
| 99 | E_DEPRECATED => "E_DEPRECATED", |
| 100 | E_USER_DEPRECATED => "E_USER_DEPRECATED", |
| 101 | E_ALL => "E_ALL", |
| 102 | ]; |
| 103 | |
| 104 | |
| 105 | if (version_compare(PHP_VERSION, '8.4.0', '<')) { |
| 106 | $allErrorTypes[E_STRICT] = "E_STRICT"; |
| 107 | } |
| 108 | |
| 109 | $isFatalError = isset($fatalErrorTypes[$error['type']]); |
| 110 | $comesFromWpStaging = strpos($error['file'], WPSTG_PLUGIN_SLUG) !== false; |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | if ($isFatalError || $comesFromWpStaging) { |
| 120 | |
| 121 | $fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a'); |
| 122 | |
| 123 | $message = sprintf( |
| 124 | "[WP STAGING Shutdown Function][%s][%s] %s - File: %s Line: %s | Is it Fatal Error? %s | Is it Thrown by WP STAGING? %s\n", |
| 125 | $allErrorTypes[$error['type']], |
| 126 | current_time('mysql'), |
| 127 | $error['message'], |
| 128 | $error['file'], |
| 129 | $error['line'], |
| 130 | $isFatalError ? 'Yes' : 'No', |
| 131 | $comesFromWpStaging ? 'Yes' : 'No' |
| 132 | ); |
| 133 | |
| 134 | if (is_resource($fileHandler)) { |
| 135 | fwrite($fileHandler, $message, 5 * MB_IN_BYTES); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | register_shutdown_function('\WPStaging\functions\shutdown_function'); |
| 141 |