| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
use 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 |
$template = ( $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 |
return $template; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns common client config. |
| 76 |
* |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
private function commonClientConfig() |
| 80 |
{ |
| 81 |
|
| 82 |
$actions = $this->campaign->actions; |
| 83 |
foreach ( $actions as &$action ) { |
| 84 |
$action[ 'message' ] = apply_filters( 'hurryt_action_message', $action[ 'message' ], |
| 85 |
$this->campaign->get_id() ); |
| 86 |
|
| 87 |
$action[ 'message' ] = nl2br( do_shortcode( $action[ 'message' ] ) ); |
| 88 |
} |
| 89 |
|
| 90 |
return [ |
| 91 |
'id' => $this->campaign->get_id(), |
| 92 |
'actions' => $actions, |
| 93 |
'template' => $this->timer(), |
| 94 |
'mode' => $this->campaign->get_mode_slug(), |
| 95 |
'sticky_bar_hide_timeout' => apply_filters( 'sticky_bar_hide_timeout', |
| 96 |
intval( $this->campaign->stickyBarDismissTimeout ), $this->campaign->get_id() ) |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
private function evergreenClientConfig() |
| 101 |
{ |
| 102 |
$evergreenCompaign = new EvergreenCampaign( |
| 103 |
$this->campaign->get_id(), |
| 104 |
new CookieDetection(), |
| 105 |
new IPDetection() |
| 106 |
); |
| 107 |
$evergreenCompaign->loadSettings(); |
| 108 |
|
| 109 |
return [ |
| 110 |
'isRegular' => false, |
| 111 |
'duration' => $this->campaign->duration_to_sec(), |
| 112 |
'should_reset' => $evergreenCompaign->should_reset(), |
| 113 |
'reset_token' => get_post_meta( $evergreenCompaign->get_id(), EvergreenCampaign::RESET_FLAG, true ), |
| 114 |
'restart' => apply_filters( 'hurryt_evergreen_restart', $this->campaign->getRestart() ), |
| 115 |
'endDate' => $evergreenCompaign->getEndDate(), |
| 116 |
'cookieName' => $evergreenCompaign->cookieName(), |
| 117 |
'reload_reset' => $evergreenCompaign->reloadReset |
| 118 |
]; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns regular config. |
| 124 |
* |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
private function regularClientConfig() |
| 128 |
{ |
| 129 |
try { |
| 130 |
$endDate = Carbon::parse( $this->campaign->getEndDatetime(), hurryt_tz() ) |
| 131 |
->getBrowserTimestamp(); |
| 132 |
if ( $this->campaign->is_recurring() ) { |
| 133 |
$endDate = $this->campaign->get_current_recurrence_end_date(); |
| 134 |
if ( $endDate ) { |
| 135 |
$endDate = $endDate->getBrowserTimestamp(); |
| 136 |
} else { |
| 137 |
$endDate = null; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$timeToNextRecurrence = $this->campaign->is_recurring() |
| 142 |
? $this->campaign->get_time_to_next_recurrence() |
| 143 |
: 0; |
| 144 |
|
| 145 |
return [ |
| 146 |
'recurr' => $this->campaign->is_recurring(), |
| 147 |
'timeToNextRecurrence' => $timeToNextRecurrence, |
| 148 |
'isRegular' => true, |
| 149 |
'endDate' => $endDate, |
| 150 |
]; |
| 151 |
|
| 152 |
} catch ( Exception $e ) { |
| 153 |
echo __( sprintf( 'HurryTimer Error: Invalid campaign (ID: %d). Please double check your settings.', |
| 154 |
$this->campaign->get_id() ), 'hurrytimer' ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns client config for the compaign. |
| 160 |
* |
| 161 |
* @return array|null |
| 162 |
*/ |
| 163 |
public function getClientConfig( $options = [] ) |
| 164 |
{ |
| 165 |
$config = $options; |
| 166 |
|
| 167 |
if ( $this->campaign->is_evergreen() ) { |
| 168 |
$config = array_merge( $config, $this->commonClientConfig(), $this->evergreenClientConfig() ); |
| 169 |
|
| 170 |
} else { |
| 171 |
$config = array_merge( $config, $this->commonClientConfig(), $this->regularClientConfig() ); |
| 172 |
} |
| 173 |
|
| 174 |
return $config; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns timer. |
| 179 |
* |
| 180 |
* @return string |
| 181 |
*/ |
| 182 |
public function timer() |
| 183 |
{ |
| 184 |
$blocks = array_filter( [ |
| 185 |
$this->daysBlock(), |
| 186 |
$this->hoursBlock(), |
| 187 |
$this->minutesBlock(), |
| 188 |
$this->secondsBlock(), |
| 189 |
] ); |
| 190 |
|
| 191 |
$template = implode( $this->separator(), $blocks ); |
| 192 |
|
| 193 |
|
| 194 |
$template = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_template", |
| 195 |
$template, $this->campaign ); |
| 196 |
|
| 197 |
$template = apply_filters( "hurryt_timer_template", $template, $this->campaign->get_id() ); |
| 198 |
|
| 199 |
|
| 200 |
return $template; |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns separator. |
| 206 |
* |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
public function separator() |
| 210 |
{ |
| 211 |
|
| 212 |
$separator = apply_filters( 'hurryt_block_separator', ':', $this->campaign->get_id() ); |
| 213 |
|
| 214 |
return $this->campaign->blockSeparatorVisibility === C::YES |
| 215 |
? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">' . $separator . '</div>' |
| 216 |
: ''; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns days block. |
| 221 |
* |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
public function daysBlock() |
| 225 |
{ |
| 226 |
$label = __( $this->campaign->labels[ 'days' ], 'hurrytimer' ); |
| 227 |
$label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_days_label", |
| 228 |
$label, $this->campaign ); |
| 229 |
|
| 230 |
$label = apply_filters( 'hurryt_days_label', $label, $this->campaign->get_id() ); |
| 231 |
return $this->campaign->daysVisibility === C::YES |
| 232 |
? $this->block( "%D", $label ) |
| 233 |
: ''; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns hours block. |
| 238 |
* |
| 239 |
* @return string |
| 240 |
*/ |
| 241 |
public function hoursBlock() |
| 242 |
{ |
| 243 |
$label = __( $this->campaign->labels[ 'hours' ], 'hurrytimer' ); |
| 244 |
|
| 245 |
$label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_hours_label", |
| 246 |
$label, $this->campaign ); |
| 247 |
|
| 248 |
$label = apply_filters( 'hurryt_hours_label', $label, $this->campaign->get_id() ); |
| 249 |
|
| 250 |
return $this->campaign->hoursVisibility === C::YES |
| 251 |
? $this->block( "%H", $label ) |
| 252 |
: ''; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Returns minutes block. |
| 257 |
* |
| 258 |
* @return string |
| 259 |
*/ |
| 260 |
public function minutesBlock() |
| 261 |
{ |
| 262 |
$label = __( $this->campaign->labels[ 'minutes' ], 'hurrytimer' ); |
| 263 |
|
| 264 |
$label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_minutes_label", |
| 265 |
$label, $this->campaign ); |
| 266 |
|
| 267 |
$label = apply_filters( 'hurryt_minutes_label', $label, $this->campaign->get_id() ); |
| 268 |
|
| 269 |
return $this->campaign->minutesVisibility === C::YES |
| 270 |
? $this->block( "%M", $label ) |
| 271 |
: ''; |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Returns seconds block. |
| 277 |
* |
| 278 |
* @return string |
| 279 |
*/ |
| 280 |
public function secondsBlock() |
| 281 |
{ |
| 282 |
|
| 283 |
$label = __( $this->campaign->labels[ 'seconds' ], 'hurrytimer' ); |
| 284 |
|
| 285 |
$label = apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_timer_seconds_label", |
| 286 |
$label, $this->campaign ); |
| 287 |
|
| 288 |
$label = apply_filters( 'hurryt_seconds_label', $label, $this->campaign->get_id() ); |
| 289 |
|
| 290 |
return $this->campaign->secondsVisibility === C::YES |
| 291 |
? $this->block( "%S", $label ) |
| 292 |
: ''; |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Returns block. |
| 298 |
* |
| 299 |
* @param $digitFormat |
| 300 |
* @param $label |
| 301 |
* |
| 302 |
* @return string |
| 303 |
*/ |
| 304 |
public function block( $digitFormat, $label ) |
| 305 |
{ |
| 306 |
return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">' |
| 307 |
. $this->digit( $digitFormat ) |
| 308 |
. $this->label( $label ) |
| 309 |
. '</div>'; |
| 310 |
} |
| 311 |
|
| 312 |
public function digit( $format ) |
| 313 |
{ |
| 314 |
return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format |
| 315 |
. '</div>'; |
| 316 |
} |
| 317 |
|
| 318 |
public function label( $text ) |
| 319 |
{ |
| 320 |
if ( $this->campaign->labelVisibility === "no" ) { |
| 321 |
return ''; |
| 322 |
} |
| 323 |
|
| 324 |
return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >' |
| 325 |
. __( $text, 'hurrytimer' ) . '</div>'; |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
public function headline() |
| 330 |
{ |
| 331 |
|
| 332 |
$headline = __( $this->campaign->headline, 'hurrytimer' ); |
| 333 |
|
| 334 |
/** |
| 335 |
* Instead of this filter |
| 336 |
* @deprecated |
| 337 |
*/ |
| 338 |
$headline = __( apply_filters( "hurryt_{$this->campaign->get_id()}_campaign_headline", |
| 339 |
$headline, $this->campaign ), 'hurrytimer' ); |
| 340 |
|
| 341 |
/** |
| 342 |
* use this one. |
| 343 |
*/ |
| 344 |
$headline = __( apply_filters( "hurryt_campaign_headline", |
| 345 |
$headline, $this->campaign->get_id() ), 'hurrytimer' ); |
| 346 |
|
| 347 |
$headline = nl2br( Placeholder_Factory::parse( $headline, $this->campaign ) ); |
| 348 |
|
| 349 |
return $this->campaign->headlineVisibility === C::YES |
| 350 |
? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">' |
| 351 |
. $headline . '</div>' |
| 352 |
: ''; |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
public function callToActionButton() |
| 357 |
{ |
| 358 |
if ( $this->campaign->callToAction[ 'enabled' ] === C::NO ) { |
| 359 |
return ''; |
| 360 |
} |
| 361 |
|
| 362 |
$cta_text = $this->campaign->callToAction[ 'text' ]; |
| 363 |
|
| 364 |
$cta_url = $this->campaign->callToAction[ 'url' ]; |
| 365 |
|
| 366 |
$target = $this->campaign->callToAction[ 'new_tab' ] === C::YES ? '_blank' : '_self'; |
| 367 |
$template = "<a class='hurrytimer-button' target='" . $target . "' href='" . $cta_url |
| 368 |
. "' >" . $cta_text . "</a>"; |
| 369 |
|
| 370 |
return "<div class='hurrytimer-button-wrap'>" . $template . "</div>"; |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
public function stickyBarCloseButton() |
| 375 |
{ |
| 376 |
if ( $this->campaign->stickyBarDismissible === C::NO |
| 377 |
|| isset( $_COOKIE[ '_dismissed_sticky_' . $this->campaign->get_id() ] ) |
| 378 |
) { |
| 379 |
return ''; |
| 380 |
} |
| 381 |
|
| 382 |
if ( $this->campaign->stickyBarDismissible === C::YES ) { |
| 383 |
return '<button type="button" class="hurrytimer-sticky-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 357 357"> |
| 384 |
<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 |
| 385 |
214.2,178.5"/></svg></button>'; |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
|