PluginProbe
FareHarbor for WordPress / 1.2
FareHarbor for WordPress v1.2
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 1.2, at fareharbor.php

326 lines 9.6 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: 1.2
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", "fareharbor_handler");
14 add_shortcode("lightframe", "lightframe_api_handler");
15
16 // Defaults
17
18 DEFINE("FH_SHORTNAME", "");
19 DEFINE("FH_EMBED_TYPE", "calendar-small");
20 DEFINE("FH_ITEMS", "");
21 DEFINE("FH_LIGHTFRAME", "yes");
22 DEFINE("FH_ASN", "");
23 DEFINE("FH_ASN_REF", "");
24 DEFINE("FH_REF", "");
25 DEFINE("FH_CLASS", "");
26 DEFINE("FH_ID", "");
27
28 DEFINE("FH_FULL_ITEMS", "no");
29 DEFINE("FH_API_VIEW", "items");
30 DEFINE("FH_API_VIEW_ITEM", "");
31 DEFINE("FH_API_VIEW_AVAILABILITY", "");
32
33
34 // [fareharbor] shortcode
35 // ---------------------------------------------
36
37 function fareharbor_handler($incomingfrompost) {
38
39 // Preprocess attributes returned from post. Trim whitespace.
40
41 $incomingfrompost = array_map('trim', $incomingfrompost);
42
43 // Strip smart quotes, because WordPress returns them as part of the value if the shortcode was set up using them.
44
45 $incomingfrompost = str_replace(
46 array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", chr(145), chr(146), chr(147), chr(148)),
47 array('', '', '', '', '', '', '', ''),
48 $incomingfrompost);
49
50 // Process options and assign defaults if needed
51
52 $incomingfrompost = shortcode_atts(array(
53 "shortname" => FH_SHORTNAME,
54 "type" => FH_EMBED_TYPE,
55 "items" => FH_ITEMS,
56 "lightframe" => FH_LIGHTFRAME,
57 "asn" => FH_ASN,
58 "asn_ref" => FH_ASN_REF,
59 "ref" => FH_REF
60 ), $incomingfrompost);
61
62 // Clean up item IDs: strip spaces and trailing commas
63
64 $incomingfrompost["items"] = str_replace(" ", "", $incomingfrompost["items"]);
65 $incomingfrompost["items"] = rtrim($incomingfrompost["items"], ",");
66
67 $fh_final_output = fareharbor_function($incomingfrompost);
68
69 //send back text to replace shortcode in post
70 return $fh_final_output;
71 }
72
73 function fareharbor_function($fh_options) {
74
75 $fh_output = '';
76
77 // Bail if a shortname isn't provided
78
79 if ( empty( $fh_options["shortname"] ) ) {
80
81 $fh_output .= '<p>Please provide a FareHarbor shortname. (Format: <code>shortname=yourshortname</code>)</p>';
82
83 } else {
84
85 $fh_output = '<script src="https://fareharbor.com/embeds/script/';
86
87 // Types: Clean up "small" and "large" options, otherwise use passed type
88
89 switch ( $fh_options["type"] ) {
90 case "small":
91 $fh_output .= "calendar-small";
92 break;
93 case "large":
94 $fh_output .= "calendar";
95 break;
96 default:
97 $fh_output .= $fh_options["type"];
98 }
99
100 $fh_output .= '/';
101
102 // Shortname
103
104 $fh_output .= $fh_options["shortname"] . '/';
105
106 // Items, if any were included
107
108 if ( !empty( $fh_options["items"] ) ) {
109 $fh_output .= 'items/' . $fh_options["items"] . '/';
110 }
111
112 // Build query string of options. "lightframe" is always included, with either yes or no.
113
114 $fh_output .= '?';
115
116 $fh_query_string_options = array('lightframe' => $fh_options["lightframe"]);
117
118 if ( !empty( $fh_options["asn"] ) ) {
119 $fh_query_string_options["asn"] = $fh_options["asn"];
120
121 if( !empty( $fh_options["asn_ref"] ) ) {
122 $fh_query_string_options["asn-ref"] = $fh_options["asn_ref"];
123 }
124 }
125
126 if ( !empty( $fh_options["ref"] ) ) {
127 $fh_query_string_options["ref"] = $fh_options["ref"];
128 }
129
130 $fh_output .= http_build_query($fh_query_string_options);
131
132
133 $fh_output .= '"></script>';
134 }
135
136 return $fh_output;
137 }
138
139 // [lightframe][/lightframe] shortcode
140 // ---------------------------------------------
141
142 function lightframe_api_handler($attributes, $content = null) {
143
144 // Preprocess attributes returned from post. Trim whitespace.
145
146 $attributes = array_map('trim', $attributes);
147
148 // Strip smart quotes, because WordPress returns them as part of the value if the shortcode was set up using them.
149
150 $attributes = str_replace(
151 array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", chr(145), chr(146), chr(147), chr(148)),
152 array('', '', '', '', '', '', '', ''),
153 $attributes);
154
155 // Process options and assign defaults if needed
156
157 $attrs = shortcode_atts(array(
158 "shortname" => FH_SHORTNAME,
159 "asn" => FH_ASN,
160 "asn_ref" => FH_ASN_REF,
161 "ref" => FH_REF,
162 "items" => FH_ITEMS,
163 "lightframe" => FH_LIGHTFRAME,
164
165 "class" => FH_CLASS,
166 "id" => FH_ID,
167
168 "full_items" => FH_FULL_ITEMS,
169
170 "view" => FH_API_VIEW,
171 "view_item" => FH_API_VIEW_ITEM,
172 "view_availability" => FH_API_VIEW_AVAILABILITY
173 ), $attributes);
174
175 // Clean up item IDs: strip spaces and trailing commas
176
177 $attrs["items"] = str_replace(" ", "", $attrs["items"]);
178 $attrs["items"] = rtrim($attrs["items"], ",");
179
180 $attrs["view_item"] = rtrim($attrs["view_item"], ",");
181
182 $output = '';
183
184 if ( empty( $attrs["shortname"] ) ) {
185
186 $output .= '<p>Please provide a FareHarbor shortname. (Format: <code>shortname=yourshortname</code>)</p>';
187
188 } elseif ( empty( $attrs["view_item"] ) && !empty( $attrs["view_availability"] ) ) {
189
190 echo '<p>Please provide <code>view_item</code> if using <code>view_availability</code.</p>';
191
192 } else {
193
194 // Fallback URL
195 // ---------------------------------------------
196
197 $fallback_url = 'https://fareharbor.com/';
198 $fallback_url .= $attrs["shortname"] . '/items/';
199
200 if( !empty( $attrs["items"] ) ) {
201
202 // If filtering but just to one item, link to it
203
204 $fallback_items = explode(',', $attrs["items"]);
205
206 if( count($fallback_items) == 1 ) {
207 $fallback_url .= $fallback_items[0] . '/';
208
209 if( $attrs["full_items"] == 'no' ) {
210 $fallback_url .= 'calendar/';
211 }
212 }
213
214 } else {
215
216 if( $attrs["view"] == 'all-availability' ) {
217 $fallback_url .= 'calendar/';
218 }
219
220 if( !empty( $attrs["view_item"] ) ) {
221 $fallback_url .= $attrs["view_item"] . '/';
222
223 if( $attrs["full_items"] == 'no' && empty( $attrs["view_availability"] ) ) {
224 $fallback_url .= 'calendar/';
225 }
226 }
227
228 if( !empty( $attrs["view_availability"] ) ) {
229 $fallback_url .= 'availability/' . $attrs["view_availability"] . '/book/';
230 }
231
232 }
233
234 $fallback_url_query_string = array();
235
236 if ( !empty( $attrs["asn"] ) ) {
237 $fallback_url_query_string["asn"] = $attrs["asn"];
238
239 if( !empty( $attrs["asn_ref"] ) ) {
240 $fallback_url_query_string["asn-ref"] = $attrs["asn_ref"];
241 }
242 }
243
244 if ( !empty( $attrs["ref"] ) ) {
245 $fallback_url_query_string["ref"] = $attrs["ref"];
246 }
247
248 if( !empty( $fallback_url_query_string ) ) {
249 $fallback_url .= '?';
250 $fallback_url .= http_build_query($fallback_url_query_string);
251 }
252
253 // $lightframe_options array, to be JSONified for Lightframe API call
254 // ---------------------------------------------
255
256 $lightframe_options["shortname"] = $attrs["shortname"];
257
258 // We should still set a default for full_items, but avoid writing it in if it's just a 'no'
259
260 if ( $attrs["full_items"] != 'no' ) {
261 $lightframe_options["fullItems"] = $attrs["full_items"];
262 }
263
264 if ( !empty( $attrs["asn"] ) ) {
265 $lightframe_options["asn"] = $attrs["asn"];
266
267 if( !empty( $attrs["asn_ref"] ) ) {
268 $lightframe_options["asnRef"] = $attrs["asn_ref"];
269 }
270 }
271
272 if ( !empty( $attrs["ref"] ) ) {
273 $lightframe_options["ref"] = $attrs["ref"];
274 }
275
276 if ( !empty( $attrs["items"] ) ) {
277 $lightframe_options["items"] = array($attrs["items"]); // Put these in an array so it gets brackets
278 }
279
280 // If the view is a string type, just write it in
281
282 if ($attrs["view"] == 'items' || $attrs["view"] == 'all-availability') {
283 $lightframe_options["view"] = $attrs["view"];
284 }
285
286 // If the view is not a string type, you need to pass just view_item= OR view_item= and view_availability=
287
288 if( !empty( $attrs["view_item"] ) && empty( $attrs["view_availability"] )) {
289 $lightframe_options["view"] = array( 'item' => $attrs["view_item"] );
290 }
291
292 if( !empty( $attrs["view_item"] ) && !empty( $attrs["view_availability"] )) {
293 $lightframe_options["view"] = array( 'item' => $attrs["view_item"], 'availability' => $attrs["view_availability"] );
294 }
295
296 // Put it all together now
297 // ---------------------------------------------
298
299 $output .= '<a href="' . $fallback_url . '" ';
300
301 if ( !empty( $attrs["class"] ) ) {
302 $output .= 'class="' . $attrs["class"] .'" ';
303 }
304
305 if ( !empty( $attrs["id"] ) ) {
306 $output .= 'id="' . $attrs["id"] .'" ';
307 }
308
309 $output .= 'onclick="FH.open(' . str_replace('"',"'", json_encode($lightframe_options)) . '); return false;">';
310
311 $output .= do_shortcode( $content ) . '</a>';
312 }
313
314 return $output;
315 }
316
317 // Add API script to bottom of page
318
319 add_action('wp_footer', 'lightframe_api_footer');
320 function lightframe_api_footer() {
321 ?>
322 <script src="https://fareharbor.com/embeds/api/v1/"></script>
323 <?php
324 }
325 ?>
326