| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class CampaignBuilder |
| 6 |
{ |
| 7 |
use CampaignBuilderLegacy; |
| 8 |
|
| 9 |
/** |
| 10 |
* Build campaign Template. |
| 11 |
* |
| 12 |
* @return string |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* @var \Hurrytimer\Campaign |
| 17 |
*/ |
| 18 |
protected $campaign; |
| 19 |
|
| 20 |
public function __construct($campaign) |
| 21 |
{ |
| 22 |
$this->campaign = $campaign; |
| 23 |
$this->campaign->loadSettings(); |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns built template. |
| 29 |
* |
| 30 |
* @param string |
| 31 |
* @param boolean |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function build($content = '', $withConfig = true) |
| 36 |
{ |
| 37 |
$config = $this->getClientConfig(); |
| 38 |
$json = json_encode($config); |
| 39 |
$legacyClass = $this->legacyCampaignClass($this->campaign->getId()); |
| 40 |
if ($this->campaign->enableSticky === C::YES) { |
| 41 |
|
| 42 |
return "<div class='hurrytimer-sticky hurryt-loading hurrytimer-sticky-{$this->campaign->getId()}'><div class='hurrytimer-sticky-inner'>" |
| 43 |
. "<div class='{$legacyClass} hurrytimer-campaign hurrytimer-campaign-{$this->campaign->getId()}'" |
| 44 |
. "data-config='{$json}' >{$content}</div></div>" . $this->stickyBarCloseButton() . "</div>"; |
| 45 |
} |
| 46 |
return "<div class='{$legacyClass} hurrytimer-campaign hurryt-loading hurrytimer-campaign-{$this->campaign->getId()}'" |
| 47 |
. "data-config='{$json}' >{$content}</div>"; |
| 48 |
} |
| 49 |
|
| 50 |
public function template() |
| 51 |
{ |
| 52 |
|
| 53 |
$template = ($this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER |
| 54 |
? $this->headline() |
| 55 |
: '') |
| 56 |
. '<div class="' . $this->legacyTimerClass() . ' hurrytimer-timer"></div>' |
| 57 |
. ($this->campaign->headlinePosition == C::HEADLINE_POSITION_BELOW_TIMER |
| 58 |
? $this->headline() : '') |
| 59 |
. $this->callToActionButton(); |
| 60 |
|
| 61 |
|
| 62 |
return $template; |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns common client config. |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
private function commonClientConfig() |
| 72 |
{ |
| 73 |
return [ |
| 74 |
'id' => $this->campaign->getId(), |
| 75 |
'actions' => $this->campaign->actions, |
| 76 |
'template' => $this->timer(), |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
private function evergreenClientConfig() |
| 81 |
{ |
| 82 |
$evergreenCompaign = new EvergreenCampaign( |
| 83 |
$this->campaign->getId(), |
| 84 |
new CookieDetection(), |
| 85 |
new IPDetection() |
| 86 |
); |
| 87 |
|
| 88 |
return [ |
| 89 |
'isRegular' => false, |
| 90 |
'duration' => $this->campaign->getDurationInSeconds(), |
| 91 |
'reset' => $evergreenCompaign->reseting(), |
| 92 |
'restart' => $this->campaign->getRestart(), |
| 93 |
'endDate' => intval($evergreenCompaign->getEndDate()), |
| 94 |
'cookieName' => $evergreenCompaign->cookieName(), |
| 95 |
]; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns regular config. |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
private function regularClientConfig() |
| 105 |
{ |
| 106 |
|
| 107 |
return [ |
| 108 |
'isRegular' => true, |
| 109 |
'endDate' => date( |
| 110 |
'Y/m/d H:i:s', |
| 111 |
strtotime($this->campaign->getEndDatetime())), |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns client config for the compaign. |
| 117 |
* |
| 118 |
* @return array|null |
| 119 |
*/ |
| 120 |
public function getClientConfig() |
| 121 |
{ |
| 122 |
if ($this->campaign->isEvergreen()) { |
| 123 |
return array_merge($this->commonClientConfig(), $this->evergreenClientConfig()); |
| 124 |
|
| 125 |
} else { |
| 126 |
return array_merge($this->commonClientConfig(), $this->regularClientConfig()); |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns timer. |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function timer() |
| 137 |
{ |
| 138 |
$blocks = array_filter([ |
| 139 |
$this->daysBlock(), |
| 140 |
$this->hoursBlock(), |
| 141 |
$this->minutesBlock(), |
| 142 |
$this->secondsBlock(), |
| 143 |
]); |
| 144 |
|
| 145 |
$template = implode($this->separator(), $blocks); |
| 146 |
|
| 147 |
|
| 148 |
return $template; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns separator. |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
public function separator() |
| 158 |
{ |
| 159 |
return $this->campaign->blockSeparatorVisibility === C::YES |
| 160 |
? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">:</div>' |
| 161 |
: ''; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns days block. |
| 166 |
* |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
public function daysBlock() |
| 170 |
{ |
| 171 |
$label = $this->campaign->labels['days']; |
| 172 |
|
| 173 |
return $this->campaign->daysVisibility === C::YES |
| 174 |
? $this->block("%D", $label) |
| 175 |
: ''; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns hours block. |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
public function hoursBlock() |
| 184 |
{ |
| 185 |
$label = $this->campaign->labels['hours']; |
| 186 |
|
| 187 |
return $this->campaign->hoursVisibility === C::YES |
| 188 |
? $this->block("%H", $label) |
| 189 |
: ''; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns minutes block. |
| 194 |
* |
| 195 |
* @return string |
| 196 |
*/ |
| 197 |
public function minutesBlock() |
| 198 |
{ |
| 199 |
$label = $this->campaign->labels['minutes']; |
| 200 |
|
| 201 |
return $this->campaign->minutesVisibility === C::YES |
| 202 |
? $this->block("%M", $label) |
| 203 |
: ''; |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Returns seconds block. |
| 209 |
* |
| 210 |
* @return string |
| 211 |
*/ |
| 212 |
public function secondsBlock() |
| 213 |
{ |
| 214 |
|
| 215 |
$label = $this->campaign->labels['seconds']; |
| 216 |
|
| 217 |
return $this->campaign->secondsVisibility === C::YES |
| 218 |
? $this->block("%S", $label) |
| 219 |
: ''; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns block. |
| 225 |
* |
| 226 |
* @param $digitFormat |
| 227 |
* @param $label |
| 228 |
* |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
public function block($digitFormat, $label) |
| 232 |
{ |
| 233 |
return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">' |
| 234 |
. $this->digit($digitFormat) |
| 235 |
. $this->label($label) |
| 236 |
. '</div>'; |
| 237 |
} |
| 238 |
|
| 239 |
public function digit($format) |
| 240 |
{ |
| 241 |
return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format |
| 242 |
. '</div>'; |
| 243 |
} |
| 244 |
|
| 245 |
public function label($text) |
| 246 |
{ |
| 247 |
if ($this->campaign->labelVisibility === "no") { |
| 248 |
return ''; |
| 249 |
} |
| 250 |
|
| 251 |
return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >' |
| 252 |
. __($text, 'hurrytimer') . '</div>'; |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
public function headline() |
| 257 |
{ |
| 258 |
|
| 259 |
$headline = __($this->campaign->headline, 'hurrytimer'); |
| 260 |
|
| 261 |
return $this->campaign->headlineVisibility === C::YES |
| 262 |
? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">' |
| 263 |
. $headline . '</div>' |
| 264 |
: ''; |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
public function callToActionButton() |
| 269 |
{ |
| 270 |
if ($this->campaign->callToAction['enabled'] === C::NO) { |
| 271 |
return ''; |
| 272 |
} |
| 273 |
|
| 274 |
$cta_text = $this->campaign->callToAction['text']; |
| 275 |
|
| 276 |
$cta_url = $this->campaign->callToAction['url']; |
| 277 |
|
| 278 |
$target = $this->campaign->callToAction['new_tab'] === C::YES ? '_blank' : '_self'; |
| 279 |
$template= "<a class='hurrytimer-button' target='" . $target . "' href='" . $cta_url . "' >" . $cta_text . "</a>"; |
| 280 |
|
| 281 |
return "<div class='hurrytimer-button-wrap'>" . $template ."</div>"; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
public function stickyBarCloseButton() |
| 286 |
{ |
| 287 |
if ($this->campaign->stickyBarDismissible === C::NO || |
| 288 |
isset($_COOKIE['_dismissed_sticky_' . $this->campaign->getId()]) |
| 289 |
) { |
| 290 |
return ''; |
| 291 |
} |
| 292 |
|
| 293 |
if ($this->campaign->stickyBarDismissible === C::YES) { |
| 294 |
return '<button type="button" class="hurrytimer-sticky-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 357 357"> |
| 295 |
<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 |
| 296 |
214.2,178.5"/></svg></button>'; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
|