PluginProbe
Booking Calendar / 11.2
Booking Calendar v11.2
11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 10.11.3 10.11.4 10.12.0 10.12.1 All 199 releases
booking / core / form_parser.php

form_parser.php in Booking Calendar 11.2, at core/form_parser.php

320 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4.
4
5
6 /**
7 * Parse booking form and get shortcode array
8 *
9 * Example:
10 * $form_shortcodes = wpbc_parse_form( $booking_form_fields );
11 *
12 *
13 * @param $booking_form_configuration - configuration booking form in shortcodes: '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
14 *
15 * @return array
16 *
17 * Return example: [ 0 => [
18 * 'full_shortcode' => '[selectbox rangetime "10:00 - 11:00" "11:00 - 12:00" "12:00 - 13:00"]',
19 * 'type' => 'select',
20 * 'name' => 'rangetime',
21 * 'options' => '',
22 * 'values_str' => '"10:00 - 11:00" "11:00 - 12:00" "12:00 - 13:00"',
23 * 'values_arr' => [
24 * 0 => [ 'value' => '10:00 - 11:00' ],
25 * 1 => [ 'value' => '11:00 - 12:00' ],
26 * 2 => [ 'value' => '12:00 - 13:00' ]
27 * ]
28 * ],
29 * 1 => [
30 * 'full_shortcode' => '[selectbox rangetime "10:00 - 12:00" "12:00 - 14:00"]', <= !!! Several shortcodes with same name - in conditional sections !!!
31 * 'type' => 'select',
32 * 'name' => 'rangetime',
33 * 'options' => '',
34 * 'values_str' => '"10:00 - 12:00" "12:00 - 14:00"',
35 * 'values_arr' => [
36 * 0 => [ 'value' => '10:00 - 12:00' ],
37 * 1 => [ 'value' => '12:00 - 14:00' ]
38 * ]
39 * ],
40 * ...
41 * 6 => [
42 * 'full_shortcode' => '[checkbox* term_and_condition use_label_element default:on "I Accept term and conditions@@accept"]',
43 * 'type' => 'checkbox*',
44 * 'name' => 'term_and_condition',
45 * 'options' => 'use_label_element default:on',
46 * 'values_str' => '"I Accept term and conditions@@accept"',
47 * 'values_arr' => [
48 * 0 => [ 'title' => 'I Accept term and conditions',
49 * 'value' => 'accept',
50 * ]
51 * ]
52 * ],
53 * 7 => [
54 * full_shortcode = "[checkbox fee]" <== !!! No value !!!
55 * type = "checkbox"
56 * name = "fee"
57 * options = ""
58 * values_str = ""
59 * values_arr = [ ] <== !!! Empty, if no value !!!
60 * ]
61 * ]
62 *
63 */
64 function wpbc_parse_form( $booking_form_configuration ){
65
66 /**
67 * NON-standard shortcodes:
68 * $regex = '%\[\s*(' . $types . ') (\s+[a-zA-Z][0-9a-zA-Z:._-]*) ([-0-9a-zA-Z:#_/|\s]*)?((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
69 * $regex_start_end_time = '%\[\s*(country[*]?|starttime[*]?|endtime[*]?)(\s*[a-zA-Z]*[0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
70 * $regex_submit_ = '%\[\s*submit (\s[-0-9a-zA-Z:#_/\s]*)? (\s+(?:"[^"]*"|\'[^\']*\'))?\s*\]%';
71 */
72
73 // Parse select shortcodes
74 $rx_shortcode_types = 'text[*]?|email[*]?|coupon[*]?|time[*]?|textarea[*]?|select[*]?|selectbox[*]?|checkbox[*]?|radio[*]?|acceptance|captchac|captchar|file[*]?|quiz';
75 // $rx_shortcode_types= 'select[*]?|selectbox[*]?|checkbox[*]?|radio[*]?';
76 $rx_shortcode_name = '\s+([a-zA-Z][0-9a-zA-Z:._-]*)';
77 $rx_shortcode_content = '((?:[^\]"\']+|"[^"]*"|\'[^\']*\')*)';
78 $regex = '%\[\s*(' . $rx_shortcode_types . ')(?=\s|\])' . $rx_shortcode_content . '\]%';
79 preg_match_all( $regex, $booking_form_configuration, $found_shortcodes, PREG_SET_ORDER );
80
81
82 /**
83 * [ [0] => [radio name id:htmlID class:class_name default:Default Value use_label_element label_first "Title A@@1" "Title Booking@@2" ""Title C"@@3" "Other title@@4"]
84 * [1] => radio
85 * [2] => name
86 * [3] => id:htmlID class:class_name default:Default Value use_label_element label_first
87 * [4] => "Title A@@1" "Title Booking@@2" ""Title C"@@3" "Other title@@4"
88 * ]
89 * OR
90 * [
91 * [0] => [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00" "12:00 PM - 02:00 PM@@12:00 - 14:00" "02:00 PM - 04:00 PM@@14:00 - 16:00" "04:00 PM - 06:00 PM@@16:00 - 18:00" "06:00 PM - 08:00 PM@@18:00 - 20:00"]
92 * [1] => select*
93 * [2] => rangetime
94 * [3] =>
95 * [4] => "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00" "12:00 PM - 02:00 PM@@12:00 - 14:00" "02:00 PM - 04:00 PM@@14:00 - 16:00" "04:00 PM - 06:00 PM@@16:00 - 18:00" "06:00 PM - 08:00 PM@@18:00 - 20:00"
96 * ]
97 */
98 $form_shortcodes = array();
99 foreach ( $found_shortcodes as $found_shortcode ) {
100
101 if ( ! preg_match( '%^' . $rx_shortcode_name . '(.*)$%s', $found_shortcode[2], $shortcode_parts ) ) {
102 continue;
103 }
104
105 list( $shortcode_options, $shortcode_values ) = wpbc_parse_form_shortcode_options_values( $shortcode_parts[2] );
106
107 $shortcode_config = array();
108 $shortcode_config['full_shortcode'] = trim( $found_shortcode[0] );
109 $shortcode_config['type'] = trim( $found_shortcode[1] );
110 $shortcode_config['name'] = trim( $shortcode_parts[1] );
111 $shortcode_config['options'] = trim( $shortcode_options );
112 $shortcode_config['values_str'] = trim( $shortcode_values );
113 $shortcode_config['values_arr'] = array();
114
115 if ( ! empty( $shortcode_config['values_str'] ) ) {
116 $shortcode_config['values_arr'] = wpbc_parse_form_shortcode_values( $shortcode_config['values_str'] );
117 }
118
119 $form_shortcodes[] = $shortcode_config;
120 }
121
122 return $form_shortcodes;
123 }
124
125
126 if ( ! function_exists( 'wpbc_parse_form_shortcode_options_values' ) ) {
127 function wpbc_parse_form_shortcode_options_values( $shortcode_tail ){
128
129 $shortcode_options = trim( $shortcode_tail );
130 $shortcode_values = '';
131
132 $value_matches = array();
133 preg_match_all( '%(?:"[^"]*"|\'[^\']*\')%', $shortcode_tail, $value_matches, PREG_OFFSET_CAPTURE );
134
135 $standalone_values = array();
136 foreach ( $value_matches[0] as $value_match ) {
137 $value_offset = $value_match[1];
138 $prev_offset = $value_offset - 1;
139
140 if ( ( $prev_offset >= 0 ) && ! ctype_space( $shortcode_tail[ $prev_offset ] ) ) {
141 continue;
142 }
143
144 while ( ( $prev_offset >= 0 ) && ctype_space( $shortcode_tail[ $prev_offset ] ) ) {
145 $prev_offset--;
146 }
147
148 if ( ( $prev_offset >= 0 ) && in_array( $shortcode_tail[ $prev_offset ], array( '=', ':' ), true ) ) {
149 continue;
150 }
151
152 $standalone_values[] = $value_match;
153 }
154
155 if ( ! empty( $standalone_values ) ) {
156 $shortcode_options = trim( substr( $shortcode_tail, 0, $standalone_values[0][1] ) );
157 $shortcode_values = implode(
158 ' ',
159 array_map(
160 function ( $value_match ) {
161 return $value_match[0];
162 },
163 $standalone_values
164 )
165 );
166 }
167
168 return array( $shortcode_options, $shortcode_values );
169 }
170 }
171
172
173 function wpbc_parse_form_shortcode_values( $shortcode_values ){
174
175 $values_arr = array();
176
177 // $shortcode_values == '"Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"... '
178 $regex = '%(?:"[^"]*"|\'[^\']*\')%';
179 preg_match_all( $regex, $shortcode_values, $found_values, PREG_PATTERN_ORDER );
180
181 /**
182 * $found_values == [ 0 => [ [0] => "Full Day@@00:00 - 24:00"
183 * [1] => "10:00 AM - 12:00 PM@@10:00 - 12:00"
184 * [2] => "12:00 PM - 02:00 PM@@12:00 - 14:00"
185 * [3] => "02:00 PM - 04:00 PM@@14:00 - 16:00"
186 * [4] => "04:00 PM - 06:00 PM@@16:00 - 18:00"
187 * [5] => "06:00 PM - 08:00 PM@@18:00 - 20:00"
188 * ]
189 * ]
190 */
191
192 foreach ( $found_values as $found_value ) {
193
194 $found_value = array_map( function ( $value ) {
195 $value = trim( $value );
196 $value = trim( $value, '"' );
197 $value = trim( $value, "'" );
198 $value = explode( '@@', $value );
199 return $value;
200 }
201 , $found_value
202 );
203
204 foreach ( $found_value as $f_v ) {
205 $value_config_arr = array();
206 if ( 1 == count( $f_v ) ) {
207 $value_config_arr['value'] = $f_v[0];
208 }
209 if ( 2 == count( $f_v ) ) {
210 $value_config_arr['title'] = $f_v[0];
211 $value_config_arr['value'] = $f_v[1];
212 }
213 $values_arr[] = $value_config_arr;
214 }
215 }
216
217 return $values_arr;
218 }
219
220
221 // ====================================================================================================================
222
223 /**
224 * Get parsed array of shortcodes with specific Name
225 *
226 * Example:
227 * $form_shortcodes = wpbc_parse_form__get_shortcodes_with_name( 'rangetime', $booking_form_fields );
228 *
229 * @param string $shortcode_name 'rangetime'
230 * @param string $booking_form_configuration '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "12:00 - 14:00"] ...'
231 *
232 * @return array = [
233 * [0] => [
234 * [full_shortcode] => [selectbox rangetime "Full Day@@00:00 - 24:00" "12:00 - 14:00"]
235 * [type] => select
236 * [name] => rangetime
237 * [options] =>
238 * [values_str] => "Full Day@@00:00 - 24:00" "12:00 - 14:00"
239 * [values_arr] => [
240 * [0] => [
241 * [title] => Full Day
242 * [value] => 00:00 - 24:00
243 * ]
244 * [1] => [
245 * [value] => 12:00 - 14:00
246 * ]
247 * ]
248 * ],
249 * [1] => [
250 * [full_shortcode] => [selectbox rangetime "14:00 - 16:00" "16:00 - 18:00" "18:00 - 20:00"]
251 * [type] => select
252 * [name] => rangetime
253 * [options] =>
254 * [values_str] => "14:00 - 16:00" "16:00 - 18:00" "18:00 - 20:00"
255 * [values_arr] => [
256 * [0] => [
257 * [value] => 14:00 - 16:00
258 * ]
259 * [1] => [
260 * [value] => 16:00 - 18:00
261 * ]
262 * [2] => [
263 * [value] => 18:00 - 20:00
264 * ]
265 * ]
266 * ]
267 * , ...
268 * ]
269 */
270 function wpbc_parse_form__get_shortcodes_with_name( $shortcode_name, $booking_form_configuration ) {
271
272 $form_shortcodes = wpbc_parse_form( $booking_form_configuration );
273
274 $return_shortcode = array();
275 foreach ( $form_shortcodes as $form_shortcode ) {
276 if ( $shortcode_name == $form_shortcode['name'] ) {
277 $return_shortcode[] = $form_shortcode;
278 }
279 }
280
281 return $return_shortcode;
282 }
283
284
285 /**
286 * Parse booking form and get value of first shortcode with this name. Be careful with conditional form:
287 * where existing several shortcodes with the same name. This function return only first shortcode!
288 *
289 * Example:
290 * $form_shortcodes = wpbc_parse_form__get_first_shortcode_values( 'rangetime', $booking_form_fields );
291 *
292 * @param string $shortcode_name 'rangetime'
293 * @param string $booking_form_configuration '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
294 *
295 * @return false | array FALSE if no field OR = [
296 * [0] => [
297 * [title] => Full Day
298 * [value] => 00:00 - 24:00
299 * ]
300 * [1] => [
301 * [title] => 10:00 AM - 12:00 PM
302 * [value] => 10:00 - 12:00
303 * ]
304 * , ...
305 * ]
306 */
307 function wpbc_parse_form__get_first_shortcode_values( $shortcode_name, $booking_form_configuration ) {
308
309 $form_shortcodes = wpbc_parse_form( $booking_form_configuration );
310
311 $return_shortcode = array();
312 foreach ( $form_shortcodes as $form_shortcode ) {
313 if ( $shortcode_name == $form_shortcode['name'] ) {
314 return $form_shortcode['values_arr'];
315 }
316 }
317
318 return false;
319 }
320