Skip to content

Commit

Permalink
Merge pull request #807 from john-shaffer/process-queue-immediately
Browse files Browse the repository at this point in the history
Add an option to process the queue immediately after adding jobs.
  • Loading branch information
leonstafford authored Sep 20, 2021
2 parents b3cd0d7 + dd73900 commit 68d6bd0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- accept self-signed certs during sitemap crawling @john-shaffer
- detect dead jobs and mark as failed @john-shaffer
- mark duplicated waiting jobs as skipped on jobs page @john-shaffer
- add an option to process the queue immediately #794 @john-shaffer

## WP2Static 7.1.7

Expand Down
9 changes: 6 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ parameters:
ignoreErrors:
# TODO I should really develop a tiny Request Handler.
# https://gist.github.com/nicolas-grekas/1028251#improving-native-php-interface
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(COOKIE|GET|POST) superglobal#'
path: src/Controller.php
count: 5
count: 6
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
path: src/CoreOptions.php
count: 19
count: 20
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
path: src/ViewRenderer.php
count: 32
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
path: src/WordPressAdmin.php
count: 2
- '/^Parameter #2 \$callable of static method WP_CLI::add_command\(\) expects callable\(\): mixed, \S+ given\.$/'

29 changes: 29 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ public static function wp2staticEnqueueJobs() : void {
JobQueue::addJob( $job_type );
}
}

if ( CoreOptions::getValue( 'processQueueImmediately' ) ) {
self::wp2staticProcessQueueAdminPost();
}
}

public static function wp2staticToggleAddon( string $addon_slug = null ) : void {
Expand Down Expand Up @@ -652,6 +656,31 @@ public static function wp2staticProcessQueue() : void {
}
}

/**
* Make a non-blocking POST request to run wp2staticProcessQueue.
*/
public static function wp2staticProcessQueueAdminPost() : void {
$url = admin_url( 'admin-post.php' ) . '?action=wp2static_process_queue';
$nonce = wp_create_nonce( 'wp2static_process_queue' );
$result = wp_remote_post(
$url,
[
'blocking' => false,
'body' => [ '_wpnonce' => $nonce ],
'cookies' => $_COOKIE,
'sslverify' => false,
'timeout' => 0.01,
]
);

if ( is_wp_error( $result ) ) {
WsLog::l(
'Error in wp2staticProcessQueueAdminPost. Request to admin-post.php failed: ' .
json_encode( $result->errors )
);
}
}

public static function wp2staticHeadless() : void {
WsLog::l( 'Running WP2Static in Headless mode' );
WsLog::l( 'Starting URL detection' );
Expand Down
15 changes: 15 additions & 0 deletions src/CoreOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public static function seedOptions() : void {
'Queues a new job every time a Post or Page is deleted.'
);

$queries[] = $wpdb->prepare(
$query_string,
'processQueueImmediately',
'0',
'Process queue immediately after enqueueing jobs',
''
);

$queries[] = $wpdb->prepare(
$query_string,
'processQueueInterval',
Expand Down Expand Up @@ -428,6 +436,7 @@ public static function savePosted( string $screen = 'core' ) : void {
case 'jobs':
$queue_on_post_save = isset( $_POST['queueJobOnPostSave'] ) ? 1 : 0;
$queue_on_post_delete = isset( $_POST['queueJobOnPostDelete'] ) ? 1 : 0;
$process_queue_immediately = isset( $_POST['processQueueImmediately'] ) ? 1 : 0;

$wpdb->update(
$table_name,
Expand All @@ -441,6 +450,12 @@ public static function savePosted( string $screen = 'core' ) : void {
[ 'name' => 'queueJobOnPostDelete' ]
);

$wpdb->update(
$table_name,
[ 'value' => $process_queue_immediately ],
[ 'name' => 'processQueueImmediately' ]
);

$process_queue_interval =
isset( $_POST['processQueueInterval'] ) ?
$_POST['processQueueInterval'] : 0;
Expand Down
2 changes: 2 additions & 0 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ function ( $path ) use ( $s ) {
}

public static function renderJobsPage() : void {
CoreOptions::init();
JobQueue::markFailedJobs();
JobQueue::squashQueue();

Expand All @@ -227,6 +228,7 @@ public static function renderJobsPage() : void {
$view['jobOptions'] = [
'queueJobOnPostSave' => CoreOptions::get( 'queueJobOnPostSave' ),
'queueJobOnPostDelete' => CoreOptions::get( 'queueJobOnPostDelete' ),
'processQueueImmediately' => CoreOptions::get( 'processQueueImmediately' ),
'processQueueInterval' => CoreOptions::get( 'processQueueInterval' ),
'autoJobQueueDetection' => CoreOptions::get( 'autoJobQueueDetection' ),
'autoJobQueueCrawling' => CoreOptions::get( 'autoJobQueueCrawling' ),
Expand Down
30 changes: 30 additions & 0 deletions src/WordPressAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ public static function registerHooks( string $bootstrap_file ) : void {
0
);

add_action(
'admin_post_wp2static_process_queue',
[ self::class, 'adminPostProcessQueue' ],
10,
0
);

add_action(
'admin_post_wp2static_crawl_queue_delete',
[ Controller::class, 'wp2staticCrawlQueueDelete' ],
Expand Down Expand Up @@ -316,5 +323,28 @@ public static function addAdminUIElements() : void {
add_filter( 'menu_order', [ Controller::class, 'setMenuOrder' ] );
}
}

/*
* Do security checks before calling Controller::wp2staticProcessQueue
*/
public static function adminPostProcessQueue() : void {
$method = $_SERVER['REQUEST_METHOD'];
if ( 'POST' !== $method ) {
$msg = "Invalid method in request to admin-post.php (wp2static_process_queue): $method";
}

$nonce = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : false;
$nonce_valid = $nonce && wp_verify_nonce( $nonce, 'wp2static_process_queue' );
if ( ! $nonce_valid ) {
$msg = 'Invalid nonce in request to admin-post.php (wpstatic_process_queue)';
}

if ( isset( $msg ) ) {
WsLog::l( $msg );
throw new \RuntimeException( $msg );
}

Controller::wp2staticProcessQueue();
}
}

13 changes: 13 additions & 0 deletions views/jobs-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@

<br>

<label
for="<?php echo $view['jobOptions']['processQueueImmediately']->name; ?>"
/><?php echo $view['jobOptions']['processQueueImmediately']->label; ?>:</label>

<input
type="checkbox"
id="<?php echo $view['jobOptions']['processQueueImmediately']->name; ?>"
name="<?php echo $view['jobOptions']['processQueueImmediately']->name; ?>"
value="1"
<?php echo (int) $view['jobOptions']['processQueueImmediately']->value === 1 ? 'checked' : ''; ?>
/>
<br>

<label
for=""
/>WP-Cron will attempt to process the Job Queue at this interval:</label>
Expand Down

0 comments on commit 68d6bd0

Please sign in to comment.