PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.9
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-email-otp.php

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

372 lines 8.4 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_Email_OTP' ) ) :
7
8 final class WPSC_Email_OTP {
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 // garbage collection.
52 add_action( 'wpsc_cron_daily', array( __CLASS__, 'garbage_collector' ) );
53 }
54
55 /**
56 * Apply schema for this model
57 *
58 * @return void
59 */
60 public static function apply_schema() {
61
62 $schema = array(
63 'id' => array(
64 'has_ref' => false,
65 'ref_class' => '',
66 'has_multiple_val' => false,
67 ),
68 'email' => array(
69 'has_ref' => false,
70 'ref_class' => '',
71 'has_multiple_val' => false,
72 ),
73 'otp' => array(
74 'has_ref' => false,
75 'ref_class' => '',
76 'has_multiple_val' => false,
77 ),
78 'date_expiry' => array(
79 'has_ref' => true,
80 'ref_class' => 'datetime',
81 'has_multiple_val' => false,
82 ),
83 'data' => array(
84 'has_ref' => false,
85 'ref_class' => '',
86 'has_multiple_val' => false,
87 ),
88 );
89 self::$schema = apply_filters( 'wpsc_email_otp_schema', $schema );
90
91 // Prevent modify.
92 $prevent_modify = array( 'id' );
93 self::$prevent_modify = apply_filters( 'wpsc_email_otp_prevent_modify', $prevent_modify );
94 }
95
96 /**
97 * Model constructor
98 *
99 * @param int $id - Optional. Data record id to retrive object for.
100 */
101 public function __construct( $id = 0 ) {
102
103 global $wpdb;
104
105 $id = intval( $id );
106
107 if ( $id > 0 ) {
108
109 $otp = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_email_otp WHERE id = " . $id, ARRAY_A );
110 if ( ! is_array( $otp ) ) {
111 return;
112 }
113
114 foreach ( $otp as $key => $val ) {
115 $this->data[ $key ] = $val !== null ? $val : '';
116 }
117 }
118 }
119
120 /**
121 * Magic get function to use with object arrow function
122 *
123 * @param string $var_name - variable name.
124 * @return mixed
125 */
126 public function __get( $var_name ) {
127
128 if ( ! isset( $this->data[ $var_name ] ) ||
129 $this->data[ $var_name ] == null ||
130 $this->data[ $var_name ] == ''
131 ) {
132 return self::$schema[ $var_name ]['has_multiple_val'] ? array() : '';
133 }
134
135 return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ?
136 WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) :
137 $this->data[ $var_name ];
138 }
139
140 /**
141 * Magic function to use setting object field with arrow function
142 *
143 * @param string $var_name - (Required) property slug.
144 * @param mixed $value - (Required) value to set for a property.
145 * @return void
146 */
147 public function __set( $var_name, $value ) {
148
149 if (
150 ! isset( $this->data[ $var_name ] ) ||
151 in_array( $var_name, self::$prevent_modify )
152 ) {
153 return;
154 }
155
156 $data_val = is_object( $value ) ?
157 WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) :
158 $value;
159
160 if ( $this->data[ $var_name ] == $data_val ) {
161 return;
162 }
163
164 $this->data[ $var_name ] = $data_val;
165 $this->is_modified = true;
166 }
167
168 /**
169 * Save changes made. If id is not present that means we have to create new otp.
170 *
171 * @return boolean
172 */
173 public function save() {
174
175 global $wpdb;
176
177 if ( ! $this->is_modified ) {
178 return true;
179 }
180
181 $data = $this->data;
182
183 unset( $data['id'] );
184 $success = $wpdb->update(
185 $wpdb->prefix . 'psmsc_email_otp',
186 $data,
187 array( 'id' => $this->data['id'] )
188 );
189
190 $this->is_modified = false;
191 return $success ? true : false;
192 }
193
194 /**
195 * Insert new OTP
196 *
197 * @param array $data - insert data.
198 * @return WPSC_Email_OTP
199 */
200 public static function insert( $data ) {
201
202 global $wpdb;
203
204 $wpdb->delete(
205 $wpdb->prefix . 'psmsc_email_otp',
206 array( 'email' => $data['email'] )
207 );
208
209 $data['otp'] = substr( str_shuffle( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ), 0, 8 );
210 $success = $wpdb->insert(
211 $wpdb->prefix . 'psmsc_email_otp',
212 $data
213 );
214
215 if ( ! $success ) {
216 return false;
217 }
218
219 return new WPSC_Email_OTP( $wpdb->insert_id );
220 }
221
222 /**
223 * Delete record of given ID
224 *
225 * @param WPSC_Email_OTP $otp - OTP object.
226 * @return boolean
227 */
228 public static function destroy( $otp ) {
229
230 global $wpdb;
231
232 $success = $wpdb->delete(
233 $wpdb->prefix . 'psmsc_email_otp',
234 array( 'id' => $otp->id )
235 );
236 if ( ! $success ) {
237 return false;
238 }
239 return true;
240 }
241
242 /**
243 * Set data to create new object using direct data. Used in find method
244 *
245 * @param array $data - data to set for object.
246 * @return void
247 */
248 private function set_data( $data ) {
249
250 foreach ( $data as $var_name => $val ) {
251 $this->data[ $var_name ] = $val !== null ? $val : '';
252 }
253 }
254
255 /**
256 * Find records based on given filters
257 *
258 * @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc.
259 * @param boolean $is_object - return data as array or object. Default object.
260 * @return mixed
261 */
262 public static function find( $filter = array(), $is_object = true ) {
263
264 global $wpdb;
265
266 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_email_otp ';
267 $where = self::get_where( $filter );
268
269 $filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 0;
270 $filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 0;
271 $filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'id';
272 $filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC';
273
274 $order = WPSC_Functions::parse_order( $filter );
275
276 $sql = $sql . $where . $order;
277 $results = $wpdb->get_results( $sql, ARRAY_A );
278
279 // total results.
280 $sql = 'SELECT count(id) FROM ' . $wpdb->prefix . 'psmsc_email_otp ';
281 $total_items = $wpdb->get_var( $sql . $where );
282
283 $response = WPSC_Functions::parse_response( $results, $total_items, $filter );
284
285 // Return array.
286 if ( ! $is_object ) {
287 return $response;
288 }
289
290 // create and return array of objects.
291 $temp_results = array();
292 foreach ( $response['results'] as $otp ) {
293
294 $ob = new WPSC_Email_OTP();
295 $data = array();
296 foreach ( $otp as $key => $val ) {
297 $data[ $key ] = $val;
298 }
299 $ob->set_data( $data );
300 $temp_results[] = $ob;
301 }
302 $response['results'] = $temp_results;
303
304 return $response;
305 }
306
307 /**
308 * Get where for find method
309 *
310 * @param array $filter - user filter.
311 * @return array
312 */
313 private static function get_where( $filter ) {
314
315 $where = '';
316
317 // Set user defined filters.
318 $meta_query = isset( $filter['meta_query'] ) && $filter['meta_query'] ? $filter['meta_query'] : array();
319 if ( $meta_query ) {
320 $meta_query = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query );
321 $where = $meta_query . ' ';
322 }
323
324 return $where ? 'WHERE ' . $where : '';
325 }
326
327 /**
328 * Load current class to reference classes
329 *
330 * @param array $classes - Associative array of class names indexed by its slug.
331 * @return array
332 */
333 public static function load_ref_class( $classes ) {
334
335 $classes['wpsc_email_otp'] = array(
336 'class' => __CLASS__,
337 'save-key' => 'id',
338 );
339 return $classes;
340 }
341
342 /**
343 * Verify OTP for the object
344 *
345 * @param int $verification_otp - OTP to verify.
346 * @return boolean
347 */
348 public function is_valid( $verification_otp ) {
349
350 $now = new DateTime();
351 return $this->otp == $verification_otp && $this->date_expiry >= $now ? true : false;
352 }
353
354 /**
355 * Garbage collector
356 *
357 * @return void
358 */
359 public static function garbage_collector() {
360
361 global $wpdb;
362
363 $now = ( new DateTime() )->format( 'Y-m-d H:i:s' );
364
365 $sql = "DELETE FROM {$wpdb->prefix}psmsc_email_otp WHERE date_expiry <= '$now'";
366 $wpdb->query( $sql );
367 }
368 }
369 endif;
370
371 WPSC_Email_OTP::init();
372