ID = isset( $item->ID ) ? $item->ID : null; $this->created = isset( $item->created ) ? $item->created : null; $this->site_id = isset( $item->site_id ) ? $item->site_id : null; $this->blog_id = isset( $item->blog_id ) ? $item->blog_id : null; $this->object_id = isset( $item->object_id ) ? $item->object_id : null; $this->user_id = isset( $item->user_id ) ? $item->user_id : null; $this->user_role = isset( $item->user_role ) ? $item->user_role : null; $this->user_meta = isset( $item->meta['user_meta'] ) ? $item->meta['user_meta'] : null; $this->summary = isset( $item->summary ) ? $item->summary : null; $this->connector = isset( $item->connector ) ? $item->connector : null; $this->context = isset( $item->context ) ? $item->context : null; $this->action = isset( $item->action ) ? $item->action : null; $this->ip = isset( $item->ip ) ? $item->ip : null; $this->meta = isset( $item->meta ) ? $item->meta : null; if ( isset( $this->meta['user_meta'] ) ) { unset( $this->meta['user_meta'] ); } } /** * Save record. * * @return int|WP_Error */ public function save() { if ( ! $this->validate() ) { return new \WP_Error( 'validation-error', esc_html__( 'Could not validate record data.', 'stream' ) ); } return wp_stream_get_instance()->db->insert( (array) $this ); } /** * Populate "$this" object with provided data. * * @param array $raw Data to be used to populate $this object. */ public function populate( array $raw ) { $keys = get_class_vars( $this ); $data = array_intersect_key( $raw, $keys ); foreach ( $data as $key => $val ) { $this->{$key} = $val; } } /** * Validates this record * * @todo Add actual validation measures. * * @return bool */ public function validate() { return true; } /** * Query record meta * * @param string $meta_key Meta key (optional). * @param bool $single Return only first found (optional). * * @return array */ public function get_meta( $meta_key = '', $single = false ) { return get_metadata( 'record', $this->ID, $meta_key, $single ); } /** * Update record meta * * @param string $meta_key Meta key. * @param mixed $meta_value New meta value. * @param mixed $prev_value Old meta value (optional). * * @return bool */ public function update_meta( $meta_key, $meta_value, $prev_value = '' ) { return update_metadata( 'record', $this->ID, $meta_key, $meta_value, $prev_value ); } /** * Determine the title of an object that a record is for. * * @return mixed The title of the object as a string, otherwise false */ public function get_object_title() { if ( ! isset( $this->object_id ) || empty( $this->object_id ) ) { return false; } $output = false; if ( isset( $this->meta->post_title ) && ! empty( $this->meta->post_title ) ) { $output = (string) $this->meta->post_title; } elseif ( isset( $this->meta->display_name ) && ! empty( $this->meta->display_name ) ) { $output = (string) $this->meta->display_name; } elseif ( isset( $this->meta->name ) && ! empty( $this->meta->name ) ) { $output = (string) $this->meta->name; } return $output; } }