PluginProbe
Booking Calendar / 11.8.1
Booking Calendar v11.8.1
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 10.11.2 10.11.3 All 202 releases
booking / includes / _capacity / resource_support.php

resource_support.php in Booking Calendar 11.8.1, at includes/_capacity/resource_support.php

328 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4.
4
5
6 class WPBC_RESOURCE_SUPPORT {
7
8 public $resource_id_arr;
9 public $as_single_resource;
10
11 private $bookings_sql_obj_arr;
12
13 function __construct( $params =array() ) {
14
15 $defaults = array(
16 'resource_id' => 1 // CSD | D
17 , 'as_single_resource' => false // If true, then skip child booking resources
18 );
19 $params = wp_parse_args( $params, $defaults );
20
21 $this->resource_id_arr = $params['resource_id'];
22 $this->as_single_resource = $params['as_single_resource'];
23
24 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
25 $this->bookings_sql_obj_arr = $this->get_free_resource();
26 } else {
27 $this->bookings_sql_obj_arr = $this->get__sql__booking_resources__arr();
28 }
29
30 $this->reindex_booking_resources__by__resource_id();
31
32 $this->update__capacity__in_booking_resources();
33 }
34
35
36 /**
37 * Get default data for Free version
38 * @return array
39 */
40 private function get_free_resource() {
41
42 $free_resource_obj = new stdClass();
43 $free_resource_obj->booking_type_id = 1;
44 $free_resource_obj->default_form = 'standard';
45 $free_resource_obj->title = __( 'Default', 'booking' );
46 $free_resource_obj->users = 1;
47 $free_resource_obj->import = '';
48 $free_resource_obj->export = '';
49 $free_resource_obj->cost = 0;
50 $free_resource_obj->prioritet = 1;
51 $free_resource_obj->parent = 0;
52 $free_resource_obj->visitors = 1;
53 $free_resource_obj->capacity = 1;
54
55 return array( $free_resource_obj );
56 }
57
58
59 /**
60 * Get booking resources from the DB, based on request parameters
61 *
62 * @return array|mixed|object|stdClass[]|null
63 */
64 function get__sql__booking_resources__arr() {
65
66 // CACHE - GET :: check if such request was cached and get it ----------------------------------------------- // FixIn: 9.7.3.14.
67 $params_for_cache = maybe_serialize( $this->resource_id_arr );
68 $cache_result = wpbc_cache__get( 'WPBC_RESOURCE_SUPPORT__sql', $params_for_cache );
69 if ( ! is_null( $cache_result ) ) {
70 return $cache_result;
71 }
72 // -----------------------------------------------------------------------------------------------------------------
73
74 global $wpdb;
75
76 // Init params
77 $params = array(
78 'resource_id' => implode( ',', $this->resource_id_arr )
79 );
80 // S a n i t i z e
81 $params_rules = array(
82 'resource_id' => array( 'validate' => 'digit_or_csd', 'default' => 1 )
83 );
84 $params = wpbc_sanitize_params_in_arr( $params, $params_rules );
85
86 // S Q L
87 $sql = array();
88 $sql_args = array();
89
90 $sql['start_select'] = "SELECT * "; // "SELECT DISTINCT calendar_date ";
91 $sql['from'] = "FROM {$wpdb->prefix}bookingtypes ";
92
93 // W H E R E
94 $sql['where'] = 'WHERE ( 1 = 1 ) ';
95 if ( ! empty( $params['resource_id'] ) ) {
96 $sql['where'] .= " AND ( ";
97 $sql['where'] .= " booking_type_id IN ( {$params['resource_id']} ) ";
98 if ( ( class_exists( 'wpdev_bk_biz_l' ) ) && ( ! $this->as_single_resource ) ) {
99 $sql['where'] .= " OR parent IN ( {$params['resource_id']} ) ";
100 }
101 $sql['where'] .= " ) ";
102 }
103 /**
104 *
105 if ( '' != $params['prop_value'] ) {
106 $sql['where'] .= " AND ( prop_value = %s ) ";
107 $sql_args[] = $params['prop_value'];
108 }
109 */
110
111 // O R D E R
112 if ( class_exists( 'wpdev_bk_biz_l' ) ) {
113 $sql['order'] = " ORDER BY parent, prioritet, title, booking_type_id";
114 } else {
115 $sql['order'] = " ORDER BY title, booking_type_id";
116 }
117
118 // L I M I T
119 $sql['limit'] = ''; /**
120 * $sql['limit'] = " LIMIT %d, %d ";
121 * $sql_args[] = ( $params['page_num'] - 1 ) * $params['page_items_count'];
122 * $sql_args[] = $params['page_items_count'];
123 */
124
125
126 /**
127 * Good Practice: https://blog.ircmaxell.com/2017/10/disclosure-wordpress-wpdb-sql-injection-technical.html
128 *
129 * $sql['where'] .= "( bk.form LIKE %s ) ";
130 * $sql_args[] = '%' . $wpdb->esc_like( $params['keyword'] ) . '%';
131 *
132 * if ( is_numeric( $params['keyword'] ) ) {
133 * $sql['where'] .= " OR ( bk.booking_id = %d ) ";
134 * $sql_args[] = intval( $params['keyword'] );
135 * }
136 *
137 */
138 if ( empty( $sql_args ) ) {
139 $sql_prepared = $sql['start_select'] . $sql['from'] . $sql['where'] . $sql['order'] . $sql['limit'];
140 } else {
141 /* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare */
142 $sql_prepared = $wpdb->prepare( $sql['start_select'] . $sql['from'] . $sql['where'] . $sql['order'] . $sql['limit'] , $sql_args );
143 }
144 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
145 $bookings_sql_obj = $wpdb->get_results( $sql_prepared );
146
147 // CACHE - SAVE :: ------------------------------------------------------------------------------------------------
148 $cache_result = wpbc_cache__save( 'WPBC_RESOURCE_SUPPORT__sql', $params_for_cache, $bookings_sql_obj );
149
150 return $bookings_sql_obj;
151 }
152
153
154 /**
155 * Update capacity property to all booking resources
156 * @return void
157 */
158 private function update__capacity__in_booking_resources(){
159
160 foreach ( $this->bookings_sql_obj_arr as $key => $resource_obj ) {
161
162 $is_this_child = ( ( isset( $resource_obj->parent ) ) && ( 0 != $resource_obj->parent ) ) ? true : false;
163 $resource_id = $resource_obj->booking_type_id;
164 $capacity_count = 1;
165
166 if ( ( ! $is_this_child ) && ( class_exists( 'wpdev_bk_biz_l' ) ) ) {
167 foreach ( $this->bookings_sql_obj_arr as $child_key => $child_resource_obj ) {
168 if ( ( isset( $resource_obj->parent ) ) && ( $resource_id == $child_resource_obj->parent ) ) {
169 $capacity_count++;
170 }
171 }
172 }
173
174 $this->bookings_sql_obj_arr[ $key ]->capacity = $capacity_count;
175 }
176 }
177
178
179 /**
180 * Reindex booking resource - set keys as resource_id
181 * @return array
182 */
183 private function reindex_booking_resources__by__resource_id (){
184
185 $resource__arr = array();
186 foreach ( $this->bookings_sql_obj_arr as $sort_key => $resource_obj ) {
187 $resource_obj->sort_key=$sort_key;
188 $resource__arr[ $resource_obj->booking_type_id ] = $resource_obj;
189 }
190 $this->bookings_sql_obj_arr = $resource__arr;
191
192 return $this->bookings_sql_obj_arr;
193 }
194
195
196 /**
197 * Get booking resource object with all properties from DB
198 *
199 * @param int $resource_id
200 *
201 * @return false|mixed|stdClass if not fount then false
202 */
203 function get__booking_resource_obj( $resource_id ) {
204
205 if ( isset( $this->bookings_sql_obj_arr[ $resource_id ] ) ) {
206 return $this->bookings_sql_obj_arr[ $resource_id ];
207 } else {
208 return false;
209 }
210 }
211
212
213 /**
214 * Get title of booking resource
215 *
216 * @param int $resource_id
217 *
218 * @return string
219 */
220 function get_resource_title( $resource_id ) {
221
222 $resource_title = '';
223
224 $resource_obj = $this->get__booking_resource_obj( $resource_id );
225
226 if ( false !== $resource_obj ) {
227 $resource_title = $resource_obj->title;
228
229 $resource_title = wpbc_lang( $resource_title );
230 }
231
232 return $resource_title;
233 }
234
235
236 /**
237 * Get ID of all booking resources [ 2, 10, 11, 12 ]
238 *
239 * @return array // [ 2, 10, 11, 12 ]
240 */
241 function get_resource_id_arr() {
242
243 $resource_id_arr = array_keys( $this->bookings_sql_obj_arr );
244 return $resource_id_arr;
245 }
246
247
248 /**
249 * Get cost of all booking resources [ 'resource_id' => cost, ... ]
250 *
251 * @return array // [ 2=>20, 10>100, 12=>101, 13=>120 ]
252 */
253 function get_resources_base_cost_arr() {
254
255 $resource__arr = array();
256 foreach ( $this->bookings_sql_obj_arr as $key => $resource_obj ) {
257 $resource__arr[ $resource_obj->booking_type_id ] = isset( $resource_obj->cost ) ? $resource_obj->cost : 0;
258 }
259 return $resource__arr;
260 }
261
262
263 public function get_resources_obj_arr(){
264 return $this->bookings_sql_obj_arr;
265 }
266 }
267
268
269
270
271
272 // <editor-fold defaultstate="collapsed" desc=" == Support for booking resource IDs == " >
273
274 // -----------------------------------------------------------------------------------------------------------------
275 // Support for booking resource IDs
276 // -----------------------------------------------------------------------------------------------------------------
277
278 /**
279 * Convert $resource_id and $additional_bk_types to one array
280 *
281 * in BL this $params['additional_bk_types'] is INT or CSD ,in low version it ARRAY
282 *
283 * @param mixed $resource_id - array, CSD, int
284 * @param mixed $additional_bk_types - array, CSD, int
285 *
286 * @return array unique array of booking resource array( 1 ) | array( 3, 9, 10, 11 )
287 */
288 function wpbc_get_unique_array_of_resources_id( $resource_id = '', $additional_bk_types = '' ){
289
290 // in BL this $params['additional_bk_types'] is INT or CSD
291 // in low version $params['additional_bk_types'] is ARRAY
292
293 // If we do not pass any parameters, then use default booking resource with ID = 1
294 if ( ( empty( $resource_id ) ) && ( empty( $additional_bk_types ) ) ) {
295 $resource_id = '1';
296 }
297
298 // Get strings from parameters
299 if ( is_array( $resource_id ) ) {
300 $resource_id = implode( ',', $resource_id );
301 } else {
302 $resource_id = (string) $resource_id;
303 }
304
305 if ( is_array( $additional_bk_types ) ) {
306 $additional_bk_types = implode( ',', $additional_bk_types );
307 } else {
308 $additional_bk_types = (string) $additional_bk_types;
309 }
310
311 $resource_id_csd = $resource_id . ',' . $additional_bk_types; // Concat 2 lists
312 $resource_id_csd = str_replace( ';', ',', $resource_id_csd ); // Check for possible mistakes in separators
313
314 $resource_id__arr = explode( ',', $resource_id_csd ); // Get one array of booking resources
315 $resource_id__arr = array_unique( $resource_id__arr ); // Removes duplicate values from an array
316 $resource_id__arr = array_filter( $resource_id__arr ); // All entries of array equal to FALSE (0, '', '0' ) will be removed.
317
318 // Sanitize ID values
319 foreach ( $resource_id__arr as $resource_key => $resource_value ) {
320 $resource_id__arr[ $resource_key ] = intval( $resource_value );
321 }
322
323 return $resource_id__arr;
324 }
325
326 // </editor-fold>
327
328