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