PluginProbe
Booking Calendar / 11.8
Booking Calendar v11.8
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 10.11.2 10.11.3 10.11.4 All 201 releases
booking / core / wpbc-core.php

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

324 lines 10.0 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 info@wpbookingcalendar.com
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 function remove_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
83 global $wpbc_bk_filter;
84
85 if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) {
86 for ( $i = 0; $i < count( $wpbc_bk_filter[ $filter_type ] ); $i++ ) {
87 if ( $wpbc_bk_filter[ $filter_type ][ $i ][0] == $filter ) {
88 $wpbc_bk_filter[ $filter_type ][ $i ] = null;
89
90 return;
91 }
92 }
93 }
94 }
95
96 function apply_bk_filter( $filter_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
97 global $wpbc_bk_filter;
98
99
100 $args = array();
101 for ( $a = 1; $a < func_num_args(); $a++ ) {
102 $args[] = func_get_arg( $a );
103 }
104
105 if ( count( $args ) > 0 ) {
106 $value = $args[0];
107 } else {
108 $value = false;
109 }
110
111 if ( is_array( $wpbc_bk_filter ) ) {
112 if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) {
113 foreach ( $wpbc_bk_filter[ $filter_type ] as $filter ) {
114 $filter_func = array_shift( $filter );
115 $parameter = $args;
116 $value = call_user_func_array( $filter_func, $parameter );
117 }
118 }
119 }
120
121 return $value;
122 }
123
124
125 function make_bk_action( $action_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
126 global $wpbc_bk_action;
127
128 $args = array();
129 for ( $a = 1; $a < func_num_args(); $a++ ) {
130 $args[] = func_get_arg( $a );
131 }
132
133 if ( is_array( $wpbc_bk_action ) ) {
134 if ( isset( $wpbc_bk_action[ $action_type ] ) ) {
135 foreach ( $wpbc_bk_action[ $action_type ] as $action ) {
136 $action_func = array_shift( $action );
137 $parameter = $action;
138 call_user_func_array( $action_func, $args );
139 }
140 }
141 }
142 }
143
144 function add_bk_action( $action_type, $action ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
145 global $wpbc_bk_action;
146
147 $args = array();
148 if ( is_array( $action ) && 1 == count( $action ) && is_object( $action[0] ) ) // array(&$this)
149 {
150 $args[] =& $action[0];
151 } else {
152 $args[] = $action;
153 }
154 for ( $a = 2; $a < func_num_args(); $a++ ) {
155 $args[] = func_get_arg( $a );
156 }
157
158 if ( is_array( $wpbc_bk_action ) ) {
159 if ( isset( $wpbc_bk_action[ $action_type ] ) ) {
160 if ( is_array( $wpbc_bk_action[ $action_type ] ) ) {
161 $wpbc_bk_action[ $action_type ][] = $args;
162 } else {
163 $wpbc_bk_action[ $action_type ] = array( $args );
164 }
165 } else {
166 $wpbc_bk_action[ $action_type ] = array( $args );
167 }
168 } else {
169 $wpbc_bk_action = array( $action_type => array( $args ) );
170 }
171 }
172
173 function remove_bk_action( $action_type, $action ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
174 global $wpbc_bk_action;
175
176 $elements_count = count( $wpbc_bk_action[ $action_type ] );
177 if ( isset( $wpbc_bk_action[ $action_type ] ) ) {
178 for ( $i = 0; $i < $elements_count; $i++ ) {
179 if ( $wpbc_bk_action[ $action_type ][ $i ][0] == $action ) {
180 $wpbc_bk_action[ $action_type ][ $i ] = null;
181
182 return;
183 }
184 }
185 }
186 }
187
188
189 function get_bk_option( $option, $default = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
190
191 global $wpbc__skip_get_bk_options;
192
193 if (
194 ! empty( $wpbc__skip_get_bk_options )
195 && is_array( $wpbc__skip_get_bk_options )
196 && in_array( $option, $wpbc__skip_get_bk_options, true )
197 ) {
198 return $default;
199 }
200
201 $u_value = apply_bk_filter( 'wpdev_bk_get_option', 'no-values', $option, $default );
202 if ( 'no-values' !== $u_value ) {
203 return $u_value;
204 }
205 return get_option( $option, $default );
206 }
207
208 function update_bk_option( $option, $newvalue ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
209
210 $u_value = apply_bk_filter( 'wpdev_bk_update_option', 'no-values', $option, $newvalue );
211 if ( 'no-values' !== $u_value ) {
212 return $u_value;
213 }
214 return update_option( $option, $newvalue );
215 }
216
217 function delete_bk_option( $option ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
218
219 $u_value = apply_bk_filter( 'wpdev_bk_delete_option', 'no-values', $option );
220 if ( 'no-values' !== $u_value ) {
221 return $u_value;
222 }
223 return delete_option( $option );
224 }
225
226 function add_bk_option( $option, $value = '', $deprecated = '', $autoload = null ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
227
228 $u_value = apply_bk_filter( 'wpdev_bk_add_option', 'no-values', $option, $value, $deprecated, $autoload );
229 if ( 'no-values' !== $u_value ) {
230 return $u_value;
231 }
232 return add_option( $option, $value, '', $autoload );
233 }
234
235
236 ////////////////////////////////////////////////////////////////////////////////
237 // Booking Meta Options - 'booking_options' IN 'wp_booking' TABLE
238 ////////////////////////////////////////////////////////////////////////////////
239
240 /**
241 * Update Booking Meta Option
242 *
243 * @param int $booking_id ID of the booking
244 * @param array $option_arr Associative array for saving into booking options
245 * Example 1: saving 'locale' => 'de_DE': [ 'locale' => 'de_DE' ],
246 * Example 2: [ 'paid' => '100.00', 'balance' => '90.00' ]
247 *
248 * @return array|object|stdClass[]|null
249 */
250 function wpbc_save_booking_meta_option( $booking_id, $option_arr ) {
251
252 global $wpdb;
253
254 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
255 $select_result = $wpdb->get_results( $wpdb->prepare( "SELECT booking_options FROM {$wpdb->prefix}booking WHERE booking_id = %d", (int) $booking_id ) );
256
257 if ( ! empty( $select_result ) ) {
258
259 $select_result = $select_result[0]->booking_options;
260
261 if ( is_null( $select_result ) ) { // NULL -> option was not defined yet for this booking row.
262 $exist_booking_option_arr = array();
263 } else {
264 $exist_booking_option_arr = maybe_unserialize( $select_result );
265 }
266 } else {
267 return false; // No booking with such ID.
268 }
269
270 // Merge exist booking option array with new data.
271 $new_booking_option_arr = array_merge(
272 $exist_booking_option_arr,
273 $option_arr
274 );
275
276 $serialized_booking_option_arr = maybe_serialize( $new_booking_option_arr );
277
278 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
279 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 ) ) ) {
280 debuge_error( 'Error saving to DB', __FILE__, __LINE__ );
281 return false;
282 }
283
284 return true;
285 }
286
287
288 /**
289 * Get Booking Meta Option
290 *
291 * @param int $booking_id ID of the booking
292 * @param false $option_name name of booking option or false (can be skipped) to return array of options
293 *
294 * @return array|mixed|string
295 */
296 function wpbc_get_booking_meta_option( $booking_id, $option_name = false ) {
297
298 global $wpdb;
299
300 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
301 $select_result = $wpdb->get_results( $wpdb->prepare( "SELECT booking_options FROM {$wpdb->prefix}booking WHERE booking_id = %d", (int) $booking_id ) );
302
303 $exist_booking_option_arr = array();
304
305 if ( ! empty( $select_result ) ) {
306
307 $select_result = $select_result[0]->booking_options;
308
309 if ( ! is_null( $select_result ) ) { // NULL -> chekc if option was not defined for this booking row.
310
311 $exist_booking_option_arr = maybe_unserialize( $select_result );
312
313 if (
314 ( ! empty( $option_name ) ) &&
315 ( ! empty( $exist_booking_option_arr[ $option_name ] ) )
316 ) {
317 return $exist_booking_option_arr[ $option_name ];
318 }
319 }
320 }
321
322 return $exist_booking_option_arr;
323 }
324