PluginProbe
Pushly / 2.2.0
Pushly v2.2.0
2.4.0 2.3.0 trunk 1.0 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 2.0.0 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1
pushly / src / php / Models / Notification.php

Notification.php in Pushly 2.2.0, at src/php/Models/Notification.php

116 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Pushly\Models;
4
5 use Pushly\Admin\Util;
6
7 class Notification implements \JsonSerializable {
8 public ?int $id = null;
9 public ?NotificationTemplate $template = null;
10 public ?NotificationDeliverySpec $delivery_spec = null;
11 public ?NotificationAudience $audience = null;
12 public ?array $meta = null;
13
14 #[\ReturnTypeWillChange]
15 public function jsonSerialize(): array {
16 $r = [
17 'template' => $this->template,
18 'delivery_spec' => $this->delivery_spec,
19 'audience' => $this->audience,
20 'meta' => $this->meta,
21 ];
22
23 if ( ! empty( $this->id ) ) {
24 $r['id'] = $this->id;
25 }
26
27 return $r;
28 }
29
30 /**
31 * Factory: builds a Notification from a post data array and pushly meta options.
32 *
33 * @param array $post Flat array of post fields (ID, title, body, landing_url, etc.)
34 * @param array $pushly_meta Pushly-specific options (segment_ids, existing_notification_id)
35 */
36 public static function from_post( array $post, array $pushly_meta ): ?self {
37 try {
38 global $wp_version;
39 $user = wp_get_current_user();
40
41 $notification = new self();
42 $notification->audience = new NotificationAudience();
43 $notification->template = new NotificationTemplate();
44 $notification->delivery_spec = new NotificationDeliverySpec();
45 $notification->meta = [
46 'wordpress_version' => $wp_version,
47 'wordpress_post_id' => $post['ID'],
48 'wordpress_user_id' => $user->ID,
49 'wordpress_user_email' => $user->user_email,
50 'plugin_version' => PUSHLY__PLUGIN_VERSION,
51 'php_version' => PHP_VERSION,
52 'post_type' => $post['post_type'] ?? null,
53 'post_status' => $post['post_status'] ?? null,
54 'editor_type' => $post['editor_type'] ?? null,
55 ];
56
57 if ( ! empty( $pushly_meta['existing_notification_id'] ) ) {
58 $notification->id = (int) $pushly_meta['existing_notification_id'];
59 }
60
61 // Audience
62 if ( empty( $pushly_meta['segment_ids'] ) ) {
63 $notification->audience->all_subscribers = true;
64 } else {
65 $notification->audience->segment_ids = array_map( 'intval', $pushly_meta['segment_ids'] );
66 }
67
68 // Template
69 $channels = new NotificationTemplateChannels();
70 $channels->web = new NotificationTemplateChannelsWeb();
71 $channels->web->title = sanitize_text_field( $post['title'] );
72 $channels->web->landing_url = $post['landing_url'];
73
74 if ( ! empty( $post['body'] ) ) {
75 $channels->web->body = sanitize_text_field( $post['body'] );
76 }
77
78 if ( ! empty( $post['image_id'] ) ) {
79 $image = wp_get_attachment_image_src( $post['image_id'], 'large' );
80 if ( ! empty( $image ) ) {
81 $channels->web->image_url = $image[0];
82 }
83 }
84
85 $notification->template->channels = $channels;
86
87 if ( ! empty( $post['tag_names'] ) ) {
88 $notification->template->keywords = array_unique(
89 array_merge( $notification->template->keywords, $post['tag_names'] )
90 );
91 }
92
93 if ( ! empty( $post['category_names'] ) ) {
94 $notification->template->keywords = array_unique(
95 array_merge( $notification->template->keywords, $post['category_names'] )
96 );
97 }
98
99 // Delivery spec
100 $notification->delivery_spec->window = 'STANDARD';
101 $send_date = new \DateTime( $post['schedule_date'], new \DateTimeZone( 'utc' ) );
102 $current_date = new \DateTimeImmutable( 'now', new \DateTimeZone( 'utc' ) );
103
104 $notification->delivery_spec->type = $send_date <= $current_date ? 'IMMEDIATE' : 'SCHEDULED';
105 if ( $notification->delivery_spec->type === 'SCHEDULED' ) {
106 $notification->delivery_spec->send_date_utc = $send_date->format( 'c' );
107 }
108
109 return $notification;
110 } catch ( \Exception $e ) {
111 Util::log_to_event_stream( 'notification_build_error', $e->getMessage() );
112 return null;
113 }
114 }
115 }
116