| 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 |
* Add one published page to a Setup Wizard test-link collection. |
| 73 |
* |
| 74 |
* @param array $page_links Page-link collection passed by reference. |
| 75 |
* @param string $page_url Public page URL. |
| 76 |
* @param string $label Button label. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
function wpbc_booking_modes_add_setup_test_page_link( &$page_links, $page_url, $label ) { |
| 81 |
|
| 82 |
$page_url = is_scalar( $page_url ) ? esc_url_raw( (string) $page_url ) : ''; |
| 83 |
$label = is_scalar( $label ) ? sanitize_text_field( (string) $label ) : ''; |
| 84 |
|
| 85 |
if ( '' === $page_url || '' === $label ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
foreach ( $page_links as $page_link ) { |
| 90 |
if ( isset( $page_link['url'] ) && $page_url === $page_link['url'] ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$page_links[] = array( |
| 96 |
'url' => $page_url, |
| 97 |
'label' => $label, |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get published booking pages suitable for the active Setup Wizard mode. |
| 103 |
* |
| 104 |
* This is a read-only discovery operation. It reuses QuickStart ownership and |
| 105 |
* shortcode discovery when available, but never creates, republishes, or edits |
| 106 |
* site content. Classic mode falls back to the plugin's published activation |
| 107 |
* pages because it intentionally has no QuickStart operation. |
| 108 |
* |
| 109 |
* @param string $mode_id Optional mode ID. Empty uses the current selection. |
| 110 |
* |
| 111 |
* @return array<int,array{url:string,label:string}> Published page links. |
| 112 |
*/ |
| 113 |
function wpbc_booking_modes_get_setup_test_page_links( $mode_id = '' ) { |
| 114 |
|
| 115 |
$mode_id = is_scalar( $mode_id ) ? sanitize_key( (string) $mode_id ) : ''; |
| 116 |
$mode_id = '' !== $mode_id ? $mode_id : wpbc_booking_modes_get_selected_mode_id(); |
| 117 |
$page_links = array(); |
| 118 |
|
| 119 |
if ( 'appointment' === $mode_id ) { |
| 120 |
$test_url = function_exists( 'wpbc_booking_modes_get_quickstart_test_url' ) |
| 121 |
? wpbc_booking_modes_get_quickstart_test_url( 'appointment' ) |
| 122 |
: ''; |
| 123 |
|
| 124 |
if ( '' === $test_url && function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 125 |
$appointment_page = wpbc_booking_modes_quickstart_find_page( 'appointment_booking', '[booking_appointment' ); |
| 126 |
if ( $appointment_page instanceof WP_Post && 'publish' === $appointment_page->post_status ) { |
| 127 |
$test_url = get_permalink( $appointment_page->ID ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, $test_url, __( 'Test Appointment Page', 'booking' ) ); |
| 132 |
} elseif ( 'rental' === $mode_id ) { |
| 133 |
$test_url = function_exists( 'wpbc_booking_modes_get_quickstart_test_url' ) |
| 134 |
? wpbc_booking_modes_get_quickstart_test_url( 'rental' ) |
| 135 |
: ''; |
| 136 |
|
| 137 |
if ( '' === $test_url && function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 138 |
$property_page = wpbc_booking_modes_quickstart_find_page( 'rental_property_booking', '[booking resource_id=' ); |
| 139 |
if ( $property_page instanceof WP_Post && 'publish' === $property_page->post_status ) { |
| 140 |
$test_url = get_permalink( $property_page->ID ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, $test_url, __( 'Test Property Page', 'booking' ) ); |
| 145 |
|
| 146 |
if ( function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 147 |
$search_page = wpbc_booking_modes_quickstart_find_page( 'rental_availability_search', '[bookingsearch' ); |
| 148 |
if ( $search_page instanceof WP_Post && 'publish' === $search_page->post_status ) { |
| 149 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, get_permalink( $search_page->ID ), __( 'Test Availability Search', 'booking' ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
} elseif ( 'classic' === $mode_id ) { |
| 153 |
if ( function_exists( 'wpbc_booking_modes_quickstart_find_page' ) ) { |
| 154 |
foreach ( array( '[booking ', '[booking]' ) as $classic_shortcode_match ) { |
| 155 |
$classic_page = wpbc_booking_modes_quickstart_find_page( 'classic_booking_page', $classic_shortcode_match ); |
| 156 |
if ( $classic_page instanceof WP_Post && 'publish' === $classic_page->post_status ) { |
| 157 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, get_permalink( $classic_page->ID ), __( 'Test Booking Page', 'booking' ) ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( function_exists( 'wpbc_get_published_activation_booking_pages' ) ) { |
| 163 |
foreach ( wpbc_get_published_activation_booking_pages() as $published_page ) { |
| 164 |
if ( ! is_array( $published_page ) ) { |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
$page_url = isset( $published_page['url'] ) ? $published_page['url'] : ''; |
| 169 |
$page_label = isset( $published_page['button_title'] ) ? $published_page['button_title'] : __( 'Test Booking Page', 'booking' ); |
| 170 |
wpbc_booking_modes_add_setup_test_page_link( $page_links, $page_url, $page_label ); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Filter published booking-page links shown in the Setup Wizard bar. |
| 177 |
* |
| 178 |
* @param array $page_links Published page links with URL and button label. |
| 179 |
* @param string $mode_id Active Booking Modes identifier. |
| 180 |
*/ |
| 181 |
$page_links = apply_filters( 'wpbc_booking_modes_setup_test_page_links', $page_links, $mode_id ); |
| 182 |
|
| 183 |
return is_array( $page_links ) ? $page_links : array(); |
| 184 |
} |
| 185 |
|