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
hurrytimer / includes / CampaignBuilder.php

CampaignBuilder.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.2.16, at includes/CampaignBuilder.php

329 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 use Carbon\Carbon;
6 use Exception;
7
8 class CampaignBuilder
9 {
10 use CampaignBuilderLegacy;
11
12 /**
13 * Build campaign Template.
14 *
15 * @return string
16 */
17
18 /**
19 * @var \Hurrytimer\Campaign
20 */
21 protected $campaign;
22
23 public function __construct( $campaign )
24 {
25 $this->campaign = $campaign;
26 $this->campaign->loadSettings();
27 }
28
29 /**
30 * Returns built template.
31 *
32 * @param string
33 * @param boolean
34 *
35 * @return string
36 */
37 public function build( $content = '', $withConfig = true )
38 {
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()}'"
51 . "data-config='{$json}' >{$content}</div>";
52 }
53
54 public function template()
55 {
56
57 $template = ( $this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER
58 ? $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
68 }
69
70 /**
71 * Returns common client config.
72 *
73 * @return array
74 */
75 private function commonClientConfig()
76 {
77 return [
78 'id' => $this->campaign->getId(),
79 'actions' => $this->campaign->actions,
80 'template' => $this->timer(),
81 ];
82 }
83
84 private function evergreenClientConfig()
85 {
86 $evergreenCompaign = new EvergreenCampaign(
87 $this->campaign->getId(),
88 new CookieDetection(),
89 new IPDetection()
90 );
91
92 return [
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(),
100 ];
101
102 }
103
104 /**
105 * Returns regular config.
106 *
107 * @return array
108 */
109 private function regularClientConfig()
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 }
122
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 }
138 }
139
140 /**
141 * Returns client config for the compaign.
142 *
143 * @return array|null
144 */
145 public function getClientConfig()
146 {
147 if ( $this->campaign->isEvergreen() ) {
148 return array_merge( $this->commonClientConfig(), $this->evergreenClientConfig() );
149
150 } else {
151 return array_merge( $this->commonClientConfig(), $this->regularClientConfig() );
152
153 }
154 }
155
156 /**
157 * Returns timer.
158 *
159 * @return string
160 */
161 public function timer()
162 {
163 $blocks = array_filter( [
164 $this->daysBlock(),
165 $this->hoursBlock(),
166 $this->minutesBlock(),
167 $this->secondsBlock(),
168 ] );
169
170 $template = implode( $this->separator(), $blocks );
171
172
173 return $template;
174
175 }
176
177 /**
178 * Returns separator.
179 *
180 * @return string
181 */
182 public function separator()
183 {
184 return $this->campaign->blockSeparatorVisibility === C::YES
185 ? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">:</div>'
186 : '';
187 }
188
189 /**
190 * Returns days block.
191 *
192 * @return string
193 */
194 public function daysBlock()
195 {
196 $label = $this->campaign->labels[ 'days' ];
197
198 return $this->campaign->daysVisibility === C::YES
199 ? $this->block( "%D", $label )
200 : '';
201 }
202
203 /**
204 * Returns hours block.
205 *
206 * @return string
207 */
208 public function hoursBlock()
209 {
210 $label = $this->campaign->labels[ 'hours' ];
211
212 return $this->campaign->hoursVisibility === C::YES
213 ? $this->block( "%H", $label )
214 : '';
215 }
216
217 /**
218 * Returns minutes block.
219 *
220 * @return string
221 */
222 public function minutesBlock()
223 {
224 $label = $this->campaign->labels[ 'minutes' ];
225
226 return $this->campaign->minutesVisibility === C::YES
227 ? $this->block( "%M", $label )
228 : '';
229
230 }
231
232 /**
233 * Returns seconds block.
234 *
235 * @return string
236 */
237 public function secondsBlock()
238 {
239
240 $label = $this->campaign->labels[ 'seconds' ];
241
242 return $this->campaign->secondsVisibility === C::YES
243 ? $this->block( "%S", $label )
244 : '';
245
246 }
247
248 /**
249 * Returns block.
250 *
251 * @param $digitFormat
252 * @param $label
253 *
254 * @return string
255 */
256 public function block( $digitFormat, $label )
257 {
258 return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">'
259 . $this->digit( $digitFormat )
260 . $this->label( $label )
261 . '</div>';
262 }
263
264 public function digit( $format )
265 {
266 return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format
267 . '</div>';
268 }
269
270 public function label( $text )
271 {
272 if ( $this->campaign->labelVisibility === "no" ) {
273 return '';
274 }
275
276 return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >'
277 . __( $text, 'hurrytimer' ) . '</div>';
278
279 }
280
281 public function headline()
282 {
283
284 $headline = __( $this->campaign->headline, 'hurrytimer' );
285
286 return $this->campaign->headlineVisibility === C::YES
287 ? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">'
288 . $headline . '</div>'
289 : '';
290
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
328 }
329