PluginProbe
Pushly / trunk
Pushly vtrunk
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 trunk, at src/php/Models/Notification.php

120 lines 3.9 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 if ( ! empty( $pushly_meta['excluded_segment_ids'] ) ) {
69 $notification->audience->excluded_segment_ids = array_map( 'intval', $pushly_meta['excluded_segment_ids'] );
70 }
71
72 // Template
73 $channels = new NotificationTemplateChannels();
74 $channels->web = new NotificationTemplateChannelsWeb();
75 $channels->web->title = sanitize_text_field( $post['title'] );
76 $channels->web->landing_url = $post['landing_url'];
77
78 if ( ! empty( $post['body'] ) ) {
79 $channels->web->body = sanitize_text_field( $post['body'] );
80 }
81
82 if ( ! empty( $post['image_id'] ) ) {
83 $image = wp_get_attachment_image_src( $post['image_id'], 'large' );
84 if ( ! empty( $image ) ) {
85 $channels->web->image_url = $image[0];
86 }
87 }
88
89 $notification->template->channels = $channels;
90
91 if ( ! empty( $post['tag_names'] ) ) {
92 $notification->template->keywords = array_unique(
93 array_merge( $notification->template->keywords, $post['tag_names'] )
94 );
95 }
96
97 if ( ! empty( $post['category_names'] ) ) {
98 $notification->template->keywords = array_unique(
99 array_merge( $notification->template->keywords, $post['category_names'] )
100 );
101 }
102
103 // Delivery spec
104 $notification->delivery_spec->window = 'STANDARD';
105 $send_date = new \DateTime( $post['schedule_date'], new \DateTimeZone( 'utc' ) );
106 $current_date = new \DateTimeImmutable( 'now', new \DateTimeZone( 'utc' ) );
107
108 $notification->delivery_spec->type = $send_date <= $current_date ? 'IMMEDIATE' : 'SCHEDULED';
109 if ( $notification->delivery_spec->type === 'SCHEDULED' ) {
110 $notification->delivery_spec->send_date_utc = $send_date->format( 'c' );
111 }
112
113 return $notification;
114 } catch ( \Exception $e ) {
115 Util::log_to_event_stream( 'notification_build_error', $e->getMessage() );
116 return null;
117 }
118 }
119 }
120