Skip to content

Commit 4820d26

Browse files
committed
Make an Ajax call to fork the process.
This should be more reliable then close_connection because close_connection might not be supported in all browsers/servers.
1 parent 02dad38 commit 4820d26

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

src/wpephpcompat.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,6 @@ public function start_test() {
174174
}
175175

176176
if ( ! $this->is_command_line() ) {
177-
// Close the connection to the browser.
178-
$this->close_connection("started");
179-
180177
/**
181178
* Kill cron after a configurable timeout.
182179
* Subtract 5 from the timeout if we can to avoid race conditions.
@@ -466,29 +463,4 @@ private function is_command_line()
466463
{
467464
return defined( 'WP_CLI' ) || defined( 'PHPUNIT_TEST' );
468465
}
469-
470-
/**
471-
* Close the connection to the browser but continue processing the operation.
472-
* @since 1.0.0
473-
* @param string $body The message to send to the client.
474-
* @return null
475-
*/
476-
private function close_connection( $body ) {
477-
ignore_user_abort( true );
478-
if (ob_get_length()) ob_end_clean();
479-
// Start buffering.
480-
ob_start();
481-
// Echo our response.
482-
echo $body;
483-
// Get the length of the buffer.
484-
$size = ob_get_length();
485-
// Close the connection.
486-
header( 'Connection: close\r\n' );
487-
header( 'Content-Encoding: none\r\n' );
488-
header( 'Content-Length: $size' );
489-
// Flush and close the buffer.
490-
ob_end_flush();
491-
// Flush the system output buffer.
492-
flush();
493-
}
494466
}

wpengine-phpcompat.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,27 @@ function start_test() {
7878

7979
$wpephpc = new \WPEPHPCompat( __DIR__ );
8080

81+
foreach ( array( 'test_version', 'only_active' ) as $key ) {
82+
if ( isset( $_POST[ $key ] ) ) {
83+
$$key = sanitize_text_field( $_POST[ $key ] );
84+
}
85+
}
86+
87+
// New scan!
8188
if ( isset( $_POST['startScan'] ) ) {
82-
$test_version = sanitize_text_field( $_POST['test_version'] );
83-
$only_active = sanitize_text_field( $_POST['only_active'] );
89+
// Make sure we clean up after the last test.
90+
$wpephpc->clean_after_scan();
8491

92+
// Fork so we can close the connection.
93+
$this->fork_scan( $test_version, $only_active );
94+
}
95+
else {
8596
$wpephpc->test_version = $test_version;
8697
$wpephpc->only_active = $only_active;
87-
$wpephpc->clean_after_scan();
98+
99+
$wpephpc->start_test();
88100
}
89101

90-
$wpephpc->start_test();
91102
wp_die();
92103
}
93104
}
@@ -147,6 +158,41 @@ function check_status() {
147158
wp_die();
148159
}
149160
}
161+
162+
/**
163+
* Make an Ajax call to start the scan in the background.
164+
*
165+
* @since 1.1.2
166+
* @param string $test_version Version of PHP to test.
167+
* @param string $only_active Scan only active plugins or all?
168+
* @return null
169+
*/
170+
function fork_scan( $test_version, $only_active ) {
171+
$query = array(
172+
'action' => 'wpephpcompat_start_test',
173+
);
174+
175+
// Keep track of these variables.
176+
$body = array(
177+
'test_version'=> $test_version,
178+
'only_active'=> $only_active,
179+
);
180+
181+
// Instantly return!
182+
$args = array(
183+
'timeout' => 0.01,
184+
'blocking' => false,
185+
'body' => $body,
186+
'cookies' => $_COOKIE,
187+
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
188+
);
189+
190+
// Build our URL.
191+
$url = add_query_arg( $query, admin_url( 'admin-ajax.php' ) );
192+
193+
// POST.
194+
wp_remote_post( esc_url_raw( $url ), $args );
195+
}
150196

151197
/**
152198
* Create custom post type to store the directories we need to process.

0 commit comments

Comments
 (0)