Skip to content

Commit acb384e

Browse files
authored
Merge pull request #889 from Codeinwp/feat/deactivate-rollback
Feat/deactivate rollback
2 parents 00b10f2 + b81c89c commit acb384e

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

inc/admin.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

inc/settings.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Optml_Settings {
115115
*
116116
* @var array All options.
117117
*/
118-
private $options;
118+
private static $options;
119119

120120
/**
121121
* Optml_Settings constructor.
@@ -125,11 +125,11 @@ public function __construct() {
125125

126126
$this->namespace = OPTML_NAMESPACE . '_settings';
127127
$this->default_schema = apply_filters( 'optml_default_settings', $this->default_schema );
128-
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
128+
self::$options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
129129

130130
if ( defined( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && $this->to_boolean( constant( 'OPTIML_ENABLED_MU' ) ) && constant( 'OPTIML_MU_SITE_ID' ) ) {
131131
switch_to_blog( constant( 'OPTIML_MU_SITE_ID' ) );
132-
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
132+
self::$options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
133133
restore_current_blog();
134134
}
135135

@@ -166,7 +166,7 @@ public function __construct() {
166166
continue;
167167
}
168168
$sanitized_value = ( $type === 'bool' ) ? ( $value === 'on' ? 'enabled' : 'disabled' ) : (int) $value;
169-
$this->options[ $key ] = $sanitized_value;
169+
self::$options[ $key ] = $sanitized_value;
170170
}
171171
}
172172
}
@@ -203,7 +203,7 @@ public function get( $key ) {
203203
return null;
204204
}
205205

206-
return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
206+
return isset( self::$options[ $key ] ) ? self::$options[ $key ] : '';
207207
}
208208

209209
/**
@@ -418,13 +418,13 @@ public function update_frontend_banner_from_remote( $value ) {
418418
return false;
419419
}
420420

421-
$opts = $this->options;
421+
$opts = self::$options;
422422
$opts['banner_frontend'] = $value ? 'enabled' : 'disabled';
423423

424424
$update = update_option( $this->namespace, $opts, false );
425425

426426
if ( $update ) {
427-
$this->options = $opts;
427+
self::$options = $opts;
428428
}
429429

430430
return $update;
@@ -446,7 +446,7 @@ public function update( $key, $value ) {
446446
if ( ! $this->is_main_mu_site() ) {
447447
return false;
448448
}
449-
$opt = $this->options;
449+
$opt = self::$options;
450450

451451
if ( $key === 'banner_frontend' ) {
452452
$api = new Optml_Api();
@@ -456,9 +456,11 @@ public function update( $key, $value ) {
456456
}
457457

458458
$opt[ $key ] = $value;
459+
459460
$update = update_option( $this->namespace, $opt, false );
461+
460462
if ( $update ) {
461-
$this->options = $opt;
463+
self::$options = $opt;
462464
}
463465
if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) {
464466
do_action( 'optml_settings_updated' );
@@ -703,12 +705,14 @@ public function get_cdn_url() {
703705
*/
704706
public function reset() {
705707
$reset_schema = $this->default_schema;
706-
$reset_schema['filters'] = $this->options['filters'];
708+
$reset_schema['filters'] = self::$options['filters'];
707709

708710
$update = update_option( $this->namespace, $reset_schema );
709711
if ( $update ) {
710-
$this->options = $reset_schema;
712+
self::$options = $reset_schema;
711713
}
714+
wp_unschedule_hook( Optml_Admin::SYNC_CRON );
715+
wp_unschedule_hook( Optml_Admin::ENRICH_CRON );
712716

713717
return $update;
714718
}

0 commit comments

Comments
 (0)