| 1 |
<?php |
| 2 |
namespace WP_Stream; |
| 3 |
|
| 4 |
class Record { |
| 5 |
public $ID; |
| 6 |
public $created; |
| 7 |
public $site_id; |
| 8 |
public $blog_id; |
| 9 |
public $object_id; |
| 10 |
public $user_id; |
| 11 |
public $user_role; |
| 12 |
public $user_meta; |
| 13 |
public $summary; |
| 14 |
public $connector; |
| 15 |
public $context; |
| 16 |
public $action; |
| 17 |
public $ip; |
| 18 |
public $meta; |
| 19 |
|
| 20 |
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; |
| 25 |
$this->object_id = isset( $item->object_id ) ? $item->object_id : null; |
| 26 |
$this->user_id = isset( $item->user_id ) ? $item->user_id : null; |
| 27 |
$this->user_role = isset( $item->user_role ) ? $item->user_role : null; |
| 28 |
$this->user_meta = isset( $item->meta['user_meta'] ) ? $item->meta['user_meta'] : null; |
| 29 |
$this->summary = isset( $item->summary ) ? $item->summary : null; |
| 30 |
$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; |
| 35 |
|
| 36 |
if ( isset( $this->meta['user_meta'] ) ) { |
| 37 |
unset( $this->meta['user_meta'] ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function save() { |
| 42 |
if ( ! $this->validate() ) { |
| 43 |
return new \WP_Error( 'validation-error', esc_html__( 'Could not validate record data.', 'stream' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
return wp_stream_get_instance()->db->insert( (array) $this ); |
| 47 |
} |
| 48 |
|
| 49 |
public function populate( array $raw ) { |
| 50 |
$keys = get_class_vars( $this ); |
| 51 |
$data = array_intersect_key( $raw, $keys ); |
| 52 |
foreach ( $data as $key => $val ) { |
| 53 |
$this->{$key} = $val; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function validate() { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Query record meta |
| 63 |
* |
| 64 |
* @param string $meta_key (optional) |
| 65 |
* @param bool $single (optional) |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function get_meta( $meta_key = '', $single = false ) { |
| 70 |
return maybe_unserialize( get_metadata( 'record', $this->ID, $meta_key, $single ) ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Update record meta |
| 75 |
* |
| 76 |
* @param string $meta_key |
| 77 |
* @param string $meta_value |
| 78 |
* @param string $prev_value (optional) |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
public function update_meta( $meta_key, $meta_value, $prev_value = '' ) { |
| 83 |
return update_metadata( 'record', $this->ID, $meta_key, $meta_value, $prev_value ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Determine the title of an object that a record is for. |
| 88 |
* |
| 89 |
* @param object Record object |
| 90 |
* @return mixed The title of the object as a string, otherwise false |
| 91 |
*/ |
| 92 |
function get_object_title() { |
| 93 |
if ( ! isset( $this->object_id ) || empty( $this->object_id ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
$output = false; |
| 98 |
|
| 99 |
if ( isset( $this->meta->post_title ) && ! empty( $this->meta->post_title ) ) { |
| 100 |
$output = (string) $this->meta->post_title; |
| 101 |
} elseif ( isset( $this->meta->display_name ) && ! empty( $this->meta->display_name ) ) { |
| 102 |
$output = (string) $this->meta->display_name; |
| 103 |
} elseif ( isset( $this->meta->name ) && ! empty( $this->meta->name ) ) { |
| 104 |
$output = (string) $this->meta->name; |
| 105 |
} |
| 106 |
|
| 107 |
return $output; |
| 108 |
} |
| 109 |
} |
| 110 |
|