From f8bf67bf914e023b1d044a3a63d971c0a7e4e8e0 Mon Sep 17 00:00:00 2001 From: John Shaffer Date: Mon, 4 May 2020 17:13:16 -0500 Subject: [PATCH 1/2] Fix job queueing when a post is saved or trashed. This fixes a regression introduced in 09847fdc92faa82ab4ddd9375b17aaebb1aec580, but rather than simply revert the relevant changes made there, I opted to move the calls to CoreOptions::getValue into the handlers, so they will only hit the DB when the hooks fire, rather than potentially every page load. --- src/Controller.php | 11 ++++++----- src/WordPressAdmin.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Controller.php b/src/Controller.php index 97a64afe1..92ce8a000 100755 --- a/src/Controller.php +++ b/src/Controller.php @@ -395,15 +395,16 @@ public static function wp2static_ui_save_job_options() : void { } public static function wp2static_save_post_handler( int $post_id ) : void { - if ( get_post_status( $post_id ) !== 'publish' ) { - return; + if ( CoreOptions::getValue( 'queueJobOnPostSave' ) && + get_post_status( $post_id ) === 'publish' ) { + self::wp2static_enqueue_jobs(); } - - self::wp2static_enqueue_jobs(); } public static function wp2static_trashed_post_handler() : void { - self::wp2static_enqueue_jobs(); + if ( CoreOptions::getValue( 'queueJobOnPostDelete' ) ) { + self::wp2static_enqueue_jobs(); + } } public static function wp2static_enqueue_jobs() : void { diff --git a/src/WordPressAdmin.php b/src/WordPressAdmin.php index b94c58cf2..e9e7470b2 100755 --- a/src/WordPressAdmin.php +++ b/src/WordPressAdmin.php @@ -185,6 +185,18 @@ public static function registerHooks( string $bootstrap_file ) : void { 1 ); + add_action( + 'save_post', + [ 'WP2Static\Controller', 'wp2static_save_post_handler' ], + 0 + ); + + add_action( + 'trashed_post', + [ 'WP2Static\Controller', 'wp2static_trashed_post_handler' ], + 0 + ); + /* * Register actions for when we should invalidate cache for * a URL(s) or whole site From 3cfea1292f2815958044342b8faeb7b517f7b995 Mon Sep 17 00:00:00 2001 From: John Shaffer Date: Mon, 4 May 2020 17:31:31 -0500 Subject: [PATCH 2/2] Remove add_action and remove_action calls from CoreOptions::savePosted. savePosted is normally called only when the Options page is POSTed, so the add_action calls aren't any use here. --- src/CoreOptions.php | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/CoreOptions.php b/src/CoreOptions.php index e609c937a..857313338 100755 --- a/src/CoreOptions.php +++ b/src/CoreOptions.php @@ -421,34 +421,6 @@ public static function savePosted( string $screen = 'core' ) : void { [ 'name' => 'queueJobOnPostDelete' ] ); - if ( $queue_on_post_save ) { - add_action( - 'save_post', - [ 'WP2Static\Controller', 'wp2static_save_post_handler' ], - 0 - ); - } else { - remove_action( - 'save_post', - [ 'WP2Static\Controller', 'wp2static_save_post_handler' ], - 0 - ); - } - - if ( $queue_on_post_delete ) { - add_action( - 'trashed_post', - [ 'WP2Static\Controller', 'wp2static_trashed_post_handler' ], - 0 - ); - } else { - remove_action( - 'trashed_post', - [ 'WP2Static\Controller', 'wp2static_trashed_post_handler' ], - 0 - ); - } - $process_queue_interval = isset( $_POST['processQueueInterval'] ) ? $_POST['processQueueInterval'] : 0;