post = $post; $properties = [ 'ID' => $post->ID, 'slug' => $post->post_name, 'title' => $post->post_title, 'status' => $post->post_status, ]; foreach ( $properties as $key => $value ) { $this->$key = $value; } $this->data_version = get_post_meta( $post->ID, 'data_version', true ); if ( ! $this->data_version ) { $this->data_version = self::MODEL_VERSION; update_post_meta( $post->ID, 'data_version', self::MODEL_VERSION ); } } /** * Get edit link. * * @return string Empty string if user cannot edit post, otherwise admin edit URL. */ public function get_edit_link() { if ( current_user_can( 'edit_post', $this->ID ) ) { return admin_url( "post.php?action=edit&post_type={$this->post->post_type}&post=" . absint( $this->ID ) ); } return ''; } /** * Get post meta value. * * @param string $key Meta key. * @param bool $single Whether to return a single value. * * @return ($single is true ? mixed : mixed[]) */ public function get_meta( $key, $single = true ) { return get_post_meta( $this->ID, $key, $single ); } /** * Update post meta value. * * @param string $key Meta key. * @param mixed $value Meta value. * * @return int|bool Meta ID on success, true on update, false on failure. */ public function update_meta( $key, $value ) { return update_post_meta( $this->ID, $key, $value ); } /** * Convert this call to action to an array. * * @return array{ * ID: int, * slug: string, * title: string, * status: string * } */ public function to_array() { return [ 'ID' => $this->ID, 'slug' => $this->slug, 'title' => $this->title, 'status' => $this->status, ]; } }