@@ -40,6 +40,8 @@ class Optml_Admin {
40
40
const OLD_USER_ENABLED_LD = 'optml_enabled_limit_dimensions ' ;
41
41
const OLD_USER_ENABLED_CL = 'optml_enabled_cloud_sites ' ;
42
42
43
+ const SYNC_CRON = 'optml_daily_sync ' ;
44
+ const ENRICH_CRON = 'optml_pull_image_data ' ;
43
45
/**
44
46
* Optml_Admin constructor.
45
47
*/
@@ -55,13 +57,12 @@ public function __construct() {
55
57
add_action ( 'admin_notices ' , [ $ this , 'add_notice ' ] );
56
58
add_action ( 'admin_notices ' , [ $ this , 'add_notice_upgrade ' ] );
57
59
add_action ( 'admin_notices ' , [ $ this , 'add_notice_conflicts ' ] );
58
- add_action ( ' optml_daily_sync ' , [ $ this , 'daily_sync ' ] );
60
+ add_action ( self :: SYNC_CRON , [ $ this , 'daily_sync ' ] );
59
61
add_action ( 'admin_init ' , [ $ this , 'redirect_old_dashboard ' ] );
60
62
61
63
if ( $ this ->settings ->is_connected () ) {
62
64
add_action ( 'init ' , [ $ this , 'check_domain_change ' ] );
63
- add_action ( 'optml_pull_image_data_init ' , [ $ this , 'pull_image_data_init ' ] );
64
- add_action ( 'optml_pull_image_data ' , [ $ this , 'pull_image_data ' ] );
65
+ add_action ( self ::ENRICH_CRON , [ $ this , 'pull_image_data ' ] );
65
66
66
67
// Backwards compatibility for older versions of WordPress < 6.0.0 requiring 3 parameters for this specific filter.
67
68
$ below_6_0_0 = version_compare ( get_bloginfo ( 'version ' ), '6.0.0 ' , '< ' );
@@ -73,15 +74,17 @@ public function __construct() {
73
74
74
75
add_action ( 'updated_post_meta ' , [ $ this , 'detect_image_alt_change ' ], 10 , 4 );
75
76
add_action ( 'added_post_meta ' , [ $ this , 'detect_image_alt_change ' ], 10 , 4 );
76
- add_action ( 'init ' , [ $ this , 'schedule_data_enhance_cron ' ] );
77
+ if ( ! wp_next_scheduled ( self ::ENRICH_CRON ) ) {
78
+ wp_schedule_event ( time () + 10 , 'hourly ' , self ::ENRICH_CRON );
79
+ }
77
80
}
78
81
add_action ( 'init ' , [ $ this , 'update_default_settings ' ] );
79
82
add_action ( 'init ' , [ $ this , 'update_limit_dimensions ' ] );
80
83
add_action ( 'init ' , [ $ this , 'update_cloud_sites_default ' ] );
81
84
add_action ( 'admin_init ' , [ $ this , 'maybe_redirect ' ] );
82
85
add_action ( 'admin_init ' , [ $ this , 'init_no_script ' ] );
83
- if ( ! is_admin () && $ this ->settings ->is_connected () && ! wp_next_scheduled ( ' optml_daily_sync ' ) ) {
84
- wp_schedule_event ( time () + 10 , 'daily ' , ' optml_daily_sync ' , [] );
86
+ if ( ! is_admin () && $ this ->settings ->is_connected () && ! wp_next_scheduled ( self :: SYNC_CRON ) ) {
87
+ wp_schedule_event ( time () + 10 , 'twicedaily ' , self :: SYNC_CRON , [] );
85
88
}
86
89
add_action ( 'optml_after_setup ' , [ $ this , 'register_public_actions ' ], 999999 );
87
90
@@ -191,16 +194,6 @@ protected function is_gzipped( $contents ) {
191
194
}
192
195
// phpcs:enable
193
196
}
194
- /**
195
- * Schedules the hourly cron that starts the querying for images alt/title attributes
196
- *
197
- * @uses action: init
198
- */
199
- public function schedule_data_enhance_cron () {
200
- if ( ! wp_next_scheduled ( 'optml_pull_image_data_init ' ) ) {
201
- wp_schedule_event ( time (), 'hourly ' , 'optml_pull_image_data_init ' );
202
- }
203
- }
204
197
205
198
/**
206
199
* Query the database for images and extract the alt/title to send them to the API
@@ -250,20 +243,6 @@ public function pull_image_data() {
250
243
$ api = new Optml_Api ();
251
244
$ api ->call_data_enrich_api ( $ image_data );
252
245
}
253
- if ( ! empty ( $ attachments ) ) {
254
- wp_schedule_single_event ( time () + 5 , 'optml_pull_image_data ' );
255
- }
256
- }
257
-
258
- /**
259
- * Schedule the event to pull image alt/title
260
- *
261
- * @uses action: optml_pull_image_data_init
262
- */
263
- public function pull_image_data_init () {
264
- if ( ! wp_next_scheduled ( 'optml_pull_image_data ' ) ) {
265
- wp_schedule_single_event ( time () + 5 , 'optml_pull_image_data ' );
266
- }
267
246
}
268
247
269
248
/**
@@ -1094,7 +1073,22 @@ public function daily_sync() {
1094
1073
if ( isset ( $ data ['extra_visits ' ] ) ) {
1095
1074
$ this ->settings ->update_frontend_banner_from_remote ( $ data ['extra_visits ' ] );
1096
1075
}
1076
+ // Here the account got deactivated, in this case we check if the user is using offloaded images and we roll them back.
1077
+ if ( isset ( $ data ['status ' ] ) && $ data ['status ' ] === 'inactive ' ) {
1078
+ // We check if the user has images offloaded.
1079
+ if ( $ this ->settings ->get ( 'transfer_status ' ) !== 'offload_images ' ) {
1080
+ return ;
1081
+ }
1097
1082
1083
+ $ in_progress = $ this ->settings ->get ( 'offloading_status ' ) !== 'disabled ' ;
1084
+ // We check if there is an in progress transfer, we stop it.
1085
+ if ( $ in_progress ) {
1086
+ $ this ->settings ->update ( 'offloading_status ' , 'disabled ' );
1087
+ }
1088
+ // We start the rollback process.
1089
+ Optml_Logger::instance ()->add_log ( 'rollback_images ' , 'Account deactivated, starting rollback. ' );
1090
+ Optml_Media_Offload::get_image_count ( 'rollback_images ' , false );
1091
+ }
1098
1092
remove_filter ( 'optml_dont_trigger_settings_updated ' , '__return_true ' );
1099
1093
}
1100
1094
0 commit comments