PluginProbe
Booking Calendar / 11.4.2
Booking Calendar v11.4.2
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / page-settings-messages / settings_messages__page.php

settings_messages__page.php in Booking Calendar 11.4.2, at includes/page-settings-messages/settings_messages__page.php

352 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Booking form visitor-message settings page.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Check whether the Form Messages settings tab is active.
14 *
15 * @return bool
16 */
17 function wpbc_settings_messages__is_page() {
18
19 if ( ! is_admin() ) {
20 return false;
21 }
22 if ( function_exists( 'wpbc_is_settings_messages_page' ) ) {
23 return wpbc_is_settings_messages_page();
24 }
25
26 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
27 $page = isset( $_REQUEST['page'] ) ? sanitize_key( wp_unslash( $_REQUEST['page'] ) ) : '';
28 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
29 $tab = isset( $_REQUEST['tab'] ) ? sanitize_key( wp_unslash( $_REQUEST['tab'] ) ) : '';
30 return ( 'wpbc-settings' === $page && 'form_messages' === $tab );
31 }
32
33 /**
34 * Get the capability used by this settings page.
35 *
36 * @return string
37 */
38 function wpbc_settings_messages__get_manage_cap() {
39
40 if ( function_exists( 'wpbc_settings_calendar__get_manage_cap' ) ) {
41 return wpbc_settings_calendar__get_manage_cap();
42 }
43
44 return 'manage_options';
45 }
46
47 /**
48 * Get message group definitions.
49 *
50 * @return array
51 */
52 function wpbc_settings_messages__get_groups() {
53 return array(
54 'date_selection' => array( 'title' => __( 'Date Selection', 'booking' ), 'icon' => 'wpbc-bi-calendar3-week' ),
55 'form_validation' => array( 'title' => __( 'Form Validation', 'booking' ), 'icon' => 'wpbc-bi-ui-checks' ),
56 'time_validation' => array( 'title' => __( 'Time Validation', 'booking' ), 'icon' => 'wpbc-bi-clock' ),
57 'submission' => array( 'title' => __( 'Booking Submission', 'booking' ), 'icon' => 'wpbc-bi-send' ),
58 'captcha' => array( 'title' => __( 'CAPTCHA', 'booking' ), 'icon' => 'wpbc-bi-shield-check' ),
59 );
60 }
61
62 /**
63 * Enqueue page assets.
64 *
65 * @param string $where_to_load Asset context.
66 * @return void
67 */
68 function wpbc_settings_messages_enqueue_css_files( $where_to_load ) {
69 if ( ! in_array( $where_to_load, array( 'admin', 'both' ), true ) || ! wpbc_settings_messages__is_page() ) {
70 return;
71 }
72 wp_enqueue_style(
73 'wpbc-settings-messages-page',
74 trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/settings_messages_page.css',
75 array( 'wpbc-all-admin' ),
76 WP_BK_VERSION_NUM
77 );
78 }
79 add_action( 'wpbc_enqueue_css_files', 'wpbc_settings_messages_enqueue_css_files', 68 );
80
81 /**
82 * Enqueue page JavaScript.
83 *
84 * @param string $where_to_load Asset context.
85 * @return void
86 */
87 function wpbc_settings_messages_enqueue_js_files( $where_to_load ) {
88 if ( ! in_array( $where_to_load, array( 'admin', 'both' ), true ) || ! wpbc_settings_messages__is_page() ) {
89 return;
90 }
91 wp_enqueue_script(
92 'wpbc-settings-messages-page',
93 trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/settings_messages_page.js',
94 array( 'jquery', 'wpbc_all_admin' ),
95 WP_BK_VERSION_NUM,
96 array( 'in_footer' => true )
97 );
98 wp_localize_script(
99 'wpbc-settings-messages-page',
100 'wpbc_settings_messages_page',
101 array(
102 'ajax_url' => admin_url( 'admin-ajax.php' ),
103 'nonce' => wp_create_nonce( 'wpbc_settings_messages_ajax_nonce' ),
104 'action' => 'WPBC_AJX_SETTINGS_MESSAGES_SAVE',
105 'i18n' => array(
106 'saving' => __( 'Saving', 'booking' ),
107 'saved' => __( 'Form messages updated.', 'booking' ),
108 'save_failed' => __( 'Unable to save form messages.', 'booking' ),
109 'reset_confirm' => __( 'Restore every form message to its plugin default?', 'booking' ),
110 ),
111 )
112 );
113 }
114 add_action( 'wpbc_enqueue_js_files', 'wpbc_settings_messages_enqueue_js_files', 68 );
115
116 /**
117 * Compose and validate posted form-message settings.
118 *
119 * @param array $post_data Raw request data.
120 * @return array|WP_Error
121 */
122 function wpbc_settings_messages__validate_data( $post_data ) {
123
124 $registry = wpbc_frontend_messages__get_registry();
125 $current_settings = wpbc_frontend_messages__get_settings();
126 $messages = ! empty( $current_settings['messages'] ) && is_array( $current_settings['messages'] ) ? $current_settings['messages'] : array();
127 $enabled = ! empty( $current_settings['enabled'] ) && is_array( $current_settings['enabled'] ) ? $current_settings['enabled'] : array();
128 $defaults = isset( $post_data['message_default'] ) && is_array( $post_data['message_default'] ) ? $post_data['message_default'] : array();
129 $translations = isset( $post_data['message_translations'] ) && is_array( $post_data['message_translations'] ) ? $post_data['message_translations'] : array();
130 $raw_messages = isset( $post_data['message_raw'] ) && is_array( $post_data['message_raw'] ) ? $post_data['message_raw'] : array();
131 $raw_modes = isset( $post_data['message_raw_mode'] ) && is_array( $post_data['message_raw_mode'] ) ? $post_data['message_raw_mode'] : array();
132 $enabled_post = isset( $post_data['message_enabled'] ) && is_array( $post_data['message_enabled'] ) ? $post_data['message_enabled'] : array();
133
134 foreach ( $registry as $message_key => $config ) {
135 $is_message_posted = array_key_exists( $message_key, $defaults ) || array_key_exists( $message_key, $translations ) || array_key_exists( $message_key, $raw_messages ) || array_key_exists( $message_key, $raw_modes );
136 if ( ! $is_message_posted ) {
137 continue;
138 }
139
140 if ( isset( $raw_modes[ $message_key ] ) && is_scalar( $raw_modes[ $message_key ] ) && '1' === (string) wp_unslash( $raw_modes[ $message_key ] ) ) {
141 $raw_value = isset( $raw_messages[ $message_key ] ) ? $raw_messages[ $message_key ] : '';
142 } else {
143 $default_text = isset( $defaults[ $message_key ] ) && is_scalar( $defaults[ $message_key ] ) ? trim( (string) $defaults[ $message_key ] ) : '';
144 $message_translations = isset( $translations[ $message_key ] ) && is_array( $translations[ $message_key ] ) ? $translations[ $message_key ] : array();
145 $translation_sections = array();
146
147 foreach ( $message_translations as $locale => $translation ) {
148 $locale = sanitize_text_field( wp_unslash( (string) $locale ) );
149 if ( ! preg_match( '/^[A-Za-z0-9_@.\-]+$/', $locale ) || ! is_scalar( $translation ) ) {
150 return new WP_Error( 'invalid_locale', __( 'A message contains an invalid locale.', 'booking' ) );
151 }
152 $translation = trim( (string) $translation );
153 if ( '' !== $translation ) {
154 $translation_sections[ $locale ] = $translation;
155 }
156 }
157
158 $raw_value = $default_text;
159 foreach ( $translation_sections as $locale => $translation ) {
160 $raw_value .= "\n[lang=" . $locale . "]\n" . $translation;
161 }
162 }
163
164 $validated = wpbc_frontend_messages__validate_value( $message_key, $raw_value );
165 if ( is_wp_error( $validated ) ) {
166 return new WP_Error(
167 $validated->get_error_code(),
168 sprintf( '%1$s: %2$s', $config['title'], $validated->get_error_message() )
169 );
170 }
171 if ( '' !== $validated && $validated !== $config['default'] ) {
172 $messages[ $message_key ] = $validated;
173 } else {
174 unset( $messages[ $message_key ] );
175 }
176 if ( ! empty( $config['optional'] ) ) {
177 $enabled[ $message_key ] = isset( $enabled_post[ $message_key ] ) ? 'On' : 'Off';
178 }
179 }
180
181 return array( 'version' => 1, 'messages' => $messages, 'enabled' => $enabled );
182 }
183
184 /**
185 * Show the standard Save button in the settings top toolbar.
186 *
187 * @param string $page_tag Current page tag.
188 * @param string $active_page_tab Current tab.
189 * @param string $active_page_subtab Current subtab.
190 * @return void
191 */
192 function wpbc_settings_messages__top_toolbar_save_button( $page_tag, $active_page_tab, $active_page_subtab ) {
193
194 if ( 'wpbc-settings' !== $page_tag || 'form_messages' !== $active_page_tab ) {
195 return;
196 }
197 if ( ! wpbc_is_mu_user_can_be_here( 'only_super_admin' ) || ! current_user_can( wpbc_settings_messages__get_manage_cap() ) ) {
198 return;
199 }
200 ?>
201 <div class="wpbc_ui_el__buttons_group wpbc_messages__top_toolbar_group">
202 <a href="javascript:void(0);"
203 class="button button-primary wpbc_messages__top_btn_save"
204 data-wpbc-messages-save="1"
205 data-wpbc-u-busy-text="<?php esc_attr_e( 'Saving', 'booking' ); ?>...">
206 <i class="menu_icon icon-1x wpbc-bi-check2-circle"></i>
207 <span class="in-button-text">&nbsp;&nbsp;<?php esc_html_e( 'Save Changes', 'booking' ); ?></span>
208 </a>
209 </div>
210 <?php
211 }
212 add_action( 'wpbc_ui_el__top_nav__content_end', 'wpbc_settings_messages__top_toolbar_save_button', 20, 3 );
213
214 /**
215 * Settings tab definition and page output.
216 */
217 class WPBC_Page_Settings_Messages extends WPBC_Page_Structure {
218
219 /** @return string */
220 public function in_page() {
221 return ( wpbc_is_mu_user_can_be_here( 'only_super_admin' ) && current_user_can( wpbc_settings_messages__get_manage_cap() ) ) ? 'wpbc-settings' : (string) wp_rand( 100000, 1000000 );
222 }
223
224 /** @return array */
225 public function tabs() {
226 return array(
227 'form_messages' => array(
228 'is_show_top_path' => true,
229 'right_vertical_sidebar__is_show' => true,
230 'right_vertical_sidebar__default_view_mode' => '',
231 'right_vertical_sidebar_compact__is_show' => true,
232 'left_navigation__default_view_mode' => 'compact',
233 'top_path_title' => __( 'Form Messages', 'booking' ),
234 'title' => __( 'Form Messages', 'booking' ),
235 'hint' => __( 'Customize visitor-facing booking form notices and validation messages.', 'booking' ),
236 'page_title' => __( 'Form Messages', 'booking' ),
237 'link' => function_exists( 'wpbc_get_settings_messages_url' ) ? wpbc_get_settings_messages_url() : admin_url( 'admin.php?page=wpbc-settings&tab=form_messages' ), 'position' => '', 'css_classes' => 'wpbc_top_tab__form_messages',
238 'icon' => '', 'font_icon' => 'wpbc-bi-chat-square-text', 'font_icon_right' => '',
239 'default' => false, 'disabled' => false, 'hided' => false, 'subtabs' => array(),
240 'folder_style' => 'order:22;',
241 ),
242 );
243 }
244
245 /** @return void */
246 public function right_sidebar_compact_content() {
247 WPBC_UI_Sidebar_Panels::render_rightbar_tabs(
248 array(
249 array( 'id' => 'wpbc_tab_messages_options', 'panel_id' => 'wpbc_messages__inspector_options', 'title' => __( 'Sections', 'booking' ), 'icon' => 'wpbc-bi-list-ul', 'selected' => true ),
250 array( 'id' => 'wpbc_tab_messages_help', 'panel_id' => 'wpbc_messages__inspector_help', 'title' => __( 'Help', 'booking' ), 'icon' => 'wpbc-bi-info-circle' ),
251 ),
252 array( 'aria_label' => __( 'Form Message Panels', 'booking' ), 'context' => 'settings_messages', 'class' => 'wpbc_messages_rightbar_tabs' )
253 );
254 }
255
256 /** @return void */
257 public function right_sidebar_content() {
258 ?>
259 <div class="wpbc_bfb__panel--library wpbc_rightbar_palette wpbc_messages_rightbar_panels">
260 <?php
261 WPBC_UI_Sidebar_Panels::render_panel(
262 array( 'id' => 'wpbc_messages__inspector_options', 'labelledby' => 'wpbc_tab_messages_options', 'class' => 'wpbc_messages__inspector_options' ),
263 array( $this, 'right_panel_options_content' )
264 );
265 WPBC_UI_Sidebar_Panels::render_panel(
266 array( 'id' => 'wpbc_messages__inspector_help', 'labelledby' => 'wpbc_tab_messages_help', 'class' => 'wpbc_messages__inspector_help', 'hidden' => true ),
267 array( $this, 'right_panel_help_content' )
268 );
269 ?>
270 </div>
271 <?php
272 }
273
274 /** @return void */
275 public function right_panel_options_content() {
276 WPBC_UI_Sidebar_Panels::render_inspector_header( __( 'Sections', 'booking' ), __( 'Jump directly to a message group on this page.', 'booking' ) );
277 ?>
278 <div class="wpbc_messages_sidebar_body">
279 <nav class="wpbc_messages_section_nav" aria-label="<?php esc_attr_e( 'Form message sections', 'booking' ); ?>">
280 <?php foreach ( wpbc_settings_messages__get_groups() as $group_key => $group ) : ?>
281 <a href="#wpbc_messages_group_<?php echo esc_attr( $group_key ); ?>"><i class="<?php echo esc_attr( $group['icon'] ); ?>" aria-hidden="true"></i><span><?php echo esc_html( $group['title'] ); ?></span></a>
282 <?php endforeach; ?>
283 </nav>
284 <hr>
285 <button type="button" class="button" data-wpbc-messages-reset-all="1"><?php esc_html_e( 'Restore All Defaults', 'booking' ); ?></button>
286 </div>
287 <?php
288 }
289
290 /** @return void */
291 public function right_panel_help_content() {
292 WPBC_UI_Sidebar_Panels::render_inspector_header( __( 'Language Shortcodes', 'booking' ), __( 'Add translations only when a custom message needs language-specific text.', 'booking' ) );
293 ?>
294 <div class="wpbc_messages_sidebar_body">
295 <p><?php esc_html_e( 'Enter the default text first, followed by one language block for each translation.', 'booking' ); ?></p>
296 <pre>Default message
297 [lang=fr_FR]
298 Message en fran&ccedil;ais
299 [lang=de_DE]
300 Nachricht auf Deutsch</pre>
301 <p><?php esc_html_e( 'Only plain text and valid [lang=LOCALE] blocks are accepted.', 'booking' ); ?></p>
302 </div>
303 <?php
304 }
305
306 /** @return void */
307 public function content() {
308 do_action( 'wpbc_hook_settings_page_header', 'page_settings_form_messages' );
309 if ( ! wpbc_is_mu_user_can_be_here( 'only_super_admin' ) || ! current_user_can( wpbc_settings_messages__get_manage_cap() ) ) {
310 return;
311 }
312
313 $registry = wpbc_frontend_messages__get_registry();
314 $settings = wpbc_frontend_messages__get_settings();
315 $groups = wpbc_settings_messages__get_groups();
316 ?>
317 <div class="wpbc_settings_messages_page">
318 <form id="wpbc_settings_messages_form" class="wpbc_settings_messages_form">
319 <input type="hidden" name="action" value="WPBC_AJX_SETTINGS_MESSAGES_SAVE">
320 <input type="hidden" name="nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpbc_settings_messages_ajax_nonce' ) ); ?>">
321 <input type="hidden" name="reset_all" value="0" data-wpbc-messages-reset-input="1">
322 <?php foreach ( $groups as $group_key => $group ) : ?>
323 <section class="wpbc_settings_messages_group" aria-labelledby="wpbc_messages_group_<?php echo esc_attr( $group_key ); ?>">
324 <h2 id="wpbc_messages_group_<?php echo esc_attr( $group_key ); ?>"><i class="<?php echo esc_attr( $group['icon'] ); ?>"></i><?php echo esc_html( $group['title'] ); ?></h2>
325 <?php foreach ( $registry as $message_key => $config ) : ?>
326 <?php if ( $group_key !== $config['group'] ) { continue; } ?>
327 <?php
328 $stored = isset( $settings['messages'][ $message_key ] ) && is_string( $settings['messages'][ $message_key ] ) ? $settings['messages'][ $message_key ] : '';
329 $field_value = ( '' !== $stored ) ? $stored : $config['default'];
330 $is_enabled = wpbc_frontend_messages__is_enabled( $message_key );
331 ?>
332 <div class="wpbc_settings_message_row" data-message-key="<?php echo esc_attr( $message_key ); ?>">
333 <label for="wpbc_message_<?php echo esc_attr( $message_key ); ?>"><strong><?php echo esc_html( $config['title'] ); ?></strong></label>
334 <p class="description"><?php echo esc_html( $config['description'] ); ?></p>
335 <?php if ( ! empty( $config['optional'] ) ) : ?>
336 <label class="wpbc_message_enabled"><input type="checkbox" name="message_enabled[<?php echo esc_attr( $message_key ); ?>]" value="On" <?php checked( $is_enabled ); ?>> <?php esc_html_e( 'Show this message', 'booking' ); ?></label>
337 <?php endif; ?>
338 <input type="hidden" name="message_raw_mode[<?php echo esc_attr( $message_key ); ?>]" value="1">
339 <textarea id="wpbc_message_<?php echo esc_attr( $message_key ); ?>" rows="3" name="message_raw[<?php echo esc_attr( $message_key ); ?>]" data-default-message="<?php echo esc_attr( $config['default'] ); ?>"><?php echo esc_textarea( $field_value ); ?></textarea>
340 <div class="wpbc_message_row_actions"><button type="button" class="button-link" data-reset-message="1"><?php esc_html_e( 'Restore Default', 'booking' ); ?></button></div>
341 </div>
342 <?php endforeach; ?>
343 </section>
344 <?php endforeach; ?>
345 </form>
346 </div>
347 <?php
348 do_action( 'wpbc_hook_settings_page_footer', 'page_settings_form_messages' );
349 }
350 }
351 add_action( 'wpbc_menu_created', array( new WPBC_Page_Settings_Messages(), '__construct' ) );
352