| @@ -46,8 +46,72 @@ | ||
| 46 | 46 | ); |
| 47 | 47 | $this->response->terminate($resp); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | + public function deferExecutionUntilShutdown() { | |
| 51 | + if (function_exists('error_clear_last')) { | |
| 52 | + error_clear_last(); | |
| 53 | + } | |
| 54 | + ob_start(); | |
| 55 | + remove_action('shutdown', 'wp_ob_end_flush_all', 1); | |
| 56 | + add_action('shutdown', array($this, 'scheduleExecutionAfterShutdown'), PHP_INT_MAX); | |
| 57 | + } | |
| 58 | + | |
| 59 | + public function scheduleExecutionAfterShutdown() { | |
| 60 | + register_shutdown_function(array($this, 'executeAfterShutdown')); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public function executeAfterShutdown() { | |
| 64 | + if ($this->request->keep_page_output) { | |
| 65 | + $this->removeContentLengthHeader(); | |
| 66 | + $this->execute(); | |
| 67 | + return; | |
| 68 | + } | |
| 69 | + if ($this->shouldPreserveFrontendResponse()) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + $this->discardBufferedOutput(); | |
| 73 | + $this->clearFrontendResponseHeaders(); | |
| 74 | + $this->execute(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + private function shouldPreserveFrontendResponse() { | |
| 78 | + if ($this->hasFatalError() || headers_sent()) { | |
| 79 | + return true; | |
| 80 | + } | |
| 81 | + if (!function_exists('http_response_code')) { | |
| 82 | + return false; | |
| 83 | + } | |
| 84 | + $response_code = http_response_code(); | |
| 85 | + return $response_code !== false && $response_code !== 200; | |
| 86 | + } | |
| 87 | + | |
| 88 | + private function hasFatalError() { | |
| 89 | + $error = error_get_last(); | |
| 90 | + $fatal_types = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR); | |
| 91 | + return is_array($error) && in_array($error['type'], $fatal_types, true); | |
| 92 | + } | |
| 93 | + | |
| 94 | + private function discardBufferedOutput() { | |
| 95 | + while (ob_get_level() > 0) { | |
| 96 | + if (!@ob_end_clean()) { | |
| 97 | + break; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + private function clearFrontendResponseHeaders() { | |
| 103 | + $this->removeContentLengthHeader(); | |
| 104 | + header_remove('Content-Encoding'); | |
| 105 | + header_remove('Location'); | |
| 106 | + } | |
| 107 | + | |
| 108 | + private function removeContentLengthHeader() { | |
| 109 | + if (!headers_sent()) { | |
| 110 | + header_remove('Content-Length'); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 50 | 114 | public function routeRequest() { |
| 51 | 115 | switch ($this->request->wing) { |
| 52 | 116 | case 'manage': |
| 53 | 117 | require_once dirname( __FILE__ ) . '/wings/manage.php'; |