Skip to content

Commit 8640f6d

Browse files
committed
Trigger rollback on deactivation
1 parent f7cc90e commit 8640f6d

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

inc/admin.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Optml_Admin {
4040
const OLD_USER_ENABLED_LD = 'optml_enabled_limit_dimensions';
4141
const OLD_USER_ENABLED_CL = 'optml_enabled_cloud_sites';
4242

43+
const SYNC_CRON = 'optml_daily_sync';
44+
const ENRICH_CRON = 'optml_pull_image_data';
4345
/**
4446
* Optml_Admin constructor.
4547
*/
@@ -55,13 +57,12 @@ public function __construct() {
5557
add_action( 'admin_notices', [ $this, 'add_notice' ] );
5658
add_action( 'admin_notices', [ $this, 'add_notice_upgrade' ] );
5759
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' ] );
5961
add_action( 'admin_init', [ $this, 'redirect_old_dashboard' ] );
6062

6163
if ( $this->settings->is_connected() ) {
6264
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' ] );
6566

6667
// Backwards compatibility for older versions of WordPress < 6.0.0 requiring 3 parameters for this specific filter.
6768
$below_6_0_0 = version_compare( get_bloginfo( 'version' ), '6.0.0', '<' );
@@ -73,15 +74,17 @@ public function __construct() {
7374

7475
add_action( 'updated_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
7576
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+
}
7780
}
7881
add_action( 'init', [ $this, 'update_default_settings' ] );
7982
add_action( 'init', [ $this, 'update_limit_dimensions' ] );
8083
add_action( 'init', [ $this, 'update_cloud_sites_default' ] );
8184
add_action( 'admin_init', [ $this, 'maybe_redirect' ] );
8285
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, [] );
8588
}
8689
add_action( 'optml_after_setup', [ $this, 'register_public_actions' ], 999999 );
8790

@@ -191,16 +194,6 @@ protected function is_gzipped( $contents ) {
191194
}
192195
// phpcs:enable
193196
}
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-
}
204197

205198
/**
206199
* 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() {
250243
$api = new Optml_Api();
251244
$api->call_data_enrich_api( $image_data );
252245
}
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-
}
267246
}
268247

269248
/**
@@ -1094,7 +1073,22 @@ public function daily_sync() {
10941073
if ( isset( $data['extra_visits'] ) ) {
10951074
$this->settings->update_frontend_banner_from_remote( $data['extra_visits'] );
10961075
}
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+
}
10971082

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+
}
10981092
remove_filter( 'optml_dont_trigger_settings_updated', '__return_true' );
10991093
}
11001094

inc/settings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ public function reset() {
701701
if ( $update ) {
702702
$this->options = $reset_schema;
703703
}
704+
wp_unschedule_hook( Optml_Admin::SYNC_CRON );
705+
wp_unschedule_hook( Optml_Admin::ENRICH_CRON );
704706

705707
return $update;
706708
}

0 commit comments

Comments
 (0)