@@ -31,31 +31,45 @@ class LimitedOffers {
31
31
public $ wp_option_dismiss_notification_key_base = 'dismiss_themeisle_notice_event_ ' ;
32
32
33
33
/**
34
- * Offer Links
34
+ * Metadata for announcements.
35
35
*
36
- * @var array<string>
36
+ * @var array
37
37
*/
38
- public $ offer_metadata = array ();
38
+ public $ assets = array ();
39
39
40
40
/**
41
41
* Timeline for the offers.
42
42
*
43
- * @var array[]
43
+ * @var array
44
44
*/
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 ();
51
46
52
47
/**
53
48
* LimitedOffers constructor.
54
49
*/
55
50
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
+
56
57
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
+ }
59
73
}
60
74
} catch ( Exception $ e ) {
61
75
if ( defined ( 'WP_DEBUG ' ) && WP_DEBUG ) {
@@ -70,6 +84,11 @@ public function __construct() {
70
84
* @return void
71
85
*/
72
86
public function load_dashboard_hooks () {
87
+
88
+ if ( empty ( $ this ->assets ['globalNoticeUrl ' ] ) ) {
89
+ return ;
90
+ }
91
+
73
92
add_filter ( 'themeisle_products_deal_priority ' , array ( $ this , 'add_priority ' ) );
74
93
add_action ( 'admin_notices ' , array ( $ this , 'render_notice ' ) );
75
94
add_action ( 'wp_ajax_dismiss_themeisle_event_notice_otter ' , array ( $ this , 'disable_notification_ajax ' ) );
@@ -87,16 +106,19 @@ public function is_active() {
87
106
/**
88
107
* Activate the Black Friday deal.
89
108
*
109
+ * @param array $data Event data.
110
+ *
90
111
* @return void
91
112
*/
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
+ )
100
122
);
101
123
}
102
124
@@ -109,77 +131,6 @@ public function get_active_deal() {
109
131
return $ this ->active ;
110
132
}
111
133
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
-
183
134
/**
184
135
* Get the localized data for the plugin.
185
136
*
@@ -188,12 +139,10 @@ public function check_date_range( $start, $end ) {
188
139
public function get_localized_data () {
189
140
return array_merge (
190
141
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 (),
195
144
),
196
- $ this ->offer_metadata
145
+ $ this ->assets
197
146
);
198
147
}
199
148
@@ -262,7 +211,7 @@ public function render_notice() {
262
211
</svg>
263
212
<span>
264
213
<?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">
266
215
<?php esc_html_e ( 'Learn more ' , 'otter-blocks ' ); ?>
267
216
</a>
268
217
</span>
0 commit comments