PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.0.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.0.1
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 2 years ago Backup 2 years ago Basic 2 years ago Core 2 years ago Framework 2 years ago Frontend 2 years ago assets 2 years ago languages 3 years ago vendor_wpstg 2 years ago Deactivate.php 2 years ago README.md 3 years ago autoloader.php 3 years ago bootstrap.php 2 years ago constantsFree.php 2 years ago freeBootstrap.php 3 years ago install.php 3 years ago opcacheBootstrap.php 2 years ago php56-compatibility.php 3 years ago readme.txt 2 years ago runtimeRequirements.php 2 years ago uninstall.php 3 years ago wp-staging-error-handler.php 3 years ago wp-staging.php 2 years ago
wp-staging-error-handler.php
135 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 *
13 * @see \Psr\Log\LogLevel
14 */
15 function debug_log($message, $logType = 'info')
16 {
17 // Keep the file handler open for the duration of the request for performance.
18 static $fileHandler;
19
20 if ($logType === 'debug' && !defined('WPSTG_DEBUG') || defined('WPSTG_DEBUG') && !WPSTG_DEBUG) {
21 return;
22 }
23
24 if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
25 error_log('[' . $logType . '] WP Staging - ' . $message, 0);
26 }
27
28 if (!defined('WPSTG_DEBUG_LOG_FILE')) {
29 return;
30 }
31
32 if (is_null($fileHandler)) {
33 // Open the file handler once per request, and keep it open, as we might need to write to it multiple times.
34 $fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a');
35
36 // On Windows OS we need to remove the lock handle first before locking it again.
37 if (stripos(PHP_OS, "WIN") === 0) {
38 flock($fileHandler, LOCK_UN);
39 }
40
41 // Make sure the lock is shared, as we might need to open the handler again if a fatal error occurs.
42 if (is_resource($fileHandler)) {
43 flock($fileHandler, LOCK_SH | LOCK_NB);
44 }
45 }
46
47 $message = sprintf(
48 "[WP STAGING Manual Logging][%s][%s] %s\n",
49 $logType,
50 current_time('mysql'),
51 $message
52 );
53
54 if (is_resource($fileHandler)) {
55 fwrite($fileHandler, $message, 5 * MB_IN_BYTES);
56 }
57 }
58
59 /**
60 * Logs fatal errors in the WP STAGING debug file.
61 */
62 function shutdown_function()
63 {
64 if (!defined('WPSTG_DEBUG_LOG_FILE') || !defined('WPSTG_PLUGIN_SLUG')) {
65 return;
66 }
67
68 $error = error_get_last();
69
70 if (!is_array($error)) {
71 return;
72 }
73
74 // Errors that bring PHP to a halt.
75 $fatalErrorTypes = [
76 E_ERROR => 'E_ERROR',
77 E_PARSE => 'E_PARSE',
78 E_USER_ERROR => 'E_USER_ERROR',
79 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
80 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
81 ];
82
83 // Provide friendly-names for the error codes
84 $allErrorTypes = [
85 E_ERROR => "E_ERROR",
86 E_WARNING => "E_WARNING",
87 E_PARSE => "E_PARSE",
88 E_NOTICE => "E_NOTICE",
89 E_CORE_ERROR => "E_CORE_ERROR",
90 E_CORE_WARNING => "E_CORE_WARNING",
91 E_COMPILE_ERROR => "E_COMPILE_ERROR",
92 E_COMPILE_WARNING => "E_COMPILE_WARNING",
93 E_USER_ERROR => "E_USER_ERROR",
94 E_USER_WARNING => "E_USER_WARNING",
95 E_USER_NOTICE => "E_USER_NOTICE",
96 E_STRICT => "E_STRICT",
97 E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
98 E_DEPRECATED => "E_DEPRECATED",
99 E_USER_DEPRECATED => "E_USER_DEPRECATED",
100 E_ALL => "E_ALL",
101 ];
102
103 $isFatalError = isset($fatalErrorTypes[$error['type']]);
104 $comesFromWpStaging = strpos($error['file'], WPSTG_PLUGIN_SLUG) !== false;
105
106 /*
107 * Logs fatal errors that happens anywhere,
108 * and notices, warnings that comes from a WP STAGING file.
109 *
110 * (It will only log notices and errors from WP STAGING
111 * if it was the last notice/warning triggered before PHP shutdown)
112 */
113 if ($isFatalError || $comesFromWpStaging) {
114 // Opening a file handler gives us more control than error_log('foo', 3, 'custom-file.log');
115 $fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a');
116
117 $message = sprintf(
118 "[WP STAGING Shutdown Function][%s][%s] %s - File: %s Line: %s | Is it Fatal Error? %s | Is it Thrown by WP STAGING? %s\n",
119 $allErrorTypes[$error['type']],
120 current_time('mysql'),
121 $error['message'],
122 $error['file'],
123 $error['line'],
124 $isFatalError ? 'Yes' : 'No',
125 $comesFromWpStaging ? 'Yes' : 'No'
126 );
127
128 if (is_resource($fileHandler)) {
129 fwrite($fileHandler, $message, 5 * MB_IN_BYTES);
130 }
131 }
132 }
133
134 register_shutdown_function('\WPStaging\functions\shutdown_function');
135