PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / trunk
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more vtrunk
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
texty / includes / Models / SmsStat.php

SmsStat.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more trunk, at includes/Models/SmsStat.php

442 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty\Models;
4
5 use Texty\Dependencies\WeDevs\WPKit\DataLayer\Model\BaseModel;
6 use Texty\Dependencies\WeDevs\WPKit\DataLayer\DataLayerFactory;
7
8 /**
9 * SMS Statistics Model
10 *
11 * Represents a single SMS message transaction record with metadata.
12 *
13 * @since 2.0.0
14 */
15 class SmsStat extends BaseModel {
16
17 /**
18 * Object type identifier for hooks
19 *
20 * @var string
21 */
22 protected string $object_type = 'sms_stat';
23
24 /**
25 * Plugin hook prefix
26 *
27 * @var string
28 */
29 protected string $hook_prefix = 'texty_';
30
31 /**
32 * Cache group name
33 *
34 * @var string
35 */
36 protected string $cache_group = 'texty_sms_stats';
37
38 /**
39 * Default properties/schema
40 *
41 * @var array
42 */
43 protected array $data = [
44 'receiver' => '',
45 'gateway' => '',
46 'status' => 'pending',
47 'notification_id' => '',
48 'notification_group' => '',
49 'message' => '',
50 'response' => '',
51 'created_at' => null,
52 'updated_at' => null,
53 'reference_id' => '',
54 ];
55
56 /**
57 * Type casting configuration
58 *
59 * @var array
60 */
61 protected array $casts = [
62 'receiver' => 'string',
63 'gateway' => 'string',
64 'status' => 'string',
65 'notification_id' => 'string',
66 'notification_group' => 'string',
67 'message' => 'string',
68 'response' => 'string',
69 'created_at' => 'date',
70 'updated_at' => 'date',
71 'reference_id' => 'string',
72 ];
73
74 // --- Getters ---
75
76 /**
77 * Get receiver phone number
78 *
79 * @param string $context 'view' or 'edit'
80 *
81 * @return string
82 * @since 2.0.0
83 */
84 public function get_receiver( string $context = 'view' ): string {
85 return $this->get_prop( 'receiver', $context );
86 }
87
88 /**
89 * Get SMS gateway name
90 *
91 * @param string $context 'view' or 'edit'
92 *
93 * @return string
94 * @since 2.0.0
95 */
96 public function get_gateway( string $context = 'view' ): string {
97 return $this->get_prop( 'gateway', $context );
98 }
99
100 /**
101 * Get message status
102 *
103 * @param string $context 'view' or 'edit'
104 *
105 * @return string
106 * @since 2.0.0
107 */
108 public function get_status( string $context = 'view' ): string {
109 return $this->get_prop( 'status', $context );
110 }
111
112 /**
113 * Get notification ID (e.g. 'registration', 'order_admin_processing')
114 *
115 * @param string $context 'view' or 'edit'
116 *
117 * @return string
118 * @since 2.0.0
119 */
120 public function get_notification_id( string $context = 'view' ): string {
121 return $this->get_prop( 'notification_id', $context );
122 }
123
124 /**
125 * Get notification group ('wp', 'wc', 'dokan', or custom)
126 *
127 * @param string $context 'view' or 'edit'
128 *
129 * @return string
130 * @since 2.0.0
131 */
132 public function get_notification_group( string $context = 'view' ): string {
133 return $this->get_prop( 'notification_group', $context );
134 }
135
136 /**
137 * Get the final, token-replaced message body that was sent
138 *
139 * @param string $context 'view' or 'edit'
140 *
141 * @return string
142 * @since 2.0.0
143 */
144 public function get_message( string $context = 'view' ): string {
145 return $this->get_prop( 'message', $context );
146 }
147
148 /**
149 * Get the gateway response payload (JSON-encoded). For failed sends this
150 * holds the WP_Error message + data; for successful sends it holds the
151 * raw gateway response array.
152 *
153 * @param string $context 'view' or 'edit'
154 *
155 * @return string
156 * @since 2.0.0
157 */
158 public function get_response( string $context = 'view' ): string {
159 return $this->get_prop( 'response', $context );
160 }
161
162 /**
163 * Get creation timestamp
164 *
165 * @param string $context 'view' or 'edit'
166 *
167 * @return \DateTimeInterface|null
168 * @since 2.0.0
169 */
170 public function get_created_at( string $context = 'view' ) {
171 return $this->get_prop( 'created_at', $context );
172 }
173
174 /**
175 * Get last updated timestamp
176 *
177 * @param string $context 'view' or 'edit'
178 *
179 * @return \DateTimeInterface|null
180 * @since 2.0.0
181 */
182 public function get_updated_at( string $context = 'view' ) {
183 return $this->get_prop( 'updated_at', $context );
184 }
185
186 /**
187 * Get external gateway reference ID
188 *
189 * @param string $context 'view' or 'edit'
190 *
191 * @return string|null
192 * @since 2.0.0
193 */
194 public function get_reference_id( string $context = 'view' ): ?string {
195 return $this->get_prop( 'reference_id', $context );
196 }
197
198 // --- Setters ---
199
200 /**
201 * Set receiver phone number
202 *
203 * @param string $receiver
204 *
205 * @return void
206 * @since 2.0.0
207 */
208 public function set_receiver( string $receiver ): void {
209 $this->set_prop( 'receiver', $receiver );
210 }
211
212 /**
213 * Set SMS gateway name
214 *
215 * @param string $gateway
216 *
217 * @return void
218 * @since 2.0.0
219 */
220 public function set_gateway( string $gateway ): void {
221 $this->set_prop( 'gateway', $gateway );
222 }
223
224 /**
225 * Set message status
226 *
227 * @param string $status
228 *
229 * @return void
230 * @since 2.0.0
231 */
232 public function set_status( string $status ): void {
233 $this->set_prop( 'status', $status );
234 }
235
236 /**
237 * Set notification ID
238 *
239 * @param string|null $notification_id
240 *
241 * @return void
242 * @since 2.0.0
243 */
244 public function set_notification_id( ?string $notification_id ): void {
245 $this->set_prop( 'notification_id', null === $notification_id ? '' : $notification_id );
246 }
247
248 /**
249 * Set notification group
250 *
251 * @param string|null $notification_group
252 *
253 * @return void
254 * @since 2.0.0
255 */
256 public function set_notification_group( ?string $notification_group ): void {
257 $this->set_prop( 'notification_group', null === $notification_group ? '' : $notification_group );
258 }
259
260 /**
261 * Set the final, token-replaced message body
262 *
263 * @param string|null $message
264 *
265 * @return void
266 * @since 2.0.0
267 */
268 public function set_message( ?string $message ): void {
269 $this->set_prop( 'message', null === $message ? '' : $message );
270 }
271
272 /**
273 * Set the gateway response payload. Arrays/objects are JSON-encoded;
274 * strings are stored as-is.
275 *
276 * @param mixed $response
277 *
278 * @return void
279 * @since 2.0.0
280 */
281 public function set_response( $response ): void {
282 if ( null === $response ) {
283 $this->set_prop( 'response', '' );
284 return;
285 }
286 if ( is_string( $response ) ) {
287 $this->set_prop( 'response', $response );
288 return;
289 }
290 $this->set_prop( 'response', wp_json_encode( $response ) ?? '' );
291 }
292
293 /**
294 * Set creation timestamp
295 *
296 * @param string|int|\DateTimeInterface|null $date
297 *
298 * @return void
299 * @since 2.0.0
300 */
301 public function set_created_at( $date ): void {
302 $this->set_date_prop( 'created_at', $date );
303 }
304
305 /**
306 * Set last updated timestamp
307 *
308 * @param string|int|\DateTimeInterface|null $date
309 *
310 * @return void
311 * @since 2.0.0
312 */
313 public function set_updated_at( $date ): void {
314 $this->set_date_prop( 'updated_at', $date );
315 }
316
317 /**
318 * Set external gateway reference ID
319 *
320 * Safety net: converts null to empty string to prevent type errors
321 *
322 * @param string|null $reference_id
323 *
324 * @return void
325 * @since 2.0.0
326 */
327 public function set_reference_id( ?string $reference_id ): void {
328 // Safety net: convert null to empty string
329 if ( null === $reference_id ) {
330 $reference_id = '';
331 }
332 $this->set_prop( 'reference_id', $reference_id );
333 }
334
335 /**
336 * Bulk set properties
337 *
338 * @param array $props
339 *
340 * @return void
341 * @since 2.0.0
342 */
343 public function set_props( array $props ): void {
344 foreach ( $props as $key => $value ) {
345 $method = 'set_' . $key;
346 if ( method_exists( $this, $method ) ) {
347 $this->$method( $value );
348 }
349 }
350 }
351
352 /**
353 * Get sent SMS records between two dates (for volume chart)
354 *
355 * @param string $start_date Date in 'Y-m-d' format
356 * @param string $end_date Date in 'Y-m-d' format
357 *
358 * @return array Array with 'total' and 'items' keys
359 * @since 2.0.0
360 */
361 public static function get_sent_sms_between_dates( string $start_date, string $end_date ): array {
362 $store = DataLayerFactory::make_store( self::class );
363 if ( ! $store ) {
364 error_log( sprintf( 'Texty SmsStat error: Failed to create data store for %s.', self::class ) );
365
366 return [
367 'total' => 0,
368 'items' => [],
369 ];
370 }
371
372 $start_datetime = $start_date . ' 00:00:00';
373 $end_datetime = $end_date . ' 23:59:59';
374
375 $result = $store->query(
376 [
377 'per_page' => -1,
378 'date_query' => [
379 'column' => 'created_at',
380 'after' => $start_datetime,
381 'before' => $end_datetime,
382 ],
383 ]
384 );
385
386 // Ensure result is an array with proper structure
387 if ( ! is_array( $result ) ) {
388 return [
389 'total' => 0,
390 'items' => [],
391 ];
392 }
393
394 return $result;
395 }
396 /**
397 * Get sent SMS records between two dates (for volume chart)
398 *
399 * @param string $start_date Date in 'Y-m-d' format
400 * @param string $end_date Date in 'Y-m-d' format
401 *
402 * @return array Array with 'total' and 'items' keys
403 * @since 2.0.0
404 */
405 public static function get_successful_sent_sms_between_dates( string $start_date, string $end_date ): array {
406 $store = DataLayerFactory::make_store( self::class );
407 if ( ! $store ) {
408 error_log( sprintf( 'Texty SmsStat error: Failed to create data store for %s.', self::class ) );
409
410 return [
411 'total' => 0,
412 'items' => [],
413 ];
414 }
415
416 $start_datetime = $start_date . ' 00:00:00';
417 $end_datetime = $end_date . ' 23:59:59';
418
419 $result = $store->query(
420 [
421 'per_page' => -1,
422 'date_query' => [
423 'column' => 'created_at',
424 'status' => 'sent',
425 'after' => $start_datetime,
426 'before' => $end_datetime,
427 ],
428 ]
429 );
430
431 // Ensure result is an array with proper structure
432 if ( ! is_array( $result ) ) {
433 return [
434 'total' => 0,
435 'items' => [],
436 ];
437 }
438
439 return $result;
440 }
441 }
442