| 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( "@(<|<)br\s*/?(>|>)(\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', '"', html_entity_decode( $text ) ); // escape quote symbols; |
| 105 |
$text = preg_replace( "/(\\\\')/i", ''', 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( ' ' ); |
| 126 |
$temporary_symbols = array( '^space^' ); |
| 127 |
$output = str_replace( $original_symbols, $temporary_symbols, $output ); // -> ^space^ |
| 128 |
|
| 129 |
// Escaping ? |
| 130 |
$output = esc_js( $output ); // it adds '\n' symbols | " into " | < -> < ... |
| 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( ' ' ); |
| 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. \"Quoted text\"\n3. \'Single quoted text\'\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 = function_exists( 'wpbc_get__booking_form_fields__configuration' ) ? wpbc_get__booking_form_fields__configuration( wpbc_get_default_resource(), 'standard' ) : ''; |
| 296 |
} |
| 297 |
|
| 298 |
$types = 'text[*]?|email[*]?|time[*]?|textarea[*]?|select[*]?|selectbox[*]?|checkbox[*]?|radio|acceptance|captchac|captchar|file[*]?|quiz'; |
| 299 |
$fields_data = wpbc_get_booking_form_shortcode_fields_by_types( $booking_form, $types ); |
| 300 |
$fields_count = $fields_data[0]; |
| 301 |
$fields_matches = $fields_data[1]; |
| 302 |
|
| 303 |
$fields_data2 = wpbc_get_booking_form_shortcode_fields_by_types( $booking_form, 'country[*]?|starttime[*]?|endtime[*]?', false, true ); |
| 304 |
$fields_count2 = $fields_data2[0]; |
| 305 |
$fields_matches2 = $fields_data2[1]; |
| 306 |
|
| 307 |
//Gathering Together 2 arrays $fields_matches and $fields_matches2 |
| 308 |
foreach ( $fields_matches2 as $key => $value ) { |
| 309 |
if ( $key == 2 ) { |
| 310 |
$value = $fields_matches2[1]; |
| 311 |
} |
| 312 |
foreach ( $value as $v ) { |
| 313 |
$fields_matches[ $key ][ count( $fields_matches[ $key ] ) ] = $v; |
| 314 |
} |
| 315 |
} |
| 316 |
$fields_count += $fields_count2; |
| 317 |
|
| 318 |
if ( $fields_count > 0 ) { |
| 319 |
return array( $fields_count, $fields_matches ); |
| 320 |
} else { |
| 321 |
return false; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
/** |
| 327 |
* >= BM - for 'Advanced costs' -- Get only SELECT, CHECKBOX & RADIO fields from booking form at the settings page or return false if no fields |
| 328 |
* |
| 329 |
* @param string $booking_form |
| 330 |
* @return mixed false | array( $fields_count, $fields_matches ) |
| 331 |
*/ |
| 332 |
function wpbc_get_select_checkbox_fields_from_booking_form( $booking_form = '' ){ |
| 333 |
|
| 334 |
if ( empty( $booking_form ) ) |
| 335 |
$booking_form = function_exists( 'wpbc_get__booking_form_fields__configuration' ) ? wpbc_get__booking_form_fields__configuration( wpbc_get_default_resource(), 'standard' ) : ''; |
| 336 |
|
| 337 |
$types = 'select[*]?|selectbox[*]?|checkbox[*]?|radio[*]?'; // FixIn: 8.1.3.7. |
| 338 |
$fields_data = wpbc_get_booking_form_shortcode_fields_by_types( $booking_form, $types ); |
| 339 |
$fields_count = $fields_data[0]; |
| 340 |
$fields_matches = $fields_data[1]; |
| 341 |
|
| 342 |
if ( $fields_count > 0 ) |
| 343 |
return array( $fields_count, $fields_matches ); |
| 344 |
else return false; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/** |
| 349 |
* Get shortcode fields in the legacy preg_match_all() result shape used by settings pages. |
| 350 |
* |
| 351 |
* The parser keeps option assignments such as default="1" in the options group and exposes only standalone |
| 352 |
* quoted tokens as field values. |
| 353 |
* |
| 354 |
* @param string $booking_form |
| 355 |
* @param string $types |
| 356 |
* @param bool $is_name_required |
| 357 |
* @param bool $is_name_forced_to_type |
| 358 |
* |
| 359 |
* @return array |
| 360 |
*/ |
| 361 |
function wpbc_get_booking_form_shortcode_fields_by_types( $booking_form, $types, $is_name_required = true, $is_name_forced_to_type = false ) { |
| 362 |
|
| 363 |
$fields_matches = array_fill( 0, 5, array() ); |
| 364 |
$regex = '%\[\s*(' . $types . ')(?=\s|\])((?:[^\]"\']+|"[^"]*"|\'[^\']*\')*)\]%'; |
| 365 |
$found_fields = array(); |
| 366 |
|
| 367 |
preg_match_all( $regex, $booking_form, $found_fields, PREG_SET_ORDER ); |
| 368 |
|
| 369 |
foreach ( $found_fields as $found_field ) { |
| 370 |
|
| 371 |
$shortcode_tail = $found_field[2]; |
| 372 |
$field_name = ''; |
| 373 |
$field_tail = $shortcode_tail; |
| 374 |
|
| 375 |
if ( $is_name_forced_to_type ) { |
| 376 |
$field_name = $found_field[1]; |
| 377 |
} elseif ( preg_match( '%^\s*([a-zA-Z][0-9a-zA-Z:._-]*)(.*)$%s', $shortcode_tail, $shortcode_parts ) ) { |
| 378 |
$field_name = $shortcode_parts[1]; |
| 379 |
$field_tail = $shortcode_parts[2]; |
| 380 |
} |
| 381 |
|
| 382 |
if ( $is_name_required && ( '' === $field_name ) ) { |
| 383 |
continue; |
| 384 |
} |
| 385 |
|
| 386 |
if ( '' === $field_name ) { |
| 387 |
$field_name = $found_field[1]; |
| 388 |
} |
| 389 |
|
| 390 |
list( $shortcode_options, $shortcode_values ) = wpbc_parse_form_shortcode_options_values( $field_tail ); |
| 391 |
|
| 392 |
$fields_matches[0][] = trim( $found_field[0] ); |
| 393 |
$fields_matches[1][] = trim( $found_field[1] ); |
| 394 |
$fields_matches[2][] = trim( $field_name ); |
| 395 |
$fields_matches[3][] = trim( $shortcode_options ); |
| 396 |
$fields_matches[4][] = trim( $shortcode_values ); |
| 397 |
} |
| 398 |
|
| 399 |
return array( count( $fields_matches[0] ), $fields_matches ); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
if ( ! function_exists( 'wpbc_parse_form_shortcode_options_values' ) ) { |
| 404 |
function wpbc_parse_form_shortcode_options_values( $shortcode_tail ){ |
| 405 |
|
| 406 |
$shortcode_options = trim( $shortcode_tail ); |
| 407 |
$shortcode_values = ''; |
| 408 |
|
| 409 |
$value_matches = array(); |
| 410 |
preg_match_all( '%(?:"[^"]*"|\'[^\']*\')%', $shortcode_tail, $value_matches, PREG_OFFSET_CAPTURE ); |
| 411 |
|
| 412 |
$standalone_values = array(); |
| 413 |
foreach ( $value_matches[0] as $value_match ) { |
| 414 |
$value_offset = $value_match[1]; |
| 415 |
$prev_offset = $value_offset - 1; |
| 416 |
|
| 417 |
if ( ( $prev_offset >= 0 ) && ! ctype_space( $shortcode_tail[ $prev_offset ] ) ) { |
| 418 |
continue; |
| 419 |
} |
| 420 |
|
| 421 |
while ( ( $prev_offset >= 0 ) && ctype_space( $shortcode_tail[ $prev_offset ] ) ) { |
| 422 |
$prev_offset--; |
| 423 |
} |
| 424 |
|
| 425 |
if ( ( $prev_offset >= 0 ) && in_array( $shortcode_tail[ $prev_offset ], array( '=', ':' ), true ) ) { |
| 426 |
continue; |
| 427 |
} |
| 428 |
|
| 429 |
$standalone_values[] = $value_match; |
| 430 |
} |
| 431 |
|
| 432 |
if ( ! empty( $standalone_values ) ) { |
| 433 |
$shortcode_options = trim( substr( $shortcode_tail, 0, $standalone_values[0][1] ) ); |
| 434 |
$shortcode_values = implode( |
| 435 |
' ', |
| 436 |
array_map( |
| 437 |
function ( $value_match ) { |
| 438 |
return $value_match[0]; |
| 439 |
}, |
| 440 |
$standalone_values |
| 441 |
) |
| 442 |
); |
| 443 |
} |
| 444 |
|
| 445 |
return array( $shortcode_options, $shortcode_values ); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
// --------------------------------------------------------------------------------------------------------------------- |
| 451 |
// Used in 'Search Availability' functionality |
| 452 |
// --------------------------------------------------------------------------------------------------------------------- |
| 453 |
|
| 454 |
/** |
| 455 |
* Parse Option Values to get Title and Value, if used @@ '10 AM - 2PM@@10:00 - 14:00' -> [ '10 AM - 2PM', '10:00 - 14:00' ] |
| 456 |
* |
| 457 |
* @param $option '10 AM - 2PM@@10:00 - 14:00' OR '10:00 - 14:00' |
| 458 |
* |
| 459 |
* @return array [ '10 AM - 2PM', '10:00 - 14:00' ] OR [ '10:00 - 14:00', '10:00 - 14:00' ] |
| 460 |
* |
| 461 |
* |
| 462 |
* Example #1: wpbc_field_option__get_tile_value( '10 AM - 2PM@@10:00 - 14:00' ) -> [ '10 AM - 2PM', '10:00 - 14:00' ] |
| 463 |
* Example #2: wpbc_field_option__get_tile_value( '10:00 - 14:00' ) -> [ '10:00 - 14:00', '10:00 - 14:00' ] |
| 464 |
*/ |
| 465 |
function wpbc_shortcode_option__get_tile_value( $option ) { |
| 466 |
$option_title_value_arr = explode( '@@', $option ); |
| 467 |
|
| 468 |
$option_title = $option_title_value_arr[0]; |
| 469 |
$option_value = ( count( $option_title_value_arr ) == 2 ) ? $option_title_value_arr[1] : $option_title_value_arr[0]; |
| 470 |
|
| 471 |
return array( $option_title, $option_value ); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
/** |
| 477 |
* Find shortcodes with Options: [search_quantity "---@@1" 'Title A@@value2' "3"] or [search_quantity] |
| 478 |
* |
| 479 |
* @param $content string ex.: '<a href="[search_result_url]" class="wpbc_book_now_link"> ... [search_result_button "Booking" 'other' "d'ata"] ... ' |
| 480 |
* @param $shortcode_to_search string ex.: 'search_result_url' |
| 481 |
* |
| 482 |
* @return array [ |
| 483 |
* [search_result_url] => [ |
| 484 |
* [replace] => [search_result_url] |
| 485 |
* [options] => [] |
| 486 |
* ] |
| 487 |
* [search_result_button] => [ |
| 488 |
* [replace] => [search_result_button "Booking" 'other' "d'ata"] |
| 489 |
* [options] => [ |
| 490 |
* [0] => Booking |
| 491 |
* [1] => other |
| 492 |
* [2] => d'ata |
| 493 |
* ] |
| 494 |
* ] |
| 495 |
* ] |
| 496 |
* |
| 497 |
* Example: to search one shortcode: wpbc_shortcode_options__get( $text, 'search_result_button' ); |
| 498 |
* Example: to search several shortcode: wpbc_shortcode_options__get( $text, 'search_result_button|search_result_url' ); |
| 499 |
* |
| 500 |
*/ |
| 501 |
function wpbc_shortcode_with_options__find( $content, $shortcode_to_search = 'search_result_button' . '|' . 'search_result_url' ) { |
| 502 |
|
| 503 |
$shortcodes_arr = array(); |
| 504 |
|
| 505 |
$regex = '%\[\s*(' . $shortcode_to_search . ')\s*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%'; |
| 506 |
|
| 507 |
$fields_count = preg_match_all($regex, $content, $fields_matches, PREG_SET_ORDER ) ; |
| 508 |
|
| 509 |
if ( $fields_count > 0 ) { |
| 510 |
foreach ( $fields_matches as $found_shortcodes_arr ) { |
| 511 |
list( $full_shortcode_to_replace, $shortcode_name, $shortcode_params ) = $found_shortcodes_arr; |
| 512 |
|
| 513 |
$shortcodes_arr[$shortcode_name] = array( 'replace' => $full_shortcode_to_replace, 'options' => array() ); |
| 514 |
|
| 515 |
$pattern_to_search = '%\s*[\'"]([^\'"]*)[\'"]\s*%'; |
| 516 |
$pattern_to_search = '%\s*[\']([^\']*)[\']|[\"]([^\"]*)[\"]\s*%'; |
| 517 |
$fields_count = preg_match_all($pattern_to_search, $shortcode_params, $options_matches, PREG_SET_ORDER ) ; |
| 518 |
|
| 519 |
foreach ( $options_matches as $found_options_arr ) { |
| 520 |
$found_option_to_replace = $found_options_arr[0]; |
| 521 |
$found_option_value = $found_options_arr[ ( count( $found_options_arr ) - 1 ) ]; |
| 522 |
|
| 523 |
$shortcodes_arr[$shortcode_name]['options'][]=$found_option_value; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
return $shortcodes_arr; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
/** |
| 534 |
* Find shortcodes with Names and Options: [selectbox location "Any@@" "United States@@USA" "France" "Spain"] |
| 535 |
* |
| 536 |
* @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"] .... ' |
| 537 |
* @param $shortcode_to_search string ex.: 'select|selectbox' |
| 538 |
* |
| 539 |
* @return array [ |
| 540 |
* [search_time] => [ |
| 541 |
* [replace] => [selectbox search_time 'Any@@' "10 AM - 2PM@@10:00 - 14:00" '3 PM - 4PM@@15:00 - 16:00'] |
| 542 |
* [type] => selectbox |
| 543 |
* [name] => search_time |
| 544 |
* [options] => [ |
| 545 |
* [0] => Any@@ |
| 546 |
* [1] => 10 AM - 2PM@@10:00 - 14:00 |
| 547 |
* [2] => 3 PM - 4PM@@15:00 - 16:00 |
| 548 |
* ] |
| 549 |
* ], |
| 550 |
* [othersearch_time] => [ |
| 551 |
* [replace] => [selectbox othersearch_time] |
| 552 |
* [type] => select |
| 553 |
* [name] => othersearch_time |
| 554 |
* [options] => [] |
| 555 |
* ] |
| 556 |
* [location] => [ |
| 557 |
* [replace] => [selectbox location "Any@@" "Spain Country@@Spain" "France" "Singapur" "Tirana"] |
| 558 |
* [type] => select |
| 559 |
* [name] => location |
| 560 |
* [options] => [ |
| 561 |
* [0] => Any@@ |
| 562 |
* [1] => Spain Country@@Spain |
| 563 |
* [2] => France |
| 564 |
* [3] => Singapur |
| 565 |
* [4] => Tirana |
| 566 |
* ] |
| 567 |
* ] |
| 568 |
* ] |
| 569 |
* |
| 570 |
* Example: to search several shortcode types: wpbc_shortcode_with_name_and_options__find( $text, 'select|selectbox' ); |
| 571 |
* Example: to search one shortcode type: wpbc_shortcode_with_name_and_options__find( $text, 'checkbox' ); |
| 572 |
* |
| 573 |
*/ |
| 574 |
function wpbc_shortcode_with_name_and_options__find( $content, $shortcode_to_search = 'select' . '|' . 'selectbox' ) { |
| 575 |
|
| 576 |
$shortcodes_arr = array(); |
| 577 |
|
| 578 |
$regex = '%\[\s*(' . $shortcode_to_search . ')\s+([^\s]+)\s*((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%'; |
| 579 |
|
| 580 |
$fields_count = preg_match_all($regex, $content, $fields_matches, PREG_SET_ORDER ) ; |
| 581 |
|
| 582 |
if ( $fields_count > 0 ) { |
| 583 |
foreach ( $fields_matches as $found_shortcodes_arr ) { |
| 584 |
list( $full_shortcode_to_replace, $shortcode_type, $shortcode_name, $shortcode_params ) = $found_shortcodes_arr; |
| 585 |
|
| 586 |
$shortcodes_arr[$shortcode_name] = array( |
| 587 |
'replace' => $full_shortcode_to_replace, |
| 588 |
'type' => $shortcode_type, |
| 589 |
'name' => $shortcode_name, |
| 590 |
'options' => array() |
| 591 |
); |
| 592 |
|
| 593 |
$pattern_to_search = '%\s*[\'"]([^\'"]*)[\'"]\s*%'; |
| 594 |
$pattern_to_search = '%\s*[\']([^\']*)[\']|[\"]([^\"]*)[\"]\s*%'; |
| 595 |
$fields_count = preg_match_all($pattern_to_search, $shortcode_params, $options_matches, PREG_SET_ORDER ) ; |
| 596 |
|
| 597 |
foreach ( $options_matches as $found_options_arr ) { |
| 598 |
$found_option_to_replace = $found_options_arr[0]; |
| 599 |
$found_option_value = $found_options_arr[ ( count( $found_options_arr ) - 1 ) ]; |
| 600 |
|
| 601 |
$shortcodes_arr[$shortcode_name]['options'][]=$found_option_value; |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
return $shortcodes_arr; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
// --------------------------------------------------------------------------------------------------------------------- |
| 612 |
// Replace <script> tags to <# |
| 613 |
// --------------------------------------------------------------------------------------------------------------------- |
| 614 |
|
| 615 |
|
| 616 |
/** |
| 617 |
* Replace <script> tags to <# |
| 618 |
* |
| 619 |
* @param $result |
| 620 |
* |
| 621 |
* @return array|string|string[]|null |
| 622 |
*/ |
| 623 |
function wpbc_replace__js_scripts__to__tpl_scripts( $result ){ |
| 624 |
|
| 625 |
// Replace <script> tags to <# |
| 626 |
$pattern = '/<script\s*(type=[\'"]+text\/javascript[\'"]+)?\s*>/i'; |
| 627 |
$result = preg_replace( $pattern, '<#', $result ); |
| 628 |
|
| 629 |
// Replace </script> tags to #> |
| 630 |
$pattern = '/<\/script>/i'; |
| 631 |
$result = preg_replace( $pattern, '#>', $result ); |
| 632 |
|
| 633 |
return $result; |
| 634 |
} |
| 635 |
|