Skip to content

Commit 77ee06f

Browse files
Merge pull request #2063 from Codeinwp/feat/black-friday-2024
Prepare for Black Friday 2024
2 parents a59152f + a346804 commit 77ee06f

File tree

2 files changed

+48
-99
lines changed

2 files changed

+48
-99
lines changed

inc/plugins/class-limited-offers.php

+46-97
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,45 @@ class LimitedOffers {
3131
public $wp_option_dismiss_notification_key_base = 'dismiss_themeisle_notice_event_';
3232

3333
/**
34-
* Offer Links
34+
* Metadata for announcements.
3535
*
36-
* @var array<string>
36+
* @var array
3737
*/
38-
public $offer_metadata = array();
38+
public $assets = array();
3939

4040
/**
4141
* Timeline for the offers.
4242
*
43-
* @var array[]
43+
* @var array
4444
*/
45-
public $timelines = array(
46-
'bf' => array(
47-
'start' => '2023-11-20 00:00:00',
48-
'end' => '2023-11-27 23:59:00',
49-
),
50-
);
45+
public $announcements = array();
5146

5247
/**
5348
* LimitedOffers constructor.
5449
*/
5550
public function __construct() {
51+
$this->announcements = apply_filters( 'themeisle_sdk_announcements', array() );
52+
53+
if ( empty( $this->announcements ) || ! is_array( $this->announcements ) ) {
54+
return;
55+
}
56+
5657
try {
57-
if ( $this->is_deal_active( 'bf' ) ) {
58-
$this->activate_bff();
58+
foreach ( $this->announcements as $announcement => $event_data ) {
59+
if ( false !== strpos( $announcement, 'black_friday' ) ) {
60+
if (
61+
empty( $event_data ) ||
62+
! is_array( $event_data ) ||
63+
empty( $event_data['active'] ) ||
64+
empty( $event_data['otter_dashboard_url'] ) ||
65+
! isset( $event_data['urgency_text'] )
66+
) {
67+
continue;
68+
}
69+
70+
$this->active = $announcement;
71+
$this->prepare_black_friday_assets( $event_data );
72+
}
5973
}
6074
} catch ( Exception $e ) {
6175
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
@@ -70,6 +84,11 @@ public function __construct() {
7084
* @return void
7185
*/
7286
public function load_dashboard_hooks() {
87+
88+
if ( empty( $this->assets['globalNoticeUrl'] ) ) {
89+
return;
90+
}
91+
7392
add_filter( 'themeisle_products_deal_priority', array( $this, 'add_priority' ) );
7493
add_action( 'admin_notices', array( $this, 'render_notice' ) );
7594
add_action( 'wp_ajax_dismiss_themeisle_event_notice_otter', array( $this, 'disable_notification_ajax' ) );
@@ -87,16 +106,19 @@ public function is_active() {
87106
/**
88107
* Activate the Black Friday deal.
89108
*
109+
* @param array $data Event data.
110+
*
90111
* @return void
91112
*/
92-
public function activate_bff() {
93-
$this->active = 'bf';
94-
95-
$this->offer_metadata = array(
96-
'bannerUrl' => OTTER_BLOCKS_URL . 'assets/images/black-friday-banner.png',
97-
'bannerAlt' => 'Otter Black Friday Sale',
98-
'linkDashboard' => tsdk_utmify( 'https://themeisle.com/plugins/otter-blocks/blackfriday/', 'blackfridayltd23', 'dashboard' ),
99-
'linkGlobal' => tsdk_utmify( 'https://themeisle.com/plugins/otter-blocks/blackfriday/', 'blackfridayltd23', 'globalnotice' ),
113+
public function prepare_black_friday_assets( $data ) {
114+
$this->assets = array_merge(
115+
$this->assets,
116+
array(
117+
'bannerUrl' => OTTER_BLOCKS_URL . 'assets/images/black-friday-banner.png',
118+
'bannerAlt' => 'Otter Black Friday Sale',
119+
'bannerStoreUrl' => esc_url_raw( $data['otter_dashboard_url'] ),
120+
'urgencyText' => esc_html( $data['urgency_text'] ),
121+
)
100122
);
101123
}
102124

@@ -109,77 +131,6 @@ public function get_active_deal() {
109131
return $this->active;
110132
}
111133

112-
/**
113-
* Check if the deal is active with the given slug.
114-
*
115-
* @param string $slug Slug of the deal.
116-
*
117-
* @throws Exception When date is invalid.
118-
*/
119-
public function is_deal_active( $slug ) {
120-
121-
if ( empty( $slug ) || ! array_key_exists( $slug, $this->timelines ) ) {
122-
return false;
123-
}
124-
125-
return $this->check_date_range( $this->timelines[ $slug ]['start'], $this->timelines[ $slug ]['end'] );
126-
}
127-
128-
/**
129-
* Get the remaining time for the deal in a human readable format.
130-
*
131-
* @param string $slug Slug of the deal.
132-
* @return string Remaining time for the deal.
133-
*/
134-
public function get_remaining_time_for_deal( $slug ) {
135-
if ( empty( $slug ) || ! array_key_exists( $slug, $this->timelines ) ) {
136-
return '';
137-
}
138-
139-
try {
140-
$end_date = new DateTime( $this->timelines[ $slug ]['end'], new DateTimeZone( 'GMT' ) );
141-
$current_date = new DateTime( 'now', new DateTimeZone( 'GMT' ) );
142-
$diff = $end_date->diff( $current_date );
143-
144-
if ( 0 < $diff->days ) {
145-
return 1 === $diff->days ? $diff->format( '%a day' ) : $diff->format( '%a days' );
146-
}
147-
148-
if ( 0 < $diff->h ) {
149-
return 1 === $diff->h ? $diff->format( '%h hour' ) : $diff->format( '%h hours' );
150-
}
151-
152-
if ( 0 < $diff->i ) {
153-
return 1 === $diff->i ? $diff->format( '%i minute' ) : $diff->format( '%i minutes' );
154-
}
155-
156-
return 1 === $diff->s ? $diff->format( '%s second' ) : $diff->format( '%s seconds' );
157-
} catch ( Exception $e ) {
158-
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
159-
error_log( $e->getMessage() ); // phpcs:ignore
160-
}
161-
}
162-
163-
return '';
164-
}
165-
166-
/**
167-
* Check if the current date is in the range of the offer.
168-
*
169-
* @param string $start Start date.
170-
* @param string $end End date.
171-
*
172-
* @throws Exception When date is invalid.
173-
*/
174-
public function check_date_range( $start, $end ) {
175-
176-
$start_date = new DateTime( $start, new DateTimeZone( 'GMT' ) );
177-
$end_date = new DateTime( $end, new DateTimeZone( 'GMT' ) );
178-
$current_date = new DateTime( 'now', new DateTimeZone( 'GMT' ) );
179-
180-
return $start_date <= $current_date && $current_date <= $end_date;
181-
}
182-
183134
/**
184135
* Get the localized data for the plugin.
185136
*
@@ -188,12 +139,10 @@ public function check_date_range( $start, $end ) {
188139
public function get_localized_data() {
189140
return array_merge(
190141
array(
191-
'active' => $this->is_active(),
192-
'dealSlug' => $this->get_active_deal(),
193-
'remainingTime' => $this->get_remaining_time_for_deal( $this->get_active_deal() ),
194-
'urgencyText' => 'Hurry Up! Only ' . $this->get_remaining_time_for_deal( $this->get_active_deal() ) . ' left',
142+
'active' => $this->is_active(),
143+
'dealSlug' => $this->get_active_deal(),
195144
),
196-
$this->offer_metadata
145+
$this->assets
197146
);
198147
}
199148

@@ -262,7 +211,7 @@ public function render_notice() {
262211
</svg>
263212
<span>
264213
<?php echo wp_kses_post( $message ); ?>
265-
<a href="<?php echo esc_url( ! empty( $this->offer_metadata['linkGlobal'] ) ? $this->offer_metadata['linkGlobal'] : '' ); ?>" target="_blank" rel="external noreferrer noopener">
214+
<a href="<?php echo esc_url( ! empty( $this->assets['globalNoticeUrl'] ) ? $this->assets['globalNoticeUrl'] : '' ); ?>" target="_blank" rel="external noreferrer noopener">
266215
<?php esc_html_e( 'Learn more', 'otter-blocks' ); ?>
267216
</a>
268217
</span>

src/dashboard/components/Main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ const Main = ({
8484
return (
8585
<Fragment>
8686
{
87-
window.otterObj.deal.active && (
87+
( window.otterObj?.deal?.active && window.otterObj.deal?.bannerStoreUrl ) && (
8888
<Deal
89-
link={window.otterObj.deal.linkDashboard}
89+
link={window.otterObj.deal.bannerStoreUrl}
9090
image={window.otterObj.deal.bannerUrl}
9191
alt={window.otterObj.deal.bannerAlt}
9292
urgencyText={window.otterObj.deal.urgencyText}

0 commit comments

Comments
 (0)