PluginProbe
FareHarbor for WordPress / 2.1
FareHarbor for WordPress v2.1
3.6.15 3.6.14 trunk 1.1 1.2 2.0 2.1 3.0 3.0.1 3.1 3.2 3.3 3.3.1 3.4 3.5 3.6 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 All 29 releases
fareharbor / fareharbor.php

fareharbor.php in FareHarbor for WordPress 2.1, at fareharbor.php

396 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: FareHarbor Reservation Calendars
4 Plugin URI: https://fareharbor.com/help/setup/wordpress-plugin/
5 Description: Adds shortcodes for adding FareHarbor embeds to your site
6 Version: 2.1
7 Author: FareHarbor
8 Author URI: https://fareharbor.com
9 */
10
11 defined('ABSPATH') or die("What are you looking at?");
12
13 add_shortcode("fareharbor", "fh_shortcode");
14 add_shortcode("lightframe", "lightframe_shortcode");
15 add_shortcode("partners", "partners_shortcode");
16
17 // Defaults
18
19 DEFINE('FH_SHORTNAME', '');
20 DEFINE('FH_EMBED_TYPE', 'calendar-small');
21 DEFINE('FH_ITEMS', '');
22 DEFINE('FH_LIGHTFRAME', 'yes');
23 DEFINE('FH_ASN', '');
24 DEFINE('FH_ASN_REF', '');
25 DEFINE('FH_REF', '');
26 DEFINE('FH_CLASS', '');
27 DEFINE('FH_ID', '');
28
29 DEFINE('FH_FULL_ITEMS', 'no');
30 DEFINE('FH_API_VIEW', 'items');
31 DEFINE('FH_API_VIEW_ITEM', '');
32 DEFINE('FH_API_VIEW_AVAILABILITY', '');
33
34 DEFINE('FH_PARTNERS_INCLUDE', '');
35
36 // Process the info returned from a shortcode
37 // ---------------------------------------------
38
39 function fh_sanitize_csv( $value ) {
40 $value = str_replace(" ", "", $value);
41 $value = rtrim($value, ",");
42 return $value;
43 }
44
45 function fh_process_attrs( $attrs ) {
46 if( is_array($attrs) ) {
47
48 // Trim whitespace
49
50 $attrs = array_map('trim', $attrs);
51
52 // Strip smart quotes, because WordPress returns them as part of the value if the shortcode was set up using them
53
54 $attrs = str_replace(
55 array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", chr(145), chr(146), chr(147), chr(148)),
56 array('', '', '', '', '', '', '', ''),
57 $attrs);
58
59 // Process options and assign defaults if needed
60
61 $attrs = shortcode_atts(array(
62 "shortname" => FH_SHORTNAME,
63 "items" => FH_ITEMS,
64 "asn" => FH_ASN,
65 "asn_ref" => FH_ASN_REF,
66 "ref" => FH_REF,
67 "lightframe" => FH_LIGHTFRAME,
68 "full_items" => FH_FULL_ITEMS,
69 "sheet" => '',
70
71 // [fareharbor] only
72
73 "type" => FH_EMBED_TYPE,
74 "lightframe" => FH_LIGHTFRAME,
75
76 // [lightframe] only
77
78 "class" => FH_CLASS,
79 "id" => FH_ID,
80 "view" => FH_API_VIEW,
81 "view_item" => FH_API_VIEW_ITEM,
82 "view_availability" => FH_API_VIEW_AVAILABILITY,
83
84 // [partners] only
85
86 "include" => FH_PARTNERS_INCLUDE
87
88 ), $attrs);
89
90 // Clean up item IDs and included companies because users can't be trusted
91
92 $attrs["items"] = fh_sanitize_csv( $attrs["items"] );
93 $attrs["view_item"] = fh_sanitize_csv( $attrs["view_item"] );
94 $attrs["include"] = fh_sanitize_csv( $attrs["include"] );
95
96 return $attrs;
97
98 }
99 }
100
101 function fh_url() {
102 $env_url = defined('FH_ENVIRONMENT') ? FH_ENVIRONMENT . '.fareharbor.com' : 'fareharbor.com';
103 return $env_url;
104 }
105
106 // [fareharbor] shortcode
107 // ---------------------------------------------
108
109 function fh_shortcode( $attrs ) {
110
111 $fh_options = fh_process_attrs( $attrs );
112
113 $output = '';
114
115 // Bail if a shortname isn't provided
116
117 if ( empty( $fh_options["shortname"] ) ) {
118
119 $output .= '<p>Please provide a FareHarbor shortname. (Format: <code>shortname="yourshortname"</code>)</p>';
120
121 } else {
122
123 $output = '<script src="https://' . fh_url() . '/embeds/script/';
124
125 // Types: Clean up "small" and "large" options, otherwise use passed type
126
127 switch ( $fh_options["type"] ) {
128 case "small":
129 $output .= "calendar-small";
130 break;
131 case "large":
132 $output .= "calendar";
133 break;
134 default:
135 $output .= $fh_options["type"];
136 }
137
138 $output .= '/';
139
140 // Shortname
141
142 $output .= $fh_options["shortname"] . '/';
143
144 // Items, if any were included
145
146 if ( !empty( $fh_options["items"] ) ) {
147 $output .= 'items/' . $fh_options["items"] . '/';
148 }
149
150 // Build query string of options. "lightframe" is always included, with either yes or no.
151
152 $output .= '?';
153
154 $fh_query_string_options = array('lightframe' => $fh_options["lightframe"]);
155
156 if ( !empty( $fh_options["asn"] ) ) {
157 $fh_query_string_options["asn"] = $fh_options["asn"];
158
159 if( !empty( $fh_options["asn_ref"] ) ) {
160 $fh_query_string_options["asn-ref"] = $fh_options["asn_ref"];
161 }
162 }
163
164 if ( !empty( $fh_options["ref"] ) ) {
165 $fh_query_string_options["ref"] = $fh_options["ref"];
166 }
167
168 if ( !empty( $fh_options["sheet"] ) ) {
169 $fh_query_string_options["sheet"] = $fh_options["sheet"];
170 }
171
172 $output .= http_build_query($fh_query_string_options);
173
174
175 $output .= '"></script>';
176 }
177
178 return $output;
179 }
180
181 // [lightframe][/lightframe] shortcode
182 // ---------------------------------------------
183
184 function lightframe_shortcode( $attrs, $content = null ) {
185
186 $attrs = fh_process_attrs( $attrs );
187
188 $output = '';
189
190 if ( empty( $attrs["shortname"] ) ) {
191
192 $output .= '<p>Please provide a FareHarbor shortname. (Format: <code>shortname="yourshortname"</code>)</p>';
193
194 } elseif ( empty( $attrs["view_item"] ) && !empty( $attrs["view_availability"] ) ) {
195
196 echo '<p>Please provide <code>view_item</code> if using <code>view_availability</code.</p>';
197
198 } else {
199
200 // Fallback URL
201 // ---------------------------------------------
202
203 $fallback_url = 'https://' . fh_url() . '/';
204 $fallback_url .= $attrs["shortname"] . '/items/';
205
206 if( !empty( $attrs["items"] ) ) {
207
208 // If filtering but just to one item, link to it
209
210 $fallback_items = explode(',', $attrs["items"]);
211
212 if( count($fallback_items) == 1 ) {
213 $fallback_url .= $fallback_items[0] . '/';
214
215 if( $attrs["full_items"] == 'no' ) {
216 $fallback_url .= 'calendar/';
217 }
218 }
219
220 } else {
221
222 if( $attrs["view"] == 'all-availability' ) {
223 $fallback_url .= 'calendar/';
224 }
225
226 if( !empty( $attrs["view_item"] ) ) {
227 $fallback_url .= $attrs["view_item"] . '/';
228
229 if( $attrs["full_items"] == 'no' && empty( $attrs["view_availability"] ) ) {
230 $fallback_url .= 'calendar/';
231 }
232 }
233
234 if( !empty( $attrs["view_availability"] ) ) {
235 $fallback_url .= 'availability/' . $attrs["view_availability"] . '/book/';
236 }
237
238 }
239
240 $fallback_url_query_string = array();
241
242 if ( !empty( $attrs["asn"] ) ) {
243 $fallback_url_query_string["asn"] = $attrs["asn"];
244
245 if( !empty( $attrs["asn_ref"] ) ) {
246 $fallback_url_query_string["asn-ref"] = $attrs["asn_ref"];
247 }
248 }
249
250 if ( !empty( $attrs["ref"] ) ) {
251 $fallback_url_query_string["ref"] = $attrs["ref"];
252 }
253
254 if ( !empty( $attrs["sheet"] ) ) {
255 $fallback_url_query_string["sheet"] = $attrs["sheet"];
256 }
257
258 if( !empty( $fallback_url_query_string ) ) {
259 $fallback_url .= '?';
260 $fallback_url .= http_build_query($fallback_url_query_string);
261 }
262
263 // $lightframe_options array, to be JSONified for Lightframe API call
264 // ---------------------------------------------
265
266 $lightframe_options["shortname"] = $attrs["shortname"];
267
268 // We should still set a default for full_items, but avoid writing it in if it's just a 'no'
269
270 if ( $attrs["full_items"] != 'no' ) {
271 $lightframe_options["fullItems"] = $attrs["full_items"];
272 }
273
274 if ( !empty( $attrs["asn"] ) ) {
275 $lightframe_options["asn"] = $attrs["asn"];
276
277 if( !empty( $attrs["asn_ref"] ) ) {
278 $lightframe_options["asnRef"] = $attrs["asn_ref"];
279 }
280 }
281
282 if ( !empty( $attrs["ref"] ) ) {
283 $lightframe_options["ref"] = $attrs["ref"];
284 }
285
286 if ( !empty( $attrs["sheet"] ) ) {
287 $lightframe_options["sheet"] = $attrs["sheet"];
288 }
289
290 if ( !empty( $attrs["items"] ) ) {
291 $lightframe_options["items"] = array($attrs["items"]); // Put these in an array so it gets brackets
292 }
293
294 // If the view is a string type, just write it in
295
296 if ($attrs["view"] == 'items' || $attrs["view"] == 'all-availability') {
297 $lightframe_options["view"] = $attrs["view"];
298 }
299
300 // If the view is not a string type, you need to pass just view_item= OR view_item= and view_availability=
301
302 if( !empty( $attrs["view_item"] ) && empty( $attrs["view_availability"] )) {
303 $lightframe_options["view"] = array( 'item' => $attrs["view_item"] );
304 }
305
306 if( !empty( $attrs["view_item"] ) && !empty( $attrs["view_availability"] )) {
307 $lightframe_options["view"] = array( 'item' => $attrs["view_item"], 'availability' => $attrs["view_availability"] );
308 }
309
310 // Put it all together now
311 // ---------------------------------------------
312
313 $output .= '<a href="' . $fallback_url . '" ';
314
315 if ( !empty( $attrs["class"] ) ) {
316 $output .= 'class="' . $attrs["class"] .'" ';
317 }
318
319 if ( !empty( $attrs["id"] ) ) {
320 $output .= 'id="' . $attrs["id"] .'" ';
321 }
322
323 $output .= 'onclick="FH.open(' . str_replace('"',"'", json_encode($lightframe_options)) . '); return false;">';
324
325 $output .= do_shortcode( $content ) . '</a>';
326 }
327
328 return $output;
329 }
330
331 // [partners] shortcode
332 // ---------------------------------------------
333
334 function partners_shortcode( $attrs ) {
335
336 $attrs = fh_process_attrs( $attrs );
337
338 $output = '';
339
340 // Bail if a shortname isn't provided
341
342 if ( empty( $attrs["shortname"] ) ) {
343
344 $output .= '<p>Please provide a FareHarbor shortname. (Format: <code>shortname="yourshortname"</code>)</p>';
345
346 } else {
347
348 $output = '<script src="https://' . fh_url() . '/embeds/script/partners/';
349
350 $output .= $attrs["shortname"] . '/';
351
352 // Build query string of options
353
354 $output .= '?';
355
356 // lightframe is always included
357
358 $fh_query_string_options = array('lightframe' => $attrs["lightframe"]);
359
360 // For safety, always set asn, just to the shortname if asn isn't given
361
362 $fh_query_string_options["asn"] = !empty( $attrs["asn"] ) ? $attrs["asn"] : $attrs["shortname"];
363
364 if( !empty( $attrs["asn_ref"] ) ) {
365 $fh_query_string_options["asn-ref"] = $attrs["asn_ref"];
366 }
367
368 if ( !empty( $attrs["ref"] ) ) {
369 $fh_query_string_options["ref"] = $attrs["ref"];
370 }
371
372 if ( $attrs["full_items"] != 'no' ) {
373 $fh_query_string_options["full-items"] = $attrs["full_items"];
374 }
375
376 if ( !empty( $attrs["include"] ) ) {
377 $fh_query_string_options["include"] = $attrs["include"];
378 }
379
380 $output .= http_build_query($fh_query_string_options);
381
382 $output .= '"></script>';
383 }
384
385 return $output;
386 }
387
388 // Add API script to footer
389 // ---------------------------------------------
390
391 add_action('wp_footer', 'lightframe_api_footer');
392 function lightframe_api_footer() {
393 echo '<!-- FareHarbor plugin activated --><script src="https://' . fh_url() . '/embeds/api/v1/"></script>';
394 }
395 ?>
396