PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.2
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / wp-staging-error-handler.php
wp-staging Last commit date
Backend 8 months ago Backup 8 months ago Basic 9 months ago Component 8 months ago Core 8 months ago Framework 8 months ago Frontend 8 months ago Notifications 8 months ago Staging 8 months ago assets 8 months ago languages 1 year ago resources 1 year ago vendor_wpstg 8 months ago views 8 months ago CONTRIBUTING.md 2 years ago Deactivate.php 8 months ago README.md 9 months ago SECURITY.md 2 years ago autoloader.php 3 years ago bootstrap.php 9 months ago constantsFree.php 8 months ago freeBootstrap.php 1 year ago install.php 1 year ago opcacheBootstrap.php 8 months ago readme.txt 8 months ago runtimeRequirements.php 9 months ago uninstall.php 11 months ago wp-staging-error-handler.php 1 year ago wp-staging.php 8 months ago
wp-staging-error-handler.php
137 lines
1 <?php
2
3 /*
4 * Low-level error handler and debugger for WP STAGING
5 */
6
7 namespace WPStaging\functions;
8
9 /**
10 * @param string $message The debug message.
11 * @param string $logType A PSR-3 compatible-log type. If "debug", it only logs if WPSTG_DEBUG is true.
12 * @param bool $logInDebugLog Whether to log the message in the WP_DEBUG_LOG file.
13 *
14 * @see \Psr\Log\LogLevel
15 */
16 function debug_log($message, $logType = 'info', $logInDebugLog = true)
17 {
18 // Keep the file handler open for the duration of the request for performance.
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 // Open the file handler once per request, and keep it open, as we might need to write to it multiple times.
35 $fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a');
36
37 // On Windows OS locking doesn't work as expected, so we don't lock the file on Windows OS.
38 // Make sure the lock is shared, as we might need to open the handler again if a fatal error occurs.
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 // If we can't write to the file, we should at least log the error to the PHP error log.
56 error_log('WP Staging - Error writing to the debug log file: ' . $ex->getMessage());
57 }
58 }
59 }
60
61 /**
62 * Logs fatal errors in the WP STAGING debug file.
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 // Errors that bring PHP to a halt.
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 // Provide friendly-names for the error codes
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_STRICT => "E_STRICT",
99 E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
100 E_DEPRECATED => "E_DEPRECATED",
101 E_USER_DEPRECATED => "E_USER_DEPRECATED",
102 E_ALL => "E_ALL",
103 ];
104
105 $isFatalError = isset($fatalErrorTypes[$error['type']]);
106 $comesFromWpStaging = strpos($error['file'], WPSTG_PLUGIN_SLUG) !== false;
107
108 /*
109 * Logs fatal errors that happens anywhere,
110 * and notices, warnings that comes from a WP STAGING file.
111 *
112 * (It will only log notices and errors from WP STAGING
113 * if it was the last notice/warning triggered before PHP shutdown)
114 */
115 if ($isFatalError || $comesFromWpStaging) {
116 // Opening a file handler gives us more control than error_log('foo', 3, 'custom-file.log');
117 $fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a');
118
119 $message = sprintf(
120 "[WP STAGING Shutdown Function][%s][%s] %s - File: %s Line: %s | Is it Fatal Error? %s | Is it Thrown by WP STAGING? %s\n",
121 $allErrorTypes[$error['type']],
122 current_time('mysql'),
123 $error['message'],
124 $error['file'],
125 $error['line'],
126 $isFatalError ? 'Yes' : 'No',
127 $comesFromWpStaging ? 'Yes' : 'No'
128 );
129
130 if (is_resource($fileHandler)) {
131 fwrite($fileHandler, $message, 5 * MB_IN_BYTES);
132 }
133 }
134 }
135
136 register_shutdown_function('\WPStaging\functions\shutdown_function');
137