PluginProbe
Booking Calendar / 10.15.7
Booking Calendar v10.15.7
11.8.2 11.8.1 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 All 202 releases
booking / includes / _functions / str_regex.php

str_regex.php in Booking Calendar 10.15.7, at includes/_functions/str_regex.php

529 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Shortcode functions
6 * @category Functions
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @file ../includes/_functions/str_regex.php
13 *
14 * @modified 2024-05-14
15 */
16
17 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
18
19 // =====================================================================================================================
20 // == String Manipulation functions ==
21 // =====================================================================================================================
22
23 /**
24 * Insert New Line symbols after <br> tags. Usefull for the settings pages to show in redable view
25 *
26 * @param string $param
27 * @return string
28 */
29 function wpbc_nl_after_br($param) {
30
31 $value = preg_replace( "@(&lt;|<)br\s*/?(&gt;|>)(\r\n)?@", "<br/>", $param );
32
33 return $value;
34 }
35
36
37 /**
38 * Replace ** to <strong> and * to <em>
39 *
40 * @param String $text
41 * @return string
42 */
43 function wpbc_replace_to_strong_symbols( $text ){
44
45 $patterns = '/(\*\*)(\s*[^\*\*]*)(\*\*)/';
46 $replacement = '<strong>${2}</strong>';
47 $value_return = preg_replace($patterns, $replacement, $text);
48
49 $patterns = '/(\*)(\s*[^\*]*)(\*)/';
50 $replacement = '<em>${2}</em>';
51 $value_return = preg_replace($patterns, $replacement, $value_return);
52
53 return $value_return;
54 }
55
56
57 /**
58 * Replace 'true' | 'false' to __('yes') | __('no'). E.g.: '...Fee: true...' => '...Fee: yes...'
59 *
60 * Replace value 'true' to localized __( 'yes', 'booking' ) in Content -- usually it's required before showing text to user for saved data of selected checkboxes, that was configured with empty value: [checkbox fee ""]
61 *
62 * @param $value
63 *
64 * @return array|string|string[]
65 */
66 function wpbc_replace__true_false__to__yes_no( $value ) { // FixIn: 9.8.9.1.
67
68 $checkbox_true_value = apply_filters( 'wpbc_checkbox_true_value', __( 'Y_E_S', 'booking' ) );
69 $value_replaced = str_replace( 'true', $checkbox_true_value, $value );
70
71 $checkbox_true_value = apply_filters( 'wpbc_checkbox_false_value', __( 'N_O', 'booking' ) );
72 $value_replaced = str_replace( 'false', $checkbox_true_value, $value_replaced );
73
74 return $value_replaced;
75 }
76
77
78 /**
79 * Convert Strings in array to Lower case
80 * @param $array
81 *
82 * @return mixed
83 */
84 function wpbc_convert__strings_in_arr__to_lower( $array ){
85 return unserialize( strtolower( serialize( $array ) ) );
86 }
87
88
89 /**
90 * Prepare text to show as HTML, replace double encoded \\n to <br> and escape \\" and \\' . Mainly used in Ajax.
91 *
92 * @param $text
93 *
94 * @return array|string|string[]|null
95 */
96 function wpbc_prepare_text_for_html( $text ){
97
98 /**
99 * Replace <p> | <br> to ' '
100 *
101 * $plain_form_data_show = preg_replace( '/<(br|p)[\t\s]*[\/]?>/i', ' ', $plain_form_data_show );
102 */
103 $text = preg_replace( '/(\\\\n)/i', '<br>', $text ); // New line in text areas replace with <br>
104 $text = preg_replace( '/(\\\\")/i', '&quot;', html_entity_decode( $text ) ); // escape quote symbols;
105 $text = preg_replace( "/(\\\\')/i", '&apos;', html_entity_decode( $text ) ); // escape quote symbols;
106
107 // Replace \r \n \t
108 $text = preg_replace( '/(\\r|\\n|\\t)/i', ' ', $text );
109
110 return $text;
111 }
112
113
114 // TODO: Need to check if we really need to make this. 2023-10-06 12:46
115 /**
116 * Escaping text for output.
117 *
118 * @param string $output
119 *
120 * @return string
121 */
122 function wpbc_escaping_text_for_output( $output ){
123
124 // Save empty spaces
125 $original_symbols = array( '&nbsp;' );
126 $temporary_symbols = array( '^space^' );
127 $output = str_replace( $original_symbols, $temporary_symbols, $output ); // &nbsp; -> ^space^
128
129 // Escaping ?
130 $output = esc_js( $output ); // it adds '\n' symbols | " into &quot | < -> &lt; ...
131 $output = html_entity_decode( $output ); // Convert back to HTML, but now we have '\n' symbols
132 $output = str_replace( "\\n", '', $output ); // Remove '\n' symbols
133
134 // Back to empty spaces.
135 $original_symbols = array( '^space^' );
136 $temporary_symbols = array( '&nbsp;' );
137 $output = str_replace( $original_symbols, $temporary_symbols, $output);
138
139 return $output;
140 }
141
142
143 /**
144 * Convert text with escaped symbols like '1. Soem text here\n2. \&quot;Quoted text\&quot;\n3. \&#039;Single quoted text\&#039;\n' to HTML:
145 *
146 * @param $text
147 *
148 * @return array|string|string[]
149 */
150 function wpbc_string__escape__then_convert__n_amp__html( $text ) {
151
152 $is_escape_sql = false; // Do not replace %
153
154 $escaped_text = wpbc_escape_any_xss_in_string($text, $is_escape_sql );
155
156 $text_html = str_replace( array( "\\r\\n", "\\r", "\\n", "\r\n", "\r","\n" ), "<br/>", $escaped_text ); // FixIn: 8.1.3.4.
157
158 $text_html = str_replace( array( "\\&" ), '&', $text_html );
159
160 return $text_html;
161 }
162
163
164 // =====================================================================================================================
165 // == Regex for Shortcodes ==
166 // =====================================================================================================================
167
168 /**
169 * Get parameters of shortcode in string '..some text [visitorbookingpayurl url='https://url.com/a/'] ...' -> [ 'url'='https://url.com.com/a/', 'start'=10, 'end'=80 ]
170 *
171 * @param string $shortcode - shortcode name - 'visitorbookingcancelurl'
172 * @param string $subject - string where to search shortcode: - '<p>1 PT. [visitorbookingpayurl url='https://wpbookingcalendar.com/faq/']</p>'
173 * @param int $pos default 0 - 0
174 * @param string $pattern_to_search default '%\s*([^=]*)=\s*[\'"]([^\'"]*)[\'"]\s*%'
175 *
176 * @return array|false [
177 * 'url' = "https://wpbookingcalendar.com/faq/"
178 * 'start' = 10
179 * 'end' = 80
180 * ]
181 *
182 * Example:
183 * wpbc_get_params_of_shortcode_in_string( 'visitorbookingpayurl', '<p>1 PT. [visitorbookingpayurl url = "https://wpbookingcalendar.com/faq/"]</p>...' );
184 *
185 * output -> [
186 * 'url' = "https://wpbookingcalendar.com/faq/"
187 * 'start' = 10
188 * 'end' = 80
189 * ]
190 *
191 */
192 function wpbc_get_params_of_shortcode_in_string( $shortcode, $subject, $pos = 0, $pattern_to_search = '%\s*([^=]*)=\s*[\'"]([^\'"]*)[\'"]\s*%' ) { //FixIn: 9.7.4.4 //FixIn: 7.0.1.8 7.0.1.52
193
194 if ( strlen( $subject ) < intval( $pos ) ) { //FixIn: (9.7.4.5)
195 return false;
196 }
197
198 $pos = strpos( $subject, '[' . $shortcode, $pos ); // FixIn: 7.0.1.52.
199
200 if ( $pos !== false ) {
201 $pos2 = strpos( $subject, ']', ( $pos + 2 ) );
202
203 $my_params = substr( $subject, $pos + strlen( '[' . $shortcode ), ( $pos2 - $pos - strlen( '[' . $shortcode ) ) );
204
205
206 preg_match_all( $pattern_to_search, $my_params, $keywords, PREG_SET_ORDER );
207
208 foreach ( $keywords as $value ) {
209 if ( count( $value ) > 1 ) {
210 $shortcode_params[ trim( $value[1] ) ] = trim( $value[2] ); // FixIn: 9.7.4.4.
211 }
212 }
213 $shortcode_params['start'] = $pos + 1;
214 $shortcode_params['end'] = $pos2;
215
216 return $shortcode_params;
217 } else {
218 return false;
219 }
220 }
221
222
223 /**
224 * Get shortcodes with params and text for replacing these shortcodes as new uniue shortcodes
225 *
226 * @param string $content_text Example: "<span class="wpbc_top_news_dismiss">[wpbc_dismiss id="wpbc_top_news__offer_2023_04_21"]</span>"
227 * @param array $shortcode_arr Example: array( 'wpbc_dismiss' )
228 *
229 * @return array
230 * Example: array(
231 content => "<span class="wpbc_top_news_dismiss">[wpbc_dismiss6764]</span>"
232 shortcodes => array(
233 'wpbc_dismiss6764' => array(
234 shortcode => "[wpbc_dismiss6764]",
235 params => array( id => "wpbc_top_news__offer_2023_04_21" )
236 shortcode_original => "[wpbc_dismiss id="wpbc_top_news__offer_2023_04_21"]"
237 )
238 )
239 )
240 */
241 function wpbc_get_shortcodes_in_text__as_unique_replace( $content_text, $shortcode_arr = array( 'wpbc_dismiss' ) ) { // FixIn: 9.6.1.8.
242
243 $replace = array();
244
245 foreach ( $shortcode_arr as $single_shortcode ) {
246
247 $pos = 0; // Loop to find if we have several such shortcodes in $content_text
248 do {
249 $shortcode_params = wpbc_get_params_of_shortcode_in_string( $single_shortcode, $content_text, $pos );
250
251 if ( ( ! empty( $shortcode_params ) ) && ( isset( $shortcode_params['end'] ) ) && ( $shortcode_params['end'] < strlen( $content_text ) ) ){
252
253 $exist_replace = substr( $content_text, $shortcode_params['start'], ( $shortcode_params['end'] - $shortcode_params['start'] ) );
254
255 $new_replace = $single_shortcode . wp_rand( 1000, 9000 );
256
257 $pos = $shortcode_params['start'] + strlen( $new_replace );
258
259 $content_text = substr_replace( $content_text, $new_replace, $shortcode_params['start'], ( $shortcode_params['end'] - $shortcode_params['start'] ) );
260
261 $params_in_shortcode = $shortcode_params;
262 unset( $params_in_shortcode['start'] );
263 unset( $params_in_shortcode['end'] );
264 $replace[ $new_replace ] = array(
265 'shortcode' => '[' . $new_replace . ']',
266 'params' => $params_in_shortcode,
267 'shortcode_original' => '[' . $exist_replace . ']',
268 );
269 } else {
270 $shortcode_params = false;
271 }
272
273 } while ( ! empty( $shortcode_params ) );
274
275 }
276
277 return array(
278 'content' => $content_text,
279 'shortcodes' => $replace
280 );
281 }
282
283
284 // ---------------------------------------------------------------------------------------------------------------------
285
286 /**
287 * >=BS - for 'Billing fields' - Get fields from booking form at the settings page or return false if no fields
288 *
289 * @param string $booking_form
290 * @return mixed false | array( $fields_count, $fields_matches )
291 */
292 function wpbc_get_fields_from_booking_form( $booking_form = '' ){
293
294 if ( empty( $booking_form ) ) {
295 $booking_form = get_bk_option( 'booking_form' );
296 }
297
298 $types = 'text[*]?|email[*]?|time[*]?|textarea[*]?|select[*]?|selectbox[*]?|checkbox[*]?|radio|acceptance|captchac|captchar|file[*]?|quiz';
299 $regex = '%\[\s*(' . $types . ')(\s+[a-zA-Z][0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)?((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
300 $regex2 = '%\[\s*(country[*]?|starttime[*]?|endtime[*]?)(\s*[a-zA-Z]*[0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
301 $fields_count = preg_match_all( $regex, $booking_form, $fields_matches );
302 $fields_count2 = preg_match_all( $regex2, $booking_form, $fields_matches2 );
303
304 //Gathering Together 2 arrays $fields_matches and $fields_matches2
305 foreach ( $fields_matches2 as $key => $value ) {
306 if ( $key == 2 ) {
307 $value = $fields_matches2[1];
308 }
309 foreach ( $value as $v ) {
310 $fields_matches[ $key ][ count( $fields_matches[ $key ] ) ] = $v;
311 }
312 }
313 $fields_count += $fields_count2;
314
315 if ( $fields_count > 0 ) {
316 return array( $fields_count, $fields_matches );
317 } else {
318 return false;
319 }
320 }
321
322
323 /**
324 * >= BM - for 'Advanced costs' -- Get only SELECT, CHECKBOX & RADIO fields from booking form at the settings page or return false if no fields
325 *
326 * @param string $booking_form
327 * @return mixed false | array( $fields_count, $fields_matches )
328 */
329 function wpbc_get_select_checkbox_fields_from_booking_form( $booking_form = '' ){
330
331 if ( empty( $booking_form ) )
332 $booking_form = get_bk_option( 'booking_form' );
333
334 $types = 'select[*]?|selectbox[*]?|checkbox[*]?|radio[*]?'; // FixIn: 8.1.3.7.
335 $regex = '%\[\s*(' . $types . ')(\s+[a-zA-Z][0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)?((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
336
337 $fields_count = preg_match_all($regex, $booking_form, $fields_matches) ;
338
339 if ( $fields_count > 0 )
340 return array( $fields_count, $fields_matches );
341 else return false;
342 }
343
344
345 // ---------------------------------------------------------------------------------------------------------------------
346 // Used in 'Search Availability' functionality
347 // ---------------------------------------------------------------------------------------------------------------------
348
349 /**
350 * Parse Option Values to get Title and Value, if used @@ '10 AM - 2PM@@10:00 - 14:00' -> [ '10 AM - 2PM', '10:00 - 14:00' ]
351 *
352 * @param $option '10 AM - 2PM@@10:00 - 14:00' OR '10:00 - 14:00'
353 *
354 * @return array [ '10 AM - 2PM', '10:00 - 14:00' ] OR [ '10:00 - 14:00', '10:00 - 14:00' ]
355 *
356 *
357 * Example #1: wpbc_field_option__get_tile_value( '10 AM - 2PM@@10:00 - 14:00' ) -> [ '10 AM - 2PM', '10:00 - 14:00' ]
358 * Example #2: wpbc_field_option__get_tile_value( '10:00 - 14:00' ) -> [ '10:00 - 14:00', '10:00 - 14:00' ]
359 */
360 function wpbc_shortcode_option__get_tile_value( $option ) {
361 $option_title_value_arr = explode( '@@', $option );
362
363 $option_title = $option_title_value_arr[0];
364 $option_value = ( count( $option_title_value_arr ) == 2 ) ? $option_title_value_arr[1] : $option_title_value_arr[0];
365
366 return array( $option_title, $option_value );
367 }
368
369
370
371 /**
372 * Find shortcodes with Options: [search_quantity "---@@1" 'Title A@@value2' "3"] or [search_quantity]
373 *
374 * @param $content string ex.: '<a href="[search_result_url]" class="wpbc_book_now_link"> ... [search_result_button "Booking" 'other' "d'ata"] ... '
375 * @param $shortcode_to_search string ex.: 'search_result_url'
376 *
377 * @return array [
378 * [search_result_url] => [
379 * [replace] => [search_result_url]
380 * [options] => []
381 * ]
382 * [search_result_button] => [
383 * [replace] => [search_result_button "Booking" 'other' "d'ata"]
384 * [options] => [
385 * [0] => Booking
386 * [1] => other
387 * [2] => d'ata
388 * ]
389 * ]
390 * ]
391 *
392 * Example: to search one shortcode: wpbc_shortcode_options__get( $text, 'search_result_button' );
393 * Example: to search several shortcode: wpbc_shortcode_options__get( $text, 'search_result_button|search_result_url' );
394 *
395 */
396 function wpbc_shortcode_with_options__find( $content, $shortcode_to_search = 'search_result_button' . '|' . 'search_result_url' ) {
397
398 $shortcodes_arr = array();
399
400 $regex = '%\[\s*(' . $shortcode_to_search . ')\s*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
401
402 $fields_count = preg_match_all($regex, $content, $fields_matches, PREG_SET_ORDER ) ;
403
404 if ( $fields_count > 0 ) {
405 foreach ( $fields_matches as $found_shortcodes_arr ) {
406 list( $full_shortcode_to_replace, $shortcode_name, $shortcode_params ) = $found_shortcodes_arr;
407
408 $shortcodes_arr[$shortcode_name] = array( 'replace' => $full_shortcode_to_replace, 'options' => array() );
409
410 $pattern_to_search = '%\s*[\'"]([^\'"]*)[\'"]\s*%';
411 $pattern_to_search = '%\s*[\']([^\']*)[\']|[\"]([^\"]*)[\"]\s*%';
412 $fields_count = preg_match_all($pattern_to_search, $shortcode_params, $options_matches, PREG_SET_ORDER ) ;
413
414 foreach ( $options_matches as $found_options_arr ) {
415 $found_option_to_replace = $found_options_arr[0];
416 $found_option_value = $found_options_arr[ ( count( $found_options_arr ) - 1 ) ];
417
418 $shortcodes_arr[$shortcode_name]['options'][]=$found_option_value;
419 }
420 }
421 }
422
423 return $shortcodes_arr;
424 }
425
426
427
428 /**
429 * Find shortcodes with Names and Options: [selectbox location "Any@@" "United States@@USA" "France" "Spain"]
430 *
431 * @param $content string ex.: '... text ... [selectbox search_time 'Any@@' "10 AM - 2PM@@10:00 - 14:00" '3 PM - 4PM@@15:00 - 16:00'] .... [selectbox othersearch_time] ... [selectbox location "Any@@" "Spain Country@@Spain" "France" "Singapur" "Tirana"] .... '
432 * @param $shortcode_to_search string ex.: 'select|selectbox'
433 *
434 * @return array [
435 * [search_time] => [
436 * [replace] => [selectbox search_time 'Any@@' "10 AM - 2PM@@10:00 - 14:00" '3 PM - 4PM@@15:00 - 16:00']
437 * [type] => selectbox
438 * [name] => search_time
439 * [options] => [
440 * [0] => Any@@
441 * [1] => 10 AM - 2PM@@10:00 - 14:00
442 * [2] => 3 PM - 4PM@@15:00 - 16:00
443 * ]
444 * ],
445 * [othersearch_time] => [
446 * [replace] => [selectbox othersearch_time]
447 * [type] => select
448 * [name] => othersearch_time
449 * [options] => []
450 * ]
451 * [location] => [
452 * [replace] => [selectbox location "Any@@" "Spain Country@@Spain" "France" "Singapur" "Tirana"]
453 * [type] => select
454 * [name] => location
455 * [options] => [
456 * [0] => Any@@
457 * [1] => Spain Country@@Spain
458 * [2] => France
459 * [3] => Singapur
460 * [4] => Tirana
461 * ]
462 * ]
463 * ]
464 *
465 * Example: to search several shortcode types: wpbc_shortcode_with_name_and_options__find( $text, 'select|selectbox' );
466 * Example: to search one shortcode type: wpbc_shortcode_with_name_and_options__find( $text, 'checkbox' );
467 *
468 */
469 function wpbc_shortcode_with_name_and_options__find( $content, $shortcode_to_search = 'select' . '|' . 'selectbox' ) {
470
471 $shortcodes_arr = array();
472
473 $regex = '%\[\s*(' . $shortcode_to_search . ')\s+([^\s]+)\s*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
474
475 $fields_count = preg_match_all($regex, $content, $fields_matches, PREG_SET_ORDER ) ;
476
477 if ( $fields_count > 0 ) {
478 foreach ( $fields_matches as $found_shortcodes_arr ) {
479 list( $full_shortcode_to_replace, $shortcode_type, $shortcode_name, $shortcode_params ) = $found_shortcodes_arr;
480
481 $shortcodes_arr[$shortcode_name] = array(
482 'replace' => $full_shortcode_to_replace,
483 'type' => $shortcode_type,
484 'name' => $shortcode_name,
485 'options' => array()
486 );
487
488 $pattern_to_search = '%\s*[\'"]([^\'"]*)[\'"]\s*%';
489 $pattern_to_search = '%\s*[\']([^\']*)[\']|[\"]([^\"]*)[\"]\s*%';
490 $fields_count = preg_match_all($pattern_to_search, $shortcode_params, $options_matches, PREG_SET_ORDER ) ;
491
492 foreach ( $options_matches as $found_options_arr ) {
493 $found_option_to_replace = $found_options_arr[0];
494 $found_option_value = $found_options_arr[ ( count( $found_options_arr ) - 1 ) ];
495
496 $shortcodes_arr[$shortcode_name]['options'][]=$found_option_value;
497 }
498 }
499 }
500
501 return $shortcodes_arr;
502 }
503
504
505
506 // ---------------------------------------------------------------------------------------------------------------------
507 // Replace <script> tags to <#
508 // ---------------------------------------------------------------------------------------------------------------------
509
510
511 /**
512 * Replace <script> tags to <#
513 *
514 * @param $result
515 *
516 * @return array|string|string[]|null
517 */
518 function wpbc_replace__js_scripts__to__tpl_scripts( $result ){
519
520 // Replace <script> tags to <#
521 $pattern = '/<script\s*(type=[\'"]+text\/javascript[\'"]+)?\s*>/i';
522 $result = preg_replace( $pattern, '<#', $result );
523
524 // Replace </script> tags to #>
525 $pattern = '/<\/script>/i';
526 $result = preg_replace( $pattern, '#>', $result );
527
528 return $result;
529 }