PluginProbe
Booking Calendar / 11.9
Booking Calendar v11.9
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 / core / wpbc-core.php

wpbc-core.php in Booking Calendar 11.9, at core/wpbc-core.php

367 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Core Functions
6 * @category Functions
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email [email protected]
11 *
12 * @modified 29.09.2015
13 */
14
15
16 if ( ! defined( 'ABSPATH' ) ) { // Exit if accessed directly.
17 exit;
18 }
19
20 // ---------------------------------------------------------------------------------------------------------------------
21 // Internal plugin action hooks system
22 // ---------------------------------------------------------------------------------------------------------------------
23 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
24 global $wpbc_bk_action, $wpbc_bk_filter;
25
26
27 /**
28 * Mark whether destructive Booking Calendar deactivation is running in this request.
29 *
30 * WordPress cannot unload plugin PHP after deactivate_plugins() is called. This
31 * state lets already-loaded Pro and MultiUser callbacks avoid using deleted data.
32 *
33 * @param bool $is_deactivating Whether deactivation is in progress.
34 */
35 function wpbc_set_plugin_deactivation_state( $is_deactivating ) {
36 global $wpbc_plugin_deactivation_in_progress;
37
38 $wpbc_plugin_deactivation_in_progress = (bool) $is_deactivating;
39 }
40
41
42 /**
43 * Check whether destructive Booking Calendar deactivation is running.
44 *
45 * @return bool
46 */
47 function wpbc_is_plugin_deactivation_in_progress() {
48 global $wpbc_plugin_deactivation_in_progress;
49
50 return ! empty( $wpbc_plugin_deactivation_in_progress );
51 }
52
53
54 function add_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
55 global $wpbc_bk_filter;
56
57 $args = array();
58 if ( is_array( $filter ) && 1 == count( $filter ) && is_object( $filter[0] ) ) {
59 $args[] =& $filter[0];
60 } else {
61 $args[] = $filter;
62 }
63 for ( $a = 2; $a < func_num_args(); $a++ ) {
64 $args[] = func_get_arg( $a );
65 }
66
67 if ( is_array( $wpbc_bk_filter ) ) {
68 if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) {
69 if ( is_array( $wpbc_bk_filter[ $filter_type ] ) ) {
70 $wpbc_bk_filter[ $filter_type ][] = $args;
71 } else {
72 $wpbc_bk_filter[ $filter_type ] = array( $args );
73 }
74 } else {
75 $wpbc_bk_filter[ $filter_type ] = array( $args );
76 }
77 } else {
78 $wpbc_bk_filter = array( $filter_type => array( $args ) );
79 }
80 }
81
82 /**
83 * Remove the first matching callback from an internal Booking Calendar filter.
84 *
85 * Registry entries must be deleted rather than replaced with null because the
86 * same request may apply this filter again after a request-local override ends.
87 *
88 * @param string $filter_type Internal filter identifier.
89 * @param callable $filter Callback registered with add_bk_filter().
90 *
91 * @return void
92 */
93 function remove_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
94 global $wpbc_bk_filter;
95
96 if (
97 ! is_array( $wpbc_bk_filter )
98 || empty( $wpbc_bk_filter[ $filter_type ] )
99 || ! is_array( $wpbc_bk_filter[ $filter_type ] )
100 ) {
101 return;
102 }
103
104 foreach ( $wpbc_bk_filter[ $filter_type ] as $filter_index => $registered_filter ) {
105 if ( ! is_array( $registered_filter ) || ! array_key_exists( 0, $registered_filter ) ) {
106 continue;
107 }
108
109 // Preserve the legacy loose callback comparison for object-method arrays.
110 if ( $registered_filter[0] == $filter ) {
111 unset( $wpbc_bk_filter[ $filter_type ][ $filter_index ] );
112 $wpbc_bk_filter[ $filter_type ] = array_values( $wpbc_bk_filter[ $filter_type ] );
113
114 if ( empty( $wpbc_bk_filter[ $filter_type ] ) ) {
115 unset( $wpbc_bk_filter[ $filter_type ] );
116 }
117
118 return;
119 }
120 }
121 }
122
123 /**
124 * Apply callbacks registered for an internal Booking Calendar filter.
125 *
126 * Additional function arguments are forwarded to every registered callback.
127 * The first additional argument is returned unchanged when no callback exists.
128 *
129 * @param string $filter_type Internal filter identifier.
130 *
131 * @return mixed Last callback result, the first supplied value, or false.
132 */
133 function apply_bk_filter( $filter_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
134 global $wpbc_bk_filter;
135
136
137 $args = array();
138 for ( $a = 1; $a < func_num_args(); $a++ ) {
139 $args[] = func_get_arg( $a );
140 }
141
142 if ( count( $args ) > 0 ) {
143 $value = $args[0];
144 } else {
145 $value = false;
146 }
147
148 if (
149 is_array( $wpbc_bk_filter )
150 && ! empty( $wpbc_bk_filter[ $filter_type ] )
151 && is_array( $wpbc_bk_filter[ $filter_type ] )
152 ) {
153 foreach ( $wpbc_bk_filter[ $filter_type ] as $filter ) {
154 if ( ! is_array( $filter ) || empty( $filter ) ) {
155 continue;
156 }
157
158 $filter_func = array_shift( $filter );
159 $parameter = $args;
160 $value = call_user_func_array( $filter_func, $parameter );
161 }
162 }
163
164 return $value;
165 }
166
167
168 function make_bk_action( $action_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
169 global $wpbc_bk_action;
170
171 $args = array();
172 for ( $a = 1; $a < func_num_args(); $a++ ) {
173 $args[] = func_get_arg( $a );
174 }
175
176 if ( is_array( $wpbc_bk_action ) ) {
177 if ( isset( $wpbc_bk_action[ $action_type ] ) ) {
178 foreach ( $wpbc_bk_action[ $action_type ] as $action ) {
179 $action_func = array_shift( $action );
180 $parameter = $action;
181 call_user_func_array( $action_func, $args );
182 }
183 }
184 }
185 }
186
187 function add_bk_action( $action_type, $action ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
188 global $wpbc_bk_action;
189
190 $args = array();
191 if ( is_array( $action ) && 1 == count( $action ) && is_object( $action[0] ) ) // array(&$this)
192 {
193 $args[] =& $action[0];
194 } else {
195 $args[] = $action;
196 }
197 for ( $a = 2; $a < func_num_args(); $a++ ) {
198 $args[] = func_get_arg( $a );
199 }
200
201 if ( is_array( $wpbc_bk_action ) ) {
202 if ( isset( $wpbc_bk_action[ $action_type ] ) ) {
203 if ( is_array( $wpbc_bk_action[ $action_type ] ) ) {
204 $wpbc_bk_action[ $action_type ][] = $args;
205 } else {
206 $wpbc_bk_action[ $action_type ] = array( $args );
207 }
208 } else {
209 $wpbc_bk_action[ $action_type ] = array( $args );
210 }
211 } else {
212 $wpbc_bk_action = array( $action_type => array( $args ) );
213 }
214 }
215
216 function remove_bk_action( $action_type, $action ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
217 global $wpbc_bk_action;
218
219 $elements_count = count( $wpbc_bk_action[ $action_type ] );
220 if ( isset( $wpbc_bk_action[ $action_type ] ) ) {
221 for ( $i = 0; $i < $elements_count; $i++ ) {
222 if ( $wpbc_bk_action[ $action_type ][ $i ][0] == $action ) {
223 $wpbc_bk_action[ $action_type ][ $i ] = null;
224
225 return;
226 }
227 }
228 }
229 }
230
231
232 function get_bk_option( $option, $default = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
233
234 global $wpbc__skip_get_bk_options;
235
236 if (
237 ! empty( $wpbc__skip_get_bk_options )
238 && is_array( $wpbc__skip_get_bk_options )
239 && in_array( $option, $wpbc__skip_get_bk_options, true )
240 ) {
241 return $default;
242 }
243
244 $u_value = apply_bk_filter( 'wpdev_bk_get_option', 'no-values', $option, $default );
245 if ( 'no-values' !== $u_value ) {
246 return $u_value;
247 }
248 return get_option( $option, $default );
249 }
250
251 function update_bk_option( $option, $newvalue ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
252
253 $u_value = apply_bk_filter( 'wpdev_bk_update_option', 'no-values', $option, $newvalue );
254 if ( 'no-values' !== $u_value ) {
255 return $u_value;
256 }
257 return update_option( $option, $newvalue );
258 }
259
260 function delete_bk_option( $option ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
261
262 $u_value = apply_bk_filter( 'wpdev_bk_delete_option', 'no-values', $option );
263 if ( 'no-values' !== $u_value ) {
264 return $u_value;
265 }
266 return delete_option( $option );
267 }
268
269 function add_bk_option( $option, $value = '', $deprecated = '', $autoload = null ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
270
271 $u_value = apply_bk_filter( 'wpdev_bk_add_option', 'no-values', $option, $value, $deprecated, $autoload );
272 if ( 'no-values' !== $u_value ) {
273 return $u_value;
274 }
275 return add_option( $option, $value, '', $autoload );
276 }
277
278
279 ////////////////////////////////////////////////////////////////////////////////
280 // Booking Meta Options - 'booking_options' IN 'wp_booking' TABLE
281 ////////////////////////////////////////////////////////////////////////////////
282
283 /**
284 * Update Booking Meta Option
285 *
286 * @param int $booking_id ID of the booking
287 * @param array $option_arr Associative array for saving into booking options
288 * Example 1: saving 'locale' => 'de_DE': [ 'locale' => 'de_DE' ],
289 * Example 2: [ 'paid' => '100.00', 'balance' => '90.00' ]
290 *
291 * @return array|object|stdClass[]|null
292 */
293 function wpbc_save_booking_meta_option( $booking_id, $option_arr ) {
294
295 global $wpdb;
296
297 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
298 $select_result = $wpdb->get_results( $wpdb->prepare( "SELECT booking_options FROM {$wpdb->prefix}booking WHERE booking_id = %d", (int) $booking_id ) );
299
300 if ( ! empty( $select_result ) ) {
301
302 $select_result = $select_result[0]->booking_options;
303
304 if ( is_null( $select_result ) ) { // NULL -> option was not defined yet for this booking row.
305 $exist_booking_option_arr = array();
306 } else {
307 $exist_booking_option_arr = maybe_unserialize( $select_result );
308 }
309 } else {
310 return false; // No booking with such ID.
311 }
312
313 // Merge exist booking option array with new data.
314 $new_booking_option_arr = array_merge(
315 $exist_booking_option_arr,
316 $option_arr
317 );
318
319 $serialized_booking_option_arr = maybe_serialize( $new_booking_option_arr );
320
321 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
322 if ( false === $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}booking SET booking_options = %s WHERE booking_id = %d", $serialized_booking_option_arr, (int) $booking_id ) ) ) {
323 debuge_error( 'Error saving to DB', __FILE__, __LINE__ );
324 return false;
325 }
326
327 return true;
328 }
329
330
331 /**
332 * Get Booking Meta Option
333 *
334 * @param int $booking_id ID of the booking
335 * @param false $option_name name of booking option or false (can be skipped) to return array of options
336 *
337 * @return array|mixed|string
338 */
339 function wpbc_get_booking_meta_option( $booking_id, $option_name = false ) {
340
341 global $wpdb;
342
343 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
344 $select_result = $wpdb->get_results( $wpdb->prepare( "SELECT booking_options FROM {$wpdb->prefix}booking WHERE booking_id = %d", (int) $booking_id ) );
345
346 $exist_booking_option_arr = array();
347
348 if ( ! empty( $select_result ) ) {
349
350 $select_result = $select_result[0]->booking_options;
351
352 if ( ! is_null( $select_result ) ) { // NULL -> chekc if option was not defined for this booking row.
353
354 $exist_booking_option_arr = maybe_unserialize( $select_result );
355
356 if (
357 ( ! empty( $option_name ) ) &&
358 ( ! empty( $exist_booking_option_arr[ $option_name ] ) )
359 ) {
360 return $exist_booking_option_arr[ $option_name ];
361 }
362 }
363 }
364
365 return $exist_booking_option_arr;
366 }
367