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