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

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

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