| 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 |
* Fetch a single record row by ID, including its metadata. |
| 140 |
* |
| 141 |
* Stream's Query class doesn't expose a single-ID filter (record__in is |
| 142 |
* broken for one-element arrays due to array_shift() in Query::query()), |
| 143 |
* so query the table directly. Pass $blog_id to scope the lookup to a |
| 144 |
* specific blog on multisite — callers running in REST/non-network-admin |
| 145 |
* contexts should pass get_current_blog_id() to prevent cross-site reads. |
| 146 |
* |
| 147 |
* @param int $id Record ID. |
| 148 |
* @param int|null $blog_id Optional blog ID to constrain the lookup to. |
| 149 |
* |
| 150 |
* @return array|null Associative row with merged 'meta' key, or null when |
| 151 |
* no matching record exists. |
| 152 |
*/ |
| 153 |
public static function get_by_id( $id, $blog_id = null ) { |
| 154 |
global $wpdb; |
| 155 |
|
| 156 |
$id = (int) $id; |
| 157 |
if ( $id <= 0 ) { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
$where = ''; |
| 162 |
$prepared = array( $id ); |
| 163 |
if ( null !== $blog_id ) { |
| 164 |
$where = ' AND blog_id = %d'; |
| 165 |
$prepared[] = (int) $blog_id; |
| 166 |
} |
| 167 |
|
| 168 |
// $wpdb->stream and {$where} are constructed from string literals (no |
| 169 |
// user input), and $prepared holds only integer IDs. |
| 170 |
$sql = "SELECT * FROM {$wpdb->stream} WHERE ID = %d{$where}"; |
| 171 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared |
| 172 |
$row = $wpdb->get_row( $wpdb->prepare( $sql, $prepared ), ARRAY_A ); |
| 173 |
|
| 174 |
if ( empty( $row ) ) { |
| 175 |
return null; |
| 176 |
} |
| 177 |
|
| 178 |
$row['meta'] = (array) get_metadata( 'record', $row['ID'] ); |
| 179 |
|
| 180 |
return $row; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Save record. |
| 185 |
* |
| 186 |
* @return int|WP_Error |
| 187 |
*/ |
| 188 |
public function save() { |
| 189 |
if ( ! $this->validate() ) { |
| 190 |
return new \WP_Error( 'validation-error', esc_html__( 'Could not validate record data.', 'stream' ) ); |
| 191 |
} |
| 192 |
|
| 193 |
return wp_stream_get_instance()->db->insert( (array) $this ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Populate "$this" object with provided data. |
| 198 |
* |
| 199 |
* @param array $raw Data to be used to populate $this object. |
| 200 |
*/ |
| 201 |
public function populate( array $raw ) { |
| 202 |
$keys = get_class_vars( $this ); |
| 203 |
$data = array_intersect_key( $raw, $keys ); |
| 204 |
foreach ( $data as $key => $val ) { |
| 205 |
$this->{$key} = $val; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Validates this record |
| 211 |
* |
| 212 |
* @todo Add actual validation measures. |
| 213 |
* |
| 214 |
* @return bool |
| 215 |
*/ |
| 216 |
public function validate() { |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Query record meta |
| 222 |
* |
| 223 |
* @param string $meta_key Meta key (optional). |
| 224 |
* @param bool $single Return only first found (optional). |
| 225 |
* |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public function get_meta( $meta_key = '', $single = false ) { |
| 229 |
return get_metadata( 'record', $this->ID, $meta_key, $single ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Update record meta |
| 234 |
* |
| 235 |
* @param string $meta_key Meta key. |
| 236 |
* @param mixed $meta_value New meta value. |
| 237 |
* @param mixed $prev_value Old meta value (optional). |
| 238 |
* |
| 239 |
* @return bool |
| 240 |
*/ |
| 241 |
public function update_meta( $meta_key, $meta_value, $prev_value = '' ) { |
| 242 |
return update_metadata( 'record', $this->ID, $meta_key, $meta_value, $prev_value ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Determine the title of an object that a record is for. |
| 247 |
* |
| 248 |
* @return mixed The title of the object as a string, otherwise false |
| 249 |
*/ |
| 250 |
public function get_object_title() { |
| 251 |
if ( ! isset( $this->object_id ) || empty( $this->object_id ) ) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
$output = false; |
| 256 |
|
| 257 |
if ( isset( $this->meta->post_title ) && ! empty( $this->meta->post_title ) ) { |
| 258 |
$output = (string) $this->meta->post_title; |
| 259 |
} elseif ( isset( $this->meta->display_name ) && ! empty( $this->meta->display_name ) ) { |
| 260 |
$output = (string) $this->meta->display_name; |
| 261 |
} elseif ( isset( $this->meta->name ) && ! empty( $this->meta->name ) ) { |
| 262 |
$output = (string) $this->meta->name; |
| 263 |
} |
| 264 |
|
| 265 |
return $output; |
| 266 |
} |
| 267 |
} |
| 268 |
|