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