PluginProbe
Booking Calendar / 10.10.1
Booking Calendar v10.10.1
11.8.3 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 All 203 releases
booking / includes / _functions / str_regex.php

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

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