← All changes
|
includes/booking-appointment/booking-appointment__config.php
+88
-6
11.6
→
11.9
View file →
| @@ -24,8 +24,66 @@ | ||
| 24 | 24 | return array_values( array_unique( array_filter( array_map( 'absint', (array) $value ) ) ) ); |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | + * Convert a shortcode-style value to a strict Boolean. | |
| 29 | + * | |
| 30 | + * @param mixed $raw_value Raw Boolean-like value. | |
| 31 | + * @param bool $default_value Value used when the raw value is null. | |
| 32 | + * | |
| 33 | + * @return bool Normalized Boolean. | |
| 34 | + */ | |
| 35 | +function wpbc_booking_appointment_normalize_boolean( $raw_value, $default_value = false ) { | |
| 36 | + if ( null === $raw_value ) { | |
| 37 | + return (bool) $default_value; | |
| 38 | + } | |
| 39 | + | |
| 40 | + if ( is_string( $raw_value ) ) { | |
| 41 | + $raw_value = strtolower( trim( $raw_value ) ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + return ! in_array( $raw_value, array( false, 0, '0', 'false', 'off', 'no', '' ), true ); | |
| 45 | +} | |
| 46 | + | |
| 47 | +/** | |
| 48 | + * Normalize a safe public Appointment catalog item width. | |
| 49 | + * | |
| 50 | + * Bare numbers are treated as pixels. Only simple dimensions are accepted so | |
| 51 | + * a shortcode value cannot introduce an arbitrary inline CSS declaration. | |
| 52 | + * | |
| 53 | + * @param mixed $raw_width Raw shortcode width. | |
| 54 | + * | |
| 55 | + * @return string Normalized CSS width or an empty string for automatic width. | |
| 56 | + */ | |
| 57 | +function wpbc_booking_appointment_normalize_css_width( $raw_width ) { | |
| 58 | + if ( is_int( $raw_width ) || is_float( $raw_width ) ) { | |
| 59 | + $raw_width = (string) $raw_width . 'px'; | |
| 60 | + } | |
| 61 | + | |
| 62 | + $raw_width = strtolower( trim( (string) $raw_width ) ); | |
| 63 | + if ( '' === $raw_width || 'auto' === $raw_width ) { | |
| 64 | + return ''; | |
| 65 | + } | |
| 66 | + if ( preg_match( '/^\d+(?:\.\d+)?$/', $raw_width ) ) { | |
| 67 | + $raw_width .= 'px'; | |
| 68 | + } | |
| 69 | + if ( ! preg_match( '/^(\d+(?:\.\d+)?)(px|%|rem|em|vw)$/', $raw_width, $matches ) ) { | |
| 70 | + return ''; | |
| 71 | + } | |
| 72 | + | |
| 73 | + $numeric_width = (float) $matches[1]; | |
| 74 | + $width_unit = $matches[2]; | |
| 75 | + $maximum_width = in_array( $width_unit, array( '%', 'vw' ), true ) ? 100 : ( 'px' === $width_unit ? 2000 : 100 ); | |
| 76 | + if ( $numeric_width <= 0 || $numeric_width > $maximum_width ) { | |
| 77 | + return ''; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $normalized_width = rtrim( rtrim( number_format( $numeric_width, 4, '.', '' ), '0' ), '.' ); | |
| 81 | + | |
| 82 | + return $normalized_width . $width_unit; | |
| 83 | +} | |
| 84 | + | |
| 85 | +/** | |
| 28 | 86 | * Normalize shortcode attributes into the stable AJAX configuration contract. |
| 29 | 87 | * |
| 30 | 88 | * @param mixed $attributes Raw shortcode attributes or decoded configuration. |
| 31 | 89 | * |
| @@ -44,8 +102,20 @@ | ||
| 44 | 102 | 'calendar_dates_end' => '', |
| 45 | 103 | 'options' => '', |
| 46 | 104 | 'form_type' => '', |
| 47 | 105 | 'auto_select_provider' => false, |
| 106 | + 'catalog_layout' => 'grid', | |
| 107 | + 'show_resource_filters' => false, | |
| 108 | + 'show_resource_image' => true, | |
| 109 | + 'show_resource_title' => true, | |
| 110 | + 'show_resource_description' => true, | |
| 111 | + 'catalog_item_width' => '', | |
| 112 | + 'catalog_item_max_width' => 0, | |
| 113 | + 'catalog_grid_items_per_row' => 0, | |
| 114 | + 'catalog_list_items_per_row' => 0, | |
| 115 | + 'show_resource_hierarchy' => true, | |
| 116 | + 'show_availability' => true, | |
| 117 | + 'show_starting_price' => true, | |
| 48 | 118 | 'show_progress' => true, |
| 49 | 119 | 'progress_item_1_title' => null, |
| 50 | 120 | 'progress_item_1_number' => null, |
| 51 | 121 | 'progress_item_2_title' => null, |
| @@ -125,12 +195,25 @@ | ||
| 125 | 195 | |
| 126 | 196 | $config['options'] = sanitize_text_field( (string) $config['options'] ); |
| 127 | 197 | $config['form_type'] = sanitize_text_field( (string) $config['form_type'] ); |
| 128 | 198 | $config['return_url'] = esc_url_raw( (string) $config['return_url'] ); |
| 129 | - $auto_select_provider = is_string( $config['auto_select_provider'] ) ? strtolower( trim( $config['auto_select_provider'] ) ) : $config['auto_select_provider']; | |
| 130 | - $config['auto_select_provider'] = ! in_array( $auto_select_provider, array( false, 0, '0', 'false', 'off', 'no' ), true ); | |
| 131 | - $show_progress = is_string( $config['show_progress'] ) ? strtolower( trim( $config['show_progress'] ) ) : $config['show_progress']; | |
| 132 | - $config['show_progress'] = ! in_array( $show_progress, array( false, 0, '0', 'false', 'off', 'no', '' ), true ); | |
| 199 | + $config['auto_select_provider'] = wpbc_booking_appointment_normalize_boolean( $config['auto_select_provider'] ); | |
| 200 | + $config['catalog_layout'] = 'list' === sanitize_key( (string) $config['catalog_layout'] ) ? 'list' : 'grid'; | |
| 201 | + $config['show_resource_filters'] = wpbc_booking_appointment_normalize_boolean( $config['show_resource_filters'] ); | |
| 202 | + $config['show_resource_image'] = wpbc_booking_appointment_normalize_boolean( $config['show_resource_image'], true ); | |
| 203 | + $config['show_resource_title'] = wpbc_booking_appointment_normalize_boolean( $config['show_resource_title'], true ); | |
| 204 | + $config['show_resource_description'] = wpbc_booking_appointment_normalize_boolean( $config['show_resource_description'], true ); | |
| 205 | + $config['catalog_item_width'] = wpbc_booking_appointment_normalize_css_width( $config['catalog_item_width'] ); | |
| 206 | + $config['catalog_item_max_width'] = absint( $config['catalog_item_max_width'] ); | |
| 207 | + if ( $config['catalog_item_max_width'] > 0 ) { | |
| 208 | + $config['catalog_item_max_width'] = min( 1200, max( 280, $config['catalog_item_max_width'] ) ); | |
| 209 | + } | |
| 210 | + $config['catalog_grid_items_per_row'] = min( 12, absint( $config['catalog_grid_items_per_row'] ) ); | |
| 211 | + $config['catalog_list_items_per_row'] = min( 12, absint( $config['catalog_list_items_per_row'] ) ); | |
| 212 | + $config['show_resource_hierarchy'] = wpbc_booking_appointment_normalize_boolean( $config['show_resource_hierarchy'], true ); | |
| 213 | + $config['show_availability'] = wpbc_booking_appointment_normalize_boolean( $config['show_availability'], true ); | |
| 214 | + $config['show_starting_price'] = wpbc_booking_appointment_normalize_boolean( $config['show_starting_price'], true ); | |
| 215 | + $config['show_progress'] = wpbc_booking_appointment_normalize_boolean( $config['show_progress'], true ); | |
| 133 | 216 | $display_text_keys = array( |
| 134 | 217 | 'progress_item_1_title', |
| 135 | 218 | 'progress_item_1_number', |
| 136 | 219 | 'progress_item_2_title', |
| @@ -146,10 +229,9 @@ | ||
| 146 | 229 | if ( null !== $config[ $display_text_key ] ) { |
| 147 | 230 | $config[ $display_text_key ] = sanitize_text_field( (string) $config[ $display_text_key ] ); |
| 148 | 231 | } |
| 149 | 232 | } |
| 150 | - $allow_past = is_string( $config['allow_past'] ) ? strtolower( trim( $config['allow_past'] ) ) : $config['allow_past']; | |
| 151 | - $config['allow_past'] = ! in_array( $allow_past, array( false, 0, '0', 'false', 'off', 'no', '' ), true ); | |
| 233 | + $config['allow_past'] = wpbc_booking_appointment_normalize_boolean( $config['allow_past'] ); | |
| 152 | 234 | |
| 153 | 235 | return (array) apply_filters( 'wpbc_booking_appointment_normalized_config', $config, $attributes ); |
| 154 | 236 | } |
| 155 | 237 | |