Skip to content

Commit 62a5e26

Browse files
authored
Merge pull request #896 from Codeinwp/feat/opml-service/1318
Add nearing quota warning
2 parents c1010f2 + 5a8291d commit 62a5e26

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

assets/src/dashboard/parts/components/Modal.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { close } from '@wordpress/icons';
44
import { useViewportMatch } from '@wordpress/compose';
55
import { Button, Icon, Modal as CoreModal } from '@wordpress/components';
66

7-
export default function Modal({ icon, labels = {}, onRequestClose = () => {}, onConfirm = () => {}, variant = 'default' }) {
7+
export default function Modal({ icon, labels = {}, onRequestClose = () => {}, onConfirm = () => {}, variant = 'default', onSecondaryAction = () => {} }) {
88

99
const isMobileViewport = useViewportMatch( 'small', '<' );
1010

@@ -53,10 +53,20 @@ export default function Modal({ icon, labels = {}, onRequestClose = () => {}, on
5353
className="text-center mx-0 my-4 text-gray-700"
5454
dangerouslySetInnerHTML={ { __html: labels.description } }
5555
/>
56-
57-
<Button variant="primary" className={ actionButtonClasses } onClick={ onConfirm }>
58-
{ labels.action }
59-
</Button>
56+
<div class="flex gap-4">
57+
<Button variant="primary" className={ actionButtonClasses } onClick={ onConfirm }>
58+
{ labels.action }
59+
</Button>
60+
{ labels.secondaryAction && (
61+
<Button
62+
variant="default"
63+
className="optml__button flex justify-center rounded font-bold min-h-40"
64+
onClick={ onSecondaryAction }
65+
>
66+
{ labels.secondaryAction }
67+
</Button>
68+
) }
69+
</div>
6070
</div>
6171
</CoreModal>
6272
);

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import Modal from '../../components/Modal';
1414
import Logs from './Logs';
1515

1616
const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
17-
const { strings, cron_disabled } = optimoleDashboardApp;
17+
const { strings, cron_disabled, show_exceed_plan_quota_notice } = optimoleDashboardApp;
18+
1819
const { conflicts, options_strings } = strings;
1920

2021
const MODAL_STATE_OFFLOAD = 'offload';
2122
const MODAL_STATE_ROLLBACK = 'rollback';
2223
const MODAL_STATE_STOP_OFFLOAD = 'stopOffload';
2324
const MODAL_STATE_STOP_ROLLBACK = 'stopRollback';
25+
const MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE = 'planQuotaNotice';
2426

2527
const {
2628
offloadConflicts,
@@ -181,7 +183,7 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
181183
setCanSave( true );
182184

183185
if ( offloadEnabled ) {
184-
setModal( MODAL_STATE_OFFLOAD );
186+
setModal( show_exceed_plan_quota_notice ? MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE : MODAL_STATE_OFFLOAD );
185187

186188
return;
187189
}
@@ -245,6 +247,29 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
245247
description: options_strings.rollback_stop_description,
246248
action: options_strings.rollback_stop_action
247249
}
250+
},
251+
[MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE]: {
252+
variant: 'warning',
253+
icon: warningAlt,
254+
onConfirm: () => {
255+
onOffloadMedia();
256+
setModal( null );
257+
},
258+
onSecondaryAction: () => {
259+
setModal( null );
260+
const options = settings;
261+
options.offload_media = 'disabled';
262+
saveSettings( options );
263+
264+
// Remove "unsaved changes" warning
265+
window.onbeforeunload = null;
266+
},
267+
labels: {
268+
title: options_strings.exceed_plan_quota_notice_title,
269+
description: options_strings.exceed_plan_quota_notice_description,
270+
action: options_strings.exceed_plan_quota_notice_start_action,
271+
secondaryAction: options_strings.exceed_plan_quota_notice_secondary_action
272+
}
248273
}
249274
};
250275

@@ -270,7 +295,7 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
270295
e.preventDefault();
271296

272297
if ( 'offload' === radioBoxValue ) {
273-
setModal( MODAL_STATE_OFFLOAD );
298+
setModal( show_exceed_plan_quota_notice ? MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE : MODAL_STATE_OFFLOAD );
274299
}
275300

276301
if ( 'rollback' === radioBoxValue ) {

inc/admin.php

+57
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ private function localize_dashboard_app() {
12331233
],
12341234
'bf_notices' => $this->get_bf_notices(),
12351235
'spc_banner' => $this->get_spc_banner(),
1236+
'show_exceed_plan_quota_notice' => $this->should_show_exceed_quota_warning(),
12361237
];
12371238
}
12381239

