| @@ -1,38 +1,135 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Manages the state of a single record | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace WP_Stream; |
| 3 | 9 | |
| 10 | +/** | |
| 11 | + * Class - Record | |
| 12 | + */ | |
| 4 | 13 | class Record { |
| 14 | + /** | |
| 15 | + * Record ID | |
| 16 | + * | |
| 17 | + * @var int | |
| 18 | + */ | |
| 5 | 19 | public $ID; |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Date record created | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 6 | 26 | public $created; |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Site ID of the site where the record was created | |
| 30 | + * | |
| 31 | + * @var int | |
| 32 | + */ | |
| 7 | 33 | public $site_id; |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Blog ID of the site where the record was created | |
| 37 | + * | |
| 38 | + * @var int | |
| 39 | + */ | |
| 8 | 40 | public $blog_id; |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Record Object ID | |
| 44 | + * | |
| 45 | + * @var int | |
| 46 | + */ | |
| 9 | 47 | public $object_id; |
| 48 | + | |
| 49 | + /** | |
| 50 | + * User ID of the record creator | |
| 51 | + * | |
| 52 | + * @var int | |
| 53 | + */ | |
| 10 | 54 | public $user_id; |
| 55 | + | |
| 56 | + /** | |
| 57 | + * User role of the record creator | |
| 58 | + * | |
| 59 | + * @var string | |
| 60 | + */ | |
| 11 | 61 | public $user_role; |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Record user meta data. | |
| 65 | + * | |
| 66 | + * @var string | |
| 67 | + */ | |
| 12 | 68 | public $user_meta; |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Record summary | |
| 72 | + * | |
| 73 | + * @var string | |
| 74 | + */ | |
| 13 | 75 | public $summary; |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Record connector | |
| 79 | + * | |
| 80 | + * @var string | |
| 81 | + */ | |
| 14 | 82 | public $connector; |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Context record was made in. | |
| 86 | + * | |
| 87 | + * @var string | |
| 88 | + */ | |
| 15 | 89 | public $context; |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Record action | |
| 93 | + * | |
| 94 | + * @var string | |
| 95 | + */ | |
| 16 | 96 | public $action; |
| 97 | + | |
| 98 | + /** | |
| 99 | + * IP of event requestee | |
| 100 | + * | |
| 101 | + * @var string | |
| 102 | + */ | |
| 17 | 103 | public $ip; |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Record meta data | |
| 107 | + * | |
| 108 | + * @var array | |
| 109 | + */ | |
| 18 | 110 | public $meta; |
| 19 | 111 | |
| 112 | + /** | |
| 113 | + * Class constructor | |
| 114 | + * | |
| 115 | + * @param object $item Record data object. | |
| 116 | + */ | |
| 20 | 117 | public function __construct( $item ) { |
| 21 | - $this->ID = isset( $item->ID ) ? $item->ID : null; | |
| 22 | - $this->created = isset( $item->created ) ? $item->created : null; | |
| 23 | - $this->site_id = isset( $item->site_id ) ? $item->site_id : null; | |
| 24 | - $this->blog_id = isset( $item->blog_id ) ? $item->blog_id : null; | |
| 118 | + $this->ID = isset( $item->ID ) ? $item->ID : null; | |
| 119 | + $this->created = isset( $item->created ) ? $item->created : null; | |
| 120 | + $this->site_id = isset( $item->site_id ) ? $item->site_id : null; | |
| 121 | + $this->blog_id = isset( $item->blog_id ) ? $item->blog_id : null; | |
| 25 | 122 | $this->object_id = isset( $item->object_id ) ? $item->object_id : null; |
| 26 | - $this->user_id = isset( $item->user_id ) ? $item->user_id : null; | |
| 123 | + $this->user_id = isset( $item->user_id ) ? $item->user_id : null; | |
| 27 | 124 | $this->user_role = isset( $item->user_role ) ? $item->user_role : null; |
| 28 | 125 | $this->user_meta = isset( $item->meta['user_meta'] ) ? $item->meta['user_meta'] : null; |
| 29 | - $this->summary = isset( $item->summary ) ? $item->summary : null; | |
| 126 | + $this->summary = isset( $item->summary ) ? $item->summary : null; | |
| 30 | 127 | $this->connector = isset( $item->connector ) ? $item->connector : null; |
| 31 | - $this->context = isset( $item->context ) ? $item->context : null; | |
| 32 | - $this->action = isset( $item->action ) ? $item->action : null; | |
| 33 | - $this->ip = isset( $item->ip ) ? $item->ip : null; | |
| 34 | - $this->meta = isset( $item->meta ) ? $item->meta : null; | |
| 128 | + $this->context = isset( $item->context ) ? $item->context : null; | |
| 129 | + $this->action = isset( $item->action ) ? $item->action : null; | |
| 130 | + $this->ip = isset( $item->ip ) ? $item->ip : null; | |
| 131 | + $this->meta = isset( $item->meta ) ? $item->meta : null; | |
| 35 | 132 | |
| 36 | 133 | if ( isset( $this->meta['user_meta'] ) ) { |
| 37 | 134 | unset( $this->meta['user_meta'] ); |
| 38 | 135 | } |
| @@ -37,8 +134,13 @@ | ||
| 37 | 134 | unset( $this->meta['user_meta'] ); |
| 38 | 135 | } |
| 39 | 136 | } |
| 40 | 137 | |
| 138 | + /** | |
| 139 | + * Save record. | |
| 140 | + * | |
| 141 | + * @return int|WP_Error | |
| 142 | + */ | |
| 41 | 143 | public function save() { |
| 42 | 144 | if ( ! $this->validate() ) { |
| 43 | 145 | return new \WP_Error( 'validation-error', esc_html__( 'Could not validate record data.', 'stream' ) ); |
| 44 | 146 | } |
| @@ -45,8 +147,13 @@ | ||
| 45 | 147 | |
| 46 | 148 | return wp_stream_get_instance()->db->insert( (array) $this ); |
| 47 | 149 | } |
| 48 | 150 | |
| 151 | + /** | |
| 152 | + * Populate "$this" object with provided data. | |
| 153 | + * | |
| 154 | + * @param array $raw Data to be used to populate $this object. | |
| 155 | + */ | |
| 49 | 156 | public function populate( array $raw ) { |
| 50 | 157 | $keys = get_class_vars( $this ); |
| 51 | 158 | $data = array_intersect_key( $raw, $keys ); |
| 52 | 159 | foreach ( $data as $key => $val ) { |
| @@ -53,8 +160,15 @@ | ||
| 53 | 160 | $this->{$key} = $val; |
| 54 | 161 | } |
| 55 | 162 | } |
| 56 | 163 | |
| 164 | + /** | |
| 165 | + * Validates this record | |
| 166 | + * | |
| 167 | + * @todo Add actual validation measures. | |
| 168 | + * | |
| 169 | + * @return bool | |
| 170 | + */ | |
| 57 | 171 | public function validate() { |
| 58 | 172 | return true; |
| 59 | 173 | } |
| 60 | 174 | |
| @@ -60,10 +174,10 @@ | ||
| 60 | 174 | |
| 61 | 175 | /** |
| 62 | 176 | * Query record meta |
| 63 | 177 | * |
| 64 | - * @param string $meta_key (optional) | |
| 65 | - * @param bool $single (optional) | |
| 178 | + * @param string $meta_key Meta key (optional). | |
| 179 | + * @param bool $single Return only first found (optional). | |
| 66 | 180 | * |
| 67 | 181 | * @return array |
| 68 | 182 | */ |
| 69 | 183 | public function get_meta( $meta_key = '', $single = false ) { |
| @@ -72,11 +186,11 @@ | ||
| 72 | 186 | |
| 73 | 187 | /** |
| 74 | 188 | * Update record meta |
| 75 | 189 | * |
| 76 | - * @param string $meta_key | |
| 77 | - * @param mixed $meta_value | |
| 78 | - * @param mixed $prev_value (optional) | |
| 190 | + * @param string $meta_key Meta key. | |
| 191 | + * @param mixed $meta_value New meta value. | |
| 192 | + * @param mixed $prev_value Old meta value (optional). | |
| 79 | 193 | * |
| 80 | 194 | * @return bool |
| 81 | 195 | */ |
| 82 | 196 | public function update_meta( $meta_key, $meta_value, $prev_value = '' ) { |
| @@ -85,12 +199,11 @@ | ||
| 85 | 199 | |
| 86 | 200 | /** |
| 87 | 201 | * Determine the title of an object that a record is for. |
| 88 | 202 | * |
| 89 | - * @param object Record object | |
| 90 | - * @return mixed The title of the object as a string, otherwise false | |
| 203 | + * @return mixed The title of the object as a string, otherwise false | |
| 91 | 204 | */ |
| 92 | - function get_object_title() { | |
| 205 | + public function get_object_title() { | |
| 93 | 206 | if ( ! isset( $this->object_id ) || empty( $this->object_id ) ) { |
| 94 | 207 | return false; |
| 95 | 208 | } |
| 96 | 209 | |