PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.10.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.10.0
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / CampaignBuilder.php

CampaignBuilder.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.10.0, at includes/CampaignBuilder.php

492 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 use Hurrytimer\Dependencies\Carbon\Carbon;
6 use Exception;
7 use Hurrytimer\Placeholders\Placeholder_Factory;
8
9 class CampaignBuilder
10 {
11 use CampaignBuilderLegacy;
12
13 /**
14 * Build campaign Template.
15 *
16 * @return string
17 */
18
19 /**
20 * @var \Hurrytimer\Campaign
21 */
22 protected $campaign;
23
24 public function __construct( $campaign )
25 {
26 $this->campaign = $campaign;
27 $this->campaign->loadSettings();
28 }
29
30 /**
31 * Returns built template.
32 *
33 * @param string
34 * @param boolean
35 *
36 * @return string
37 */
38 public function build( $content = '', $options = [] )
39 {
40 $config = $this->getClientConfig( $options );
41 $json = htmlspecialchars( json_encode( $config ), ENT_QUOTES, 'UTF-8' );
42 $legacyClass = $this->legacyCampaignClass( $this->campaign->get_id() );
43
44 if ( $this->campaign->enableSticky === C::YES && $options[ 'sticky' ] ) {
45
46 return '<div class="hurrytimer-sticky hurryt-loading hurrytimer-sticky-' . $this->campaign->get_id() . '"><div class="hurrytimer-sticky-inner"><div class="' . $legacyClass . ' hurrytimer-campaign hurrytimer-campaign-' . $this->campaign->get_id() . '" data-config="' . $json . '" >' . $content . '</div></div>' . $this->stickyBarCloseButton()
47 . '</div>';
48
49 }
50
51 return '<div class="' . $legacyClass . ' hurrytimer-campaign hurryt-loading hurrytimer-campaign-' . $this->campaign->get_id() . '"'
52 . ' data-config="' . $json . '" >' . $content . '</div>';
53 }
54
55 /**
56 * The timer elements template.
57 */
58 public function template()
59 {
60
61 return ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER
62 ? $this->headline()
63 : '' )
64 . '<div class="' . $this->legacyTimerClass() . ' hurrytimer-timer"></div>'
65 . ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_BELOW_TIMER
66 ? $this->headline() : '' )
67 . $this->callToActionButton();
68
69 }
70
71 /**
72 * Returns common client config.
73 *
74 * @return array
75 */
76 private function commonClientConfig()
77 {
78
79 $actions = $this->campaign->actions;
80 foreach ( $actions as &$action ) {
81
82 $foceLineBreaks = apply_filters('hurryt_action_message_force_line_breaks', false);
83 $rawMessage = $action['message'];
84
85 if(!preg_match('#\<br(\s*)?\/?|<script|<style\>#i', $action['message'] ) || $foceLineBreaks ){
86 $action[ 'message' ] = nl2br( $action[ 'message' ]);
87 }
88
89 $action[ 'message' ] = do_shortcode( $action[ 'message' ] );
90
91 $action['message'] = apply_filters('hurryt_action_message', $action[ 'message' ], $this->campaign->get_id(), $rawMessage);
92
93 $action['coupon'] = apply_filters('hurryt_action_coupon', $action['coupon'], $this->campaign->get_id());
94 $action['redirectUrl'] = apply_filters('hurryt_action_redirect_url', $action['redirectUrl'], $this->campaign->get_id());
95
96 }
97
98 return [
99 'id' => $this->campaign->get_id(),
100 'product_ids'=> $this->campaign->getWcProductsSelection(),
101 'actions' => $actions,
102 'template' => $this->timer(),
103 'methods' => $this->campaign->detectionMethods,
104 'mode' => $this->campaign->get_mode_slug(),
105 'sticky_bar_hide_timeout' => apply_filters( 'sticky_bar_hide_timeout',
106 intval( $this->campaign->stickyBarDismissTimeout ), $this->campaign->get_id() ),
107 ];
108 }
109
110 private function evergreenClientConfig()
111 {
112 $evergreenCompaign = new EvergreenCampaign( $this->campaign->get_id() );
113 $evergreenCompaign->loadSettings();
114 return [
115 'isRegular' => false,
116 'restart_duration'=> $this->campaign->getRestartDuration(true),
117 'duration' => $this->campaign->durationInSeconds(),
118 'should_reset' => $evergreenCompaign->shouldResetTimer(),
119 'reset_token' => $evergreenCompaign->getInitiatedResetToken(),
120 'restart' => apply_filters( 'hurryt_evergreen_restart', $this->campaign->getRestart() ),
121 'endDate' => $evergreenCompaign->getEndDate(),
122 'cookieName' => Cookie_Detection::cookieName( $this->campaign->get_id() ),
123 'reload_reset' => $evergreenCompaign->reloadReset,
124 ];
125
126 }
127
128 /**
129 * Returns regular config.
130 *
131 * @return array
132 */
133 private function regularClientConfig()
134 {
135 try {
136 $endDate = Carbon::parse( $this->campaign->getEndDatetime(), hurryt_tz() )->getBrowserTimestamp();
137 if ( $this->campaign->is_recurring() ) {
138 $endDate = $this->campaign->getRecurrenceEndDate();
139 if ( $endDate ) {
140 $endDate = $endDate->getBrowserTimestamp();
141 } else {
142 $endDate = null;
143 }
144 }
145
146 $timeToNextRecurrence = $this->campaign->is_recurring() ? $this->campaign->timeToNextRecurrence() : 0;
147
148 return [
149 'recurr' => $this->campaign->is_recurring(),
150 'timeToNextRecurrence' => $timeToNextRecurrence,
151 'isRegular' => true,
152 'endDate' => $endDate,
153 ];
154
155 } catch ( Exception $e ) {
156 echo __( sprintf( 'HurryTimer Error: Invalid campaign (ID: %d). Please double check your settings.',
157 $this->campaign->get_id() ), 'hurrytimer' );
158 }
159 }
160
161 /**
162 * Returns client config for the compaign.
163 *
164 * @param array $options
165 * @return array|null
166 */
167 public function getClientConfig( $options = [] )
168 {
169 $config = $options;
170
171 if ( $this->campaign->is_evergreen() ) {
172 $config = array_merge( $config, $this->commonClientConfig(), $this->evergreenClientConfig() );
173
174 } else {
175 $config = array_merge( $config, $this->commonClientConfig(), $this->regularClientConfig() );
176 }
177
178 return $config;
179 }
180
181 /**
182 * Returns timer.
183 *
184 * @return string
185 */
186 public function timer()
187 {
188 $blocks = array_filter( [
189 $this->monthsBlock(),
190 $this->daysBlock(),
191 $this->hoursBlock(),
192 $this->minutesBlock(),
193 $this->secondsBlock(),
194 ] );
195
196 $template = implode( $this->separator(), $blocks );
197
198 $template = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_template",
199 $template, $this->campaign );
200
201 $template = apply_filters( "hurryt_timer_template", $template, $this->campaign->get_id() );
202
203
204 return $template;
205
206 }
207
208 /**
209 * Returns separator.
210 *
211 * @return string
212 */
213 public function separator()
214 {
215
216 $separator = apply_filters( 'hurryt_block_separator', ':', $this->campaign->get_id() );
217
218 return $this->campaign->blockSeparatorVisibility === C::YES
219 ? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">' . $separator . '</div>'
220 : '';
221 }
222
223 /**
224 * Returns days block.
225 *
226 * @return string
227 */
228 public function monthsBlock()
229 {
230 $label = $this->campaign->labels[ 'months' ];
231 $label = apply_filters( 'hurryt_months_label', $label, $this->campaign->get_id() );
232
233 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
234
235 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
236 $label.='%!m';
237 }
238
239 $directive = $zero_padded ? "%m": "%-m";
240 return $this->campaign->monthsVisibility === C::YES ? $this->block($directive , $label ) : '';
241 }
242
243
244 /**
245 * Returns days block.
246 *
247 * @return string
248 */
249 public function daysBlock()
250 {
251 $label = $this->campaign->labels[ 'days' ];
252
253 /**
254 * @deprecated Use `hurryt_days_label` instead.
255 */
256 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_days_label",
257 $label, $this->campaign );
258
259 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
260
261 $directive = $zero_padded ? '%D' : '%-D';
262 if ( $this->campaign->monthsVisibility == C::YES ) {
263 $directive = $zero_padded ? '%n' : '%-n';
264 }
265
266 $directive = apply_filters( 'hurryt_days_directive', $directive, $this->campaign->get_id() );
267 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
268 $label.='%!D';
269 }
270 $label = apply_filters( 'hurryt_days_label', $label, $this->campaign->get_id() );
271 return $this->campaign->daysVisibility === C::YES ? $this->block( $directive, $label ) : '';
272
273
274 }
275
276 /**
277 * Returns hours block.
278 *
279 * @return string
280 */
281 public function hoursBlock()
282 {
283 $label = $this->campaign->labels[ 'hours' ];
284
285 /**
286 * @deprecated Use `hurryt_hours_label` instead.
287 */
288
289 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_hours_label",
290 $label, $this->campaign );
291
292
293 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
294
295 $directive = $zero_padded ? '%H' : '%-H';
296
297 if ( $this->campaign->daysVisibility == C::NO ) {
298 $directive = $zero_padded ? '%I': '%-I';
299 }
300
301 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
302 $label.='%!H';
303 }
304
305 $label = apply_filters( 'hurryt_hours_label', $label, $this->campaign->get_id() );
306
307 return $this->campaign->hoursVisibility === C::YES ? $this->block($directive, $label ) : '';
308
309
310 }
311
312 /**
313 * Returns minutes block.
314 *
315 * @return string
316 */
317 public function minutesBlock()
318 {
319 $label = $this->campaign->labels[ 'minutes' ];
320
321 /** @deprecated Use `hurryt_minutes_label` instead. */
322 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_minutes_label",
323 $label, $this->campaign );
324
325
326 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
327
328 $directive = $zero_padded ?'%M': '%-M';
329
330 if ( $this->campaign->hoursVisibility == C::NO ) {
331 $directive = $zero_padded ? '%N': '%-N';
332 }
333
334 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
335 $label.='%!M';
336 }
337
338 $label = apply_filters( 'hurryt_minutes_label', $label, $this->campaign->get_id() );
339
340 return $this->campaign->minutesVisibility === C::YES
341 ? $this->block( $directive, $label )
342 : '';
343
344 }
345
346 /**
347 * Returns seconds block.
348 *
349 * @return string
350 */
351 public function secondsBlock()
352 {
353
354 $label = $this->campaign->labels[ 'seconds' ];
355
356 /** @deprecated Use `hurryt_seconds_label` instead. */
357 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_seconds_label",
358 $label, $this->campaign );
359
360
361 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
362
363 $directive = $zero_padded ?'%S': '%-S';
364
365 if ( $this->campaign->minutesVisibility == C::NO ) {
366 $directive = $zero_padded ? '%T': '%-T';
367 }
368
369 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
370 $label.='%!S';
371 }
372
373 $label = apply_filters( 'hurryt_seconds_label', $label, $this->campaign->get_id() );
374
375 return $this->campaign->secondsVisibility === C::YES
376 ? $this->block( $directive, $label )
377 : '';
378
379 }
380
381 /**
382 * Returns block.
383 *
384 * @param $digitFormat
385 * @param $label
386 *
387 * @return string
388 */
389 public function block( $digitFormat, $label )
390 {
391 return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">'
392 . $this->digit( $digitFormat )
393 . $this->label( $label )
394 . '</div>';
395 }
396
397 public function digit( $format )
398 {
399 return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format
400 . '</div>';
401 }
402
403 public function label( $text )
404 {
405 if ( $this->campaign->labelVisibility === "no" ) {
406 return '';
407 }
408
409 return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >'
410 . $text . '</div>';
411
412 }
413
414 public function headline()
415 {
416 $headline = $this->campaign->headline;
417
418 /**
419 * @deprecated Use `hurryt_campaign_headline` instead.
420 */
421 $headline = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_headline",
422 $headline, $this->campaign);
423
424 $headline = apply_filters( "hurryt_campaign_headline", $headline, $this->campaign->get_id());
425
426 $headline = nl2br( Placeholder_Factory::parse( $headline, $this->campaign ) );
427
428 $headline = do_shortcode($headline);
429
430 return $this->campaign->headlineVisibility === C::YES
431 ? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">'
432 . $headline . '</div>'
433 : '';
434
435 }
436
437 public function callToActionButton()
438 {
439 if ( $this->campaign->callToAction[ 'enabled' ] === C::NO ) {
440 return '';
441 }
442
443 $cta_text = $this->campaign->callToAction[ 'text' ];
444
445 $cta_url = $this->campaign->callToAction[ 'url' ];
446
447 /** @deprecated Use `hurryt_cta_text` instead. */
448 $cta_text = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_cta_text",
449 $cta_text, $this->campaign );
450
451 $cta_text = apply_filters('hurryt_cta_text', $cta_text, $this->campaign->get_id());
452
453 /** @deprecated Use `hurryt_cta_url` instead. */
454 $cta_url = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_cta_url", $cta_url,
455 $this->campaign );
456
457 $cta_url = apply_filters('hurryt_cta_url', $cta_url, $this->campaign->get_id());
458
459
460 $target = $this->campaign->callToAction[ 'new_tab' ] === C::YES ? '_blank' : '_self';
461 $template = "<a class='hurrytimer-button' target='" . $target . "' href='" . $cta_url
462 . "' >" . $cta_text . "</a>";
463
464 /** @deprecated Use `hurryt_cta_template` instead. */
465 $template = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_cta_template",
466 $template, $this->campaign );
467
468 $template = apply_filters('hurryt_cta_template', $template, $this->campaign->get_id());
469
470 return "<div class='hurrytimer-button-wrap'>" . $template . "</div>";
471
472 }
473
474 public function stickyBarCloseButton()
475 {
476 if ( $this->campaign->stickyBarDismissible === C::NO
477 || isset( $_COOKIE[ '_dismissed_sticky_' . $this->campaign->get_id() ] )
478 ) {
479 return '';
480 }
481
482 if ( $this->campaign->stickyBarDismissible === C::YES ) {
483 return '<button type="button" class="hurrytimer-sticky-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 357 357">
484 <polygon points="357,35.7 321.3,0 178.5,142.8 35.7,0 0,35.7 142.8,178.5 0,321.3 35.7,357 178.5,214.2 321.3,357 357,321.3
485 214.2,178.5"/></svg></button>';
486
487 }
488
489 }
490
491 }
492