PluginProbe
Stream – Activity Log & Audit Trail / 3.8.0
Stream – Activity Log & Audit Trail v3.8.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / alerts / class-alert-type-email.php

class-alert-type-email.php in Stream – Activity Log & Audit Trail 3.8.0, at alerts/class-alert-type-email.php

213 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email Alerts.
4 *
5 * Idea for future expansion: allow customization of email.
6 *
7 * @package WP_Stream
8 */
9
10 namespace WP_Stream;
11
12 /**
13 * Class Alert_Type_Email
14 *
15 * @package WP_Stream
16 */
17 class Alert_Type_Email extends Alert_Type {
18
19 /**
20 * Alert type name
21 *
22 * @var string
23 */
24 public $name = 'Email';
25
26 /**
27 * Alert type slug
28 *
29 * @var string
30 */
31 public $slug = 'email';
32
33 /**
34 * Class Constructor
35 *
36 * @param Plugin $plugin Plugin object.
37 * @return void
38 */
39 public function __construct( $plugin ) {
40 parent::__construct( $plugin );
41 $this->plugin = $plugin;
42 if ( ! is_admin() ) {
43 return;
44 }
45 add_filter(
46 'wp_stream_alerts_save_meta',
47 array(
48 $this,
49 'add_alert_meta',
50 ),
51 10,
52 2
53 );
54 }
55
56 /**
57 * Sends an email to the given recipient.
58 *
59 * @param int $record_id Record that triggered notification.
60 * @param array $recordarr Record details.
61 * @param Alert $alert Alert options.
62 * @return void
63 */
64 public function alert( $record_id, $recordarr, $alert ) {
65 $options = wp_parse_args(
66 $alert->alert_meta,
67 array(
68 'email_recipient' => '',
69 'email_subject' => '',
70 'trigger_action' => '',
71 'trigger_connector' => '',
72 'trigger_context' => '',
73 )
74 );
75
76 if ( empty( $options['email_recipient'] ) && empty( $options['email_subject'] ) ) {
77 return;
78 }
79
80 // translators: Placeholder refers to the title of a site (e.g. "FooBar Website").
81 $message = sprintf( __( 'A Stream Alert was triggered on %s.', 'stream' ), get_bloginfo( 'name' ) ) . "\n\n";
82
83 $user_id = $recordarr['user_id'];
84 $user = get_user_by( 'id', $user_id );
85
86 // translators: Placeholder refers to a username (e.g. "administrator").
87 $message .= sprintf( __( "User:\t%s", 'stream' ), $user->user_login ) . "\n";
88
89 if ( ! empty( $alert->alert_meta['trigger_context'] ) ) {
90 $context = $this->plugin->alerts->alert_triggers['context']->get_display_value( 'list_table', $alert );
91
92 // translators: Placeholder refers to the context of the record (e.g. "Plugins").
93 $message .= sprintf( __( "Context:\t%s", 'stream' ), $context ) . "\n";
94 }
95 if ( ! empty( $alert->alert_meta['trigger_action'] ) ) {
96 $action = $this->plugin->alerts->alert_triggers['action']->get_display_value( 'list_table', $alert );
97
98 // translators: Placeholder refers to the action of the record (e.g. "Installed").
99 $message .= sprintf( __( "Action:\t%s", 'stream' ), $action ) . "\n";
100 }
101
102 $post = null;
103 if ( isset( $recordarr['object_id'] ) ) {
104 $post_id = $recordarr['object_id'];
105 $post = get_post( $post_id );
106 }
107 if ( is_object( $post ) && ! empty( $post ) ) {
108 $post_type = get_post_type_object( $post->post_type );
109
110 $message .= $post_type->labels->singular_name . ":\t" . $post->post_title . "\n\n";
111
112 $edit_post_link = get_edit_post_link( $post->ID, 'raw' );
113
114 // translators: Placeholder refers to the post type singular name (e.g. "Post").
115 $message .= sprintf( __( 'Edit %s', 'stream' ), $post_type->labels->singular_name ) . "\n<$edit_post_link>\n";
116 }
117
118 $message .= "\n";
119
120 $edit_alert_link = admin_url( 'edit.php?post_type=wp_stream_alerts#post-' . $alert->ID );
121 $message .= __( 'Edit Alert', 'stream' ) . "\n<$edit_alert_link>";
122
123 wp_mail( $options['email_recipient'], $options['email_subject'], $message );
124 }
125
126 /**
127 * Displays a settings form for the alert type
128 *
129 * @param Alert $alert Alert object for the currently displayed alert.
130 * @return void
131 */
132 public function display_fields( $alert ) {
133 $alert_meta = array();
134 if ( is_object( $alert ) ) {
135 $alert_meta = $alert->alert_meta;
136 }
137 $options = wp_parse_args(
138 $alert_meta,
139 array(
140 'email_recipient' => '',
141 'email_subject' => '',
142 )
143 );
144
145 $form = new Form_Generator();
146 echo '<span class="wp_stream_alert_type_description">' . esc_html__( 'Send a notification email to the recipient.', 'stream' ) . '</span>';
147 echo '<label for="wp_stream_email_recipient"><span class="title">' . esc_html__( 'Recipient', 'stream' ) . '</span>';
148 echo '<span class="input-text-wrap">';
149 echo $form->render_field(
150 'text',
151 array(
152 'name' => 'wp_stream_email_recipient',
153 'title' => esc_attr( __( 'Email Recipient', 'stream' ) ),
154 'value' => $options['email_recipient'],
155 )
156 ); // Xss ok.
157 echo '</span></label>';
158
159 echo '<label for="wp_stream_email_subject"><span class="title">' . esc_html__( 'Subject', 'stream' ) . '</span>';
160 echo '<span class="input-text-wrap">';
161 echo $form->render_field(
162 'text',
163 array(
164 'name' => 'wp_stream_email_subject',
165 'title' => esc_attr( __( 'Email Subject', 'stream' ) ),
166 'value' => $options['email_subject'],
167 )
168 ); // Xss ok.
169 echo '</span></label>';
170 }
171
172 /**
173 * Validates and saves form settings for later use.
174 *
175 * @param Alert $alert Alert object for the currently displayed alert.
176 * @return void
177 */
178 public function save_fields( $alert ) {
179 check_admin_referer( 'save_alert', 'wp_stream_alerts_nonce' );
180
181 if ( isset( $_POST['wp_stream_email_recipient'] ) ) {
182 $alert->alert_meta['email_recipient'] = sanitize_text_field( wp_unslash( $_POST['wp_stream_email_recipient'] ) );
183 }
184
185 if ( isset( $_POST['wp_stream_email_subject'] ) ) {
186 $alert->alert_meta['email_subject'] = sanitize_text_field( wp_unslash( $_POST['wp_stream_email_subject'] ) );
187 }
188 }
189
190 /**
191 * Add alert meta if this is a highlight alert
192 *
193 * @param array $alert_meta The metadata to be inserted for this alert.
194 * @param string $alert_type The type of alert being added or updated.
195 *
196 * @return mixed
197 */
198 public function add_alert_meta( $alert_meta, $alert_type ) {
199 if ( $this->slug === $alert_type ) {
200 $email_recipient = wp_stream_filter_input( INPUT_POST, 'wp_stream_email_recipient' );
201 if ( ! empty( $email_recipient ) ) {
202 $alert_meta['email_recipient'] = $email_recipient;
203 }
204 $email_subject = wp_stream_filter_input( INPUT_POST, 'wp_stream_email_subject' );
205 if ( ! empty( $email_subject ) ) {
206 $alert_meta['email_subject'] = $email_subject;
207 }
208 }
209
210 return $alert_meta;
211 }
212 }
213