PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / trunk
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent vtrunk
3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / models / class-wpsc-category.php

class-wpsc-category.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent trunk, at includes/models/class-wpsc-category.php

427 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_Category' ) ) :
7
8 final class WPSC_Category {
9
10 /**
11 * Object data in key => val pair.
12 *
13 * @var array
14 */
15 private $data = array();
16
17 /**
18 * Set whether or not current object properties modified
19 *
20 * @var boolean
21 */
22 private $is_modified = false;
23
24 /**
25 * Schema for this model
26 *
27 * @var array
28 */
29 public static $schema = array();
30
31 /**
32 * Prevent fields to modify
33 *
34 * @var array
35 */
36 public static $prevent_modify = array();
37
38 /**
39 * DB object caching
40 *
41 * @var array
42 */
43 private static $cache = array();
44
45 /**
46 * Initialize this class
47 *
48 * @return void
49 */
50 public static function init() {
51
52 // Apply schema for this model.
53 add_action( 'init', array( __CLASS__, 'apply_schema' ), 2 );
54
55 // Get object of this class.
56 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
57 }
58
59 /**
60 * Apply schema for this model
61 *
62 * @return void
63 */
64 public static function apply_schema() {
65
66 $schema = array(
67 'id' => array(
68 'has_ref' => false,
69 'ref_class' => '',
70 'has_multiple_val' => false,
71 ),
72 'name' => array(
73 'has_ref' => false,
74 'ref_class' => '',
75 'has_multiple_val' => false,
76 ),
77 'load_order' => array(
78 'has_ref' => false,
79 'ref_class' => '',
80 'has_multiple_val' => false,
81 ),
82 );
83 self::$schema = apply_filters( 'wpsc_category_schema', $schema );
84
85 // Prevent modify.
86 $prevent_modify = array( 'id' );
87 self::$prevent_modify = apply_filters( 'wpsc_category_prevent_modify', $prevent_modify );
88 }
89
90 /**
91 * Model constructor
92 *
93 * @param int $id - Optional. Data record id to retrive object for.
94 */
95 public function __construct( $id = 0 ) {
96
97 global $wpdb;
98
99 $id = intval( $id );
100
101 if ( isset( self::$cache[ $id ] ) ) {
102 $this->data = self::$cache[ $id ]->data;
103 return;
104 }
105
106 if ( $id > 0 ) {
107
108 $category = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_categories WHERE id = " . $id, ARRAY_A );
109 if ( ! is_array( $category ) ) {
110 return;
111 }
112
113 foreach ( $category as $key => $val ) {
114
115 if ( $key == 'name' ) {
116
117 $this->data[ $key ] = $val !== null ? WPSC_Translations::get( 'wpsc-category-' . $category['id'], stripslashes( $val ) ) : '';
118
119 } else {
120 $this->data[ $key ] = $val !== null ? $val : '';
121 }
122 }
123
124 self::$cache[ $id ] = $this;
125 }
126 }
127
128 /**
129 * Convert object into an array
130 *
131 * @return array
132 */
133 public function to_array() {
134
135 return $this->data;
136 }
137
138 /**
139 * Magic get function to use with object arrow function
140 *
141 * @param string $var_name - variable name.
142 * @return mixed
143 */
144 public function __get( $var_name ) {
145
146 if ( ! isset( $this->data[ $var_name ] ) ||
147 $this->data[ $var_name ] == null ||
148 $this->data[ $var_name ] == ''
149 ) {
150 return self::$schema[ $var_name ]['has_multiple_val'] ? array() : '';
151 }
152
153 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
154
155 $response = array();
156 $values = $this->data[ $var_name ] ? explode( '|', $this->data[ $var_name ] ) : array();
157 foreach ( $values as $val ) {
158 $response[] = self::$schema[ $var_name ]['has_ref'] ?
159 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $val ) :
160 $val;
161 }
162 return $response;
163
164 } else {
165
166 return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ?
167 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) :
168 $this->data[ $var_name ];
169 }
170 }
171
172 /**
173 * Magic function to use setting object field with arrow function
174 *
175 * @param string $var_name - (Required) property slug.
176 * @param mixed $value - (Required) value to set for a property.
177 * @return void
178 */
179 public function __set( $var_name, $value ) {
180
181 if (
182 ! isset( $this->data[ $var_name ] ) ||
183 in_array( $var_name, self::$prevent_modify )
184 ) {
185 return;
186 }
187
188 $data_val = '';
189 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
190
191 $data_vals = array_map(
192 fn( $val ) => is_object( $val ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $val ) : $val,
193 $value
194 );
195
196 $data_val = $data_vals ? implode( '|', $data_vals ) : '';
197
198 } else {
199
200 $data_val = is_object( $value ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) : $value;
201 }
202
203 if ( $this->data[ $var_name ] == $data_val ) {
204 return;
205 }
206
207 $this->data[ $var_name ] = $data_val;
208 $this->is_modified = true;
209 }
210
211 /**
212 * Save changes made
213 *
214 * @return boolean
215 */
216 public function save() {
217
218 global $wpdb;
219
220 if ( ! $this->is_modified ) {
221 return true;
222 }
223
224 $data = $this->data;
225 $success = true;
226
227 if ( ! isset( $data['id'] ) ) {
228
229 $ct = self::insert( $data );
230 if ( $ct ) {
231 $this->data = $ct->data;
232 $success = true;
233 } else {
234 $success = false;
235 }
236 } else {
237
238 unset( $data['id'] );
239 $success = $wpdb->update(
240 $wpdb->prefix . 'psmsc_categories',
241 $data,
242 array( 'id' => $this->data['id'] )
243 );
244
245 // Set string translation..
246 WPSC_Translations::add( 'wpsc-category-' . $this->data['id'], $this->data['name'] );
247 }
248 $this->is_modified = false;
249 self::$cache[ $this->id ] = $this;
250 return $success ? true : false;
251 }
252
253 /**
254 * Insert new record
255 *
256 * @param array $data - insert data.
257 * @return WPSC_Category
258 */
259 public static function insert( $data ) {
260
261 global $wpdb;
262
263 if ( ! isset( $data['load_order'] ) ) {
264 $max_order = $wpdb->get_var( "SELECT MAX(load_order) FROM {$wpdb->prefix}psmsc_categories" );
265 $data['load_order'] = intval( $max_order ) + 1;
266 }
267
268 $success = $wpdb->insert(
269 $wpdb->prefix . 'psmsc_categories',
270 $data
271 );
272
273 if ( ! $success ) {
274 return false;
275 }
276
277 $id = $wpdb->insert_id;
278
279 // string translation.
280 WPSC_Translations::add( 'wpsc-category-' . $id, $data['name'] );
281
282 $category = new WPSC_Category( $id );
283
284 return $category;
285 }
286
287 /**
288 * Delete record of given ID
289 *
290 * @param WPSC_Category $category - category object.
291 * @return boolean
292 */
293 public static function destroy( $category ) {
294
295 global $wpdb;
296
297 $success = $wpdb->delete(
298 $wpdb->prefix . 'psmsc_categories',
299 array( 'id' => $category->id )
300 );
301 if ( ! $success ) {
302 return false;
303 }
304
305 unset( self::$cache[ $category->id ] );
306
307 // remove string translations.
308 WPSC_Translations::remove( 'wpsc-category-' . $category->id );
309
310 return true;
311 }
312
313 /**
314 * Set data to create new object using direct data. Used in find method
315 *
316 * @param array $data - data to set for object.
317 * @return void
318 */
319 private function set_data( $data ) {
320
321 foreach ( $data as $var_name => $val ) {
322
323 if ( $var_name == 'name' ) {
324
325 $this->data[ $var_name ] = $val !== null ? WPSC_Translations::get( 'wpsc-category-' . $data['id'], stripslashes( $val ) ) : '';
326
327 } else {
328
329 $this->data[ $var_name ] = $val !== null ? $val : '';
330 }
331 }
332 self::$cache[ $this->id ] = $this;
333 }
334
335 /**
336 * Find records based on given filters
337 *
338 * @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc.
339 * @param boolean $is_object - return data as array or object. Default object.
340 * @return mixed
341 */
342 public static function find( $filter = array(), $is_object = true ) {
343
344 global $wpdb;
345
346 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_categories ';
347 $where = self::get_where( $filter );
348
349 $filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 0;
350 $filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 0;
351 $filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'load_order';
352 $filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC';
353
354 $order = WPSC_Functions::parse_order( $filter );
355
356 $sql = $sql . $where . $order;
357 $results = $wpdb->get_results( $sql, ARRAY_A );
358
359 // total results.
360 $sql = 'SELECT count(id) FROM ' . $wpdb->prefix . 'psmsc_categories ';
361 $total_items = $wpdb->get_var( $sql . $where );
362
363 $response = WPSC_Functions::parse_response( $results, $total_items, $filter );
364
365 // Return array.
366 if ( ! $is_object ) {
367 return $response;
368 }
369
370 // create and return array of objects.
371 $temp_results = array();
372 foreach ( $response['results'] as $category ) {
373
374 $ob = new WPSC_Category();
375 $data = array();
376 foreach ( $category as $key => $val ) {
377 $data[ $key ] = $val;
378 }
379 $ob->set_data( $data );
380 $temp_results[] = $ob;
381 }
382 $response['results'] = $temp_results;
383
384 return $response;
385 }
386
387 /**
388 * Get where for find method
389 *
390 * @param array $filter - user filter.
391 * @return array
392 */
393 private static function get_where( $filter ) {
394
395 $where = '';
396
397 // Set user defined filters.
398 $meta_query = isset( $filter['meta_query'] ) && $filter['meta_query'] ? $filter['meta_query'] : array();
399 $meta_query = apply_filters( 'wpsc_category_meta_query', $meta_query, $filter );
400 if ( $meta_query ) {
401
402 $meta_query = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query );
403 $where = $meta_query . ' ';
404 }
405
406 return $where ? 'WHERE ' . $where : '';
407 }
408
409 /**
410 * Load current class to reference classes
411 *
412 * @param array $classes - Associative array of class names indexed by its slug.
413 * @return array
414 */
415 public static function load_ref_class( $classes ) {
416
417 $classes['wpsc_category'] = array(
418 'class' => __CLASS__,
419 'save-key' => 'id',
420 );
421 return $classes;
422 }
423 }
424 endif;
425
426 WPSC_Category::init();
427