| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle unexpected shutdown. |
| 4 |
* |
| 5 |
* @version 2016-12-20 11:41 UTC+01 |
| 6 |
* @package watchful |
| 7 |
* @author Watchful |
| 8 |
* @authorUrl https://watchful.net |
| 9 |
* @copyright Copyright (c) 2020 watchful.net |
| 10 |
* @license GNU/GPL |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Watchful; |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
use Watchful\Helpers\ResponseFormatter; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class for handling unexpected shutdowns. |
| 24 |
*/ |
| 25 |
class ShutdownHandler |
| 26 |
{ |
| 27 |
|
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
add_action('shutdown', array($this, 'shutdown'), 99); |
| 31 |
} |
| 32 |
|
| 33 |
public function shutdown() |
| 34 |
{ |
| 35 |
$error = error_get_last(); |
| 36 |
if (empty($error) || $error['type'] !== E_ERROR) { |
| 37 |
return; |
| 38 |
} |
| 39 |
$response = array( |
| 40 |
'error' => 1, |
| 41 |
'code' => $error['type'], |
| 42 |
'message' => $error['message'], |
| 43 |
'details' => $error, |
| 44 |
); |
| 45 |
|
| 46 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output is JSON-encoded; HTML escaping would corrupt the payload. |
| 47 |
echo ResponseFormatter::add_response_delimiters(wp_json_encode($response)); |
| 48 |
} |
| 49 |
} |
| 50 |
|