← All changes
|
includes/booking-resource-selector/booking-resource-selector__config.php
+89
-23
11.5
→
11.9
View file →
| @@ -44,8 +44,47 @@ | ||
| 44 | 44 | return ! in_array( $raw_value, array( false, 0, '0', 'false', 'off', 'no', '' ), true ); |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | + * Normalize a safe public catalog item width. | |
| 49 | + * | |
| 50 | + * Bare numbers are treated as pixels for shortcode convenience. Only simple | |
| 51 | + * dimensions are accepted; CSS functions and arbitrary declarations are | |
| 52 | + * rejected before the value can reach an inline custom property. | |
| 53 | + * | |
| 54 | + * @param mixed $raw_width Raw shortcode width. | |
| 55 | + * | |
| 56 | + * @return string Normalized CSS width or an empty string for automatic width. | |
| 57 | + */ | |
| 58 | +function wpbc_booking_resource_selector_normalize_css_width( $raw_width ) { | |
| 59 | + if ( is_int( $raw_width ) || is_float( $raw_width ) ) { | |
| 60 | + $raw_width = (string) $raw_width . 'px'; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $raw_width = strtolower( trim( (string) $raw_width ) ); | |
| 64 | + if ( '' === $raw_width || 'auto' === $raw_width ) { | |
| 65 | + return ''; | |
| 66 | + } | |
| 67 | + if ( preg_match( '/^\d+(?:\.\d+)?$/', $raw_width ) ) { | |
| 68 | + $raw_width .= 'px'; | |
| 69 | + } | |
| 70 | + if ( ! preg_match( '/^(\d+(?:\.\d+)?)(px|%|rem|em|vw)$/', $raw_width, $matches ) ) { | |
| 71 | + return ''; | |
| 72 | + } | |
| 73 | + | |
| 74 | + $numeric_width = (float) $matches[1]; | |
| 75 | + $width_unit = $matches[2]; | |
| 76 | + $maximum_width = in_array( $width_unit, array( '%', 'vw' ), true ) ? 100 : ( 'px' === $width_unit ? 2000 : 100 ); | |
| 77 | + if ( $numeric_width <= 0 || $numeric_width > $maximum_width ) { | |
| 78 | + return ''; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $normalized_width = rtrim( rtrim( number_format( $numeric_width, 4, '.', '' ), '0' ), '.' ); | |
| 82 | + | |
| 83 | + return $normalized_width . $width_unit; | |
| 84 | +} | |
| 85 | + | |
| 86 | +/** | |
| 48 | 87 | * Return the Booking Resource that should be checked on the selection screen. |
| 49 | 88 | * |
| 50 | 89 | * The public `resource_id` attribute is the primary default-selection |
| 51 | 90 | * parameter. `selected_resource_id` remains as a compatibility fallback for |
| @@ -75,29 +114,41 @@ | ||
| 75 | 114 | */ |
| 76 | 115 | function wpbc_booking_resource_selector_normalize_config( $attributes ) { |
| 77 | 116 | $attributes = is_array( $attributes ) ? $attributes : array(); |
| 78 | 117 | $defaults = array( |
| 79 | - 'resource_id' => 0, | |
| 80 | - 'selected_resource_id' => 0, | |
| 81 | - 'resource_ids' => array(), | |
| 82 | - 'aggregate_resource_ids' => array(), | |
| 83 | - 'cal_count' => 1, | |
| 84 | - 'start_month_calendar' => false, | |
| 85 | - 'calendar_dates_start' => '', | |
| 86 | - 'calendar_dates_end' => '', | |
| 87 | - 'selected_dates' => '', | |
| 88 | - 'options' => '', | |
| 89 | - 'form_type' => '', | |
| 90 | - 'auto_select_resource' => false, | |
| 91 | - 'show_progress' => true, | |
| 92 | - 'progress_item_1_title' => null, | |
| 93 | - 'progress_item_1_number' => null, | |
| 94 | - 'progress_item_2_title' => null, | |
| 95 | - 'progress_item_2_number' => null, | |
| 96 | - 'screen_1_title' => null, | |
| 97 | - 'screen_1_description' => null, | |
| 98 | - 'allow_past' => false, | |
| 99 | - 'return_url' => '', | |
| 118 | + 'resource_id' => 0, | |
| 119 | + 'selected_resource_id' => 0, | |
| 120 | + 'resource_ids' => array(), | |
| 121 | + 'aggregate_resource_ids' => array(), | |
| 122 | + 'cal_count' => 1, | |
| 123 | + 'start_month_calendar' => false, | |
| 124 | + 'calendar_dates_start' => '', | |
| 125 | + 'calendar_dates_end' => '', | |
| 126 | + 'selected_dates' => '', | |
| 127 | + 'options' => '', | |
| 128 | + 'form_type' => '', | |
| 129 | + 'auto_select_resource' => false, | |
| 130 | + 'catalog_layout' => 'grid', | |
| 131 | + 'show_resource_filters' => false, | |
| 132 | + 'show_resource_image' => true, | |
| 133 | + 'show_resource_title' => true, | |
| 134 | + 'show_resource_description' => true, | |
| 135 | + 'catalog_item_width' => '', | |
| 136 | + 'catalog_item_max_width' => 0, | |
| 137 | + 'catalog_grid_items_per_row' => 0, | |
| 138 | + 'catalog_list_items_per_row' => 0, | |
| 139 | + 'show_resource_hierarchy' => true, | |
| 140 | + 'show_availability' => true, | |
| 141 | + 'show_starting_price' => true, | |
| 142 | + 'show_progress' => true, | |
| 143 | + 'progress_item_1_title' => null, | |
| 144 | + 'progress_item_1_number' => null, | |
| 145 | + 'progress_item_2_title' => null, | |
| 146 | + 'progress_item_2_number' => null, | |
| 147 | + 'screen_1_title' => null, | |
| 148 | + 'screen_1_description' => null, | |
| 149 | + 'allow_past' => false, | |
| 150 | + 'return_url' => '', | |
| 100 | 151 | ); |
| 101 | 152 | |
| 102 | 153 | $attribute_aliases = array( |
| 103 | 154 | 'resources' => 'resource_ids', |
| @@ -145,10 +196,25 @@ | ||
| 145 | 196 | $config['options'] = sanitize_text_field( (string) $config['options'] ); |
| 146 | 197 | $config['form_type'] = sanitize_text_field( (string) $config['form_type'] ); |
| 147 | 198 | $config['return_url'] = esc_url_raw( (string) $config['return_url'] ); |
| 148 | 199 | $config['auto_select_resource'] = wpbc_booking_resource_selector_normalize_boolean( $config['auto_select_resource'] ); |
| 149 | - $config['show_progress'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_progress'], true ); | |
| 150 | - $config['allow_past'] = wpbc_booking_resource_selector_normalize_boolean( $config['allow_past'] ); | |
| 200 | + $config['catalog_layout'] = 'list' === sanitize_key( (string) $config['catalog_layout'] ) ? 'list' : 'grid'; | |
| 201 | + $config['show_resource_filters'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_resource_filters'] ); | |
| 202 | + $config['show_resource_image'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_resource_image'], true ); | |
| 203 | + $config['show_resource_title'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_resource_title'], true ); | |
| 204 | + $config['show_resource_description'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_resource_description'], true ); | |
| 205 | + $config['catalog_item_width'] = wpbc_booking_resource_selector_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_resource_selector_normalize_boolean( $config['show_resource_hierarchy'], true ); | |
| 213 | + $config['show_availability'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_availability'], true ); | |
| 214 | + $config['show_starting_price'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_starting_price'], true ); | |
| 215 | + $config['show_progress'] = wpbc_booking_resource_selector_normalize_boolean( $config['show_progress'], true ); | |
| 216 | + $config['allow_past'] = wpbc_booking_resource_selector_normalize_boolean( $config['allow_past'] ); | |
| 151 | 217 | |
| 152 | 218 | $display_text_keys = array( |
| 153 | 219 | 'progress_item_1_title', |
| 154 | 220 | 'progress_item_1_number', |