| @@ -8,8 +8,15 @@ | ||
| 8 | 8 | |
| 9 | 9 | protected $options; |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | + * If set to true, the preloader will not close the browser connection when running via AJAX. | |
| 13 | + * | |
| 14 | + * @var bool | |
| 15 | + */ | |
| 16 | + protected $dont_close_browser_connection = false; | |
| 17 | + | |
| 18 | + /** | |
| 12 | 19 | * WP_Optimize_Page_Cache_Preloader constructor. |
| 13 | 20 | */ |
| 14 | 21 | public function __construct() { |
| 15 | 22 | parent::__construct(); |
| @@ -117,9 +124,9 @@ | ||
| 117 | 124 | |
| 118 | 125 | $is_wp_cli = defined('WP_CLI') && WP_CLI; |
| 119 | 126 | |
| 120 | 127 | // close browser connection and continue work for ajax actions. |
| 121 | - if (defined('DOING_AJAX') && DOING_AJAX) { | |
| 128 | + if (defined('DOING_AJAX') && DOING_AJAX && !$this->dont_close_browser_connection) { | |
| 122 | 129 | if (true === $silent) { |
| 123 | 130 | $output = ''; |
| 124 | 131 | } else { |
| 125 | 132 | $output = wp_json_encode($response); |
| @@ -127,8 +134,11 @@ | ||
| 127 | 134 | |
| 128 | 135 | WP_Optimize()->close_browser_connection($output); |
| 129 | 136 | } |
| 130 | 137 | |
| 138 | + // Reset the flag to not close the browser connection for the next runs. | |
| 139 | + $this->dont_close_browser_connection = false; | |
| 140 | + | |
| 131 | 141 | // trying to change time limit. |
| 132 | 142 | WP_Optimize()->change_time_limit(); |
| 133 | 143 | |
| 134 | 144 | $status = $this->get_status($this->task_type); |
| @@ -153,8 +163,24 @@ | ||
| 153 | 163 | |
| 154 | 164 | // return $response in WP-CLI mode |
| 155 | 165 | if ($is_wp_cli) { |
| 156 | 166 | return $response; |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + /** | |
| 171 | + * Set the flag to not close the browser connection when running via AJAX. | |
| 172 | + * | |
| 173 | + * @return void | |
| 174 | + */ | |
| 175 | + public function maybe_dont_close_browser_connection() { | |
| 176 | + | |
| 177 | + if ($this->dont_close_browser_connection) return; | |
| 178 | + | |
| 179 | + $dont_close_browser_connection = apply_filters('wpo_' . $this->preload_type . '_preloader_dont_close_browser_connection', $this->dont_close_browser_connection); | |
| 180 | + | |
| 181 | + if ($dont_close_browser_connection) { | |
| 182 | + $this->dont_close_browser_connection = true; | |
| 157 | 183 | } |
| 158 | 184 | } |
| 159 | 185 | |
| 160 | 186 | /** |