forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialOffersSnippet.class.php
334 lines (299 loc) · 8.38 KB
/
SpecialOffersSnippet.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* containst SpecialOffersSnippet class definition
*/
/**
* Manages the integration of the special offers snippet
* @author etessore
* @version 1.0.4
* @package classes
*/
/*
* Changelog:
* 1.0.4
* new javascript implementation: FB.CrossCom.consume();
* 1.0.3
* remove dependency from FeatureWithAsset and used ThemeHelpers::load_js()
* 1.0.2
* Fixed Notice: wp_enqueue_script was called incorrectly
* 1.0.1
* Added support for multiple iframes
* 1.0.0
* Initial Release
*/
class SpecialOffersSnippet {
const baseurl = 'http://hotelsitecontents.fastbooking.com/router.php';
const default_divdest = 'FB_so';
const com_js = 'http://hotelsitecontents.fastbooking.com/js/fb.js';
const pop_js = 'http://hotelsitecontents.fastbooking.com/js/pop.js';
/**
* @var array stores the list of parameter to be passed on GET
*/
public $params;
/**
* @var string stores the url of the snippet
*/
private $url;
/**
* @var int the index of snippet occurency
*/
public $index;
/**
* @var SubstitutionTemplate the template to be substituted
*/
public $templates;
//private $assets = array('js', 'css');
/**
* Initializes the snippet
* @param string $hid the hotel hid
*/
public function __construct($hid){
$tpl = <<< EOF
%pre%
%comjstag%
%popjstag%
<div class="offers-container">
<div id="%divdest%"%option_divdest%>
<div class="loading">%loading%</div>
</div>
%controls%
<div class="">
<a hrref="javascript:;" class="offers-toggler"></a>
</div>
</div>
%promojstag%
%post%
EOF;
$this->templates = new SubstitutionTemplate();
$this->templates
->set_tpl($tpl)
->set_markup('option_divdest', '')
->set_markup('post', '')
->set_markup('pre', '')
->set_markup('controls', '')
->set_markup('loading', __('Loading Offers...', 'theme'));
$this
->add_param('snippet', 'promotion')
->add_param('hid', $hid)
->add_param('divdest', self::default_divdest);
$this->index = 0;
if(!is_admin()){
//ThemeHelpers::load_js('snippet-com');
}
}
/**
* Builds the url for the remote iframe
* @param array $params additional query params for the iframe
*/
public static function calculate_url($params=array()){
$url_arr = parse_url(self::baseurl);
$url_arr['query'] =
(empty($url_arr['query']))
? http_build_query($params)
: $url_arr['query'].'&'.http_build_query($params);
return self::build_url($url_arr);
}
/**
* Retrieves the url from array $url_arr
* If defined use http_build_url, else custom code
* @param array $url_arr the array of attributes
* @see http://php.net/manual/en/function.parse-url.php
*/
private static function build_url($url_arr){
if(function_exists('http_build_url')){
return http_build_url($url_arr);
} else {
$scheme = isset($url_arr['scheme']) ? $url_arr['scheme'] . '://' : '';
$host = isset($url_arr['host']) ? $url_arr['host'] : '';
$port = isset($url_arr['port']) ? ':' . $url_arr['port'] : '';
$user = isset($url_arr['user']) ? $url_arr['user'] : '';
$pass = isset($url_arr['pass']) ? ':' . $url_arr['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($url_arr['path']) ? $url_arr['path'] : '';
$query = isset($url_arr['query']) ? '?' . $url_arr['query'] : '';
$fragment = isset($url_arr['fragment']) ? '#' . $url_arr['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
}
/**
* Adds a new parameter or update if exists
* @param string $key
* @param int|string $value
* @return SpecialOffersSnippet $this for chainability
*/
public function add_param($key, $value){
$this->params[$key] = $value;
return $this;
}
/**
* Enables the offers to cycle
* @return SpecialOffersSnippet $this for chainability
*/
public function enable_cycle(){
$this->templates->set_markup('option_divdest', ' class="cycle"');
ThemeHelpers::load_js('offers-cycle');
return $this;
}
/**
* Disables the offers to cycle
* @return SpecialOffersSnippet $this for chainability
*/
public function disable_cycle(){
$this->templates->set_markup('option_divdest', '');
return $this;
}
/**
* Uses Fancybox instead of the default popup system
* @return SpecialOffersSnippet $this for chainability
*/
public function enable_fancybox(){
ThemeHelpers::load_js('jquery-fancybox');
ThemeHelpers::load_css('jquery-fancybox');
$content = <<< EOF
function FBso_popin(id) {
$.fancybox('<div id="FB_so"><div id="popinFBso_conteneur"><div id="popinFBso_contenu">'+$('#'+id).html()+'</div></div></div>', {width: 705, autoSize: false});
}
EOF;
$this->templates->set_markup('post', HtmlHelper::script($content));
return $this;
}
/**
* Adds 3 controls: nex pause\play and prev.
* @param string $markup
* @return SpecialOffersSnippet $this for chainability
*/
public function show_controls($markup=''){
ThemeHelpers::load_css('controls');
$markup = empty($markup)
? '
<div class="controls">
<ul>
<li class="prev"></li>
<li class="play-pause pause"></li>
<li class="next"></li>
</ul>
</div>
'
: $markup;
$this->templates->set_markup('controls', $markup);
return $this;
}
/**
* Retrives the $key param for this snippet
* @param string $key name of the parameter
*/
public function get_param($key){
return $this->params[$key];
}
/**
* Removes a param
* @param string $key the param to be deleted
* @return SpecialOffersSnippet $this for chainability
*/
public function del_param($key){
unset($this->params[$key]);
return $this;
}
/**
* Retrieves the src of the iframe
* @return string the url
*/
public function get_iframe_src(){
return self::calculate_url($this->params);
}
/**
* Calculates the divdest for the current iframe integration
* First time it will be self::default_divdest
* For the other an "_<number>" will be appended
* If the parameter divdest is customized it will return that one
*/
protected function get_divdest(){
if(substr($this->get_param('divdest'), 0, strlen(self::default_divdest)) == self::default_divdest){
if($this->index == 0){
return self::default_divdest;
} else {
return self::default_divdest.'_'.$this->index;
}
}
return $this->get_param('divdest');
}
/**
* Retrieves the script tag for com.js
*
* Checks if is the first implementation, otherwhise it will return an empty string
* @return string html script tag to com.js
*/
protected function get_com_js(){
if($this->index == 1)
return
HtmlHelper::script(
'/* */',
array(
'src' => self::com_js,
'id' => 'script_com_js'
)
);
return '';
}
/**
* Retrieves the script for initialize the snippet system
* @return string html script tag
*/
protected function get_promo_js(){
$content = new SubstitutionTemplate();
$content
->set_tpl('FB.CrossCom.consume("%url%", "%divedest%");')
->set_markup('url', $this->get_iframe_src())
->set_markup('divedest', $this->get_param('divdest'));
return
HtmlHelper::script(
$content->replace_markup(),
array(
'id'=>'iframe_'.$this->get_param('divdest')
)
);
}
/**
* Gets the pop.js for the 'ugly pop-up' feature
* @return string
*/
protected function get_pop_js(){
if($this->index == 1)
return
HtmlHelper::script(
'/* */',
array(
'src' => self::pop_js,
'id' => 'script_com_js'
)
);
return '';
}
/**
* Retrieves the markup for the offers
*/
public function get_markup(){
$this->add_param('divdest', $this->get_divdest());
$this->index++;
return $this->templates
//->set_markup('iframe', $this->get_iframe_src())
->set_markup('divdest', $this->get_param('divdest'))
->set_markup('comjstag', $this->get_com_js())
->set_markup('popjstag', $this->get_pop_js())
->set_markup('promojstag', $this->get_promo_js())
->replace_markup();
}
/**
* Prints the markup for the offers
*/
public function the_markup(){
echo $this->get_markup();
}
/**
* Prints the iframe src
*/
public function the_iframe_src(){
print($this->get_iframe_src());
return $this;
}
}