PluginProbe
FareHarbor for WordPress / 2.0
FareHarbor for WordPress v2.0
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.0, at fareharbor.php

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