PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.16
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.16
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
← All changes | includes/CampaignBuilder.php +71 -243 trunk2.2.16 View file →
@@ -1,11 +1,10 @@
1 1 <?php
2 2
3 3 namespace Hurrytimer;
4 4
5 -use Hurrytimer\Dependencies\Carbon\Carbon;
5 +use Carbon\Carbon;
6 6 use Exception;
7 -use Hurrytimer\Placeholders\Placeholder_Factory;
8 7
9 8 class CampaignBuilder
10 9 {
11 10 use CampaignBuilderLegacy;
@@ -34,39 +33,39 @@
34 33 * @param boolean
35 34 *
36 35 * @return string
37 36 */
38 - public function build( $content = '', $options = [] )
37 + public function build( $content = '', $withConfig = true )
39 38 {
40 - $config = $this->getClientConfig( $options );
41 - $json = htmlspecialchars( json_encode( $config ), ENT_QUOTES, 'UTF-8' );
42 - $legacyClass = $this->legacyCampaignClass( $this->campaign->get_id() );
39 + $config = $this->getClientConfig();
40 + $json = json_encode( $config );
41 + $legacyClass = $this->legacyCampaignClass( $this->campaign->getId() );
42 + if ( $this->campaign->enableSticky === C::YES ) {
43 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 -
44 + return "<div class='hurrytimer-sticky hurryt-loading hurrytimer-sticky-{$this->campaign->getId()}'><div class='hurrytimer-sticky-inner'>"
45 + . "<div class='{$legacyClass} hurrytimer-campaign hurrytimer-campaign-{$this->campaign->getId()}'"
46 + . "data-config='{$json}' >{$content}</div></div>" . $this->stickyBarCloseButton()
47 + . "</div>";
49 48 }
50 49
51 - return '<div class="' . $legacyClass . ' hurrytimer-campaign hurryt-loading hurrytimer-campaign-' . $this->campaign->get_id() . '"'
52 - . ' data-config="' . $json . '" >' . $content . '</div>';
50 + return "<div class='{$legacyClass} hurrytimer-campaign hurryt-loading hurrytimer-campaign-{$this->campaign->getId()}'"
51 + . "data-config='{$json}' >{$content}</div>";
53 52 }
54 53
55 - /**
56 - * The timer elements template.
57 - */
58 54 public function template()
59 55 {
60 56
61 - return ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER
57 + $template = ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER
62 58 ? $this->headline()
63 59 : '' )
64 - . '<div class="' . $this->legacyTimerClass() . ' hurrytimer-timer"></div>'
65 - . ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_BELOW_TIMER
60 + . '<div class="' . $this->legacyTimerClass() . ' hurrytimer-timer"></div>'
61 + . ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_BELOW_TIMER
66 62 ? $this->headline() : '' )
67 - . $this->callToActionButton();
63 + . $this->callToActionButton();
68 64
65 +
66 + return $template;
67 +
69 68 }
70 69
71 70 /**
72 71 * Returns common client config.
@@ -74,65 +73,33 @@
74 73 * @return array
75 74 */
76 75 private function commonClientConfig()
77 76 {
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 77 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() ),
78 + 'id' => $this->campaign->getId(),
79 + 'actions' => $this->campaign->actions,
80 + 'template' => $this->timer(),
107 81 ];
108 82 }
109 83
110 - /**
111 - * Returns client config for evergreen mode.
112 - *
113 - * @return array
114 - */
115 84 private function evergreenClientConfig()
116 85 {
117 - $evergreenCompaign = new EvergreenCampaign( $this->campaign->get_id() );
118 - $evergreenCompaign->loadSettings();
86 + $evergreenCompaign = new EvergreenCampaign(
87 + $this->campaign->getId(),
88 + new CookieDetection(),
89 + new IPDetection()
90 + );
91 +
119 92 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,
93 + 'isRegular' => false,
94 + 'duration' => $this->campaign->durationArrayToSeconds(),
95 + 'reset' => $evergreenCompaign->reseting(),
96 + 'restart' => apply_filters( 'hurryt_evergreen_restart',
97 + $this->campaign->getRestart() ),
98 + 'endDate' => intval( $evergreenCompaign->getEndDate() ),
99 + 'cookieName' => $evergreenCompaign->cookieName(),
132 100 ];
133 101
134 - return $config;
135 102 }
136 103
137 104 /**
138 105 * Returns regular config.
@@ -141,11 +108,12 @@
141 108 */
142 109 private function regularClientConfig()
143 110 {
144 111 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();
112 + $endDate = Carbon::parse( $this->campaign->getEndDatetime(), hurryt_tz() )
113 + ->getBrowserTimestamp();
114 + if ( $this->campaign->isRecurring() ) {
115 + $endDate = $this->campaign->getRecurringDeadline();
148 116 if ( $endDate ) {
149 117 $endDate = $endDate->getBrowserTimestamp();
150 118 } else {
151 119 $endDate = null;
@@ -151,22 +119,22 @@
151 119 $endDate = null;
152 120 }
153 121 }
154 122
155 - $timeToNextRecurrence = $this->campaign->is_recurring() ? $this->campaign->timeToNextRecurrence() : 0;
123 + $timeToNextRecurrence = $this->campaign->isRecurring()
124 + ? $this->campaign->getTimeToNextRecurrence()
125 + : 0;
156 126
157 - $config = [
158 - 'recurr' => $this->campaign->is_recurring(),
127 + return [
128 + 'recurr' => $this->campaign->isRecurring(),
159 129 'timeToNextRecurrence' => $timeToNextRecurrence,
160 130 'isRegular' => true,
161 131 'endDate' => $endDate,
162 132 ];
163 133
164 - return $config;
165 -
166 134 } catch ( Exception $e ) {
167 - error_log($e->getMessage());
168 - throw new Exception($e->getMessage());
135 + echo __( sprintf( 'HurryTimer Error: Invalid campaign (ID: %d). Please double check your settings.',
136 + $this->campaign->getId() ), 'hurrytimer' );
169 137 }
170 138 }
171 139
172 140 /**
@@ -171,21 +139,19 @@
171 139
172 140 /**
173 141 * Returns client config for the compaign.
174 142 *
175 - * @param array $options
176 143 * @return array|null
177 144 */
178 - public function getClientConfig( $options = [] )
145 + public function getClientConfig()
179 146 {
180 - $config = $options;
181 - if ( $this->campaign->is_evergreen() ) {
182 - $config = array_merge( $config, $this->commonClientConfig(), $this->evergreenClientConfig() );
147 + if ( $this->campaign->isEvergreen() ) {
148 + return array_merge( $this->commonClientConfig(), $this->evergreenClientConfig() );
149 +
183 150 } else {
184 - $config = array_merge( $config, $this->commonClientConfig(), $this->regularClientConfig() );
151 + return array_merge( $this->commonClientConfig(), $this->regularClientConfig() );
152 +
185 153 }
186 -
187 - return $config;
188 154 }
189 155
190 156 /**
191 157 * Returns timer.
@@ -194,9 +160,8 @@
194 160 */
195 161 public function timer()
196 162 {
197 163 $blocks = array_filter( [
198 - $this->monthsBlock(),
199 164 $this->daysBlock(),
200 165 $this->hoursBlock(),
201 166 $this->minutesBlock(),
202 167 $this->secondsBlock(),
@@ -203,14 +168,9 @@
203 168 ] );
204 169
205 170 $template = implode( $this->separator(), $blocks );
206 171
207 - $template = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_template",
208 - $template, $this->campaign );
209 172
210 - $template = apply_filters( "hurryt_timer_template", $template, $this->campaign->get_id() );
211 -
212 -
213 173 return $template;
214 174
215 175 }
216 176
@@ -220,13 +180,10 @@
220 180 * @return string
221 181 */
222 182 public function separator()
223 183 {
224 -
225 - $separator = apply_filters( 'hurryt_block_separator', ':', $this->campaign->get_id() );
226 -
227 184 return $this->campaign->blockSeparatorVisibility === C::YES
228 - ? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">' . $separator . '</div>'
185 + ? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">:</div>'
229 186 : '';
230 187 }
231 188
232 189 /**
@@ -233,54 +190,15 @@
233 190 * Returns days block.
234 191 *
235 192 * @return string
236 193 */
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 194 public function daysBlock()
259 195 {
260 196 $label = $this->campaign->labels[ 'days' ];
261 197
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 -
198 + return $this->campaign->daysVisibility === C::YES
199 + ? $this->block( "%D", $label )
200 + : '';
283 201 }
284 202
285 203 /**
286 204 * Returns hours block.
@@ -290,33 +208,11 @@
290 208 public function hoursBlock()
291 209 {
292 210 $label = $this->campaign->labels[ 'hours' ];
293 211
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 -
212 + return $this->campaign->hoursVisibility === C::YES
213 + ? $this->block( "%H", $label )
214 + : '';
319 215 }
320 216
321 217 /**
322 218 * Returns minutes block.
@@ -326,29 +222,10 @@
326 222 public function minutesBlock()
327 223 {
328 224 $label = $this->campaign->labels[ 'minutes' ];
329 225
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 226 return $this->campaign->minutesVisibility === C::YES
350 - ? $this->block( $directive, $label )
227 + ? $this->block( "%M", $label )
351 228 : '';
352 229
353 230 }
354 231
@@ -360,30 +237,11 @@
360 237 public function secondsBlock()
361 238 {
362 239
363 240 $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 241
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 242 return $this->campaign->secondsVisibility === C::YES
385 - ? $this->block( $directive, $label )
243 + ? $this->block( "%S", $label )
386 244 : '';
387 245
388 246 }
389 247
@@ -397,17 +255,17 @@
397 255 */
398 256 public function block( $digitFormat, $label )
399 257 {
400 258 return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">'
401 - . $this->digit( $digitFormat )
402 - . $this->label( $label )
403 - . '</div>';
259 + . $this->digit( $digitFormat )
260 + . $this->label( $label )
261 + . '</div>';
404 262 }
405 263
406 264 public function digit( $format )
407 265 {
408 266 return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format
409 - . '</div>';
267 + . '</div>';
410 268 }
411 269
412 270 public function label( $text )
413 271 {
@@ -415,31 +273,20 @@
415 273 return '';
416 274 }
417 275
418 276 return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >'
419 - . $text . '</div>';
277 + . __( $text, 'hurrytimer' ) . '</div>';
420 278
421 279 }
422 280
423 281 public function headline()
424 282 {
425 - $headline = $this->campaign->headline;
426 283
427 - /**
428 - * @deprecated Use `hurryt_campaign_headline` instead.
429 - */
430 - $headline = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_headline",
431 - $headline, $this->campaign);
284 + $headline = __( $this->campaign->headline, 'hurrytimer' );
432 285
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 286 return $this->campaign->headlineVisibility === C::YES
440 287 ? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">'
441 - . $headline . '</div>'
288 + . $headline . '</div>'
442 289 : '';
443 290
444 291 }
445 292
@@ -452,31 +299,12 @@
452 299 $cta_text = $this->campaign->callToAction[ 'text' ];
453 300
454 301 $cta_url = $this->campaign->callToAction[ 'url' ];
455 302
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 303 $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>";
304 + $template = "<a class='hurrytimer-button' target='" . $target . "' href='" . $cta_url
305 + . "' >" . $cta_text . "</a>";
472 306
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 307 return "<div class='hurrytimer-button-wrap'>" . $template . "</div>";
480 308
481 309 }
482 310
@@ -482,9 +310,9 @@
482 310
483 311 public function stickyBarCloseButton()
484 312 {
485 313 if ( $this->campaign->stickyBarDismissible === C::NO
486 - || isset( $_COOKIE[ '_dismissed_sticky_' . $this->campaign->get_id() ] )
314 + || isset( $_COOKIE[ '_dismissed_sticky_' . $this->campaign->getId() ] )
487 315 ) {
488 316 return '';
489 317 }
490 318