| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- /*******************************************************************************
- * Copyright (c) 2019, Code Atlantic LLC
- ******************************************************************************/
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- /**
- * Class PUM_Abstract_Model_Post
- */
- abstract class PUM_Abstract_Model_Post {
- /**
- * The current model version.
- *
- * Used for compatibility testing.
- * 1 - v1.0.0
- *
- * @var int
- */
- public $model_version = 1;
- /**
- * The version of the data currently stored for the current item.
- *
- * 1 - v1.0.0
- *
- * @var int
- */
- public $data_version;
- /**
- * The post ID
- */
- public $ID = 0;
- /**
- * Declare the default properties in WP_Post as we can't extend it
- */
- public $post_author = 0;
- /**
- * @var string
- */
- public $post_date = '0000-00-00 00:00:00';
- /**
- * @var string
- */
- public $post_date_gmt = '0000-00-00 00:00:00';
- /**
- * @var string
- */
- public $post_content = '';
- /**
- * @var string
- */
- public $post_title = '';
- /**
- * @var string
- */
- public $post_excerpt = '';
- /**
- * @var string
- */
- public $post_status = 'publish';
- /**
- * @var string
- */
- public $comment_status = 'open';
- /**
- * @var string
- */
- public $ping_status = 'open';
- /**
- * @var string
- */
- public $post_password = '';
- /**
- * @var string
- */
- public $post_name = '';
- /**
- * @var string
- */
- public $post_type = '';
- /**
- * @var string
- */
- public $to_ping = '';
- /**
- * @var string
- */
- public $pinged = '';
- /**
- * @var string
- */
- public $post_modified = '0000-00-00 00:00:00';
- /**
- * @var string
- */
- public $post_modified_gmt = '0000-00-00 00:00:00';
- /**
- * @var string
- */
- public $post_content_filtered = '';
- /**
- * @var int
- */
- public $post_parent = 0;
- /**
- * @var string
- */
- public $guid = '';
- /**
- * @var int
- */
- public $menu_order = 0;
- /**
- * @var string
- */
- public $post_mime_type = '';
- /**
- * @var int
- */
- public $comment_count = 0;
- /**
- * @var
- */
- public $filter;
- /**
- * @var WP_Post
- */
- public $post;
- /**
- * The required post type of the object.
- */
- protected $required_post_type = false;
- /**
- * Whether the object is valid.
- */
- protected $valid = true;
- /**
- * Get things going
- *
- * @param WP_Post|int $post
- */
- public function __construct( $post ) {
- if ( ! is_a( $post, 'WP_Post' ) ) {
- $post = get_post( $post );
- }
- $this->setup( $post );
- }
- /**
- * Given the post data, let's set the variables
- *
- * @param WP_Post $post
- */
- protected function setup( $post ) {
- if ( ! is_a( $post, 'WP_Post' ) || ! $this->is_required_post_type( $post ) ) {
- $this->valid = false;
- return;
- }
- $this->post = $post;
- foreach ( get_object_vars( $post ) as $key => $value ) {
- $this->$key = $value;
- }
- }
- /**
- * @param WP_Post $post
- *
- * @return bool
- */
- protected function is_required_post_type( $post ) {
- if ( $this->required_post_type ) {
- if ( is_array( $this->required_post_type ) && ! in_array( $post->post_type, $this->required_post_type ) ) {
- return false;
- } else if ( is_string( $this->required_post_type ) && $this->required_post_type !== $post->post_type ) {
- return false;
- }
- }
- return true;
- }
- /**
- * is triggered when invoking inaccessible methods in an object context.
- *
- * @param $name string
- * @param $arguments array
- *
- * @return mixed
- * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
- */
- public function __call( $name, $arguments ) {
- if ( method_exists( $this, 'get_' . $name ) ) {
- return call_user_func_array( array( $this, 'get_' . $name ), $arguments );
- }
- }
- /**
- * Magic __get function to dispatch a call to retrieve a private property
- *
- * @param $key
- *
- * @return mixed|WP_Error
- */
- public function __get( $key ) {
- if ( method_exists( $this, 'get_' . $key ) ) {
- return call_user_func( array( $this, 'get_' . $key ) );
- } else {
- $meta = $this->get_meta( $key );
- if ( $meta ) {
- return $meta;
- }
- return new WP_Error( 'post-invalid-property', sprintf( __( 'Can\'t get property %s' ), $key ) );
- }
- }
- /**
- * Is object valid.
- *
- * @return bool.
- */
- public function is_valid() {
- return $this->valid;
- }
- /**
- * @param $key
- * @param bool $single
- *
- * @return mixed|false
- */
- public function get_meta( $key, $single = true ) {
- /**
- * Checks for remapped meta values. This allows easily adding compatibility layers in the object meta.
- */
- if ( false !== $remapped_value = $this->remapped_meta( $key ) ) {
- return $remapped_value;
- }
- return get_post_meta( $this->ID, $key, $single );
- }
- /**
- * @param string $key
- * @param mixed $value
- * @param bool $unique
- *
- * @return bool|int
- */
- public function add_meta( $key, $value, $unique = false ) {
- return add_post_meta( $this->ID, $key, $value, $unique );
- }
- /**
- * @param string $key
- * @param mixed $value
- *
- * @return bool|int
- */
- public function update_meta( $key, $value ) {
- return update_post_meta( $this->ID, $key, $value );
- }
- /**
- * @param string $key
- *
- * @return bool
- */
- public function delete_meta( $key ) {
- return delete_post_meta( $this->ID, $key );
- }
- /**
- * Allows for easy backward compatibility layer management in each child class.
- *
- * @param string $key
- *
- * @return bool
- */
- public function remapped_meta( $key = '' ) {
- return false;
- }
- /**
- * @return int
- */
- public function author_id() {
- return (int) $this->post_author;
- }
- /**
- * Convert object to array.
- *
- * @return array Object as array.
- */
- public function to_array() {
- $post = get_object_vars( $this );
- return $post;
- }
- /**
- * @return bool
- */
- public function is_trash() {
- return get_post_status( $this->ID ) == 'trash';
- }
- /**
- * @return bool
- */
- public function is_published() {
- return get_post_status( $this->ID ) == 'publish';
- }
- /**
- * @return bool
- */
- public function is_draft() {
- return get_post_status( $this->ID ) == 'draft';
- }
- /**
- * @return bool
- */
- public function is_private() {
- return get_post_status( $this->ID ) == 'private';
- }
- /**
- * @return bool
- */
- public function is_pending() {
- return get_post_status( $this->ID ) == 'pending';
- }
- }
|