@@ -43,6 +43,8 @@ class Optml_Admin {
4343 const OLD_USER_ENABLED_LD = 'optml_enabled_limit_dimensions ' ;
4444 const OLD_USER_ENABLED_CL = 'optml_enabled_cloud_sites ' ;
4545
46+ const SYNC_CRON = 'optml_daily_sync ' ;
47+ const ENRICH_CRON = 'optml_pull_image_data ' ;
4648 /**
4749 * Optml_Admin constructor.
4850 */
@@ -58,13 +60,12 @@ public function __construct() {
5860 add_action ( 'admin_notices ' , [ $ this , 'add_notice ' ] );
5961 add_action ( 'admin_notices ' , [ $ this , 'add_notice_upgrade ' ] );
6062 add_action ( 'admin_notices ' , [ $ this , 'add_notice_conflicts ' ] );
61- add_action ( ' optml_daily_sync ' , [ $ this , 'daily_sync ' ] );
63+ add_action ( self :: SYNC_CRON , [ $ this , 'daily_sync ' ] );
6264 add_action ( 'admin_init ' , [ $ this , 'redirect_old_dashboard ' ] );
6365
6466 if ( $ this ->settings ->is_connected () ) {
6567 add_action ( 'init ' , [ $ this , 'check_domain_change ' ] );
66- add_action ( 'optml_pull_image_data_init ' , [ $ this , 'pull_image_data_init ' ] );
67- add_action ( 'optml_pull_image_data ' , [ $ this , 'pull_image_data ' ] );
68+ add_action ( self ::ENRICH_CRON , [ $ this , 'pull_image_data ' ] );
6869
6970 // Backwards compatibility for older versions of WordPress < 6.0.0 requiring 3 parameters for this specific filter.
7071 $ below_6_0_0 = version_compare ( get_bloginfo ( 'version ' ), '6.0.0 ' , '< ' );
@@ -76,15 +77,17 @@ public function __construct() {
7677
7778 add_action ( 'updated_post_meta ' , [ $ this , 'detect_image_alt_change ' ], 10 , 4 );
7879 add_action ( 'added_post_meta ' , [ $ this , 'detect_image_alt_change ' ], 10 , 4 );
79- add_action ( 'init ' , [ $ this , 'schedule_data_enhance_cron ' ] );
80+ if ( ! wp_next_scheduled ( self ::ENRICH_CRON ) ) {
81+ wp_schedule_event ( time () + 10 , 'hourly ' , self ::ENRICH_CRON );
82+ }
8083 }
8184 add_action ( 'init ' , [ $ this , 'update_default_settings ' ] );
8285 add_action ( 'init ' , [ $ this , 'update_limit_dimensions ' ] );
8386 add_action ( 'init ' , [ $ this , 'update_cloud_sites_default ' ] );
8487 add_action ( 'admin_init ' , [ $ this , 'maybe_redirect ' ] );
8588 add_action ( 'admin_init ' , [ $ this , 'init_no_script ' ] );
86- if ( ! is_admin () && $ this ->settings ->is_connected () && ! wp_next_scheduled ( ' optml_daily_sync ' ) ) {
87- wp_schedule_event ( time () + 10 , 'daily ' , ' optml_daily_sync ' , [] );
89+ if ( ! is_admin () && $ this ->settings ->is_connected () && ! wp_next_scheduled ( self :: SYNC_CRON ) ) {
90+ wp_schedule_event ( time () + 10 , 'twicedaily ' , self :: SYNC_CRON , [] );
8891 }
8992 add_action ( 'optml_after_setup ' , [ $ this , 'register_public_actions ' ], 999999 );
9093
@@ -194,16 +197,6 @@ protected function is_gzipped( $contents ) {
194197 }
195198 // phpcs:enable
196199 }
197- /**
198- * Schedules the hourly cron that starts the querying for images alt/title attributes
199- *
200- * @uses action: init
201- */
202- public function schedule_data_enhance_cron () {
203- if ( ! wp_next_scheduled ( 'optml_pull_image_data_init ' ) ) {
204- wp_schedule_event ( time (), 'hourly ' , 'optml_pull_image_data_init ' );
205- }
206- }
207200
208201 /**
209202 * Query the database for images and extract the alt/title to send them to the API
@@ -253,20 +246,6 @@ public function pull_image_data() {
253246 $ api = new Optml_Api ();
254247 $ api ->call_data_enrich_api ( $ image_data );
255248 }
256- if ( ! empty ( $ attachments ) ) {
257- wp_schedule_single_event ( time () + 5 , 'optml_pull_image_data ' );
258- }
259- }
260-
261- /**
262- * Schedule the event to pull image alt/title
263- *
264- * @uses action: optml_pull_image_data_init
265- */
266- public function pull_image_data_init () {
267- if ( ! wp_next_scheduled ( 'optml_pull_image_data ' ) ) {
268- wp_schedule_single_event ( time () + 5 , 'optml_pull_image_data ' );
269- }
270249 }
271250
272251 /**
@@ -1097,7 +1076,24 @@ public function daily_sync() {
10971076 if ( isset ( $ data ['extra_visits ' ] ) ) {
10981077 $ this ->settings ->update_frontend_banner_from_remote ( $ data ['extra_visits ' ] );
10991078 }
1079+ // Here the account got deactivated, in this case we check if the user is using offloaded images and we roll them back.
1080+ if ( isset ( $ data ['status ' ] ) && $ data ['status ' ] === 'inactive ' ) {
1081+ // We check if the user has images offloaded.
1082+ if ( $ this ->settings ->get ( 'offload_media ' ) === 'disabled ' ) {
1083+ return ;
1084+ }
11001085
1086+ $ in_progress = $ this ->settings ->get ( 'offloading_status ' ) !== 'disabled ' ;
1087+ // We check if there is an in progress transfer, we stop it.
1088+ if ( $ in_progress ) {
1089+ $ this ->settings ->update ( 'offloading_status ' , 'disabled ' );
1090+ }
1091+ $ this ->settings ->update ( 'rollback_status ' , 'enabled ' );
1092+ // We start the rollback process.
1093+ Optml_Logger::instance ()->add_log ( 'rollback_images ' , 'Account deactivated, starting rollback. ' );
1094+ Optml_Media_Offload::get_image_count ( 'rollback_images ' , false );
1095+ $ this ->settings ->update ( 'offload_media ' , 'disabled ' );
1096+ }
11011097 remove_filter ( 'optml_dont_trigger_settings_updated ' , '__return_true ' );
11021098 }
11031099
0 commit comments