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