PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / trunk
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce vtrunk
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 trunk, at includes/CampaignBuilder.php

501 lines 15.8 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 /**
111 * Returns client config for evergreen mode.
112 *
113 * @return array
114 */
115 private function evergreenClientConfig()
116 {
117 $evergreenCompaign = new EvergreenCampaign( $this->campaign->get_id() );
118 $evergreenCompaign->loadSettings();
119 return [
120 'isRegular' => false,
121 'restart_duration'=> $this->campaign->getRestartDuration(true),
122 'duration' => $this->campaign->durationInSeconds(),
123 'evergreenEndType' => $this->campaign->getEvergreenEndType(),
124 'evergreenEndTime' => $this->campaign->getEvergreenEndTime(),
125 'evergreenEndDay' => $this->campaign->getEvergreenEndDay(),
126 'should_reset' => $evergreenCompaign->shouldResetTimer(),
127 'reset_token' => $evergreenCompaign->getInitiatedResetToken(),
128 'restart' => apply_filters( 'hurryt_evergreen_restart', $this->campaign->getRestart() ),
129 'endDate' => $evergreenCompaign->getEndDate(),
130 'cookieName' => Cookie_Detection::cookieName( $this->campaign->get_id() ),
131 'reload_reset' => $evergreenCompaign->reloadReset,
132 ];
133
134 return $config;
135 }
136
137 /**
138 * Returns regular config.
139 *
140 * @return array
141 */
142 private function regularClientConfig()
143 {
144 try {
145 $endDate = Carbon::parse( $this->campaign->getEndDatetime(), hurryt_tz($this->campaign->timezone) )->getBrowserTimestamp();
146 if ( $this->campaign->is_recurring() ) {
147 $endDate = $this->campaign->getRecurrenceEndDate();
148 if ( $endDate ) {
149 $endDate = $endDate->getBrowserTimestamp();
150 } else {
151 $endDate = null;
152 }
153 }
154
155 $timeToNextRecurrence = $this->campaign->is_recurring() ? $this->campaign->timeToNextRecurrence() : 0;
156
157 $config = [
158 'recurr' => $this->campaign->is_recurring(),
159 'timeToNextRecurrence' => $timeToNextRecurrence,
160 'isRegular' => true,
161 'endDate' => $endDate,
162 ];
163
164 return $config;
165
166 } catch ( Exception $e ) {
167 error_log($e->getMessage());
168 throw new Exception($e->getMessage());
169 }
170 }
171
172 /**
173 * Returns client config for the compaign.
174 *
175 * @param array $options
176 * @return array|null
177 */
178 public function getClientConfig( $options = [] )
179 {
180 $config = $options;
181 if ( $this->campaign->is_evergreen() ) {
182 $config = array_merge( $config, $this->commonClientConfig(), $this->evergreenClientConfig() );
183 } else {
184 $config = array_merge( $config, $this->commonClientConfig(), $this->regularClientConfig() );
185 }
186
187 return $config;
188 }
189
190 /**
191 * Returns timer.
192 *
193 * @return string
194 */
195 public function timer()
196 {
197 $blocks = array_filter( [
198 $this->monthsBlock(),
199 $this->daysBlock(),
200 $this->hoursBlock(),
201 $this->minutesBlock(),
202 $this->secondsBlock(),
203 ] );
204
205 $template = implode( $this->separator(), $blocks );
206
207 $template = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_template",
208 $template, $this->campaign );
209
210 $template = apply_filters( "hurryt_timer_template", $template, $this->campaign->get_id() );
211
212
213 return $template;
214
215 }
216
217 /**
218 * Returns separator.
219 *
220 * @return string
221 */
222 public function separator()
223 {
224
225 $separator = apply_filters( 'hurryt_block_separator', ':', $this->campaign->get_id() );
226
227 return $this->campaign->blockSeparatorVisibility === C::YES
228 ? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">' . $separator . '</div>'
229 : '';
230 }
231
232 /**
233 * Returns days block.
234 *
235 * @return string
236 */
237 public function monthsBlock()
238 {
239 $label = $this->campaign->labels[ 'months' ];
240 $label = apply_filters( 'hurryt_months_label', $label, $this->campaign->get_id() );
241
242 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
243
244 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
245 $label.='%!m';
246 }
247
248 $directive = $zero_padded ? "%m": "%-m";
249 return $this->campaign->monthsVisibility === C::YES ? $this->block($directive , $label ) : '';
250 }
251
252
253 /**
254 * Returns days block.
255 *
256 * @return string
257 */
258 public function daysBlock()
259 {
260 $label = $this->campaign->labels[ 'days' ];
261
262 /**
263 * @deprecated Use `hurryt_days_label` instead.
264 */
265 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_days_label",
266 $label, $this->campaign );
267
268 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
269
270 $directive = $zero_padded ? '%D' : '%-D';
271 if ( $this->campaign->monthsVisibility == C::YES ) {
272 $directive = $zero_padded ? '%n' : '%-n';
273 }
274
275 $directive = apply_filters( 'hurryt_days_directive', $directive, $this->campaign->get_id() );
276 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
277 $label.='%!D';
278 }
279 $label = apply_filters( 'hurryt_days_label', $label, $this->campaign->get_id() );
280 return $this->campaign->daysVisibility === C::YES ? $this->block( $directive, $label ) : '';
281
282
283 }
284
285 /**
286 * Returns hours block.
287 *
288 * @return string
289 */
290 public function hoursBlock()
291 {
292 $label = $this->campaign->labels[ 'hours' ];
293
294 /**
295 * @deprecated Use `hurryt_hours_label` instead.
296 */
297
298 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_hours_label",
299 $label, $this->campaign );
300
301
302 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
303
304 $directive = $zero_padded ? '%H' : '%-H';
305
306 if ( $this->campaign->daysVisibility == C::NO ) {
307 $directive = $zero_padded ? '%I': '%-I';
308 }
309
310 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
311 $label.='%!H';
312 }
313
314 $label = apply_filters( 'hurryt_hours_label', $label, $this->campaign->get_id() );
315
316 return $this->campaign->hoursVisibility === C::YES ? $this->block($directive, $label ) : '';
317
318
319 }
320
321 /**
322 * Returns minutes block.
323 *
324 * @return string
325 */
326 public function minutesBlock()
327 {
328 $label = $this->campaign->labels[ 'minutes' ];
329
330 /** @deprecated Use `hurryt_minutes_label` instead. */
331 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_minutes_label",
332 $label, $this->campaign );
333
334
335 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
336
337 $directive = $zero_padded ?'%M': '%-M';
338
339 if ( $this->campaign->hoursVisibility == C::NO ) {
340 $directive = $zero_padded ? '%N': '%-N';
341 }
342
343 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
344 $label.='%!M';
345 }
346
347 $label = apply_filters( 'hurryt_minutes_label', $label, $this->campaign->get_id() );
348
349 return $this->campaign->minutesVisibility === C::YES
350 ? $this->block( $directive, $label )
351 : '';
352
353 }
354
355 /**
356 * Returns seconds block.
357 *
358 * @return string
359 */
360 public function secondsBlock()
361 {
362
363 $label = $this->campaign->labels[ 'seconds' ];
364
365 /** @deprecated Use `hurryt_seconds_label` instead. */
366 $label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_seconds_label",
367 $label, $this->campaign );
368
369
370 $zero_padded = (bool)apply_filters('hurrytimer_zero_padded_digits', true, $this->campaign->get_id());
371
372 $directive = $zero_padded ?'%S': '%-S';
373
374 if ( $this->campaign->minutesVisibility == C::NO ) {
375 $directive = $zero_padded ? '%T': '%-T';
376 }
377
378 if( apply_filters('hurrytimer_auto_pluralize', false ) ){
379 $label.='%!S';
380 }
381
382 $label = apply_filters( 'hurryt_seconds_label', $label, $this->campaign->get_id() );
383
384 return $this->campaign->secondsVisibility === C::YES
385 ? $this->block( $directive, $label )
386 : '';
387
388 }
389
390 /**
391 * Returns block.
392 *
393 * @param $digitFormat
394 * @param $label
395 *
396 * @return string
397 */
398 public function block( $digitFormat, $label )
399 {
400 return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">'
401 . $this->digit( $digitFormat )
402 . $this->label( $label )
403 . '</div>';
404 }
405
406 public function digit( $format )
407 {
408 return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format
409 . '</div>';
410 }
411
412 public function label( $text )
413 {
414 if ( $this->campaign->labelVisibility === "no" ) {
415 return '';
416 }
417
418 return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >'
419 . $text . '</div>';
420
421 }
422
423 public function headline()
424 {
425 $headline = $this->campaign->headline;
426
427 /**
428 * @deprecated Use `hurryt_campaign_headline` instead.
429 */
430 $headline = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_headline",
431 $headline, $this->campaign);
432
433 $headline = apply_filters( "hurryt_campaign_headline", $headline, $this->campaign->get_id());
434
435 $headline = nl2br( Placeholder_Factory::parse( $headline, $this->campaign ) );
436
437 $headline = do_shortcode($headline);
438
439 return $this->campaign->headlineVisibility === C::YES
440 ? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">'
441 . $headline . '</div>'
442 : '';
443
444 }
445
446 public function callToActionButton()
447 {
448 if ( $this->campaign->callToAction[ 'enabled' ] === C::NO ) {
449 return '';
450 }
451
452 $cta_text = $this->campaign->callToAction[ 'text' ];
453
454 $cta_url = $this->campaign->callToAction[ 'url' ];
455
456 /** @deprecated Use `hurryt_cta_text` instead. */
457 $cta_text = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_cta_text",
458 $cta_text, $this->campaign );
459
460 $cta_text = apply_filters('hurryt_cta_text', $cta_text, $this->campaign->get_id());
461
462 /** @deprecated Use `hurryt_cta_url` instead. */
463 $cta_url = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_cta_url", $cta_url,
464 $this->campaign );
465
466 $cta_url = apply_filters('hurryt_cta_url', $cta_url, $this->campaign->get_id());
467
468
469 $target = $this->campaign->callToAction[ 'new_tab' ] === C::YES ? '_blank' : '_self';
470 $template = "<a class='hurrytimer-button' target='" . esc_attr($target) . "' href='" . esc_url($cta_url)
471 . "' >" . wp_kses_post($cta_text) . "</a>";
472
473 /** @deprecated Use `hurryt_cta_template` instead. */
474 $template = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_cta_template",
475 $template, $this->campaign );
476
477 $template = apply_filters('hurryt_cta_template', $template, $this->campaign->get_id());
478
479 return "<div class='hurrytimer-button-wrap'>" . $template . "</div>";
480
481 }
482
483 public function stickyBarCloseButton()
484 {
485 if ( $this->campaign->stickyBarDismissible === C::NO
486 || isset( $_COOKIE[ '_dismissed_sticky_' . $this->campaign->get_id() ] )
487 ) {
488 return '';
489 }
490
491 if ( $this->campaign->stickyBarDismissible === C::YES ) {
492 return '<button type="button" class="hurrytimer-sticky-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 357 357">
493 <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
494 214.2,178.5"/></svg></button>';
495
496 }
497
498 }
499
500 }
501