| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Single Alert handler. | |
| 3 | + * Manages a single alert, acting as a model. | |
| 4 | 4 | * |
| 5 | 5 | * @package WP_Stream |
| 6 | 6 | */ |
| 7 | 7 | |
| @@ -56,9 +56,9 @@ | ||
| 56 | 56 | */ |
| 57 | 57 | public $alert_meta; |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | - * Hold Plugin class | |
| 60 | + * Holds instance of plugin object | |
| 61 | 61 | * |
| 62 | 62 | * @var Plugin |
| 63 | 63 | */ |
| 64 | 64 | public $plugin; |
| @@ -65,19 +65,20 @@ | ||
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * Class constructor |
| 68 | 68 | * |
| 69 | - * @param object $item Alert data. | |
| 70 | - * @param Plugin $plugin Plugin class. | |
| 69 | + * @param ?object $item Alert data. | |
| 70 | + * @param Plugin $plugin Instance of plugin object. | |
| 71 | + * | |
| 71 | 72 | * @return void |
| 72 | 73 | */ |
| 73 | 74 | public function __construct( $item, $plugin ) { |
| 74 | - $this->plugin = $plugin; | |
| 75 | + $this->plugin = $plugin; | |
| 75 | 76 | |
| 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; | |
| 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; | |
| 80 | 81 | |
| 81 | 82 | $this->alert_type = isset( $item->alert_type ) ? $item->alert_type : null; |
| 82 | 83 | $this->alert_meta = isset( $item->alert_meta ) ? $item->alert_meta : array(); |
| 83 | 84 | } |
| @@ -82,10 +83,11 @@ | ||
| 82 | 83 | $this->alert_meta = isset( $item->alert_meta ) ? $item->alert_meta : array(); |
| 83 | 84 | } |
| 84 | 85 | |
| 85 | 86 | /** |
| 86 | - * Save state of the Alert to database | |
| 87 | + * Saves alert state. | |
| 87 | 88 | * |
| 89 | + * @todo Clean up/Remove unnecessary conditional statements. | |
| 88 | 90 | * @return int The Post ID of the alert. |
| 89 | 91 | */ |
| 90 | 92 | public function save() { |
| 91 | 93 | |
| @@ -98,20 +100,23 @@ | ||
| 98 | 100 | 'post_author' => $this->author, |
| 99 | 101 | 'post_type' => Alerts::POST_TYPE, |
| 100 | 102 | ); |
| 101 | 103 | |
| 104 | + // Remove empty "ID" field, if new post. | |
| 102 | 105 | if ( empty( $args['ID'] ) ) { |
| 103 | 106 | unset( $args['ID'] ); |
| 104 | 107 | } |
| 105 | 108 | |
| 109 | + // Create or update alert and assign the ID. | |
| 106 | 110 | $post_id = wp_insert_post( $args ); |
| 107 | 111 | if ( empty( $args['ID'] ) ) { |
| 108 | 112 | $this->ID = $post_id; |
| 109 | 113 | } |
| 110 | 114 | |
| 115 | + // Save alert type and meta. | |
| 111 | 116 | $meta = array( |
| 112 | - 'alert_type' => $this->alert_type, | |
| 113 | - 'alert_meta' => $this->alert_meta, | |
| 117 | + 'alert_type' => $this->alert_type, | |
| 118 | + 'alert_meta' => $this->alert_meta, | |
| 114 | 119 | ); |
| 115 | 120 | |
| 116 | 121 | foreach ( $meta as $key => $value ) { |
| 117 | 122 | $this->update_meta( $key, $value ); |
| @@ -122,8 +127,10 @@ | ||
| 122 | 127 | |
| 123 | 128 | /** |
| 124 | 129 | * Process settings form data |
| 125 | 130 | * |
| 131 | + * @todo Confirm if the function is necessary, it's currently unreference | |
| 132 | + * anywhere else in the plugin. | |
| 126 | 133 | * @param array $data Processed post object data. |
| 127 | 134 | * @return array New post object data. |
| 128 | 135 | */ |
| 129 | 136 | public function process_settings_form( $data ) { |
| @@ -128,13 +135,13 @@ | ||
| 128 | 135 | */ |
| 129 | 136 | public function process_settings_form( $data ) { |
| 130 | 137 | |
| 131 | 138 | $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, | |
| 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, | |
| 137 | 144 | ); |
| 138 | 145 | |
| 139 | 146 | foreach ( $args as $key => $value ) { |
| 140 | 147 | $data[ $key ] = $value; |
| @@ -140,10 +147,10 @@ | ||
| 140 | 147 | $data[ $key ] = $value; |
| 141 | 148 | } |
| 142 | 149 | |
| 143 | 150 | $meta_input = array( |
| 144 | - 'alert_type' => $this->alert_type, | |
| 145 | - 'alert_meta' => $this->alert_meta, | |
| 151 | + 'alert_type' => $this->alert_type, | |
| 152 | + 'alert_meta' => $this->alert_meta, | |
| 146 | 153 | ); |
| 147 | 154 | |
| 148 | 155 | foreach ( $meta_input as $key => $value ) { |
| 149 | 156 | $this->update_meta( $key, $value ); |
| @@ -155,15 +162,15 @@ | ||
| 155 | 162 | /** |
| 156 | 163 | * Query record meta |
| 157 | 164 | * |
| 158 | 165 | * @param string $meta_key Meta key to retrieve (optional). Otherwise will |
| 159 | - * grab all meta data for the ID. | |
| 166 | + * grab all meta data for the ID. | |
| 160 | 167 | * @param bool $single Whether to only retrieve the first value (optional). |
| 161 | 168 | * |
| 162 | 169 | * @return mixed Single value if $single is true, array if false. |
| 163 | 170 | */ |
| 164 | 171 | public function get_meta( $meta_key = '', $single = false ) { |
| 165 | - return maybe_unserialize( get_post_meta( $this->ID, $meta_key, $single ) ); | |
| 172 | + return get_post_meta( $this->ID, $meta_key, $single ); | |
| 166 | 173 | } |
| 167 | 174 | |
| 168 | 175 | /** |
| 169 | 176 | * Update record meta |
| @@ -179,12 +186,12 @@ | ||
| 179 | 186 | |
| 180 | 187 | /** |
| 181 | 188 | * Determine the title of the alert. |
| 182 | 189 | * |
| 183 | - * @todo enhance human readibility | |
| 190 | + * @todo enhance human readability | |
| 184 | 191 | * @return string The title of the alert |
| 185 | 192 | */ |
| 186 | - function get_title() { | |
| 193 | + public function get_title() { | |
| 187 | 194 | |
| 188 | 195 | $alert_type = $this->get_alert_type_obj()->name; |
| 189 | 196 | |
| 190 | 197 | $output = array(); |
| @@ -192,9 +199,9 @@ | ||
| 192 | 199 | $output[ $trigger_type ] = $this->plugin->alerts->alert_triggers[ $trigger_type ]->get_display_value( 'list_table', $this ); |
| 193 | 200 | } |
| 194 | 201 | $title = ''; |
| 195 | 202 | foreach ( $this->plugin->alerts->alert_triggers as $trigger_type => $trigger_obj ) { |
| 196 | - $value = $trigger_obj->get_display_value( 'list_table', $this ); | |
| 203 | + $value = $trigger_obj->get_display_value( 'list_table', $this ); | |
| 197 | 204 | $title .= $value . ' > '; |
| 198 | 205 | } |
| 199 | 206 | $title = rtrim( $title, ' > ' ); |
| 200 | 207 | return $title; |
| @@ -200,9 +207,9 @@ | ||
| 200 | 207 | return $title; |
| 201 | 208 | } |
| 202 | 209 | |
| 203 | 210 | /** |
| 204 | - * Retrive current alert type object | |
| 211 | + * Retrieve current alert type object | |
| 205 | 212 | * |
| 206 | 213 | * @return Alert_Type |
| 207 | 214 | */ |
| 208 | 215 | public function get_alert_type_obj() { |
| @@ -229,9 +236,8 @@ | ||
| 229 | 236 | * Trigger alert for a specific record. |
| 230 | 237 | * |
| 231 | 238 | * @param int $record_id Record ID. |
| 232 | 239 | * @param int $recordarr Record Data. |
| 233 | - * @return void | |
| 234 | 240 | */ |
| 235 | 241 | public function send_alert( $record_id, $recordarr ) { |
| 236 | 242 | $this->get_alert_type_obj()->alert( $record_id, $recordarr, $this ); |
| 237 | 243 | } |
| @@ -266,13 +272,15 @@ | ||
| 266 | 272 | } |
| 267 | 273 | if ( empty( $record->ID ) ) { |
| 268 | 274 | return false; |
| 269 | 275 | } |
| 270 | - $record = new Record( $record ); | |
| 276 | + $record = new Record( $record ); | |
| 271 | 277 | $alerts_triggered = $record->get_meta( Alerts::ALERTS_TRIGGERED_META_KEY, true ); |
| 272 | 278 | |
| 273 | 279 | if ( empty( $alerts_triggered ) || ! is_array( $alerts_triggered ) ) { |
| 274 | - $alerts_triggered = array( $alert_slug => $alert_meta ); | |
| 280 | + $alerts_triggered = array( | |
| 281 | + $alert_slug => $alert_meta, | |
| 282 | + ); | |
| 275 | 283 | } elseif ( ! array_key_exists( $alert_slug, $alerts_triggered ) || ! is_array( $alerts_triggered[ $alert_slug ] ) ) { |
| 276 | 284 | $alerts_triggered[ $alert_slug ] = $alert_meta; |
| 277 | 285 | } |
| 278 | 286 | return $record->update_meta( Alerts::ALERTS_TRIGGERED_META_KEY, $alerts_triggered ); |
| @@ -290,22 +298,22 @@ | ||
| 290 | 298 | * |
| 291 | 299 | * Using that ID, it fetches that Alert post's meta, then |
| 292 | 300 | * returns the value of the requested setting (ie., "post meta" field). |
| 293 | 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 | + * | |
| 294 | 307 | * @see Alert_Type_Highlight::post_class() for an example. |
| 295 | 308 | * |
| 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 | 309 | * @return mixed |
| 302 | 310 | */ |
| 303 | - public function get_single_alert_setting_from_record( $record, $alert_slug, $setting, $default = false ) { | |
| 311 | + public function get_single_alert_setting_from_record( $record, $alert_slug, $setting, $default_value = false ) { | |
| 304 | 312 | if ( ! is_object( $record ) || ! is_string( $alert_slug ) || ! is_string( $setting ) ) { |
| 305 | 313 | return false; |
| 306 | 314 | } |
| 307 | - $record = new Record( $record ); | |
| 315 | + $record = new Record( $record ); | |
| 308 | 316 | $alerts_triggered = $record->get_meta( Alerts::ALERTS_TRIGGERED_META_KEY, true ); |
| 309 | 317 | |
| 310 | 318 | // Ensure we have a meta array and that this record has triggered a highlight alert. |
| 311 | 319 | if ( empty( $alerts_triggered ) || ! is_array( $alerts_triggered ) || ! array_key_exists( $alert_slug, $alerts_triggered ) ) { |
| @@ -316,10 +324,13 @@ | ||
| 316 | 324 | if ( empty( $values ) ) { |
| 317 | 325 | return false; |
| 318 | 326 | } |
| 319 | 327 | |
| 320 | - // Grab an Alert post ID. | |
| 321 | - // @todo determine which Alert post takes priority. | |
| 328 | + /** | |
| 329 | + * Grab an Alert post ID. | |
| 330 | + * | |
| 331 | + * @todo Determine which Alert post takes priority. | |
| 332 | + */ | |
| 322 | 333 | if ( is_array( $values ) ) { |
| 323 | 334 | $post_id = $values[0]; |
| 324 | 335 | } else { |
| 325 | 336 | $post_id = $values; |
| @@ -330,8 +341,8 @@ | ||
| 330 | 341 | } |
| 331 | 342 | |
| 332 | 343 | $alert = $this->plugin->alerts->get_alert( $post_id ); |
| 333 | 344 | |
| 334 | - $value = ! empty( $alert->alert_meta[ $setting ] ) ? $alert->alert_meta[ $setting ] : $default; | |
| 345 | + $value = ! empty( $alert->alert_meta[ $setting ] ) ? $alert->alert_meta[ $setting ] : $default_value; | |
| 335 | 346 | return $value; |
| 336 | 347 | } |
| 337 | 348 | } |