| 1 |
<?php |
| 2 |
/** |
| 3 |
* Connect Setup Wizard booking profiles to administration presentation modes. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.5.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get mode choices presented by the Setup Wizard. |
| 15 |
* |
| 16 |
* The wizard owns booking-form and availability configuration, while the |
| 17 |
* Booking Modes registry remains authoritative for which presentation modes |
| 18 |
* are available to the current owner. Keeping these responsibilities separate |
| 19 |
* prevents a booking behavior such as full-day booking from silently forcing |
| 20 |
* Rental terminology when the user explicitly selected Classic mode. |
| 21 |
* |
| 22 |
* @return array<string,array<string,mixed>> Setup choices keyed by mode ID. |
| 23 |
*/ |
| 24 |
function wpbc_booking_modes_get_setup_mode_choices() { |
| 25 |
|
| 26 |
$registered_modes = wpbc_booking_modes_get_registered_modes(); |
| 27 |
$allowed_mode_ids = wpbc_booking_modes_get_allowed_mode_ids(); |
| 28 |
$choice_definitions = array( |
| 29 |
'appointment' => array( |
| 30 |
'title' => __( 'Appointments and services', 'booking' ), |
| 31 |
'description' => __( 'Customers choose a Service, Provider, date, and start time.', 'booking' ), |
| 32 |
'preference_title' => __( 'Appointment preferences', 'booking' ), |
| 33 |
'allowed_booking_types' => array( 'time_slots_appointments' ), |
| 34 |
'default_booking_type' => 'time_slots_appointments', |
| 35 |
'fixed_appointment_type' => 'durationtime', |
| 36 |
), |
| 37 |
'rental' => array( |
| 38 |
'title' => __( 'Properties and rentals', 'booking' ), |
| 39 |
'description' => __( 'Guests reserve properties or other resources for one or more dates.', 'booking' ), |
| 40 |
'preference_title' => __( 'Rental preferences', 'booking' ), |
| 41 |
'allowed_booking_types' => array( 'full_days_bookings', 'changeover_multi_dates_bookings' ), |
| 42 |
'default_booking_type' => 'full_days_bookings', |
| 43 |
), |
| 44 |
'classic' => array( |
| 45 |
'title' => __( 'Classic Booking Calendar', 'booking' ), |
| 46 |
'description' => __( 'Keep the traditional Booking Calendar terminology and configuration.', 'booking' ), |
| 47 |
'preference_title' => __( 'Choose booking behavior', 'booking' ), |
| 48 |
'allowed_booking_types' => array( 'full_days_bookings', 'time_slots_appointments', 'changeover_multi_dates_bookings' ), |
| 49 |
'default_booking_type' => 'full_days_bookings', |
| 50 |
), |
| 51 |
); |
| 52 |
$setup_choices = array(); |
| 53 |
|
| 54 |
foreach ( $choice_definitions as $mode_id => $choice_definition ) { |
| 55 |
if ( ! isset( $registered_modes[ $mode_id ] ) || ! in_array( $mode_id, $allowed_mode_ids, true ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$setup_choices[ $mode_id ] = array_merge( |
| 60 |
array( |
| 61 |
'id' => $mode_id, |
| 62 |
'fixed_appointment_type' => '', |
| 63 |
), |
| 64 |
$choice_definition |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
return $setup_choices; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Resolve the administration mode associated with a Setup Wizard booking type. |
| 73 |
* |
| 74 |
* The Setup Wizard remains authoritative for booking-form and availability |
| 75 |
* configuration. This mapping changes only the owner-scoped administration |
| 76 |
* presentation selected after that configuration is saved. |
| 77 |
* |
| 78 |
* @param string $booking_type Sanitized Setup Wizard booking type identifier. |
| 79 |
* @param string $requested_mode_id Optional explicit mode selected in Step 4. |
| 80 |
* |
| 81 |
* @return string Allowed Booking Modes identifier, or an empty string when the |
| 82 |
* booking type must not change the current selection. |
| 83 |
*/ |
| 84 |
function wpbc_booking_modes_get_setup_mode_id( $booking_type, $requested_mode_id = '' ) { |
| 85 |
|
| 86 |
$booking_type = is_scalar( $booking_type ) ? sanitize_key( (string) $booking_type ) : ''; |
| 87 |
$requested_mode_id = is_scalar( $requested_mode_id ) ? sanitize_key( (string) $requested_mode_id ) : ''; |
| 88 |
|
| 89 |
if ( '' !== $requested_mode_id ) { |
| 90 |
return in_array( $requested_mode_id, wpbc_booking_modes_get_allowed_mode_ids(), true ) ? $requested_mode_id : ''; |
| 91 |
} |
| 92 |
|
| 93 |
$mode_map = array( |
| 94 |
'full_days_bookings' => 'rental', |
| 95 |
'time_slots_appointments' => 'appointment', |
| 96 |
'changeover_multi_dates_bookings' => 'rental', |
| 97 |
); |
| 98 |
|
| 99 |
/** |
| 100 |
* Filter the Setup Wizard booking-type to administration-mode mapping. |
| 101 |
* |
| 102 |
* @param array $mode_map Mode identifiers keyed by Setup Wizard booking type. |
| 103 |
* @param string $booking_type Sanitized booking type currently being resolved. |
| 104 |
*/ |
| 105 |
$mode_map = apply_filters( 'wpbc_booking_modes_setup_booking_type_map', $mode_map, $booking_type ); |
| 106 |
|
| 107 |
if ( ! is_array( $mode_map ) || ! isset( $mode_map[ $booking_type ] ) ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
$mode_id = is_scalar( $mode_map[ $booking_type ] ) ? sanitize_key( (string) $mode_map[ $booking_type ] ) : ''; |
| 112 |
|
| 113 |
return in_array( $mode_id, wpbc_booking_modes_get_allowed_mode_ids(), true ) ? $mode_id : ''; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Persist the presentation mode selected indirectly by the Setup Wizard. |
| 118 |
* |
| 119 |
* This handler runs only after the wizard has validated, applied, and stored |
| 120 |
* its booking-type configuration. It deliberately does not run QuickStart or |
| 121 |
* mutate bookings, resources, Services, pages, availability, or prices. |
| 122 |
* |
| 123 |
* @param array $cleaned_data Validated Setup Wizard booking-type data. |
| 124 |
* |
| 125 |
* @return bool|WP_Error True when saved, false when no mapping applies, or a |
| 126 |
* storage error returned by the Booking Modes API. |
| 127 |
*/ |
| 128 |
function wpbc_booking_modes_apply_setup_booking_type( $cleaned_data ) { |
| 129 |
|
| 130 |
if ( ! is_array( $cleaned_data ) || empty( $cleaned_data['wpbc_swp_booking_types'] ) ) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
$requested_mode_id = isset( $cleaned_data['wpbc_swp_booking_mode'] ) ? $cleaned_data['wpbc_swp_booking_mode'] : ''; |
| 135 |
$mode_id = wpbc_booking_modes_get_setup_mode_id( $cleaned_data['wpbc_swp_booking_types'], $requested_mode_id ); |
| 136 |
|
| 137 |
if ( '' === $mode_id ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
return wpbc_booking_modes_set_selected_mode_id( $mode_id ); |
| 142 |
} |
| 143 |
add_action( 'wpbc_setup_wizard_booking_type_saved', 'wpbc_booking_modes_apply_setup_booking_type' ); |
| 144 |
|
| 145 |
/** |
| 146 |
* Add one published page to a Setup Wizard test-link collection. |
| 147 |
* |
| 148 |
* @param array $page_links Page-link collection passed by reference. |
| 149 |
* @param string $page_url Public page URL. |
| 150 |
* @param string $label Button label. |
| 151 |
* |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
function wpbc_booking_modes_add_setup_test_page_link( &$page_links, $page_url, $label ) { |
| 155 |
|
| 156 |
$page_url = is_scalar( $page_url ) ? esc_url_raw( (string) $page_url ) : ''; |
| 157 |
$label = is_scalar( $label ) ? sanitize_text_field( (string) $label ) : ''; |
| 158 |
|
| 159 |
if ( '' === $page_url || '' === $label ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
foreach ( $page_links as $page_link ) { |
| 164 |
if ( isset( $page_link['url'] ) && $page_url === $page_link['url'] ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
$page_links[] = array( |
| 170 |
'url' => $page_url, |
| 171 |
'label' => $label, |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get published booking pages suitable for the active Setup Wizard mode. |
| 177 |
* |
| 178 |
* This is a read-only discovery operation. It reuses QuickStart ownership and |
| 179 |
* shortcode discovery when available, but never creates, republishes, or edits |
| 180 |
* site content. Classic mode falls back to the plugin's published activation |
| 181 |
* pages because it intentionally has no QuickStart operation. |
| 182 |
* |
| 183 |
* @param string $mode_id Optional mode ID. Empty uses the current selection. |
| 184 |
* |
| 185 |
* @return array<int,array{url:string,label:string}> Published page links. |
| 186 |
*/ |
| 187 |
function wpbc_booking_modes_get_setup_test_page_links( $mode_id = '' ) { |
| 188 |
|
| 189 |
$mode_id = is_scalar( $mode_id ) ? sanitize_key( (string) $mode_id ) : ''; |
| 190 |
$mode_id = '' !== $mode_id ? $mode_id : wpbc_booking_modes_get_selected_mode_id(); |
| 191 |
$page_links = array(); |
| 192 |
|
| 193 |
if ( 'appointment' === $mode_id ) { |
| 194 |
$test_url = function_exists( 'wpbc_booking_modes_get_quickstart_test_url' ) |
| 195 |
? wpbc_booking_modes_get_quickstart_test_url( 'appointment' ) |
| 196 |
: ''; |
| 197 |
|
| 198 |
if ( '' === $test_url && function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 199 |
$appointment_page = wpbc_booking_modes_quickstart_find_page( 'appointment_booking', '[booking_appointment' ); |
| 200 |
if ( $appointment_page instanceof WP_Post && 'publish' === $appointment_page->post_status ) { |
| 201 |
$test_url = get_permalink( $appointment_page->ID ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, $test_url, __( 'Test Appointment Page', 'booking' ) ); |
| 206 |
} elseif ( 'rental' === $mode_id ) { |
| 207 |
$test_url = function_exists( 'wpbc_booking_modes_get_quickstart_test_url' ) |
| 208 |
? wpbc_booking_modes_get_quickstart_test_url( 'rental' ) |
| 209 |
: ''; |
| 210 |
|
| 211 |
if ( '' === $test_url && function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 212 |
$property_page = wpbc_booking_modes_quickstart_find_page( 'rental_property_booking', '[booking resource_id=' ); |
| 213 |
if ( $property_page instanceof WP_Post && 'publish' === $property_page->post_status ) { |
| 214 |
$test_url = get_permalink( $property_page->ID ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, $test_url, __( 'Test Property Page', 'booking' ) ); |
| 219 |
|
| 220 |
if ( function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 221 |
$search_page = wpbc_booking_modes_quickstart_find_page( 'rental_availability_search', '[bookingsearch' ); |
| 222 |
if ( $search_page instanceof WP_Post && 'publish' === $search_page->post_status ) { |
| 223 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, get_permalink( $search_page->ID ), __( 'Test Availability Search', 'booking' ) ); |
| 224 |
} |
| 225 |
} |
| 226 |
} elseif ( 'classic' === $mode_id ) { |
| 227 |
if ( function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 228 |
foreach ( array( '[booking ', '[booking]' ) as $classic_shortcode_match ) { |
| 229 |
$classic_page = wpbc_booking_modes_quickstart_find_page( 'classic_booking_page', $classic_shortcode_match ); |
| 230 |
if ( $classic_page instanceof WP_Post && 'publish' === $classic_page->post_status ) { |
| 231 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, get_permalink( $classic_page->ID ), __( 'Test Booking Page', 'booking' ) ); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if ( function_exists( 'wpbc_get_published_activation_booking_pages' ) ) { |
| 237 |
foreach ( wpbc_get_published_activation_booking_pages() as $published_page ) { |
| 238 |
if ( ! is_array( $published_page ) ) { |
| 239 |
continue; |
| 240 |
} |
| 241 |
|
| 242 |
$page_url = isset( $published_page['url'] ) ? $published_page['url'] : ''; |
| 243 |
$page_label = isset( $published_page['button_title'] ) ? $published_page['button_title'] : __( 'Test Booking Page', 'booking' ); |
| 244 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, $page_url, $page_label ); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Filter published booking-page links shown in the Setup Wizard bar. |
| 251 |
* |
| 252 |
* @param array $page_links Published page links with URL and button label. |
| 253 |
* @param string $mode_id Active Booking Modes identifier. |
| 254 |
*/ |
| 255 |
$page_links = apply_filters( 'wpbc_booking_modes_setup_test_page_links', $page_links, $mode_id ); |
| 256 |
|
| 257 |
return is_array( $page_links ) ? $page_links : array(); |
| 258 |
} |
| 259 |
|