44
55final class Spacefast_Plugin {
66 const AUTOMATIC_DEBOUNCE_SECONDS = MINUTE_IN_SECONDS ;
7+ const STATIC_DELIVERY_STALE_SECONDS = 2 * HOUR_IN_SECONDS ;
78 const LOCK_OPTION = 'spacefast_wordpress_worker_lock ' ;
89 const NOTICE_TRANSIENT = 'spacefast_wordpress_admin_notice ' ;
910
@@ -42,6 +43,7 @@ public static function boot(): void {
4243 add_action ( 'update_option_blogname ' , array ( __CLASS__ , 'wordpress_setting_changed ' ), 10 , 3 );
4344 add_action ( 'update_option_blogdescription ' , array ( __CLASS__ , 'wordpress_setting_changed ' ), 10 , 3 );
4445 add_action ( 'update_option_blog_public ' , array ( __CLASS__ , 'wordpress_setting_changed ' ), 10 , 3 );
46+ add_action ( 'update_option_home ' , array ( __CLASS__ , 'wordpress_setting_changed ' ), 10 , 3 );
4547 add_filter ( 'site_status_tests ' , array ( __CLASS__ , 'site_health_tests ' ) );
4648 }
4749
@@ -55,7 +57,12 @@ public static function activate( bool $network_wide = false ): void {
5557 add_option ( Spacefast_Sync_State::OPTION , Spacefast_Sync_State ::defaults (), '' , false );
5658 }
5759 $ state = Spacefast_Sync_State ::get ();
58- if ( (int ) $ state ['desired ' ] > (int ) $ state ['delivered ' ] ) {
60+ $ active_delivery = 'building ' === (string ) $ state ['last_status ' ]
61+ && '' !== (string ) $ state ['last_build_id ' ];
62+ $ active_export = self ::static_delivery_active ( $ state );
63+ if ( $ active_delivery || $ active_export ) {
64+ self ::schedule ( time () + 1 );
65+ } elseif ( (int ) $ state ['desired ' ] > (int ) $ state ['delivered ' ] ) {
5966 self ::schedule_after_change ( (int ) $ state ['last_change_at ' ] );
6067 }
6168 }
@@ -259,6 +266,13 @@ public static function create_space(): void {
259266 * @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>}
260267 */
261268 private static function connect_space ( array $ space , array $ team ): array {
269+ $ current_settings = Spacefast_Settings ::get ();
270+ if ( ! hash_equals ( (string ) $ current_settings ['space_id ' ], (string ) $ space ['id ' ] ) ) {
271+ wp_clear_scheduled_hook ( Spacefast_Sync_State::HOOK );
272+ Spacefast_Sync_State ::save ( Spacefast_Sync_State ::defaults () );
273+ Spacefast_Static_Publisher ::reset ();
274+ self ::$ change_recorded = false ;
275+ }
262276 Spacefast_Settings ::merge (
263277 array (
264278 'team_id ' => (string ) ( $ space ['teamId ' ] ?? $ team ['id ' ] ?? '' ),
@@ -273,7 +287,7 @@ private static function connect_space( array $space, array $team ): array {
273287 );
274288 self ::mark_settings_pending ();
275289 $ settings_sync = self ::sync_space_settings ();
276- if ( ! $ settings_sync ['ok ' ] ) {
290+ if ( ! $ settings_sync ['ok ' ] && ' reauthorization_required ' !== $ settings_sync [ ' code ' ] ) {
277291 self ::static_publish_failed ( $ settings_sync ['message ' ] );
278292 return $ settings_sync ;
279293 }
@@ -370,16 +384,24 @@ public static function manual_static_publish(): void {
370384 self ::notice ( 'error ' , __ ( 'Install and activate Simply Static before publishing. ' , 'spacefast-wordpress ' ) );
371385 self ::redirect ();
372386 }
387+ $ before = Spacefast_Sync_State ::get ();
388+ $ was_active = self ::static_delivery_active ( $ before );
389+ $ previous_attempt = (int ) $ before ['last_attempt_at ' ];
373390 self ::record_change ( 'manual ' , true );
374391 wp_clear_scheduled_hook ( Spacefast_Sync_State::HOOK );
375392 self ::deliver ();
376393 $ state = Spacefast_Sync_State ::get ();
377- $ active = in_array ( (string ) $ state ['last_status ' ], array ( 'exporting ' , 'uploading ' , 'finalizing ' ), true );
394+ $ active = self ::static_delivery_active ( $ state );
395+ $ started = $ active && ( ! $ was_active || (int ) $ state ['last_attempt_at ' ] > $ previous_attempt );
396+ $ message = $ started
397+ ? __ ( 'Publishing started. You can leave this page. ' , 'spacefast-wordpress ' )
398+ : ( $ active
399+ ? __ ( 'A publish is already running. Your latest changes will publish next. ' , 'spacefast-wordpress ' )
400+ : ( (string ) $ state ['last_message ' ] ?: __ ( 'The publish could not start. The current live version is safe. ' , 'spacefast-wordpress ' ) )
401+ );
378402 self ::notice (
379403 $ active ? 'success ' : 'error ' ,
380- $ active
381- ? __ ( 'Publishing started. You can leave this page. ' , 'spacefast-wordpress ' )
382- : ( (string ) $ state ['last_message ' ] ?: __ ( 'The publish could not start. The current live version is safe. ' , 'spacefast-wordpress ' ) )
404+ $ message
383405 );
384406 self ::redirect ();
385407 }
@@ -641,7 +663,7 @@ public static function deliver(): void {
641663
642664 try {
643665 $ snapshot = Spacefast_Sync_State ::get ();
644- if ( ! empty ( $ snapshot ['settings_pending ' ] ) ) {
666+ if ( ! empty ( $ snapshot ['settings_pending ' ] ) && Spacefast_OAuth :: has_scope ( ' spaces:write ' ) ) {
645667 $ settings_sync = self ::sync_space_settings ();
646668 if ( ! $ settings_sync ['ok ' ] ) {
647669 Spacefast_Sync_State ::mutate (
@@ -726,6 +748,10 @@ private static function poll_headless_build( array $snapshot ): void {
726748 self ::schedule ( time () + 15 );
727749 return ;
728750 }
751+ if ( ! in_array ( $ status , array ( 'succeeded ' , 'failed ' , 'canceled ' , 'skipped ' ), true ) ) {
752+ self ::schedule ( time () + 60 );
753+ return ;
754+ }
729755 $ diagnostics = isset ( $ build ['diagnostics ' ] ) && is_array ( $ build ['diagnostics ' ] ) ? $ build ['diagnostics ' ] : array ();
730756 $ message = '' ;
731757 foreach ( $ diagnostics as $ diagnostic ) {
@@ -739,22 +765,42 @@ private static function poll_headless_build( array $snapshot ): void {
739765 );
740766 if ( 'succeeded ' === $ status ) {
741767 Spacefast_Settings ::merge ( array ( 'verified_at ' => time () ) );
742- if ( ( int ) $ current [ ' desired ' ] > ( int ) $ current [ ' delivered ' ] ) {
743- self :: schedule_after_change ( (int ) $ current ['last_change_at ' ] );
744- }
768+ }
769+ if ( (int ) $ current ['desired ' ] > ( int ) $ current [ ' delivered ' ] ) {
770+ self :: schedule_after_change ( ( int ) $ current [ ' last_change_at ' ] );
745771 }
746772 }
747773
748774 /** @param array<string,mixed> $snapshot Current delivery state. */
749775 private static function deliver_static ( array $ snapshot ): void {
750- if ( in_array ( (string ) $ snapshot ['last_status ' ], array ( 'exporting ' , 'uploading ' , 'finalizing ' ), true ) ) return ;
776+ if ( self ::static_delivery_active ( $ snapshot ) ) {
777+ if ( ! self ::static_delivery_stale ( $ snapshot ) ) {
778+ self ::schedule ( max ( time () + 60 , (int ) $ snapshot ['last_attempt_at ' ] + self ::STATIC_DELIVERY_STALE_SECONDS ) );
779+ return ;
780+ }
781+ Spacefast_Static_Publisher ::reset ();
782+ $ snapshot = Spacefast_Sync_State ::mutate (
783+ static function ( array $ state ): array {
784+ $ state ['last_status ' ] = 'pending ' ;
785+ $ state ['last_message ' ] = '' ;
786+ $ state ['active_generation ' ] = 0 ;
787+ return $ state ;
788+ }
789+ );
790+ }
751791 if ( (int ) $ snapshot ['desired ' ] <= (int ) $ snapshot ['delivered ' ] ) return ;
752792 if ( ! self ::simply_static_available () ) {
753793 self ::static_publish_failed ( __ ( 'Simply Static is required. Install it, then retry; the current live version is safe. ' , 'spacefast-wordpress ' ) );
754794 return ;
755795 }
756796 self ::configure_simply_static ();
757797 try {
798+ Spacefast_Sync_State ::mutate (
799+ static function ( array $ state ): array {
800+ $ state ['last_attempt_at ' ] = time ();
801+ return $ state ;
802+ }
803+ );
758804 $ started = \Simply_Static \Plugin ::instance ()->run_static_export ();
759805 if ( ! $ started ) {
760806 self ::schedule ( time () + 60 );
@@ -764,6 +810,7 @@ private static function deliver_static( array $snapshot ): void {
764810 static function ( array $ state ): array {
765811 $ state ['last_status ' ] = 'exporting ' ;
766812 $ state ['last_message ' ] = '' ;
813+ $ state ['last_attempt_at ' ] = time ();
767814 return $ state ;
768815 }
769816 );
@@ -772,6 +819,17 @@ static function ( array $state ): array {
772819 }
773820 }
774821
822+ /** @param array<string,mixed> $state Current delivery state. */
823+ private static function static_delivery_active ( array $ state ): bool {
824+ return in_array ( (string ) $ state ['last_status ' ], array ( 'exporting ' , 'uploading ' , 'finalizing ' ), true );
825+ }
826+
827+ /** @param array<string,mixed> $state Current delivery state. */
828+ private static function static_delivery_stale ( array $ state ): bool {
829+ $ last_attempt = (int ) $ state ['last_attempt_at ' ];
830+ return 0 === $ last_attempt || $ last_attempt + self ::STATIC_DELIVERY_STALE_SECONDS <= time ();
831+ }
832+
775833 public static function register_simply_static (): void {
776834 if ( ! self ::simply_static_available () ) {
777835 return ;
@@ -845,11 +903,23 @@ static function ( array $state ): array {
845903 );
846904 $ next ['last_status ' ] = 'exporting ' ;
847905 $ next ['active_generation ' ] = (int ) $ next ['desired ' ];
906+ $ next ['last_attempt_at ' ] = time ();
848907 return $ next ;
849908 }
850909 );
851910 }
852911
912+ public static function static_publish_progress ( string $ status ): void {
913+ if ( ! in_array ( $ status , array ( 'uploading ' , 'finalizing ' ), true ) ) return ;
914+ Spacefast_Sync_State ::mutate (
915+ static function ( array $ state ) use ( $ status ): array {
916+ $ state ['last_status ' ] = $ status ;
917+ $ state ['last_attempt_at ' ] = time ();
918+ return $ state ;
919+ }
920+ );
921+ }
922+
853923 public static function static_publish_completed ( string $ version_id , string $ status ): void {
854924 $ current = Spacefast_Sync_State ::mutate (
855925 static fn ( array $ state ): array => Spacefast_Sync_State ::acknowledge_static (
@@ -869,6 +939,7 @@ public static function static_publish_failed( string $message ): void {
869939 static function ( array $ state ) use ( $ message ): array {
870940 $ state ['last_status ' ] = 'blocked ' ;
871941 $ state ['last_message ' ] = $ message ;
942+ $ state ['active_generation ' ] = 0 ;
872943 return $ state ;
873944 }
874945 );
0 commit comments