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