PluginProbe
Booking Calendar / 10.11
Booking Calendar v10.11
11.8 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 All 200 releases
booking / core / form_parser.php

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

267 lines 14.1 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_options = '[-0-9a-zA-Z:#_/|\s]*';
78 $rx_shortcode_values = '(?:\s*(?:"[^"]*"|\'[^\']*\'))*';
79 $regex = '%\[\s*(' . $rx_shortcode_types . ')(' . $rx_shortcode_name . ')(' . $rx_shortcode_options . ')?(' . $rx_shortcode_values . ')?\s*\]%';
80 preg_match_all( $regex, $booking_form_configuration, $found_shortcodes, PREG_SET_ORDER );
81
82
83 /**
84 * [ [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"]
85 * [1] => radio
86 * [2] => name
87 * [3] => id:htmlID class:class_name default:Default Value use_label_element label_first
88 * [4] => "Title A@@1" "Title Booking@@2" ""Title C"@@3" "Other title@@4"
89 * ]
90 * OR
91 * [
92 * [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"]
93 * [1] => select*
94 * [2] => rangetime
95 * [3] =>
96 * [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"
97 * ]
98 */
99 $form_shortcodes = array();
100 foreach ( $found_shortcodes as $found_shortcode ) {
101
102 $shortcode_config = array();
103 $shortcode_config['full_shortcode'] = trim( $found_shortcode[0] );
104 $shortcode_config['type'] = trim( $found_shortcode[1] );
105 $shortcode_config['name'] = trim( $found_shortcode[2] );
106 $shortcode_config['options'] = trim( $found_shortcode[3] );
107 $shortcode_config['values_str'] = trim( $found_shortcode[4] );
108 $shortcode_config['values_arr'] = array();
109
110 if ( ! empty( $shortcode_config['values_str'] ) ) {
111 $shortcode_config['values_arr'] = wpbc_parse_form_shortcode_values( $shortcode_config['values_str'] );
112 }
113
114 $form_shortcodes[] = $shortcode_config;
115 }
116
117 return $form_shortcodes;
118 }
119
120
121 function wpbc_parse_form_shortcode_values( $shortcode_values ){
122
123 $values_arr = array();
124
125 // $shortcode_values == '"Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"... '
126 $regex = '%(?:"[^"]*"|\'[^\']*\')%';
127 preg_match_all( $regex, $shortcode_values, $found_values, PREG_PATTERN_ORDER );
128
129 /**
130 * $found_values == [ 0 => [ [0] => "Full Day@@00:00 - 24:00"
131 * [1] => "10:00 AM - 12:00 PM@@10:00 - 12:00"
132 * [2] => "12:00 PM - 02:00 PM@@12:00 - 14:00"
133 * [3] => "02:00 PM - 04:00 PM@@14:00 - 16:00"
134 * [4] => "04:00 PM - 06:00 PM@@16:00 - 18:00"
135 * [5] => "06:00 PM - 08:00 PM@@18:00 - 20:00"
136 * ]
137 * ]
138 */
139
140 foreach ( $found_values as $found_value ) {
141
142 $found_value = array_map( function ( $value ) {
143 $value = trim( $value );
144 $value = trim( $value, '"' );
145 $value = trim( $value, "'" );
146 $value = explode( '@@', $value );
147 return $value;
148 }
149 , $found_value
150 );
151
152 foreach ( $found_value as $f_v ) {
153 $value_config_arr = array();
154 if ( 1 == count( $f_v ) ) {
155 $value_config_arr['value'] = $f_v[0];
156 }
157 if ( 2 == count( $f_v ) ) {
158 $value_config_arr['title'] = $f_v[0];
159 $value_config_arr['value'] = $f_v[1];
160 }
161 $values_arr[] = $value_config_arr;
162 }
163 }
164
165 return $values_arr;
166 }
167
168
169 // ====================================================================================================================
170
171 /**
172 * Get parsed array of shortcodes with specific Name
173 *
174 * Example:
175 * $form_shortcodes = wpbc_parse_form__get_shortcodes_with_name( 'rangetime', $booking_form_fields );
176 *
177 * @param string $shortcode_name 'rangetime'
178 * @param string $booking_form_configuration '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "12:00 - 14:00"] ...'
179 *
180 * @return array = [
181 * [0] => [
182 * [full_shortcode] => [selectbox rangetime "Full Day@@00:00 - 24:00" "12:00 - 14:00"]
183 * [type] => select
184 * [name] => rangetime
185 * [options] =>
186 * [values_str] => "Full Day@@00:00 - 24:00" "12:00 - 14:00"
187 * [values_arr] => [
188 * [0] => [
189 * [title] => Full Day
190 * [value] => 00:00 - 24:00
191 * ]
192 * [1] => [
193 * [value] => 12:00 - 14:00
194 * ]
195 * ]
196 * ],
197 * [1] => [
198 * [full_shortcode] => [selectbox rangetime "14:00 - 16:00" "16:00 - 18:00" "18:00 - 20:00"]
199 * [type] => select
200 * [name] => rangetime
201 * [options] =>
202 * [values_str] => "14:00 - 16:00" "16:00 - 18:00" "18:00 - 20:00"
203 * [values_arr] => [
204 * [0] => [
205 * [value] => 14:00 - 16:00
206 * ]
207 * [1] => [
208 * [value] => 16:00 - 18:00
209 * ]
210 * [2] => [
211 * [value] => 18:00 - 20:00
212 * ]
213 * ]
214 * ]
215 * , ...
216 * ]
217 */
218 function wpbc_parse_form__get_shortcodes_with_name( $shortcode_name, $booking_form_configuration ) {
219
220 $form_shortcodes = wpbc_parse_form( $booking_form_configuration );
221
222 $return_shortcode = array();
223 foreach ( $form_shortcodes as $form_shortcode ) {
224 if ( $shortcode_name == $form_shortcode['name'] ) {
225 $return_shortcode[] = $form_shortcode;
226 }
227 }
228
229 return $return_shortcode;
230 }
231
232
233 /**
234 * Parse booking form and get value of first shortcode with this name. Be careful with conditional form:
235 * where existing several shortcodes with the same name. This function return only first shortcode!
236 *
237 * Example:
238 * $form_shortcodes = wpbc_parse_form__get_first_shortcode_values( 'rangetime', $booking_form_fields );
239 *
240 * @param string $shortcode_name 'rangetime'
241 * @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"] ...'
242 *
243 * @return false | array FALSE if no field OR = [
244 * [0] => [
245 * [title] => Full Day
246 * [value] => 00:00 - 24:00
247 * ]
248 * [1] => [
249 * [title] => 10:00 AM - 12:00 PM
250 * [value] => 10:00 - 12:00
251 * ]
252 * , ...
253 * ]
254 */
255 function wpbc_parse_form__get_first_shortcode_values( $shortcode_name, $booking_form_configuration ) {
256
257 $form_shortcodes = wpbc_parse_form( $booking_form_configuration );
258
259 $return_shortcode = array();
260 foreach ( $form_shortcodes as $form_shortcode ) {
261 if ( $shortcode_name == $form_shortcode['name'] ) {
262 return $form_shortcode['values_arr'];
263 }
264 }
265
266 return false;
267 }