| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shared global time-picker appearance controls for Booking Form Builder time fields. |
| 4 |
* |
| 5 |
* Every time-based field uses this renderer so the global display toggle and |
| 6 |
* skin selector stay consistent and are not duplicated across field packs. |
| 7 |
* |
| 8 |
* @package Booking Calendar |
| 9 |
* @since 11.4.4 |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Check whether the current user can edit global time-picker options. |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
function wpbc_bfb_time_picker__can_manage_global_options() { |
| 22 |
return ( ! function_exists( 'wpbc_is_mu_user_can_be_here' ) || wpbc_is_mu_user_can_be_here( 'only_super_admin' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Normalize a time-picker skin path or URL to the value stored in the database. |
| 27 |
* |
| 28 |
* @param string $skin_value Skin path or URL. |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
function wpbc_bfb_time_picker__normalize_skin_value( $skin_value ) { |
| 33 |
|
| 34 |
$skin_value = is_scalar( $skin_value ) ? (string) $skin_value : ''; |
| 35 |
$replace = array( WPBC_PLUGIN_DIR, WPBC_PLUGIN_URL ); |
| 36 |
$upload_dir = wp_upload_dir(); |
| 37 |
|
| 38 |
if ( ! empty( $upload_dir['basedir'] ) ) { |
| 39 |
$replace[] = $upload_dir['basedir']; |
| 40 |
} |
| 41 |
if ( ! empty( $upload_dir['baseurl'] ) ) { |
| 42 |
$replace[] = $upload_dir['baseurl']; |
| 43 |
} |
| 44 |
|
| 45 |
return str_replace( $replace, '', $skin_value ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Resolve a stored time-picker skin path to its public URL. |
| 50 |
* |
| 51 |
* @param string $relative_skin Relative skin path. |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
function wpbc_bfb_time_picker__get_skin_url( $relative_skin ) { |
| 56 |
|
| 57 |
$relative_skin = wpbc_bfb_time_picker__normalize_skin_value( $relative_skin ); |
| 58 |
$upload_dir = wp_upload_dir(); |
| 59 |
$upload_url = ! empty( $upload_dir['baseurl'] ) ? $upload_dir['baseurl'] : ''; |
| 60 |
|
| 61 |
if ( 0 === strpos( $relative_skin, '/wpbc_time_picker_skins/' ) && ! empty( $upload_url ) ) { |
| 62 |
return $upload_url . $relative_skin; |
| 63 |
} |
| 64 |
|
| 65 |
return WPBC_PLUGIN_URL . $relative_skin; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get time-picker skins for the shared Inspector selectbox. |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
function wpbc_bfb_time_picker__get_skin_options() { |
| 74 |
|
| 75 |
$automatic_skin = '/css/time_picker_skins/form_style.css'; |
| 76 |
$options = array(); |
| 77 |
$upload_dir = wp_upload_dir(); |
| 78 |
|
| 79 |
if ( function_exists( 'wpbc_dir_list' ) ) { |
| 80 |
$skin_directories = array( WPBC_PLUGIN_DIR . '/css/time_picker_skins/' ); |
| 81 |
if ( ! empty( $upload_dir['basedir'] ) ) { |
| 82 |
$skin_directories[] = $upload_dir['basedir'] . '/wpbc_time_picker_skins/'; |
| 83 |
} |
| 84 |
|
| 85 |
$files = wpbc_dir_list( |
| 86 |
$skin_directories |
| 87 |
); |
| 88 |
|
| 89 |
foreach ( $files as $skin_file ) { |
| 90 |
$relative_skin = wpbc_bfb_time_picker__normalize_skin_value( $skin_file[1] ); |
| 91 |
$options[ $relative_skin ] = array( |
| 92 |
'title' => $skin_file[2], |
| 93 |
'attr' => array( |
| 94 |
'data-wpbc-time-picker-skin-url' => wpbc_bfb_time_picker__get_skin_url( $relative_skin ), |
| 95 |
), |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Keep the recommended option available even if a directory scan is filtered or unavailable. |
| 101 |
if ( ! isset( $options[ $automatic_skin ] ) && file_exists( WPBC_PLUGIN_DIR . $automatic_skin ) ) { |
| 102 |
$options[ $automatic_skin ] = array( |
| 103 |
'title' => __( 'Automatic — Match Booking Form', 'booking' ), |
| 104 |
'attr' => array( |
| 105 |
'data-wpbc-time-picker-skin-url' => wpbc_bfb_time_picker__get_skin_url( $automatic_skin ), |
| 106 |
), |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
if ( isset( $options[ $automatic_skin ] ) ) { |
| 111 |
$automatic_option = $options[ $automatic_skin ]; |
| 112 |
$automatic_option['title'] = __( 'Automatic — Match Booking Form', 'booking' ); |
| 113 |
unset( $options[ $automatic_skin ] ); |
| 114 |
$options = array_merge( array( $automatic_skin => $automatic_option ), $options ); |
| 115 |
} |
| 116 |
|
| 117 |
$bundled_titles = array( |
| 118 |
'/css/time_picker_skins/light__24_8.css' => __( 'Light', 'booking' ), |
| 119 |
'/css/time_picker_skins/grey.css' => __( 'Grey', 'booking' ), |
| 120 |
'/css/time_picker_skins/black.css' => __( 'Black', 'booking' ), |
| 121 |
'/css/time_picker_skins/blue.css' => __( 'Blue', 'booking' ), |
| 122 |
'/css/time_picker_skins/green.css' => __( 'Green', 'booking' ), |
| 123 |
'/css/time_picker_skins/orange.css' => __( 'Orange', 'booking' ), |
| 124 |
'/css/time_picker_skins/marine.css' => __( 'Marine', 'booking' ), |
| 125 |
); |
| 126 |
foreach ( $bundled_titles as $skin_path => $skin_title ) { |
| 127 |
if ( isset( $options[ $skin_path ] ) ) { |
| 128 |
$options[ $skin_path ]['title'] = $skin_title; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Filter time-picker skins displayed in Booking Form Builder time fields. |
| 134 |
* |
| 135 |
* @param array $options Time-picker skin options. |
| 136 |
*/ |
| 137 |
return apply_filters( 'wpbc_bfb_time_picker_skin_options', $options ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Render global time-picker display and skin controls in a time-field Inspector. |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
function wpbc_bfb_time_picker__print_inspector_group() { |
| 146 |
|
| 147 |
if ( ! wpbc_bfb_time_picker__can_manage_global_options() ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$is_enabled = ( 'On' === get_bk_option( 'booking_timeslot_picker' ) ); |
| 152 |
$current_skin = wpbc_bfb_time_picker__normalize_skin_value( get_bk_option( 'booking_timeslot_picker_skin' ) ); |
| 153 |
$toggle_option = 'booking_timeslot_picker'; |
| 154 |
$skin_option = 'booking_timeslot_picker_skin'; |
| 155 |
$toggle_nonce_action = 'wpbc_nonce_' . $toggle_option; |
| 156 |
$skin_nonce_action = 'wpbc_nonce_' . $skin_option; |
| 157 |
$skin_select_id = 'wpbc_bfb__time_field__booking_timeslot_picker_skin'; |
| 158 |
|
| 159 |
?> |
| 160 |
<div class="inspector__row row__bordered wpbc_bfb__inspector_time_picker_appearance" data-wpbc-time-picker-appearance="display" style="align-items:flex-start;justify-content:space-between;"> |
| 161 |
<div class="inspector__control" style="flex:1 1 auto;"> |
| 162 |
<label class="inspector__label" for="wpbc_bfb__time_field__booking_timeslot_picker"> |
| 163 |
<strong><?php esc_html_e( 'Time Picker Display', 'booking' ); ?></strong> |
| 164 |
</label> |
| 165 |
<span class="wpbc_ui__toggle" style="margin-top:6px;"> |
| 166 |
<input id="wpbc_bfb__time_field__booking_timeslot_picker" type="checkbox" class="inspector__checkbox js-toggle-timeslot-picker" <?php checked( $is_enabled ); ?> /> |
| 167 |
<label class="wpbc_ui__toggle_icon" for="wpbc_bfb__time_field__booking_timeslot_picker" aria-hidden="true"></label> |
| 168 |
<label class="wpbc_ui__toggle_label" for="wpbc_bfb__time_field__booking_timeslot_picker"><?php esc_html_e( 'Show time slots as clickable choices instead of a select box.', 'booking' ); ?></label> |
| 169 |
</span> |
| 170 |
<p class="wpbc_bfb__help" style="margin-top:6px;"><?php esc_html_e( 'This is a global option used by all compatible time fields.', 'booking' ); ?></p> |
| 171 |
</div> |
| 172 |
<a href="javascript:void(0);" |
| 173 |
class="button button-secondary" |
| 174 |
onclick="wpbc_save_option_from_element(this);" |
| 175 |
data-wpbc-u-save-name="<?php echo esc_attr( $toggle_option ); ?>" |
| 176 |
data-wpbc-u-save-nonce="<?php echo esc_attr( wp_create_nonce( $toggle_nonce_action ) ); ?>" |
| 177 |
data-wpbc-u-save-action="<?php echo esc_attr( $toggle_nonce_action ); ?>" |
| 178 |
data-wpbc-u-save-value-from="#wpbc_bfb__time_field__booking_timeslot_picker" |
| 179 |
data-wpbc-u-autosave-on-form-save="1" |
| 180 |
data-wpbc-u-busy-text="<?php esc_attr_e( 'Saving', 'booking' ); ?>..."> |
| 181 |
<?php esc_html_e( 'Save Display', 'booking' ); ?> |
| 182 |
</a> |
| 183 |
</div> |
| 184 |
|
| 185 |
<div class="inspector__row row__bordered js-time-picker-skin-row wpbc_bfb__inspector_time_picker_appearance" data-wpbc-time-picker-appearance="skin" style="align-items:flex-start;justify-content:space-between;<?php echo $is_enabled ? '' : 'display:none;'; ?>" <?php echo $is_enabled ? '' : 'hidden aria-hidden="true"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 186 |
<div class="inspector__control" style="flex:1 1 auto;"> |
| 187 |
<label for="<?php echo esc_attr( $skin_select_id ); ?>" class="inspector__label" style="display:block;margin:0 0 6px;"> |
| 188 |
<strong><?php esc_html_e( 'Time Slot Style', 'booking' ); ?></strong> |
| 189 |
</label> |
| 190 |
<div class="wpbc_ajx_toolbar wpbc_no_borders"> |
| 191 |
<div class="ui_container ui_container_small0"> |
| 192 |
<div class="ui_group"> |
| 193 |
<div class="ui_element ui_nowrap"> |
| 194 |
<?php |
| 195 |
wpbc_flex_select( |
| 196 |
array( |
| 197 |
'id' => $skin_select_id, |
| 198 |
'name' => $skin_option, |
| 199 |
'label' => '', |
| 200 |
'class' => 'js-wpbc-bfb-time-picker-skin', |
| 201 |
'value' => $current_skin, |
| 202 |
'disabled' => false, |
| 203 |
'disabled_options' => array(), |
| 204 |
'options' => wpbc_bfb_time_picker__get_skin_options(), |
| 205 |
) |
| 206 |
); |
| 207 |
wpbc_smpl_form__ui__selectbox_prior_btn( $skin_select_id, false ); |
| 208 |
wpbc_smpl_form__ui__selectbox_next_btn( $skin_select_id, false ); |
| 209 |
?> |
| 210 |
</div> |
| 211 |
</div> |
| 212 |
</div> |
| 213 |
</div> |
| 214 |
<p class="wpbc_bfb__help" style="margin-top:6px;"><?php esc_html_e( 'Automatic follows the Booking Form Style and accent color. Other skins keep their own colors.', 'booking' ); ?></p> |
| 215 |
</div> |
| 216 |
<a href="javascript:void(0);" |
| 217 |
class="button button-primary" |
| 218 |
onclick="wpbc_save_option_from_element(this);" |
| 219 |
data-wpbc-u-save-name="<?php echo esc_attr( $skin_option ); ?>" |
| 220 |
data-wpbc-u-save-nonce="<?php echo esc_attr( wp_create_nonce( $skin_nonce_action ) ); ?>" |
| 221 |
data-wpbc-u-save-action="<?php echo esc_attr( $skin_nonce_action ); ?>" |
| 222 |
data-wpbc-u-save-value-from="#<?php echo esc_attr( $skin_select_id ); ?>" |
| 223 |
data-wpbc-u-save-callback="wpbc_bfb_time_picker_skin_control_saved" |
| 224 |
data-wpbc-u-autosave-on-form-save="1" |
| 225 |
data-wpbc-u-busy-text="<?php esc_attr_e( 'Saving', 'booking' ); ?>..."> |
| 226 |
<?php esc_html_e( 'Save Style', 'booking' ); ?> |
| 227 |
</a> |
| 228 |
</div> |
| 229 |
<?php |
| 230 |
} |
| 231 |
|