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-background-email.php

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

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