@@ -1876,6 +1877,10 @@ private function get_dashboard_strings() {
18761877
'rollback_stop_action' => __( 'Cancel the transfer from Optimole', 'optimole-wp' ),
18771878
'cloud_library_btn_text' => __( 'Go to Cloud Library', 'optimole-wp' ),
18781879
'cloud_library_btn_link' => add_query_arg( 'page', 'optimole-dam', admin_url( 'admin.php' ) ),
1880+
'exceed_plan_quota_notice_title' => __( 'Your site has already reached over 50% of your monthly visits limit within just two weeks.', 'optimole-wp' ),
1881+
'exceed_plan_quota_notice_description' => sprintf( /* translators: 1 is the starting anchor tag, 2 is the ending anchor tag */ __( 'Based on this trend, you are likely to exceed your free quota before the month ends. To avoid any disruption in service, we strongly recommend %1$supgrading%2$s your plan or waiting until your traffic stabilizes before offloading your images. Do you still wish to proceed?', 'optimole-wp' ), '<a style="white-space: nowrap;" target=”_blank” href="https://dashboard.optimole.com/settings/billing/">', '</a>' ),
1882+
'exceed_plan_quota_notice_start_action' => __( 'Yes, Transfer to Optimole Cloud', 'optimole-wp' ),
1883+
'exceed_plan_quota_notice_secondary_action' => __( 'No, keep images on my website', 'optimole-wp' ),
18791884
],
18801885
'help' => [
18811886
'section_one_title' => __( 'Help and Support', 'optimole-wp' ),
@@ -2069,4 +2074,56 @@ public function survey_category( $value, $scale = 1 ) {
20692074

20702075
return 0;
20712076
}
2077+
2078+
/**
2079+
* Determines whether the exceed quota warning should be displayed to users.
2080+
*
2081+
* This function checks if the user's quota usage has exceeded a predefined limit
2082+
* and returns a boolean value indicating whether the warning should be shown.
2083+
*
2084+
* @return bool True if the exceed quota warning should be displayed, false otherwise.
2085+
*/
2086+
public function should_show_exceed_quota_warning() {
2087+
if ( ! $this->settings->is_connected() ) {
2088+
return false;
2089+
}
2090+
if ( get_option( 'optml_notice_hide_upg', 'no' ) === 'yes' ) {
2091+
return false;
2092+
}
2093+
2094+
$service_data = $this->settings->get( 'service_data' );
2095+
2096+
if ( ! isset( $service_data['plan'] ) ) {
2097+
return false;
2098+
}
2099+
if ( $service_data['plan'] !== 'free' ) {
2100+
return false;
2101+
}
2102+
if ( ! isset( $service_data['renews_on'] ) ) {
2103+
return false;
2104+
}
2105+
$renews_on = $service_data['renews_on'];
2106+
$timestamp_before_two_weeks = strtotime( '-2 weeks', $renews_on );
2107+
$today_timestamp = strtotime( 'today' );
2108+
2109+
if ( $timestamp_before_two_weeks < $today_timestamp ) {
2110+
return false;
2111+
}
2112+
2113+
$visitors_limit = isset( $service_data['visitors_limit'] ) ? (int) $service_data['visitors_limit'] : 0;
2114+
$visitors_left = isset( $service_data['visitors_left'] ) ? (int) $service_data['visitors_left'] : 0;
2115+
2116+
if ( ! $visitors_limit || ! $visitors_left ) {
2117+
return false;
2118+
}
2119+
2120+
$used_quota = $visitors_limit - $visitors_left;
2121+
$is_50_percent_used = ( $used_quota / $visitors_limit ) >= 0.5;
2122+
2123+
if ( ! $is_50_percent_used ) {
2124+
return false;
2125+
}
2126+
2127+
return true;
2128+
}
20722129
}

0 commit comments

Comments
 (0)