PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.4
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 / models / class-wpsc-scheduled-task.php

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

394 lines 9.0 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_Scheduled_Task' ) ) :
7
8 final class WPSC_Scheduled_Task {
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 * Initialize this class
40 *
41 * @return void
42 */
43 public static function init() {
44
45 // Apply schema for this model.
46 add_action( 'init', array( __CLASS__, 'apply_schema' ), 2 );
47
48 // Get object of this class.
49 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
50 }
51
52 /**
53 * Apply schema for this model
54 *
55 * @return void
56 */
57 public static function apply_schema() {
58
59 $schema = array(
60 'id' => array(
61 'has_ref' => false,
62 'ref_class' => '',
63 'has_multiple_val' => false,
64 ),
65 'class' => array(
66 'has_ref' => false,
67 'ref_class' => '',
68 'has_multiple_val' => false,
69 ),
70 'method' => array(
71 'has_ref' => false,
72 'ref_class' => '',
73 'has_multiple_val' => false,
74 ),
75 'args' => array(
76 'has_ref' => false,
77 'ref_class' => '',
78 'has_multiple_val' => true,
79 ),
80 'is_manual' => array(
81 'has_ref' => false,
82 'ref_class' => '',
83 'has_multiple_val' => false,
84 ),
85 'warning_text' => array(
86 'has_ref' => false,
87 'ref_class' => '',
88 'has_multiple_val' => false,
89 ),
90 'warning_link_text' => array(
91 'has_ref' => false,
92 'ref_class' => '',
93 'has_multiple_val' => false,
94 ),
95 'progressbar_text' => array(
96 'has_ref' => false,
97 'ref_class' => '',
98 'has_multiple_val' => false,
99 ),
100 'pages' => array(
101 'has_ref' => false,
102 'ref_class' => '',
103 'has_multiple_val' => false,
104 ),
105 );
106 self::$schema = apply_filters( 'wpsc_scheduled_task_schema', $schema );
107
108 // Prevent modify.
109 $prevent_modify = array( 'id' );
110 self::$prevent_modify = apply_filters( 'wpsc_scheduled_task_prevent_modify', $prevent_modify );
111 }
112
113 /**
114 * Model constructor
115 *
116 * @param int $id - Optional. Data record id to retrive object for.
117 */
118 public function __construct( $id = 0 ) {
119
120 global $wpdb;
121
122 $id = intval( $id );
123
124 if ( $id > 0 ) {
125
126 $task = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_scheduled_tasks WHERE id = " . $id, ARRAY_A );
127 if ( ! is_array( $task ) ) {
128 return;
129 }
130
131 foreach ( $task as $key => $val ) {
132 $this->data[ $key ] = $val !== null ? $val : '';
133 }
134 }
135 }
136
137 /**
138 * Convert object into an array
139 *
140 * @return array
141 */
142 public function to_array() {
143
144 return $this->data;
145 }
146
147 /**
148 * Magic get function to use with object arrow function
149 *
150 * @param string $var_name - variable name.
151 * @return mixed
152 */
153 public function __get( $var_name ) {
154
155 if ( ! isset( $this->data[ $var_name ] ) ||
156 $this->data[ $var_name ] == null ||
157 $this->data[ $var_name ] == ''
158 ) {
159 return self::$schema[ $var_name ]['has_multiple_val'] ? array() : '';
160 }
161
162 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
163
164 $response = array();
165 $values = $this->data[ $var_name ] ? explode( '|', $this->data[ $var_name ] ) : array();
166 foreach ( $values as $val ) {
167 $response[] = self::$schema[ $var_name ]['has_ref'] ?
168 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $val ) :
169 $val;
170 }
171 return $response;
172
173 } else {
174
175 return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ?
176 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) :
177 $this->data[ $var_name ];
178 }
179 }
180
181 /**
182 * Magic function to use setting object field with arrow function
183 *
184 * @param string $var_name - (Required) property slug.
185 * @param mixed $value - (Required) value to set for a property.
186 * @return void
187 */
188 public function __set( $var_name, $value ) {
189
190 if (
191 ! isset( $this->data[ $var_name ] ) ||
192 in_array( $var_name, self::$prevent_modify )
193 ) {
194 return;
195 }
196
197 $data_val = '';
198 if ( self::$schema[ $var_name ]['has_multiple_val'] ) {
199
200 $data_vals = array_map(
201 fn( $val ) => is_object( $val ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $val ) : $val,
202 $value
203 );
204
205 $data_val = $data_vals ? implode( '|', $data_vals ) : '';
206
207 } else {
208
209 $data_val = is_object( $value ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) : $value;
210 }
211
212 if ( $this->data[ $var_name ] == $data_val ) {
213 return;
214 }
215
216 $this->data[ $var_name ] = $data_val;
217 $this->is_modified = true;
218 }
219
220 /**
221 * Save changes made
222 *
223 * @return boolean
224 */
225 public function save() {
226
227 global $wpdb;
228
229 if ( ! $this->is_modified ) {
230 return true;
231 }
232
233 $data = $this->data;
234 $success = true;
235
236 unset( $data['id'] );
237 $success = $wpdb->update(
238 $wpdb->prefix . 'psmsc_scheduled_tasks',
239 $data,
240 array( 'id' => $this->data['id'] )
241 );
242
243 $this->is_modified = false;
244 return $success ? true : false;
245 }
246
247 /**
248 * Insert new record
249 *
250 * @param array $data - insert data.
251 * @return WPSC_Scheduled_Task
252 */
253 public static function insert( $data ) {
254
255 global $wpdb;
256
257 $success = $wpdb->insert(
258 $wpdb->prefix . 'psmsc_scheduled_tasks',
259 $data
260 );
261
262 if ( ! $success ) {
263 return false;
264 }
265
266 $id = $wpdb->insert_id;
267 return new self( $id );
268 }
269
270 /**
271 * Delete record of given ID
272 *
273 * @param WPSC_Scheduled_Task $task - task object.
274 * @return boolean
275 */
276 public static function destroy( $task ) {
277
278 global $wpdb;
279
280 $success = $wpdb->delete(
281 $wpdb->prefix . 'psmsc_scheduled_tasks',
282 array( 'id' => $task->id )
283 );
284
285 if ( ! $success ) {
286 return false;
287 }
288
289 return true;
290 }
291
292 /**
293 * Set data to create new object using direct data. Used in find method
294 *
295 * @param array $data - data to set for object.
296 * @return void
297 */
298 private function set_data( $data ) {
299
300 foreach ( $data as $var_name => $val ) {
301 $this->data[ $var_name ] = $val !== null ? $val : '';
302 }
303 }
304
305 /**
306 * Find records based on given filters
307 *
308 * @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc.
309 * @param boolean $is_object - return data as array or object. Default object.
310 * @return mixed
311 */
312 public static function find( $filter = array(), $is_object = true ) {
313
314 global $wpdb;
315
316 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_scheduled_tasks ';
317 $where = self::get_where( $filter );
318
319 $filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 20;
320 $filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 1;
321 $filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'id';
322 $filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC';
323
324 $order = WPSC_Functions::parse_order( $filter );
325
326 $sql = $sql . $where . $order;
327 $results = $wpdb->get_results( $sql, ARRAY_A );
328
329 // total results.
330 $sql = 'SELECT count(id) FROM ' . $wpdb->prefix . 'psmsc_scheduled_tasks ';
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 $task ) {
343
344 $ob = new WPSC_Scheduled_Task();
345 $data = array();
346 foreach ( $task as $key => $val ) {
347 $data[ $key ] = $val;
348 }
349 $ob->set_data( $data );
350 $temp_results[] = $ob;
351 }
352 $response['results'] = $temp_results;
353
354 return $response;
355 }
356
357 /**
358 * Get where for find method
359 *
360 * @param array $filter - user filter.
361 * @return array
362 */
363 private static function get_where( $filter ) {
364
365 $where = array( '1=1' );
366
367 // Set user defined filters.
368 $meta_query = isset( $filter['meta_query'] ) ? $filter['meta_query'] : array();
369 if ( $meta_query ) {
370 $where[] = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query );
371 }
372
373 return 'WHERE ' . implode( ' AND ', $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_scheduled_task'] = array(
385 'class' => __CLASS__,
386 'save-key' => 'id',
387 );
388 return $classes;
389 }
390 }
391 endif;
392
393 WPSC_Scheduled_Task::init();
394