: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Abstract for post models
* @copyright Copyright (c) 2023, Code Atlantic LLC
if ( ! defined( 'ABSPATH' ) ) {
* Class PUM_Abstract_Model_Post
abstract class PUM_Abstract_Model_Post {
* The current model version.
* Used for compatibility testing.
public $model_version = 1;
* The version of the data currently stored for the current item.
* Declare the default properties in WP_Post as we can't extend it
public $post_date = '0000-00-00 00:00:00';
public $post_date_gmt = '0000-00-00 00:00:00';
public $post_content = '';
public $post_excerpt = '';
public $post_status = 'publish';
public $comment_status = 'open';
public $ping_status = 'open';
public $post_password = '';
public $post_modified = '0000-00-00 00:00:00';
public $post_modified_gmt = '0000-00-00 00:00:00';
public $post_content_filtered = '';
public $post_mime_type = '';
public $comment_count = 0;
* The required post type of the object.
protected $required_post_type = false;
* Whether the object is valid.
* @param WP_Post|int $post
public function __construct( $post ) {
if ( ! is_a( $post, 'WP_Post' ) ) {
$post = get_post( $post );
* Given the post data, let's set the variables
protected function setup( $post ) {
if ( ! is_a( $post, 'WP_Post' ) || ! $this->is_required_post_type( $post ) ) {
foreach ( get_object_vars( $post ) as $key => $value ) {
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 ) ) {
} elseif ( is_string( $this->required_post_type ) && $this->required_post_type !== $post->post_type ) {
* is triggered when invoking inaccessible methods in an object context.
* @param $arguments array
* @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( [ $this, 'get_' . $name ], $arguments );
* Magic __get function to dispatch a call to retrieve a private property
public function __get( $key ) {
if ( method_exists( $this, 'get_' . $key ) ) {
return call_user_func( [ $this, 'get_' . $key ] );
$meta = $this->get_meta( $key );
return new WP_Error( 'post-invalid-property', sprintf( __( 'Can\'t get property %s' ), $key ) );
public function is_valid() {
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 get_post_meta( $this->ID, $key, $single );
public function add_meta( $key, $value, $unique = false ) {
return add_post_meta( $this->ID, $key, $value, $unique );
public function update_meta( $key, $value ) {
return update_post_meta( $this->ID, $key, $value );
public function delete_meta( $key ) {
return delete_post_meta( $this->ID, $key );
* Allows for easy backward compatibility layer management in each child class.
public function remapped_meta( $key = '' ) {
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 );
public function is_trash() {
return get_post_status( $this->ID ) === 'trash';
public function is_published() {
return get_post_status( $this->ID ) === 'publish';
public function is_draft() {
return get_post_status( $this->ID ) === 'draft';
public function is_private() {
return get_post_status( $this->ID ) === 'private';
public function is_pending() {
return get_post_status( $this->ID ) === 'pending';