forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialOffersSnippetCached.class.php
151 lines (134 loc) · 4.46 KB
/
SpecialOffersSnippetCached.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
<?php
/**
* Class SpecialOffersSnippedCached
* Manages the cache subsystem for the special offers snippet
* @author etessore
* @version 1.0.0
* @package classes
*/
class SpecialOffersSnippetCached extends SpecialOffersSnippet{
/**
* Prefix of the transient where we store the html markup
*/
const TRANSIENT_CACHE = 'SO_cache_';
/**
* Prefix of the transient where we store the last update unix timestamp
*/
const TRANSIENT_LAST_UPDATE = 'SO_lastupdate_';
/**
* Stores the default transient expiration: 60 * 60 * 24 s = 1 day
*/
const TRANSIENT_EXPIRATION = 86400;
/**
* @var string stores the HID
*/
public $hid;
/**
* @var string stores the unique id of the cache
*/
protected $unique;
public function __construct($hid){
$this->hid = $hid;
parent::__construct($hid);
$this->tpl = <<< EOF
%pre%
<div class="offers-container">
<div id="%divdest%"%option_divdest%>
%offers_list%
</div>
%controls%
<div class="">
<a href="javascript:;" class="offers-toggler"></a>
</div>
</div>
%jsfix%
%post%
EOF;
$this->templates
->set_tpl($this->tpl)
->set_markup('jsfix', '');
}
protected function get_uniq(){
if(is_null($this->unique))
$this->unique = md5(http_build_query($this->params));
return $this->unique;
}
/**
* Purges the cache, fetches new data and stores them into the cache
*/
public function refresh(){
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $this->get_iframe_src());
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($handler);
curl_close($handler);
preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches);
set_transient(self::TRANSIENT_CACHE.$this->get_uniq(), $matches[1], self::TRANSIENT_EXPIRATION);
set_transient(self::TRANSIENT_LAST_UPDATE.$this->get_uniq(), time());
}
/**
* Prints some javascript to simulate the iframe
* loaded event while serving the cache
*/
public function javascript_fix(){
add_action('wp_footer', array(&$this, 'on_footer_scripts'));
//return HtmlHelper::script('', array('src'=>'http://hotelsitecontents.fastbooking.com/js/fb.js'));
$toret = <<<EOF
var FB = FB || {};
FB.Loader = {};
FB.Loader.eventType = {
BEFORE_LOADING: 0,
AFTER_LOADING: 1
};
FB.Loader.Events = [];
FB.Loader.attachEvent = function (e, t) {
this.Events.push(t);
};
EOF;
return HtmlHelper::script($toret);
}
/**
* Adds some javascripts to maintain compatibility with iframe style implementation
* It's called by Wordpress on footer
*/
public function on_footer_scripts(){
?>
<script id="cachecd-offers-loader-js-fix">
jQuery(document).ready(function(){
jQuery.each(FB.Loader.Events, function(index, element){
if(typeof(element.onNotify != 'undefined')){
element.onNotify();
}
});
});
</script>
<?php
}
/**
* Retrieves the markup for the offers
* @return string html markup
*/
public function get_markup(){
$cache = get_transient(self::TRANSIENT_CACHE.$this->get_uniq());
if(!isset($_REQUEST['refresh']) && $cache){
if(class_exists('TestManager')){
TestManager::get_instance()->add_parameter('offersSnippetCacheAge',time()-get_transient(self::TRANSIENT_LAST_UPDATE.$this->get_uniq()));
}
return $this->templates
->set_markup('offers_list', $cache)
->set_markup('divdest', $this->get_param('divdest'))
->set_markup('jsfix', $this->javascript_fix())
->replace_markup();
} else {
$this->refresh();
$offers = new parent($this->hid);
foreach($this->params as $k => $v){
$offers->add_param($k, $v);
}
foreach($this->templates->static_markup as $k => $v){
$offers->templates->set_markup($k, $v);
}
return $offers->get_markup();
}
}
}