Skip to content

Commit b466825

Browse files
refactor: legacy free user with offload check
1 parent bc58c04 commit b466825

5 files changed

Lines changed: 45 additions & 16 deletions

File tree

assets/src/dashboard/parts/connected/settings/OffloadMedia.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* global optimoleDashboardApp */
22

33
import classnames from 'classnames';
4-
import { useEffect, useState } from '@wordpress/element';
5-
import { useDispatch, useSelect, useMemo } from '@wordpress/data';
4+
import { useEffect, useState, useMemo } from '@wordpress/element';
5+
import { useDispatch, useSelect } from '@wordpress/data';
66
import { Icon } from '@wordpress/icons';
77

88
import { warning, rollback as rollbackIcon, offload, warningAlt, sync } from '../../../utils/icons';
@@ -38,8 +38,8 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
3838
offloadFinishNotice,
3939
offloadLimitReached,
4040
offloadLimit,
41-
canUseMediaOffloading,
42-
isMediaOffloadingLegacyFreeUser
41+
canUseMediaOffloadingFlag,
42+
canShowFreeUserWithOffloadNoticeFlag
4343
} = useSelect( select => {
4444
const {
4545
getOffloadConflicts,
@@ -54,12 +54,11 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
5454
isLoading,
5555
getSiteSettings,
5656
getOffloadLimit,
57-
getUserData
57+
getUserData,
58+
canShowFreeUserWithOffloadNotice
5859
} = select( 'optimole' );
5960

6061
const userData = getUserData();
61-
const canUseMediaOffloading = Boolean( userData?.can_use_offloading );
62-
const isMediaOffloadingLegacyFreeUser = Boolean( userData?.is_legacy_offloading_free_user );
6362

6463
return {
6564
offloadConflicts: getOffloadConflicts(),
@@ -75,8 +74,8 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
7574
offloadFinishNotice: getSiteSettings( 'show_offload_finish_notice' ),
7675
offloadLimitReached: 'enabled' === getSiteSettings( 'offload_limit_reached' ),
7776
offloadLimit: getOffloadLimit(),
78-
canUseMediaOffloading,
79-
isMediaOffloadingLegacyFreeUser
77+
canUseMediaOffloadingFlag: Boolean( userData?.can_use_offloading ),
78+
canShowFreeUserWithOffloadNoticeFlag: canShowFreeUserWithOffloadNotice()
8079
};
8180
}, []);
8281

@@ -94,8 +93,8 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
9493
const isRollbackInProgress = 'disabled' !== settings['rollback_status'];
9594

9695
const showDisabledOffloadingNotice = useMemo( () => {
97-
return ! canUseMediaOffloading && isMediaOffloadingLegacyFreeUser && 'enabled' === settings?.['offload_media'];
98-
}, [ settings, canUseMediaOffloading, isMediaOffloadingLegacyFreeUser ]);
96+
return 'enabled' === settings?.['offload_media'] && ! canUseMediaOffloadingFlag && canShowFreeUserWithOffloadNoticeFlag;
97+
}, [ settings, canUseMediaOffloadingFlag, canShowFreeUserWithOffloadNoticeFlag ]);
9998

10099
useEffect( () => {
101100
if ( isOffloadingInProgress ) {
@@ -193,7 +192,7 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
193192
[MODAL_STATE_OFFLOAD]: {
194193
icon: offload,
195194
onConfirm: () => {
196-
if ( canUseMediaOffloading ) {
195+
if ( canUseMediaOffloadingFlag ) {
197196
onOffloadMedia();
198197
} else {
199198
window.open( optimoleDashboardApp.optimoleHome + 'pricing', '_blank' );
@@ -203,9 +202,9 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
203202
labels: {
204203
title: options_strings.offloading_start_title,
205204
description: options_strings.offloading_start_description,
206-
action: canUseMediaOffloading ? options_strings.offloading_start_action : optimoleDashboardApp.strings.upgrade.title_long
205+
action: canUseMediaOffloadingFlag ? options_strings.offloading_start_action : optimoleDashboardApp.strings.upgrade.title_long
207206
},
208-
afterContentChildren: ! canUseMediaOffloading && (
207+
afterContentChildren: ! canUseMediaOffloadingFlag && (
209208
<Notice
210209
type="warning"
211210
text={options_strings.upgrade_to_use_offloading_notice_desc}

assets/src/dashboard/store/reducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const DEFAULT_STATE = {
3131
logs: {
3232
offload: '',
3333
rollback: ''
34-
}
34+
},
35+
canShowFreeUserWithOffloadNotice: 'yes' === optimoleDashboardApp?.show_free_user_with_offload_notice
3536
};
3637

3738
const reducer = ( state = DEFAULT_STATE, action ) => {

assets/src/dashboard/store/selectors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ const selectors = {
9696
},
9797
isSubApiKey( state ) {
9898
return state.apiKey && state.apiKey.startsWith( 'optml-s' );
99+
},
100+
canShowFreeUserWithOffloadNotice( state ) {
101+
return Boolean( state?.canShowFreeUserWithOffloadNotice );
99102
}
100103
};
101104

inc/admin.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function __construct() {
113113
}
114114

115115
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
116+
add_action( 'admin_init', [ $this, 'mark_free_user_with_offload' ] );
116117
}
117118

118119
/**
@@ -1368,6 +1369,7 @@ private function localize_dashboard_app() {
13681369
'bf_notices' => $this->get_bf_notices(),
13691370
'spc_banner' => $this->get_spc_banner(),
13701371
'show_exceed_plan_quota_notice' => $this->should_show_exceed_quota_warning(),
1372+
'show_free_user_with_offload_notice' => get_option( 'optml_free_user_with_offload', 'no' ),
13711373
];
13721374
}
13731375

@@ -2276,4 +2278,28 @@ public function should_show_exceed_quota_warning() {
22762278

22772279
return true;
22782280
}
2281+
2282+
/**
2283+
* Mark the users that are on free plan and have offload enabled.
2284+
*
2285+
* User for displaying a notice after plugin update about offload restrictions.
2286+
*
2287+
* @return void
2288+
*/
2289+
public function mark_free_user_with_offload() {
2290+
if ( ! $this->settings->is_connected() ) {
2291+
return;
2292+
}
2293+
2294+
$service_data = $this->settings->get( 'service_data' );
2295+
if ( isset( $service_data['plan'] ) && 'free' !== $service_data['plan'] ) {
2296+
return;
2297+
}
2298+
2299+
if ( false !== get_option( 'optml_free_user_with_offload', false ) ) {
2300+
return;
2301+
}
2302+
2303+
update_option( 'optml_free_user_with_offload', $this->settings->is_offload_enabled() ? 'yes' : 'no' );
2304+
}
22792305
}

inc/main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ final class Optml_Main {
77
/**
88
* Optml_Main The single instance of Starter_Plugin.
99
*
10-
* @var object
10+
* @var Optml_Main|null
1111
* @access private
1212
* @since 1.0.0
1313
*/

0 commit comments

Comments
 (0)