This repository was archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckfrontWidget.php
More file actions
241 lines (218 loc) · 8.16 KB
/
CheckfrontWidget.php
File metadata and controls
241 lines (218 loc) · 8.16 KB
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
<?php
/**
* Checkfront Widget
* PHP 5-7
*
* @package CheckfrontWidget
* @version 3.0
* @author Checkfront <code@checkfront.com>
* @copyright 2008-2016 Checkfront Inc
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link http://www.checkfront.com/developers/
* @link https://github.com/Checkfront/PHP-Widget
*
*
* This makes use of the Checkfront embeddable widgets. It allows you
* to embed a booking widget into any website, allows you to customize
* the look and feel, and takes care of sizing, caching etc.
*
* If the booking page is hosted under SSL and e-commerce is enabled
* the customer will stay on your site for the duration of the booking
* unless your are using Paypal or Google Checkout.
*
*
* This class is designed to provide a quick way of integrating Checkfront
* into a website. If you wish to further extend the platform, consider
* using the Checkfront SDK and API instead:
* http://api.checkfront.com/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class CheckfrontWidget {
public $version = '3.0';
public $interface_version = '23';
public $host= '';
public $url = '';
public $src = '';
public $plugin_url = '';
public $pipe_url = '';
public $load_msg = 'Searching Availability';
public $continue_msg = 'Continue to Secure Booking System';
public $offline_msg= 'Bookings not yet available here.';
public $arg = array();
function __construct($cnf) {
if(isset($cnf['host'])) $this->set_host($cnf['host']);
if(isset($cnf['pipe_url'])) $this->set_pipe($cnf['pipe_url']);
if(isset($cnf['plugin_url'])) $this->set_plugin_url($cnf['plugin_url']);
$this->provider = (isset($cnf['provider'])) ? $cnf['provider'] : 'php';
if($cnf['load_msg']) $this->load_msg = strip_tags($cnf['load_msg']);
if($cnf['continue_msg']) $this->continue_msg = strip_tags($cnf['continue_msg']);
}
/**
* set the url base for the widget. should be an absolute url local to the site
* eg: http://www.mysite.com/checkfront-plugin -- must also match the current schema (http:// | https://)
* @param string $url
*/
function set_plugin_url($url) {
$this->plugin_url = preg_replace('|/$|','',$url);
}
/**
* set location of the pipe.html file. Must be located on the same domain.
* @param string $url
*/
function set_pipe($url) {
$this->pipe_url = preg_replace('|/$|','',$url);
}
/**
* sets the checkfront host
* @param string $host
*/
private function set_host($host) {
$this->host = $host;
$this->url = "//{$this->host}";
}
/**
* set valid host
*
* @param string $host
* @return string $host
*/
public function valid_host($host) {
if(!preg_match('~^http://|https://~',$host)) $host = 'https://' . $host;
if($uri = parse_url($host)) {
if($uri['host']) {
return $uri['host'];
}
}
return '';
}
/**
* display error when plugin is not yet configured
*
* @return string $html formatted message
*/
public function error_config() {
return '<p style="padding: .5em; border: solid 1px red;">' . $this->offline_msg .'</p>';
}
/**
* render booking widget
*
* @param array $cnf shortcode parameters
* @return string $html rendering code
*/
public function render($cnf) {
$cnf = $this->clean($cnf);
return $this->portal($cnf);
}
/**
* clean short code params
*
* @param array $cnf shortcode parameters
* @return array $cnf formatted parameters
*/
private function clean($cnf) {
foreach($cnf as $cnf_id => $data) {
$data = preg_replace("/\#|'|>|</",'',strip_tags($data));
$cnf[$cnf_id] = $data;
}
return $cnf;
}
/**
* set the category and item id filters
*
* @param string $ids
* @return string $ids eg 1,2,5
*/
private function set_ids($ids) {
return preg_replace("/[^0-9,]/",'',$ids);
}
/**
* @param array $cnf shortcode parameters
* @return string $html rendering code
*/
private function portal($cnf=array()) {
$cnf_default = array(
'widget_id' => '',
'item_id' => '',
'filter_category_id' => '',
'category_id' => '',
'layout' => '',
'discount_code' => '',
'discount' => '',
'lang_id' => '',
'tid' => '',
'partner_id' => '',
'options' => '',
'date' => '',
'end_date' => '',
'style' => '',
'host' => '',
);
$cnf = array_merge($cnf_default, $cnf ? $cnf : []);
if($cnf['host'] and $this->valid_host($cnf['host'])) {
$this->set_host($cnf['host']);
}
$cnf['widget_id'] = (isset($cnf['widget_id']) and $cnf['widget_id'] > 0) ? $cnf['widget_id'] : '01';
$html = "\n<!-- CHECKFRONT BOOKING PLUGIN v{$this->interface_version}-->\n";
$html .= '<div id="CHECKFRONT_WIDGET_' . $cnf['widget_id'] . '"><p id="CHECKFRONT_LOADER" style="background: url(\'//' . $this->host . '/images/loader.gif\') left center no-repeat; padding: 5px 5px 5px 20px">' . $this->load_msg . '...</p></div>';
$html .= "\n<script>\nnew CHECKFRONT.Widget ({\n";
$html .= "host: '{$this->host}',\n";
if($this->pipe_url) {
$html .= "pipe: '{$this->pipe_url}',\n";
}
$html .= "target: 'CHECKFRONT_WIDGET_{$cnf['widget_id']}',\n";
// Comma separated list of item_ids or skus to filter by
if($cnf['item_id']) {
$cnf['item_id'] = $this->set_ids($cnf['item_id']);
$html .= "item_id: '{$cnf['item_id']}',\n";
}
// Comma separated list of category ids to filter by.
if($cnf['category_id']) {
$cnf['category_id'] = $this->set_ids($cnf['category_id']);
$html .= "category_id: '{$cnf['category_id']}',\n";
}
if($cnf['filter_category_id']) {
$cnf['filter_category_id'] = $this->set_ids($cnf['filter_category_id']);
$html .= "filter_category_id: '{$cnf['filter_category_id']}',\n";
}
if($cnf['width'] and $cnf['width'] > 0) $html .= "width: '{$cnf['width']}',\n";
if($cnf['layout']) $html .= "layout: '{$cnf['layout']}',\n";
if($cnf['tid']) $html .= "tid: '{$cnf['tid']}',\n";
if($cnf['partner_id']) $html .= "partner_id: '{$cnf['partner_id']}',\n";
if($cnf['discount_code']) $html .= "discount_code: '{$cnf['discount_code']}',\n";
elseif($cnf['discount']) $html .= "discount_code: '{$cnf['discount']}',\n";
if($cnf['lang_id']) $html .= "lang_id: '{$cnf['lang_id']}',\n";
if($cnf['locale_id']) $html .= "locale_id: '{$cnf['locale_id']}',\n";
if($cnf['options']) $html.= "options: '{$cnf['options']}',\n";
if($cnf['date']) $html.= "date: '{$cnf['date']}',\n";
if($cnf['end_date']) $html.= "end_date: '{$cnf['end_date']}',\n";
if($cnf['style']) $html .= "style: '{$cnf['style']}',\n";
$html .= "provider: '{$this->provider}'\n";
$html .="}).render();\n</script>\n";
$html .= '<noscript><a href="https://' . $this->host . '/reserve/" style="font-size: 16px">' . $this->continue_msg . ' »</a></noscript>' . "\n";
return $html;
}
}