PluginProbe
Stream – Activity Log & Audit Trail / 3.1
Stream – Activity Log & Audit Trail v3.1
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 / classes / class-alert.php

class-alert.php in Stream – Activity Log & Audit Trail 3.1, at classes/class-alert.php

338 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Single Alert handler.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class Alert
12 *
13 * @package WP_Stream
14 */
15 class Alert {
16
17 /**
18 * Alert post ID
19 *
20 * @var int
21 */
22 public $ID;
23
24 /**
25 * Creation date
26 *
27 * @var string
28 */
29 public $date;
30
31 /**
32 * Status
33 *
34 * @var string
35 */
36 public $status;
37
38 /**
39 * Alert author ID
40 *
41 * @var int
42 */
43 public $author;
44
45 /**
46 * Alert type
47 *
48 * @var string
49 */
50 public $alert_type;
51
52 /**
53 * Alert meta data
54 *
55 * @var int
56 */
57 public $alert_meta;
58
59 /**
60 * Hold Plugin class
61 *
62 * @var Plugin
63 */
64 public $plugin;
65
66 /**
67 * Class constructor
68 *
69 * @param object $item Alert data.
70 * @param Plugin $plugin Plugin class.
71 * @return void
72 */
73 public function __construct( $item, $plugin ) {
74 $this->plugin = $plugin;
75
76 $this->ID = isset( $item->ID ) ? $item->ID : null;
77 $this->status = isset( $item->status ) ? $item->status : 'wp_stream_disabled';
78 $this->date = isset( $item->date ) ? $item->date : null;
79 $this->author = isset( $item->author ) ? $item->author : null;
80
81 $this->alert_type = isset( $item->alert_type ) ? $item->alert_type : null;
82 $this->alert_meta = isset( $item->alert_meta ) ? $item->alert_meta : array();
83 }
84
85 /**
86 * Save state of the Alert to database
87 *
88 * @return int The Post ID of the alert.
89 */
90 public function save() {
91
92 $args = array(
93 'ID' => $this->ID,
94 'post_date' => $this->date,
95 'post_status' => $this->status,
96 'post_content' => '',
97 'post_title' => $this->get_title(),
98 'post_author' => $this->author,
99 'post_type' => Alerts::POST_TYPE,
100 );
101
102 if ( empty( $args['ID'] ) ) {
103 unset( $args['ID'] );
104 }
105
106 $post_id = wp_insert_post( $args );
107 if ( empty( $args['ID'] ) ) {
108 $this->ID = $post_id;
109 }
110
111 $meta = array(
112 'alert_type' => $this->alert_type,
113 'alert_meta' => $this->alert_meta,
114 );
115
116 foreach ( $meta as $key => $value ) {
117 $this->update_meta( $key, $value );
118 }
119
120 return $post_id;
121 }
122
123 /**
124 * Process settings form data
125 *
126 * @param array $data Processed post object data.
127 * @return array New post object data.
128 */
129 public function process_settings_form( $data ) {
130
131 $args = array(
132 'post_date' => $this->date,
133 'post_status' => $this->status,
134 'post_title' => $this->get_title(),
135 'post_author' => $this->author,
136 'post_type' => Alerts::POST_TYPE,
137 );
138
139 foreach ( $args as $key => $value ) {
140 $data[ $key ] = $value;
141 }
142
143 $meta_input = array(
144 'alert_type' => $this->alert_type,
145 'alert_meta' => $this->alert_meta,
146 );
147
148 foreach ( $meta_input as $key => $value ) {
149 $this->update_meta( $key, $value );
150 }
151
152 return $data;
153 }
154
155 /**
156 * Query record meta
157 *
158 * @param string $meta_key Meta key to retrieve (optional). Otherwise will
159 * grab all meta data for the ID.
160 * @param bool $single Whether to only retrieve the first value (optional).
161 *
162 * @return mixed Single value if $single is true, array if false.
163 */
164 public function get_meta( $meta_key = '', $single = false ) {
165 return maybe_unserialize( get_post_meta( $this->ID, $meta_key, $single ) );
166 }
167
168 /**
169 * Update record meta
170 *
171 * @param string $meta_key Meta key to update.
172 * @param string $meta_value Value to update with.
173 * @param string $prev_value Previous value to change (optional).
174 * @return array
175 */
176 public function update_meta( $meta_key, $meta_value, $prev_value = '' ) {
177 return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value );
178 }
179
180 /**
181 * Determine the title of the alert.
182 *
183 * @todo enhance human readibility
184 * @return string The title of the alert
185 */
186 function get_title() {
187
188 $alert_type = $this->get_alert_type_obj()->name;
189
190 $output = array();
191 foreach ( array( 'action', 'author', 'context' ) as $trigger_type ) {
192 $output[ $trigger_type ] = $this->plugin->alerts->alert_triggers[ $trigger_type ]->get_display_value( 'list_table', $this );
193 }
194 $title = '';
195 foreach ( $this->plugin->alerts->alert_triggers as $trigger_type => $trigger_obj ) {
196 $value = $trigger_obj->get_display_value( 'list_table', $this );
197 $title .= $value . ' > ';
198 }
199 $title = rtrim( $title, ' > ' );
200 return $title;
201 }
202
203 /**
204 * Retrive current alert type object
205 *
206 * @return Alert_Type
207 */
208 public function get_alert_type_obj() {
209 if ( array_key_exists( $this->alert_type, $this->plugin->alerts->alert_types ) ) {
210 $obj = $this->plugin->alerts->alert_types[ $this->alert_type ];
211 } else {
212 $obj = new Alert_Type_None( $this->plugin );
213 }
214 return $obj;
215 }
216
217 /**
218 * Check if record matches trigger criteria.
219 *
220 * @param int $record_id Record ID.
221 * @param array $recordarr Record data.
222 * @return bool True if a positive match. False otherwise.
223 */
224 public function check_record( $record_id, $recordarr ) {
225 return apply_filters( 'wp_stream_alert_trigger_check', true, $record_id, $recordarr, $this );
226 }
227
228 /**
229 * Trigger alert for a specific record.
230 *
231 * @param int $record_id Record ID.
232 * @param int $recordarr Record Data.
233 * @return void
234 */
235 public function send_alert( $record_id, $recordarr ) {
236 $this->get_alert_type_obj()->alert( $record_id, $recordarr, $this );
237 }
238
239 /**
240 * Record Alerts triggered by a Record.
241 *
242 * This should be used any time an alert is triggered.
243 *
244 * Stores the post ID of an Alert triggered by a record.
245 *
246 * As an example, this exists so that its value(s) can then be used
247 * to fetch meta from that Alert in later operations.
248 *
249 * This also creates the Alert triggered meta if not found.
250 *
251 * @see Alert_Type_Highlight::alert() for an example.
252 *
253 * @param object $record The Record.
254 * @param string $alert_slug The Alert Type slug.
255 * @param array $alert_meta Alert meta.
256 *
257 * @return bool If the meta was updated successfully.
258 */
259 public static function update_record_triggered_alerts( $record, $alert_slug, $alert_meta ) {
260 if ( ! is_string( $alert_slug ) ) {
261 return false;
262 }
263
264 if ( is_array( $record ) ) {
265 $record = (object) $record;
266 }
267 if ( empty( $record->ID ) ) {
268 return false;
269 }
270 $record = new Record( $record );
271 $alerts_triggered = $record->get_meta( Alerts::ALERTS_TRIGGERED_META_KEY, true );
272
273 if ( empty( $alerts_triggered ) || ! is_array( $alerts_triggered ) ) {
274 $alerts_triggered = array( $alert_slug => $alert_meta );
275 } elseif ( ! array_key_exists( $alert_slug, $alerts_triggered ) || ! is_array( $alerts_triggered[ $alert_slug ] ) ) {
276 $alerts_triggered[ $alert_slug ] = $alert_meta;
277 }
278 return $record->update_meta( Alerts::ALERTS_TRIGGERED_META_KEY, $alerts_triggered );
279 }
280
281 /**
282 * Get a meta value from the Alert that a Record has triggered.
283 *
284 * If a Record has triggered an Alert (post), this fetches a specific
285 * Alert meta (i.e., "post meta") value from that Alert.
286 *
287 * First, it gets the array of Alerts that the Record has triggered.
288 * Then, using the requested Alert Type, it grabs the first item (post ID) in
289 * that Type.
290 *
291 * Using that ID, it fetches that Alert post's meta, then
292 * returns the value of the requested setting (ie., "post meta" field).
293 *
294 * @see Alert_Type_Highlight::post_class() for an example.
295 *
296 * @param object $record The Record object.
297 * @param string $alert_slug The slug of the Alert Type.
298 * @param string $setting The requested meta value of the Alert.
299 * @param mixed $default The default value if no value is found.
300 *
301 * @return mixed
302 */
303 public function get_single_alert_setting_from_record( $record, $alert_slug, $setting, $default = false ) {
304 if ( ! is_object( $record ) || ! is_string( $alert_slug ) || ! is_string( $setting ) ) {
305 return false;
306 }
307 $record = new Record( $record );
308 $alerts_triggered = $record->get_meta( Alerts::ALERTS_TRIGGERED_META_KEY, true );
309
310 // Ensure we have a meta array and that this record has triggered a highlight alert.
311 if ( empty( $alerts_triggered ) || ! is_array( $alerts_triggered ) || ! array_key_exists( $alert_slug, $alerts_triggered ) ) {
312 return false;
313 }
314
315 $values = $alerts_triggered[ $alert_slug ];
316 if ( empty( $values ) ) {
317 return false;
318 }
319
320 // Grab an Alert post ID.
321 // @todo determine which Alert post takes priority.
322 if ( is_array( $values ) ) {
323 $post_id = $values[0];
324 } else {
325 $post_id = $values;
326 }
327
328 if ( ! is_numeric( $post_id ) ) {
329 return false;
330 }
331
332 $alert = $this->plugin->alerts->get_alert( $post_id );
333
334 $value = ! empty( $alert->alert_meta[ $setting ] ) ? $alert->alert_meta[ $setting ] : $default;
335 return $value;
336 }
337 }
338