PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.2
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.2
3.5.4 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 / admin / ai-chatbot / models / class-wpsc-acb-messages.php

class-wpsc-acb-messages.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.2, at includes/admin/ai-chatbot/models/class-wpsc-acb-messages.php

411 lines 9.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_ACB_Messages' ) ) :
7
8 final class WPSC_ACB_Messages {
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 'session_id' => array(
73 'has_ref' => false,
74 'ref_class' => '',
75 'has_multiple_val' => false,
76 ),
77 'sender' => array(
78 'has_ref' => false,
79 'ref_class' => '',
80 'has_multiple_val' => false,
81 ),
82 'message' => array(
83 'has_ref' => false,
84 'ref_class' => '',
85 'has_multiple_val' => false,
86 ),
87 'token_count' => array(
88 'has_ref' => false,
89 'ref_class' => '',
90 'has_multiple_val' => false,
91 ),
92 'date_created' => array(
93 'has_ref' => true,
94 'ref_class' => 'datetime',
95 'has_multiple_val' => false,
96 ),
97 );
98 self::$schema = apply_filters( 'wpsc_acb_messages_schema', $schema );
99
100 // Prevent modify.
101 $prevent_modify = array( 'id', 'session_id' );
102 self::$prevent_modify = apply_filters( 'wpsc_acb_messages_prevent_modify', $prevent_modify );
103 }
104
105 /**
106 * Model constructor
107 *
108 * @param int $id - Optional. Data record id to retrieve object for.
109 * @return void
110 */
111 public function __construct( $id = 0 ) {
112
113 global $wpdb;
114 $id = intval( $id );
115
116 if ( isset( self::$cache[ $id ] ) ) {
117 $this->data = self::$cache[ $id ]->data;
118 return;
119 }
120
121 if ( $id > 0 ) {
122 $tag = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}psmsc_acb_messages WHERE id = %d", $id ), ARRAY_A );
123 if ( ! is_array( $tag ) ) {
124 return;
125 }
126
127 foreach ( $tag as $key => $val ) {
128 $this->data[ $key ] = $val !== null ? $val : '';
129 }
130 self::$cache[ $id ] = $this;
131 }
132 }
133
134 /**
135 * Magic get function to use with object arrow function
136 *
137 * @param string $var_name - variable name.
138 * @return mixed
139 */
140 public function __get( $var_name ) {
141
142 if ( ! isset( $this->data[ $var_name ] ) ||
143 $this->data[ $var_name ] == null ||
144 $this->data[ $var_name ] == ''
145 ) {
146 return self::$schema[ $var_name ]['has_multiple_val'] ? array() : '';
147 }
148
149 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
150
151 $response = array();
152 $values = $this->data[ $var_name ] ? explode( '|', $this->data[ $var_name ] ) : array();
153 foreach ( $values as $val ) {
154 $response[] = self::$schema[ $var_name ]['has_ref'] ?
155 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $val ) :
156 $val;
157 }
158 return $response;
159 } else {
160
161 return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ?
162 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) :
163 $this->data[ $var_name ];
164 }
165 }
166
167 /**
168 * Magic function to use setting object field with arrow function
169 *
170 * @param string $var_name - (Required) property slug.
171 * @param mixed $value - (Required) value to set for a property.
172 * @return void
173 */
174 public function __set( $var_name, $value ) {
175
176 if ( ! isset( $this->data[ $var_name ] ) ) {
177 return;
178 }
179
180 if ( in_array( $var_name, self::$prevent_modify ) ) {
181 return;
182 }
183
184 $data_val = '';
185 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
186 $data_vals = array_map(
187 fn( $val ) => is_object( $val ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $val ) : $val,
188 $value
189 );
190 $data_val = $data_vals ? implode( '|', $data_vals ) : '';
191 } else {
192
193 $data_val = is_object( $value ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) : $value;
194 }
195
196 if ( isset( $this->data[ $var_name ] ) && $this->data[ $var_name ] == $data_val ) {
197 return;
198 }
199
200 $this->data[ $var_name ] = $data_val;
201 $this->is_modified = true;
202 }
203
204 /**
205 * Save changes made
206 *
207 * @return boolean
208 */
209 public function save() {
210
211 global $wpdb;
212
213 if ( ! $this->is_modified ) {
214 return true;
215 }
216
217 $data = $this->data;
218 $success = true;
219
220 if ( ! isset( $data['id'] ) ) {
221
222 $cr = self::insert( $data );
223 if ( $cr ) {
224 $this->data = $cr->data;
225 $success = true;
226 } else {
227 $success = false;
228 }
229 } else {
230
231 unset( $data['id'] );
232 $success = $wpdb->update(
233 $wpdb->prefix . 'psmsc_acb_messages',
234 $data,
235 array( 'id' => $this->data['id'] )
236 );
237 }
238 $this->is_modified = false;
239 self::$cache[ $this->id ] = $this;
240 return $success ? true : false;
241 }
242
243 /**
244 * Insert new record
245 *
246 * @param array $data - insert data.
247 * @return boolean|WPSC_ACB_Messages
248 */
249 public static function insert( $data ) {
250
251 global $wpdb;
252 $success = $wpdb->insert(
253 $wpdb->prefix . 'psmsc_acb_messages',
254 $data
255 );
256
257 if ( ! $success ) {
258 return false;
259 }
260
261 $log = new WPSC_ACB_Messages( $wpdb->insert_id );
262 self::$cache[ $log->id ] = $log;
263 return $log;
264 }
265
266 /**
267 * Make it inactive so that garbage collector will delete files associated in
268 * background and then delete the record. This will improve its performance.
269 *
270 * @param WPSC_ACB_Messages $log - log object.
271 * @return boolean
272 */
273 public static function destroy( $log ) {
274
275 global $wpdb;
276 $success = $wpdb->delete(
277 $wpdb->prefix . 'psmsc_acb_messages',
278 array( 'id' => $log->id )
279 );
280
281 unset( self::$cache[ $log->id ] );
282 return ! $success ? false : true;
283 }
284
285 /**
286 * Set data to create new object using direct data. Used in find method
287 *
288 * @param array $data - data to set for object.
289 * @return void
290 */
291 private function set_data( $data ) {
292
293 foreach ( $data as $var_name => $val ) {
294 $this->data[ $var_name ] = $val !== null ? $val : '';
295 }
296 self::$cache[ $this->id ] = $this;
297 }
298
299 /**
300 * Find records based on given filters
301 *
302 * @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc.
303 * @param boolean $is_object - return data as array or object. Default object.
304 * @return mixed
305 */
306 public static function find( $filter = array(), $is_object = true ) {
307
308 global $wpdb;
309
310 $filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 20;
311 $filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 1;
312 $filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'date_created';
313 $filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC';
314
315 $sql = 'SELECT acbm.* FROM ' . $wpdb->prefix . 'psmsc_acb_messages acbm ';
316 $where = self::get_where( $filter );
317
318 // Add table alias to orderby.
319 $filter['orderby'] = 'acbm.' . $filter['orderby'];
320
321 $order = WPSC_Functions::parse_order( $filter );
322 $limit = WPSC_Functions::parse_limit( $filter );
323
324 $group_by = 'GROUP BY acbm.id ';
325
326 $sql = $sql . $where . $group_by . $order . $limit;
327 $results = $wpdb->get_results( $sql, ARRAY_A );
328
329 // total results.
330 $sql = 'SELECT count(DISTINCT acbm.id) FROM ' . $wpdb->prefix . 'psmsc_acb_messages acbm ';
331 $total_items = $wpdb->get_var( $sql . $where );
332
333 $response = WPSC_Functions::parse_response( $results, $total_items, $filter );
334
335 // Return array.
336 if ( ! $is_object ) {
337 return $response;
338 }
339
340 // create and return array of objects.
341 $temp_results = array();
342 foreach ( $response['results'] as $customer ) {
343
344 $ob = new WPSC_ACB_Messages();
345 $data = array();
346 foreach ( $customer as $key => $val ) {
347 $data[ $key ] = $val;
348 }
349 $ob->set_data( $data );
350 $temp_results[] = $ob;
351 }
352 $response['results'] = $temp_results;
353 return $response;
354 }
355
356 /**
357 * Get where for find method
358 *
359 * @param array $filter - user filter.
360 * @return array
361 */
362 private static function get_where( $filter ) {
363
364 $where = '';
365
366 // Set user defined filters.
367 $meta_query = isset( $filter['meta_query'] ) && $filter['meta_query'] ? $filter['meta_query'] : array();
368 if ( $meta_query ) {
369 $meta_query = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query );
370 $where = $meta_query . ' ';
371 }
372
373 return $where ? 'WHERE ' . $where : '';
374 }
375
376 /**
377 * Load current class to reference classes
378 *
379 * @param array $classes - Associative array of class names indexed by its slug.
380 * @return array
381 */
382 public static function load_ref_class( $classes ) {
383
384 $classes['wpsc_acb_messages'] = array(
385 'class' => __CLASS__,
386 'save-key' => 'id',
387 );
388 return $classes;
389 }
390
391 /**
392 * Count tickets based on given filters
393 *
394 * @param array $filter - array containing array items like search, where, etc.
395 * @return int
396 */
397 public static function count( $filter = array() ) {
398
399 global $wpdb;
400
401 $sql = 'SELECT COUNT(DISTINCT acbm.id) FROM ' . $wpdb->prefix . 'psmsc_acb_messages acbm ';
402 $where = self::get_where( $filter );
403 $sql = $sql . $where;
404
405 return (int) $wpdb->get_var( $sql );
406 }
407 }
408 endif;
409
410 WPSC_ACB_Messages::init();
411