PluginProbe
Booking Calendar / 11.4.3
Booking Calendar v11.4.3
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 10.11 All 204 releases
booking / includes / save-load-option / option-save-policies.php

option-save-policies.php in Booking Calendar 11.4.3, at includes/save-load-option/option-save-policies.php

214 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Option-specific save policies for the generic WPBC option saver.
4 *
5 * Keep feature-specific permissions and value normalization here so
6 * save-load-option.php can stay focused on transport, nonce checks, and storage.
7 *
8 * @package Booking Calendar
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Shared helpers for global option save policies.
17 */
18 class WPBC_Option_Save_Policy_Global_Options {
19
20 /**
21 * Check if current user can save global options.
22 *
23 * In MultiUser regular users do not have access to global options.
24 *
25 * @return bool
26 */
27 public static function can_save_global_options() {
28 return ( ! function_exists( 'wpbc_is_mu_user_can_be_here' ) || wpbc_is_mu_user_can_be_here( 'only_super_admin' ) );
29 }
30
31 /**
32 * Normalize On/Off option values.
33 *
34 * @param mixed $value Raw value.
35 *
36 * @return string
37 */
38 public static function normalize_on_off( $value ) {
39 return ( 'On' === $value ) ? 'On' : 'Off';
40 }
41 }
42
43 /**
44 * Calendar global option policies.
45 */
46 class WPBC_Option_Save_Policy_Global_Calendar {
47
48 /**
49 * Normalize and validate global calendar skin value before saving.
50 *
51 * @param mixed $data_raw Raw calendar skin value.
52 * @param string $data_name Option name.
53 *
54 * @return string
55 */
56 public static function normalize_calendar_skin( $data_raw, $data_name = '' ) {
57
58 $skin_value = is_scalar( $data_raw ) ? (string) $data_raw : '';
59 $replace = array();
60
61 if ( defined( 'WPBC_PLUGIN_DIR' ) ) {
62 $replace[] = WPBC_PLUGIN_DIR;
63 }
64 if ( defined( 'WPBC_PLUGIN_URL' ) ) {
65 $replace[] = WPBC_PLUGIN_URL;
66 }
67
68 $upload_dir = wp_upload_dir();
69 if ( ! empty( $upload_dir['basedir'] ) ) {
70 $replace[] = $upload_dir['basedir'];
71 }
72 if ( ! empty( $upload_dir['baseurl'] ) ) {
73 $replace[] = $upload_dir['baseurl'];
74 }
75
76 $skin_value = str_replace( $replace, '', $skin_value );
77 $skin_value = sanitize_text_field( $skin_value );
78
79 if (
80 '' === $skin_value ||
81 false !== strpos( $skin_value, '..' ) ||
82 ! preg_match( '/\.css$/i', $skin_value ) ||
83 ( 0 !== strpos( $skin_value, '/css/skins/' ) && 0 !== strpos( $skin_value, '/wpbc_skins/' ) )
84 ) {
85 wp_send_json_error( array( 'message' => __( 'Invalid calendar skin.', 'booking' ) ) );
86 }
87
88 $file_path = '';
89 if ( 0 === strpos( $skin_value, '/wpbc_skins/' ) ) {
90 $file_path = ( ! empty( $upload_dir['basedir'] ) ) ? $upload_dir['basedir'] . $skin_value : '';
91 } elseif ( defined( 'WPBC_PLUGIN_DIR' ) ) {
92 $file_path = WPBC_PLUGIN_DIR . $skin_value;
93 }
94
95 if ( empty( $file_path ) || ! file_exists( $file_path ) ) {
96 wp_send_json_error( array( 'message' => __( 'Calendar skin file does not exist.', 'booking' ) ) );
97 }
98
99 return $skin_value;
100 }
101
102 /**
103 * Get allowed global calendar legend option names.
104 *
105 * @return array
106 */
107 public static function get_calendar_legend_option_names() {
108 return array(
109 'booking_is_show_legend',
110 'booking_legend_is_show_item_available',
111 'booking_legend_text_for_item_available',
112 'booking_legend_is_show_item_pending',
113 'booking_legend_text_for_item_pending',
114 'booking_legend_is_show_item_approved',
115 'booking_legend_text_for_item_approved',
116 'booking_legend_is_show_item_partially',
117 'booking_legend_text_for_item_partially',
118 'booking_legend_is_show_item_unavailable',
119 'booking_legend_text_for_item_unavailable',
120 'booking_legend_is_show_numbers',
121 'booking_legend_is_vertical',
122 );
123 }
124
125 /**
126 * Normalize a global calendar legend option before saving.
127 *
128 * @param string $option_key Option name.
129 * @param mixed $value Option value.
130 * @param string $data_name Submitted data name.
131 *
132 * @return string
133 */
134 public static function normalize_calendar_legend_option( $option_key, $value, $data_name = '' ) {
135
136 $on_off_options = array(
137 'booking_is_show_legend',
138 'booking_legend_is_show_item_available',
139 'booking_legend_is_show_item_pending',
140 'booking_legend_is_show_item_approved',
141 'booking_legend_is_show_item_partially',
142 'booking_legend_is_show_item_unavailable',
143 'booking_legend_is_show_numbers',
144 'booking_legend_is_vertical',
145 );
146
147 if ( in_array( $option_key, $on_off_options, true ) ) {
148 return WPBC_Option_Save_Policy_Global_Options::normalize_on_off( $value );
149 }
150
151 return sanitize_text_field( (string) $value );
152 }
153 }
154
155 /**
156 * Time-slot global option policies.
157 */
158 class WPBC_Option_Save_Policy_Global_Time {
159
160 /**
161 * Normalize the global timeslot picker toggle.
162 *
163 * @param mixed $data_raw Raw value.
164 * @param string $data_name Option name.
165 *
166 * @return string
167 */
168 public static function normalize_timeslot_picker( $data_raw, $data_name = '' ) {
169 return WPBC_Option_Save_Policy_Global_Options::normalize_on_off( $data_raw );
170 }
171 }
172
173 /**
174 * Register built-in policies.
175 *
176 * @return void
177 */
178 function wpbc_register_builtin_option_save_policies() {
179
180 if ( ! class_exists( 'wpbc_option_saver_loader' ) ) {
181 return;
182 }
183
184 wpbc_option_saver_loader::register_option_policy(
185 'booking_skin',
186 array(
187 'can_save' => array( 'WPBC_Option_Save_Policy_Global_Options', 'can_save_global_options' ),
188 'permission_message' => __( 'You do not have permission to save global calendar options.', 'booking' ),
189 'normalize_raw' => array( 'WPBC_Option_Save_Policy_Global_Calendar', 'normalize_calendar_skin' ),
190 )
191 );
192
193 wpbc_option_saver_loader::register_option_policy(
194 'wpbc_calendar_legend_options',
195 array(
196 'can_save' => array( 'WPBC_Option_Save_Policy_Global_Options', 'can_save_global_options' ),
197 'permission_message' => __( 'You do not have permission to save global calendar options.', 'booking' ),
198 'force_mode' => 'split',
199 'allowed_keys' => array( 'WPBC_Option_Save_Policy_Global_Calendar', 'get_calendar_legend_option_names' ),
200 'normalize_item' => array( 'WPBC_Option_Save_Policy_Global_Calendar', 'normalize_calendar_legend_option' ),
201 )
202 );
203
204 wpbc_option_saver_loader::register_option_policy(
205 'booking_timeslot_picker',
206 array(
207 'can_save' => array( 'WPBC_Option_Save_Policy_Global_Options', 'can_save_global_options' ),
208 'permission_message' => __( 'You do not have permission to save global time-slot options.', 'booking' ),
209 'normalize_raw' => array( 'WPBC_Option_Save_Policy_Global_Time', 'normalize_timeslot_picker' ),
210 )
211 );
212 }
213 add_action( 'init', 'wpbc_register_builtin_option_save_policies', 5 );
214