PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
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 4.0.2, at classes/class-alert.php

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