PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.0.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.0.0
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.0.0, at includes/CampaignBuilder.php

243 lines 5.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 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 return "<div class='{$legacyClass} hurrytimer-campaign hurrytimer-campaign-{$this->campaign->getId()}'"
41 . "data-config='{$json}' >{$content}</div>";
42 }
43
44 public function template()
45 {
46 return
47 ($this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER
48 ? $this->headline()
49 : '')
50 . '<div class="' . $this->legacyTimerClass() . ' hurrytimer-timer"></div>'
51 . ($this->campaign->headlinePosition == C::HEADLINE_POSITION_BELOW_TIMER
52 ? $this->headline() : '');
53 }
54
55 /**
56 * Returns common client config.
57 *
58 * @return array
59 */
60 private function commonClientConfig()
61 {
62 return [
63 'id' => $this->campaign->getId(),
64 'actions' => $this->campaign->actions,
65 'template' => $this->timer(),
66 ];
67 }
68
69 private function evergreenClientConfig()
70 {
71 $evergreenCompaign = new EvergreenCampaign(
72 $this->campaign->getId(),
73 new CookieDetection(),
74 new IPDetection()
75 );
76
77 return [
78 'isRegular' => false,
79 'duration' => $this->campaign->getDurationInSeconds(),
80 'reset' => $evergreenCompaign->reseting(),
81 'restart' => $this->campaign->getRestart(),
82 'endDate' => $evergreenCompaign->getEndDate(),
83 'cookieName' => $evergreenCompaign->cookieName(),
84 ];
85
86 }
87
88 /**
89 * Returns regular config.
90 *
91 * @return array
92 */
93 private function regularClientConfig()
94 {
95
96 return [
97 'isRegular' => true,
98 'endDate' => date(
99 'Y/m/d H:i:s',
100 strtotime($this->campaign->getEndDatetime())),
101 ];
102 }
103
104 /**
105 * Returns client config for the compaign.
106 *
107 * @return array|null
108 */
109 public function getClientConfig()
110 {
111
112
113 if ($this->campaign->isEvergreen()) {
114 return array_merge($this->commonClientConfig(), $this->evergreenClientConfig());
115
116 } else {
117 return array_merge($this->commonClientConfig(), $this->regularClientConfig());
118
119 }
120 }
121
122 /**
123 * Returns timer.
124 *
125 * @return string
126 */
127 public function timer()
128 {
129 $blocks = array_filter([
130 $this->daysBlock(),
131 $this->hoursBlock(),
132 $this->minutesBlock(),
133 $this->secondsBlock(),
134 ]);
135
136 return implode($this->separator(), $blocks);
137
138 }
139
140 /**
141 * Returns separator.
142 *
143 * @return string
144 */
145 public function separator()
146 {
147 return $this->campaign->blockSeparatorVisibility === C::YES
148 ? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">:</div>'
149 : '';
150 }
151
152 /**
153 * Returns days block.
154 *
155 * @return string
156 */
157 public function daysBlock()
158 {
159 return $this->campaign->daysVisibility === C::YES
160 ? $this->block("%D", $this->campaign->labels['days'])
161 : '';
162 }
163
164 /**
165 * Returns hours block.
166 *
167 * @return string
168 */
169 public function hoursBlock()
170 {
171 return $this->campaign->hoursVisibility === C::YES
172 ? $this->block("%H", $this->campaign->labels['hours'])
173 : '';
174 }
175
176 /**
177 * Returns minutes block.
178 *
179 * @return string
180 */
181 public function minutesBlock()
182 {
183 return $this->campaign->minutesVisibility === C::YES
184 ? $this->block("%M", $this->campaign->labels['minutes'])
185 : '';
186
187 }
188
189 /**
190 * Returns seconds block.
191 *
192 * @return string
193 */
194 public function secondsBlock()
195 {
196 return $this->campaign->secondsVisibility === C::YES
197 ? $this->block("%S", $this->campaign->labels['seconds'])
198 : '';
199
200 }
201
202 /**
203 * Returns block.
204 *
205 * @param $digitFormat
206 * @param $label
207 *
208 * @return string
209 */
210 public function block($digitFormat, $label)
211 {
212 return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">'
213 . $this->digit($digitFormat)
214 . $this->label($label)
215 . '</div>';
216 }
217
218 public function digit($format)
219 {
220 return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format
221 . '</div>';
222 }
223
224 public function label($text)
225 {
226 if ($this->campaign->labelVisibility === "no") {
227 return '';
228 }
229
230 return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >'
231 . __($text, 'hurrytimer') . '</div>';
232
233 }
234
235 public function headline()
236 {
237 return $this->campaign->headlineVisibility === C::YES
238 ? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">'
239 . __($this->campaign->headline, 'hurrytimer') . '</div>'
240 : '';
241 }
242 }
243