first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,386 @@
<?php
/**
* Abstract for post models
*
* @package PUM
* @copyright Copyright (c) 2023, 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;
} elseif ( 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( [ $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( [ $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';
}
}

View File

@@ -0,0 +1,309 @@
<?php
/**
* Abstract for user model
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Core class used to implement the custom WP_User object.
*
* @property string $nickname
* @property string $description
* @property string $user_description
* @property string $first_name
* @property string $user_firstname
* @property string $last_name
* @property string $user_lastname
* @property string $user_login
* @property string $user_pass
* @property string $user_nicename
* @property string $user_email
* @property string $user_url
* @property string $user_registered
* @property string $user_activation_key
* @property string $user_status
* @property int $user_level
* @property string $display_name
* @property string $spam
* @property string $deleted
* @property string $locale
*/
abstract class PUM_Abstract_Model_User {
/**
* 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 user's ID.
*
* @var int
*/
public $ID = 0;
/**
* @var \WP_User
*/
public $user;
/**
* @var array An array of keys that can be accessed via the $this->user (WP_User) object.
*/
public $core_data_keys = [
'nickname',
'description',
'user_description',
'first_name',
'user_firstname',
'last_name',
'user_lastname',
'user_login',
'user_pass',
'user_nicename',
'user_email',
'user_url',
'user_registered',
'user_activation_key',
'user_status',
'user_level',
'display_name',
'spam',
'deleted',
'locale',
'data',
'ID',
'caps',
'cap_key',
'roles',
'allcaps',
'filter',
];
/**
* The required permission|user_role|capability|user_level of the user.
*/
protected $required_permission = '';
/**
* Get things going
*
* @param WP_User|int $user
*/
public function __construct( $user ) {
if ( ! is_a( $user, 'WP_User' ) ) {
$user = new WP_User( $user );
}
$this->setup( $user );
}
/**
* Given the user data, let's set the variables
*
* @param WP_User $user The User Object
*/
protected function setup( $user ) {
if ( ! is_a( $user, 'WP_User' ) || ( $this->required_permission && ! $user->has_cap( $this->required_permission ) ) ) {
return;
}
if ( ! isset( $user->data->ID ) ) {
$user->data->ID = 0;
}
$this->user = $user;
// Set $this->ID based on the users ID.
$this->ID = $user->ID;
}
/**
* @param $key
*
* @return bool
*/
public function __isset( $key ) {
if ( in_array( $key, $this->core_data_keys ) ) {
return isset( $this->user->$key );
}
}
/**
* @param $key
*/
public function __unset( $key ) {
if ( in_array( $key, $this->core_data_keys ) ) {
unset( $this->user->$key );
}
}
/**
* Magic __get function to dispatch a call to retrieve a private property
*
* @param $key
*
* @return mixed|WP_Error
*/
public function __get( $key ) {
if ( in_array( $key, $this->core_data_keys ) ) {
return $this->user->$key;
} elseif ( method_exists( $this, 'get_' . $key ) ) {
return call_user_func( [ $this, 'get_' . $key ] );
} else {
$meta = get_user_meta( $this->ID, $key, true );
if ( $meta ) {
return $meta;
}
return new WP_Error( 'user-invalid-property', sprintf( __( 'Can\'t get property %s' ), $key ) );
}
}
/**
* @param $name
* @param $arguments
*
* @return mixed
*/
public function __call( $name, $arguments ) {
if ( method_exists( $this->user, $name ) ) {
return call_user_func_array( [ $this->user, $name ], $arguments );
}
}
/**
* Get per site or global user options.
*
* @param $key
*
* @return mixed
*/
public function get_option( $key ) {
return get_user_option( $key, $this->ID );
}
/**
* Used to set per site or global user options.
*
* @param $key
* @param $value
* @param bool $global
*
* @return bool|int
*/
public function update_option( $key, $value, $global = false ) {
return update_user_option( $this->ID, $key, $value, $global );
}
/**
* Used to delete per site or global user options.
*
* @param $key
* @param bool $global
*
* @return bool
*/
public function delete_option( $key, $global = false ) {
return delete_user_option( $this->ID, $key, $global );
}
/**
* Get user meta.
*
* @param $key
* @param bool $single
*
* @return mixed
*/
public function get_meta( $key, $single = true ) {
return get_user_meta( $this->ID, $key, $single );
}
/**
* Add user meta.
*
* @param $key
* @param $value
*
* @return bool|int
*/
public function add_meta( $key, $value, $unique = false ) {
return add_user_meta( $this->ID, $key, $value, $unique );
}
/**
* Update user meta.
*
* @param $key
* @param $value
*
* @return bool|int
*/
public function update_meta( $key, $value ) {
return update_user_meta( $this->ID, $key, $value );
}
/**
* Delete user meta.
*
* @param $key
* @param $value
*
* @return bool|int
*/
public function delete_meta( $key, $value = '' ) {
return delete_user_meta( $this->ID, $key, $value );
}
/**
* @param int $size
*
* @return false|string
*/
public function get_avatar( $size = 35 ) {
return get_avatar( $this->ID, $size );
}
/**
* Convert object to array.
*
* @return array Object as array.
*/
public function to_array() {
$user = $this->user->to_array();
foreach ( get_object_vars( $this ) as $k => $v ) {
$user[ $k ] = $v;
}
return $user;
}
}