@@ -43,6 +43,8 @@ class Optml_Admin {
43
43
const OLD_USER_ENABLED_LD = 'optml_enabled_limit_dimensions ' ;
44
44
const OLD_USER_ENABLED_CL = 'optml_enabled_cloud_sites ' ;
45
45
46
+ const SYNC_CRON = 'optml_daily_sync ' ;
47
+ const ENRICH_CRON = 'optml_pull_image_data ' ;
46
48
/**
47
49
* Optml_Admin constructor.
48
50
*/
@@ -58,13 +60,12 @@ public function __construct() {
58
60
add_action ( 'admin_notices ' , [ $ this , 'add_notice ' ] );
59
61
add_action ( 'admin_notices ' , [ $ this , 'add_notice_upgrade ' ] );
60
62
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 ' ] );
62
64
add_action ( 'admin_init ' , [ $ this , 'redirect_old_dashboard ' ] );
63
65
64
66
if ( $ this ->settings ->is_connected () ) {
65
67
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 ' ] );
68
69
69
70
// Backwards compatibility for older versions of WordPress < 6.0.0 requiring 3 parameters for this specific filter.
70
71
$ below_6_0_0 = version_compare ( get_bloginfo ( 'version ' ), '6.0.0 ' , '< ' );
@@ -76,15 +77,17 @@ public function __construct() {
76
77
77
78
add_action ( 'updated_post_meta ' , [ $ this , 'detect_image_alt_change ' ], 10 , 4 );
78
79
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
+ }
80
83
}
81
84
add_action ( 'init ' , [ $ this , 'update_default_settings ' ] );
82
85
add_action ( 'init ' , [ $ this , 'update_limit_dimensions ' ] );
83
86
add_action ( 'init ' , [ $ this , 'update_cloud_sites_default ' ] );
84
87
add_action ( 'admin_init ' , [ $ this , 'maybe_redirect ' ] );
85
88
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 , [] );
88
91
}
89
92
add_action ( 'optml_after_setup ' , [ $ this , 'register_public_actions ' ], 999999 );
90
93
@@ -194,16 +197,6 @@ protected function is_gzipped( $contents ) {
194
197
}
195
198
// phpcs:enable
196
199
}
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
- }
207
200
208
201
/**
209
202
* 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() {
253
246
$ api = new Optml_Api ();
254
247
$ api ->call_data_enrich_api ( $ image_data );
255
248
}
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
- }
270
249
}
271
250
272
251
/**
@@ -1097,7 +1076,24 @@ public function daily_sync() {
1097
1076
if ( isset ( $ data ['extra_visits ' ] ) ) {
1098
1077
$ this ->settings ->update_frontend_banner_from_remote ( $ data ['extra_visits ' ] );
1099
1078
}
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
+ }
1100
1085
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
+ }
1101
1097
remove_filter ( 'optml_dont_trigger_settings_updated ' , '__return_true ' );
1102
1098
}
1103
1099
0 commit comments