diff --git a/README.md b/README.md index b45a268..b85d1b0 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,15 @@ WordPress REST API during its build. Publishing, updating, unpublishing, or deleting public REST-visible post types, taxonomy changes, navigation changes, media changes, and public user changes schedule a production build. Autosaves, revisions, and draft-only transitions are ignored. Multiple hooks in one -request collapse into one generation. +request collapse into one generation. Each new public-content change restarts a +60-second quiet window, so editing a batch of content produces one build. +Merely opening or navigating wp-admin does not schedule anything. Delivery uses WP-Cron with a stable idempotency key. Network failures, timeouts, rate limits, and server errors retry with bounded exponential backoff. A change -arriving while a delivery runs remains pending and schedules one successor. -**Build now** uses the same path immediately. The generic **Build now** action +arriving while a delivery runs remains pending and schedules one successor after +the quiet window. **Build now** skips the window, runs immediately, and clears +the superseded scheduled event. The generic **Build now** action in the Spacefast dashboard triggers the same connected production repository. The plugin sends no post content, title, author, or other WordPress data to diff --git a/includes/class-spacefast-plugin.php b/includes/class-spacefast-plugin.php index 9562d29..3c6d0e3 100644 --- a/includes/class-spacefast-plugin.php +++ b/includes/class-spacefast-plugin.php @@ -3,6 +3,7 @@ defined( 'ABSPATH' ) || exit; final class Spacefast_Plugin { + const AUTOMATIC_DEBOUNCE_SECONDS = MINUTE_IN_SECONDS; const LOCK_OPTION = 'spacefast_wordpress_worker_lock'; const NOTICE_TRANSIENT = 'spacefast_wordpress_admin_notice'; @@ -48,7 +49,7 @@ public static function activate( bool $network_wide = false ): void { } $state = Spacefast_Sync_State::get(); if ( (int) $state['desired'] > (int) $state['delivered'] ) { - self::schedule( time() + 15 ); + self::schedule_after_change( (int) $state['last_change_at'] ); } } @@ -225,6 +226,7 @@ public static function manual_build(): void { self::redirect(); } self::record_change( 'manual' ); + wp_clear_scheduled_hook( Spacefast_Sync_State::HOOK ); self::deliver(); self::redirect(); } @@ -311,14 +313,26 @@ private static function record_change( string $reason ): void { } self::$change_recorded = true; $event_id = (string) wp_generate_uuid4(); - Spacefast_Sync_State::mutate( + $state = Spacefast_Sync_State::mutate( static fn( array $state ): array => Spacefast_Sync_State::record_change( $state, $reason, $event_id ) ); - self::schedule( time() + 15 ); + self::schedule_after_change( (int) $state['last_change_at'] ); + } + + private static function schedule_after_change( int $changed_at ): void { + $timestamp = max( time() + 1, $changed_at + self::AUTOMATIC_DEBOUNCE_SECONDS ); + $scheduled = wp_next_scheduled( Spacefast_Sync_State::HOOK ); + if ( $scheduled === $timestamp ) { + return; + } + if ( $scheduled ) { + wp_unschedule_event( $scheduled, Spacefast_Sync_State::HOOK ); + } + wp_schedule_single_event( $timestamp, Spacefast_Sync_State::HOOK ); } private static function schedule( int $timestamp ): void { @@ -372,7 +386,7 @@ static function ( array $state ): array { ) ); if ( (int) $current['desired'] > (int) $current['delivered'] ) { - self::schedule( time() + 15 ); + self::schedule_after_change( (int) $current['last_change_at'] ); } return; } diff --git a/includes/class-spacefast-sync-state.php b/includes/class-spacefast-sync-state.php index 612c899..2042045 100644 --- a/includes/class-spacefast-sync-state.php +++ b/includes/class-spacefast-sync-state.php @@ -7,7 +7,7 @@ final class Spacefast_Sync_State { const HOOK = 'spacefast_wordpress_deliver'; /** - * @return array{desired:int,delivered:int,event_id:string,reasons:array,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string} + * @return array{desired:int,delivered:int,event_id:string,reasons:array,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string,last_change_at:int} */ public static function defaults(): array { return array( @@ -21,13 +21,14 @@ public static function defaults(): array { 'last_message' => '', 'last_build_id' => '', 'last_version_id' => '', + 'last_change_at' => 0, 'last_attempt_at' => 0, 'last_success_at' => 0, ); } /** - * @return array{desired:int,delivered:int,event_id:string,reasons:array,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string} + * @return array{desired:int,delivered:int,event_id:string,reasons:array,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string,last_change_at:int} */ public static function get(): array { $value = get_option( self::OPTION, array() ); @@ -54,6 +55,7 @@ public static function record_change( array $state, string $reason, string $even ); $state['attempts'] = 0; $state['next_at'] = 0; + $state['last_change_at'] = time(); $state['last_status'] = 'pending'; $state['last_message'] = ''; return $state; diff --git a/readme.txt b/readme.txt index 92a7dcb..5a3ee72 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: spacefast Tags: static site, headless cms, simply static, deployment Requires at least: 6.5 Requires PHP: 8.1 -Stable tag: 0.3.0 +Stable tag: 0.3.1 License: GPLv2 or later Publish a Simply Static export to Spacefast, or rebuild a headless site when WordPress content changes. @@ -31,6 +31,10 @@ OAuth access is limited to the Team you authorize and the mode you choose. WordP == Changelog == += 0.3.1 = +* Debounce automatic headless builds until public content has been quiet for 60 seconds. +* Keep manual builds immediate and avoid duplicate scheduled delivery afterward. + = 0.3.0 = * Replace copied API keys with Team-scoped OAuth 2 authorization and Space selection in WordPress. * Add refresh-token rotation and remote revocation on disconnect. diff --git a/spacefast-wordpress.php b/spacefast-wordpress.php index 34a8eb6..17076b6 100644 --- a/spacefast-wordpress.php +++ b/spacefast-wordpress.php @@ -3,7 +3,7 @@ * Plugin Name: Spacefast * Plugin URI: https://spacefast.com/ * Description: Publishes static WordPress exports or rebuilds a headless Spacefast site. - * Version: 0.3.0 + * Version: 0.3.1 * Update URI: https://github.com/spacefast/wordpress * Requires at least: 6.5 * Requires PHP: 8.1 @@ -14,7 +14,7 @@ defined( 'ABSPATH' ) || exit; -define( 'SPACEFAST_WORDPRESS_VERSION', '0.3.0' ); +define( 'SPACEFAST_WORDPRESS_VERSION', '0.3.1' ); define( 'SPACEFAST_WORDPRESS_FILE', __FILE__ ); require_once __DIR__ . '/includes/class-spacefast-settings.php'; diff --git a/tests/acceptance/wordpress.php b/tests/acceptance/wordpress.php index 7fb815a..d8504df 100644 --- a/tests/acceptance/wordpress.php +++ b/tests/acceptance/wordpress.php @@ -40,6 +40,9 @@ function spacefast_accept( bool $condition, string $message ): void { 'access_token' => 'acceptance-access', 'refresh_token' => 'acceptance-refresh', 'expires_at' => time() + 900, + 'team_id' => 'team_acceptance', + 'space_id' => 'spc_acceptance', + 'verified_at' => time(), ) ); global $wpdb; @@ -51,8 +54,24 @@ function spacefast_accept( bool $condition, string $message ): void { ); spacefast_accept( ! in_array( $autoload, array( 'yes', 'on', 'auto-on' ), true ), 'OAuth tokens are autoloaded.' ); +$changed_at = time(); +$post_id = wp_insert_post( + array( + 'post_title' => 'Debounce acceptance', + 'post_status' => 'publish', + 'post_type' => 'post', + ) +); +spacefast_accept( ! is_wp_error( $post_id ), 'Could not publish acceptance content.' ); +$scheduled_at = wp_next_scheduled( Spacefast_Sync_State::HOOK ); +spacefast_accept( false !== $scheduled_at, 'Publishing public content did not schedule a build.' ); +spacefast_accept( + $scheduled_at >= $changed_at + 59 && $scheduled_at <= time() + 61, + 'Public content did not receive the 60-second quiet window.' +); + $plugin = get_plugin_data( WP_PLUGIN_DIR . '/spacefast-wordpress/spacefast-wordpress.php', false, false ); -spacefast_accept( '0.3.0' === $plugin['Version'], 'Unexpected plugin version.' ); +spacefast_accept( '0.3.1' === $plugin['Version'], 'Unexpected plugin version.' ); spacefast_accept( 'https://github.com/spacefast/wordpress' === $plugin['UpdateURI'], 'Update URI is missing.' ); Spacefast_Settings::disconnect(); diff --git a/tests/behavior.php b/tests/behavior.php index 5b4ea27..65835ce 100644 --- a/tests/behavior.php +++ b/tests/behavior.php @@ -321,6 +321,7 @@ static function ( string $url, array $args ) use ( &$revoke_requests ): array { check( 2 === $state['desired'], 'increments desired generation' ); check( 'event-two' === $state['event_id'], 'keeps latest stable event id' ); check( array( 'postpublished', 'taxonomy' ) === $state['reasons'], 'coalesces reasons' ); +check( 0 < $state['last_change_at'], 'records when the latest public content change happened' ); $state['desired'] = 3; $ack = Spacefast_Sync_State::acknowledge( $state, 2, 'bld_test' ); check( 2 === $ack['delivered'], 'acknowledges only observed generation' ); @@ -372,13 +373,19 @@ static function ( string $url, array $args ) use ( &$revoke_requests ): array { Spacefast_Sync_State::release_lock( 'spacefast_test_lock', 'owner-a' ); check( false === get_option( 'spacefast_test_lock', false ), 'owner releases lock' ); -$schedule = new ReflectionMethod( Spacefast_Plugin::class, 'schedule' ); -$GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ] = time() + DAY_IN_SECONDS; -$earlier = time() + 15; -$schedule->invoke( null, $earlier ); +$debounce = new ReflectionMethod( Spacefast_Plugin::class, 'schedule_after_change' ); +$changed_at = time(); +$GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ] = $changed_at + 10; +$debounce->invoke( null, $changed_at ); check( - $earlier === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ], - 'new content pulls a far-future retry forward' + $changed_at + MINUTE_IN_SECONDS === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ], + 'public content waits for a 60-second quiet window' +); +$later_change = $changed_at + 20; +$debounce->invoke( null, $later_change ); +check( + $later_change + MINUTE_IN_SECONDS === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ], + 'a later content change restarts the quiet window' ); Spacefast_Sync_State::save(