PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.7
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-option.php

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

475 lines 10.9 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_Option' ) ) :
7
8 final class WPSC_Option {
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 * Cache for ticket list search items
47 *
48 * @var array
49 */
50 private static $tl_search_items;
51
52 /**
53 * Initialize this class
54 *
55 * @return void
56 */
57 public static function init() {
58
59 // Apply schema for this model.
60 add_action( 'init', array( __CLASS__, 'apply_schema' ), 2 );
61
62 // Get object of this class.
63 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
64 }
65
66 /**
67 * Apply schema for this model
68 *
69 * @return void
70 */
71 public static function apply_schema() {
72
73 $schema = array(
74 'id' => array(
75 'has_ref' => false,
76 'ref_class' => '',
77 'has_multiple_val' => false,
78 ),
79 'name' => array(
80 'has_ref' => false,
81 'ref_class' => '',
82 'has_multiple_val' => false,
83 ),
84 'custom_field' => array(
85 'has_ref' => true,
86 'ref_class' => 'wpsc_cf',
87 'has_multiple_val' => false,
88 ),
89 'date_created' => array(
90 'has_ref' => true,
91 'ref_class' => 'datetime',
92 'has_multiple_val' => false,
93 ),
94 'load_order' => array(
95 'has_ref' => false,
96 'ref_class' => '',
97 'has_multiple_val' => false,
98 ),
99 );
100 self::$schema = apply_filters( 'wpsc_option_schema', $schema );
101
102 // Prevent modify.
103 $prevent_modify = array( 'id', 'date_created' );
104 self::$prevent_modify = apply_filters( 'wpsc_option_prevent_modify', $prevent_modify );
105 }
106
107 /**
108 * Model constructor
109 *
110 * @param int $id - Optional. Data record id to retrive object for.
111 */
112 public function __construct( $id = 0 ) {
113
114 global $wpdb;
115
116 $id = intval( $id );
117
118 if ( isset( self::$cache[ $id ] ) ) {
119 $this->data = self::$cache[ $id ]->data;
120 return;
121 }
122
123 if ( $id > 0 ) {
124
125 $option = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_options WHERE id = " . $id, ARRAY_A );
126 if ( ! is_array( $option ) ) {
127 return;
128 }
129
130 foreach ( $option as $key => $val ) {
131
132 if ( $key == 'name' ) {
133 $name = WPSC_Translations::get( 'wpsc-option-' . $option['id'], stripslashes( $val ) );
134 if ( ! $name ) {
135 WPSC_Translations::add( 'wpsc-option-' . $option['id'], $val );
136 WPSC_Translations::register_strings();
137 }
138 $this->data[ $key ] = $val !== null ? WPSC_Translations::get( 'wpsc-option-' . $option['id'], stripslashes( $val ) ) : '';
139
140 } else {
141
142 $this->data[ $key ] = $val !== null ? $val : '';
143 }
144 }
145
146 self::$cache[ $id ] = $this;
147 }
148 }
149
150 /**
151 * Convert object into an array
152 *
153 * @return array
154 */
155 public function to_array() {
156
157 return $this->data;
158 }
159
160 /**
161 * Magic get function to use with object arrow function
162 *
163 * @param string $var_name - variable name.
164 * @return mixed
165 */
166 public function __get( $var_name ) {
167
168 if ( ! isset( $this->data[ $var_name ] ) ||
169 $this->data[ $var_name ] == null ||
170 $this->data[ $var_name ] == ''
171 ) {
172 return self::$schema[ $var_name ]['has_multiple_val'] ? array() : '';
173 }
174
175 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
176
177 $response = array();
178 $values = $this->data[ $var_name ] ? explode( '|', $this->data[ $var_name ] ) : array();
179 foreach ( $values as $val ) {
180 $response[] = self::$schema[ $var_name ]['has_ref'] ?
181 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $val ) :
182 $val;
183 }
184 return $response;
185
186 } else {
187
188 return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ?
189 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) :
190 $this->data[ $var_name ];
191 }
192 }
193
194 /**
195 * Magic function to use setting object field with arrow function
196 *
197 * @param string $var_name - (Required) property slug.
198 * @param mixed $value - (Required) value to set for a property.
199 * @return void
200 */
201 public function __set( $var_name, $value ) {
202
203 if (
204 ! isset( $this->data[ $var_name ] ) ||
205 in_array( $var_name, self::$prevent_modify )
206 ) {
207 return;
208 }
209
210 $data_val = '';
211 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
212
213 $data_vals = array_map(
214 fn( $val ) => is_object( $val ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $val ) : $val,
215 $value
216 );
217
218 $data_val = $data_vals ? implode( '|', $data_vals ) : '';
219
220 } else {
221
222 $data_val = is_object( $value ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) : $value;
223 }
224
225 if ( $this->data[ $var_name ] == $data_val ) {
226 return;
227 }
228
229 $this->data[ $var_name ] = $data_val;
230 $this->is_modified = true;
231 }
232
233 /**
234 * Save changes made
235 *
236 * @return boolean
237 */
238 public function save() {
239
240 global $wpdb;
241
242 if ( ! $this->is_modified ) {
243 return true;
244 }
245
246 $data = $this->data;
247 $success = true;
248
249 unset( $data['id'] );
250 $success = $wpdb->update(
251 $wpdb->prefix . 'psmsc_options',
252 $data,
253 array( 'id' => $this->data['id'] )
254 );
255
256 // Set string translation..
257 WPSC_Translations::add( 'wpsc-option-' . $this->data['id'], $this->data['name'] );
258
259 $this->is_modified = false;
260 self::$cache[ $this->id ] = $this;
261 return $success ? true : false;
262 }
263
264 /**
265 * Insert new record
266 *
267 * @param array $data - insert data.
268 * @return WPSC_Option
269 */
270 public static function insert( $data ) {
271
272 global $wpdb;
273
274 if ( ! isset( $data['load_order'] ) ) {
275 $max_order = $wpdb->get_var( "SELECT MAX(load_order) FROM {$wpdb->prefix}psmsc_options" );
276 $data['load_order'] = intval( $max_order ) + 1;
277 }
278
279 $success = $wpdb->insert(
280 $wpdb->prefix . 'psmsc_options',
281 $data
282 );
283
284 if ( ! $success ) {
285 return false;
286 }
287
288 $id = $wpdb->insert_id;
289
290 // string translation.
291 WPSC_Translations::add( 'wpsc-option-' . $id, $data['name'] );
292
293 $option = new WPSC_Option( $id );
294
295 return $option;
296 }
297
298 /**
299 * Delete record of given ID
300 *
301 * @param WPSC_Option $option - option object.
302 * @return boolean
303 */
304 public static function destroy( $option ) {
305
306 global $wpdb;
307
308 $success = $wpdb->delete(
309 $wpdb->prefix . 'psmsc_options',
310 array( 'id' => $option->id )
311 );
312 if ( ! $success ) {
313 return false;
314 }
315
316 unset( self::$cache[ $option->id ] );
317
318 // remove string translations.
319 WPSC_Translations::remove( 'wpsc-option-' . $option->id );
320
321 return true;
322 }
323
324 /**
325 * Set data to create new object using direct data. Used in find method
326 *
327 * @param array $data - data to set for object.
328 * @return void
329 */
330 private function set_data( $data ) {
331
332 foreach ( $data as $var_name => $val ) {
333
334 if ( $var_name == 'name' ) {
335
336 $this->data[ $var_name ] = $val !== null ? WPSC_Translations::get( 'wpsc-option-' . $data['id'], stripslashes( $val ) ) : '';
337
338 } else {
339
340 $this->data[ $var_name ] = $val !== null ? $val : '';
341 }
342 }
343
344 self::$cache[ $this->id ] = $this;
345 }
346
347 /**
348 * Find records based on given filters
349 *
350 * @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc.
351 * @param boolean $is_object - return data as array or object. Default object.
352 * @return mixed
353 */
354 public static function find( $filter = array(), $is_object = true ) {
355
356 global $wpdb;
357
358 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_options ';
359 $where = self::get_where( $filter );
360
361 $filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 0;
362 $filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 0;
363 $filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'load_order';
364 $filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC';
365
366 $order = WPSC_Functions::parse_order( $filter );
367
368 $sql = $sql . $where . $order;
369 $results = $wpdb->get_results( $sql, ARRAY_A );
370
371 // total results.
372 $sql = 'SELECT count(id) FROM ' . $wpdb->prefix . 'psmsc_options ';
373 $total_items = $wpdb->get_var( $sql . $where );
374
375 $response = WPSC_Functions::parse_response( $results, $total_items, $filter );
376
377 // Return array.
378 if ( ! $is_object ) {
379 return $response;
380 }
381
382 // create and return array of objects.
383 $temp_results = array();
384 foreach ( $response['results'] as $option ) {
385
386 $ob = new WPSC_Option();
387 $data = array();
388 foreach ( $option as $key => $val ) {
389 $data[ $key ] = $val;
390 }
391 $ob->set_data( $data );
392 $temp_results[] = $ob;
393 }
394 $response['results'] = $temp_results;
395
396 return $response;
397 }
398
399 /**
400 * Get where for find method
401 *
402 * @param array $filter - user filter.
403 * @return array
404 */
405 private static function get_where( $filter ) {
406
407 $where = array( '1=1' );
408
409 // Set user defined filters.
410 $meta_query = isset( $filter['meta_query'] ) ? $filter['meta_query'] : array();
411 if ( $meta_query ) {
412 $where[] = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query );
413 }
414
415 // Search.
416 $search = WPSC_Functions::get_filter_search_str( $filter );
417 if ( $search ) {
418 $search_query = array(
419 'CONVERT(name USING utf8) LIKE \'%' . $search . '%\'',
420 );
421 $search_query = apply_filters( 'psc_option_search_query', $search_query, $filter );
422 $where[] = '( ' . implode( ' OR ', $search_query ) . ' )';
423 }
424
425 return 'WHERE ' . implode( ' AND ', $where ) . ' ';
426 }
427
428 /**
429 * Load current class to reference classes
430 *
431 * @param array $classes - Associative array of class names indexed by its slug.
432 * @return array
433 */
434 public static function load_ref_class( $classes ) {
435
436 $classes['wpsc_option'] = array(
437 'class' => __CLASS__,
438 'save-key' => 'id',
439 );
440 return $classes;
441 }
442
443 /**
444 * Returns option ids to be used for search in ticket list
445 *
446 * @param string $search - search string.
447 * @return array
448 */
449 public static function get_tl_search_string( $search ) {
450
451 $search_items = array();
452 if ( is_array( self::$tl_search_items ) ) {
453 $search_items = self::$tl_search_items;
454 } else {
455 $options = self::find(
456 array(
457 'search' => $search,
458 'items_per_page' => 0,
459 )
460 )['results'];
461 if ( $options ) {
462 foreach ( $options as $option ) {
463 $search_items[] = $option->id;
464 }
465 }
466 self::$tl_search_items = $search_items;
467 }
468
469 return $search_items;
470 }
471 }
472 endif;
473
474 WPSC_Option::init();
475