/**
* Core User Role & Capabilities API
*
* @package WordPress
* @subpackage Users
*/
/**
* Maps a capability to the primitive capabilities required of the given user to
* satisfy the capability being checked.
*
* This function also accepts an ID of an object to map against if the capability is a meta capability. Meta
* capabilities such as `edit_post` and `edit_user` are capabilities used by this function to map to primitive
* capabilities that a user or role requires, such as `edit_posts` and `edit_others_posts`.
*
* Example usage:
*
* map_meta_cap( 'edit_posts', $user->ID );
* map_meta_cap( 'edit_post', $user->ID, $post->ID );
* map_meta_cap( 'edit_post_meta', $user->ID, $post->ID, $meta_key );
*
* This function does not check whether the user has the required capabilities,
* it just returns what the required capabilities are.
*
* @since 2.0.0
* @since 4.9.6 Added the `export_others_personal_data`, `erase_others_personal_data`,
* and `manage_privacy_options` capabilities.
* @since 5.1.0 Added the `update_php` capability.
* @since 5.2.0 Added the `resume_plugin` and `resume_theme` capabilities.
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
* @since 5.7.0 Added the `create_app_password`, `list_app_passwords`, `read_app_password`,
* `edit_app_password`, `delete_app_passwords`, `delete_app_password`,
* and `update_https` capabilities.
*
* @global array $post_type_meta_caps Used to get post type meta capabilities.
*
* @param string $cap Capability being checked.
* @param int $user_id User ID.
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return string[] Primitive capabilities required of the user.
*/
function map_meta_cap( $cap, $user_id, ...$args ) {
$caps = array();
switch ( $cap ) {
case 'remove_user':
// In multisite the user must be a super admin to remove themselves.
if ( isset( $args[0] ) && $user_id == $args[0] && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'remove_users';
}
break;
case 'promote_user':
case 'add_users':
$caps[] = 'promote_users';
break;
case 'edit_user':
case 'edit_users':
// Allow user to edit themselves.
if ( 'edit_user' === $cap && isset( $args[0] ) && $user_id == $args[0] ) {
break;
}
// In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin.
if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'edit_users'; // edit_user maps to edit_users.
}
break;
case 'delete_post':
case 'delete_page':
if ( ! isset( $args[0] ) ) {
if ( 'delete_post' === $cap ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' );
} else {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific page.' );
}
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$post = get_post( $args[0] );
if ( ! $post ) {
$caps[] = 'do_not_allow';
break;
}
if ( 'revision' === $post->post_type ) {
$caps[] = 'do_not_allow';
break;
}
if ( ( get_option( 'page_for_posts' ) == $post->ID ) || ( get_option( 'page_on_front' ) == $post->ID ) ) {
$caps[] = 'manage_options';
break;
}
$post_type = get_post_type_object( $post->post_type );
if ( ! $post_type ) {
/* translators: 1: Post type, 2: Capability name. */
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
$message,
'' . $post->post_type . '',
'' . $cap . ''
),
'4.4.0'
);
$caps[] = 'edit_others_posts';
break;
}
if ( ! $post_type->map_meta_cap ) {
$caps[] = $post_type->cap->$cap;
// Prior to 3.1 we would re-call map_meta_cap here.
if ( 'delete_post' === $cap ) {
$cap = $post_type->cap->$cap;
}
break;
}
// If the post author is set and the user is the author...
if ( $post->post_author && $user_id == $post->post_author ) {
// If the post is published or scheduled...
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
$caps[] = $post_type->cap->delete_published_posts;
} elseif ( 'trash' === $post->post_status ) {
$status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
$caps[] = $post_type->cap->delete_published_posts;
} else {
$caps[] = $post_type->cap->delete_posts;
}
} else {
// If the post is draft...
$caps[] = $post_type->cap->delete_posts;
}
} else {
// The user is trying to edit someone else's post.
$caps[] = $post_type->cap->delete_others_posts;
// The post is published or scheduled, extra cap required.
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
$caps[] = $post_type->cap->delete_published_posts;
} elseif ( 'private' === $post->post_status ) {
$caps[] = $post_type->cap->delete_private_posts;
}
}
/*
* Setting the privacy policy page requires `manage_privacy_options`,
* so deleting it should require that too.
*/
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) {
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) );
}
break;
/*
* edit_post breaks down to edit_posts, edit_published_posts, or
* edit_others_posts.
*/
case 'edit_post':
case 'edit_page':
if ( ! isset( $args[0] ) ) {
if ( 'edit_post' === $cap ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' );
} else {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific page.' );
}
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$post = get_post( $args[0] );
if ( ! $post ) {
$caps[] = 'do_not_allow';
break;
}
if ( 'revision' === $post->post_type ) {
$post = get_post( $post->post_parent );
if ( ! $post ) {
$caps[] = 'do_not_allow';
break;
}
}
$post_type = get_post_type_object( $post->post_type );
if ( ! $post_type ) {
/* translators: 1: Post type, 2: Capability name. */
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
$message,
'' . $post->post_type . '',
'' . $cap . ''
),
'4.4.0'
);
$caps[] = 'edit_others_posts';
break;
}
if ( ! $post_type->map_meta_cap ) {
$caps[] = $post_type->cap->$cap;
// Prior to 3.1 we would re-call map_meta_cap here.
if ( 'edit_post' === $cap ) {
$cap = $post_type->cap->$cap;
}
break;
}
// If the post author is set and the user is the author...
if ( $post->post_author && $user_id == $post->post_author ) {
// If the post is published or scheduled...
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
$caps[] = $post_type->cap->edit_published_posts;
} elseif ( 'trash' === $post->post_status ) {
$status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
$caps[] = $post_type->cap->edit_published_posts;
} else {
$caps[] = $post_type->cap->edit_posts;
}
} else {
// If the post is draft...
$caps[] = $post_type->cap->edit_posts;
}
} else {
// The user is trying to edit someone else's post.
$caps[] = $post_type->cap->edit_others_posts;
// The post is published or scheduled, extra cap required.
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
$caps[] = $post_type->cap->edit_published_posts;
} elseif ( 'private' === $post->post_status ) {
$caps[] = $post_type->cap->edit_private_posts;
}
}
/*
* Setting the privacy policy page requires `manage_privacy_options`,
* so editing it should require that too.
*/
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) {
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) );
}
break;
case 'read_post':
case 'read_page':
if ( ! isset( $args[0] ) ) {
if ( 'read_post' === $cap ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' );
} else {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific page.' );
}
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$post = get_post( $args[0] );
if ( ! $post ) {
$caps[] = 'do_not_allow';
break;
}
if ( 'revision' === $post->post_type ) {
$post = get_post( $post->post_parent );
if ( ! $post ) {
$caps[] = 'do_not_allow';
break;
}
}
$post_type = get_post_type_object( $post->post_type );
if ( ! $post_type ) {
/* translators: 1: Post type, 2: Capability name. */
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
$message,
'' . $post->post_type . '',
'' . $cap . ''
),
'4.4.0'
);
$caps[] = 'edit_others_posts';
break;
}
if ( ! $post_type->map_meta_cap ) {
$caps[] = $post_type->cap->$cap;
// Prior to 3.1 we would re-call map_meta_cap here.
if ( 'read_post' === $cap ) {
$cap = $post_type->cap->$cap;
}
break;
}
$status_obj = get_post_status_object( get_post_status( $post ) );
if ( ! $status_obj ) {
/* translators: 1: Post status, 2: Capability name. */
$message = __( 'The post status %1$s is not registered, so it may not be reliable to check the capability %2$s against a post with that status.' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
$message,
'' . get_post_status( $post ) . '',
'' . $cap . ''
),
'5.4.0'
);
$caps[] = 'edit_others_posts';
break;
}
if ( $status_obj->public ) {
$caps[] = $post_type->cap->read;
break;
}
if ( $post->post_author && $user_id == $post->post_author ) {
$caps[] = $post_type->cap->read;
} elseif ( $status_obj->private ) {
$caps[] = $post_type->cap->read_private_posts;
} else {
$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
}
break;
case 'publish_post':
if ( ! isset( $args[0] ) ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' );
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$post = get_post( $args[0] );
if ( ! $post ) {
$caps[] = 'do_not_allow';
break;
}
$post_type = get_post_type_object( $post->post_type );
if ( ! $post_type ) {
/* translators: 1: Post type, 2: Capability name. */
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
$message,
'' . $post->post_type . '',
'' . $cap . ''
),
'4.4.0'
);
$caps[] = 'edit_others_posts';
break;
}
$caps[] = $post_type->cap->publish_posts;
break;
case 'edit_post_meta':
case 'delete_post_meta':
case 'add_post_meta':
case 'edit_comment_meta':
case 'delete_comment_meta':
case 'add_comment_meta':
case 'edit_term_meta':
case 'delete_term_meta':
case 'add_term_meta':
case 'edit_user_meta':
case 'delete_user_meta':
case 'add_user_meta':
$object_type = explode( '_', $cap )[1];
if ( ! isset( $args[0] ) ) {
if ( 'post' === $object_type ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' );
} elseif ( 'comment' === $object_type ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific comment.' );
} elseif ( 'term' === $object_type ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific term.' );
} else {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific user.' );
}
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$object_id = (int) $args[0];
$object_subtype = get_object_subtype( $object_type, $object_id );
if ( empty( $object_subtype ) ) {
$caps[] = 'do_not_allow';
break;
}
$caps = map_meta_cap( "edit_{$object_type}", $user_id, $object_id );
$meta_key = isset( $args[1] ) ? $args[1] : false;
if ( $meta_key ) {
$allowed = ! is_protected_meta( $meta_key, $object_type );
if ( ! empty( $object_subtype ) && has_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
/**
* Filters whether the user is allowed to edit a specific meta key of a specific object type and subtype.
*
* The dynamic portions of the hook name, `$object_type`, `$meta_key`,
* and `$object_subtype`, refer to the metadata object type (comment, post, term or user),
* the meta key value, and the object subtype respectively.
*
* @since 4.9.8
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
} else {
/**
* Filters whether the user is allowed to edit a specific meta key of a specific object type.
*
* Return true to have the mapped meta caps from `edit_{$object_type}` apply.
*
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
*
* @since 3.3.0 As `auth_post_meta_{$meta_key}`.
* @since 4.6.0
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
}
if ( ! empty( $object_subtype ) ) {
/**
* Filters whether the user is allowed to edit meta for specific object types/subtypes.
*
* Return true to have the mapped meta caps from `edit_{$object_type}` apply.
*
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
* The dynamic portion of the hook name, `$object_subtype` refers to the object subtype being filtered.
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
*
* @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`.
* @since 4.7.0 Renamed from `auth_post_{$post_type}_meta_{$meta_key}` to
* `auth_{$object_type}_{$object_subtype}_meta_{$meta_key}`.
* @deprecated 4.9.8 Use {@see 'auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}'} instead.
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters_deprecated(
"auth_{$object_type}_{$object_subtype}_meta_{$meta_key}",
array( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ),
'4.9.8',
"auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}"
);
}
if ( ! $allowed ) {
$caps[] = $cap;
}
}
break;
case 'edit_comment':
if ( ! isset( $args[0] ) ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific comment.' );
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$comment = get_comment( $args[0] );
if ( ! $comment ) {
$caps[] = 'do_not_allow';
break;
}
$post = get_post( $comment->comment_post_ID );
/*
* If the post doesn't exist, we have an orphaned comment.
* Fall back to the edit_posts capability, instead.
*/
if ( $post ) {
$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
} else {
$caps = map_meta_cap( 'edit_posts', $user_id );
}
break;
case 'unfiltered_upload':
if ( defined( 'ALLOW_UNFILTERED_UPLOADS' ) && ALLOW_UNFILTERED_UPLOADS && ( ! is_multisite() || is_super_admin( $user_id ) ) ) {
$caps[] = $cap;
} else {
$caps[] = 'do_not_allow';
}
break;
case 'edit_css':
case 'unfiltered_html':
// Disallow unfiltered_html for all users, even admins and super admins.
if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) {
$caps[] = 'do_not_allow';
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'unfiltered_html';
}
break;
case 'edit_files':
case 'edit_plugins':
case 'edit_themes':
// Disallow the file editors.
if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) {
$caps[] = 'do_not_allow';
} elseif ( ! wp_is_file_mod_allowed( 'capability_edit_themes' ) ) {
$caps[] = 'do_not_allow';
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = $cap;
}
break;
case 'update_plugins':
case 'delete_plugins':
case 'install_plugins':
case 'upload_plugins':
case 'update_themes':
case 'delete_themes':
case 'install_themes':
case 'upload_themes':
case 'update_core':
/*
* Disallow anything that creates, deletes, or updates core, plugin, or theme files.
* Files in uploads are excepted.
*/
if ( ! wp_is_file_mod_allowed( 'capability_update_core' ) ) {
$caps[] = 'do_not_allow';
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} elseif ( 'upload_themes' === $cap ) {
$caps[] = 'install_themes';
} elseif ( 'upload_plugins' === $cap ) {
$caps[] = 'install_plugins';
} else {
$caps[] = $cap;
}
break;
case 'install_languages':
case 'update_languages':
if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) {
$caps[] = 'do_not_allow';
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'install_languages';
}
break;
case 'activate_plugins':
case 'deactivate_plugins':
case 'activate_plugin':
case 'deactivate_plugin':
$caps[] = 'activate_plugins';
if ( is_multisite() ) {
// update_, install_, and delete_ are handled above with is_super_admin().
$menu_perms = get_site_option( 'menu_items', array() );
if ( empty( $menu_perms['plugins'] ) ) {
$caps[] = 'manage_network_plugins';
}
}
break;
case 'resume_plugin':
$caps[] = 'resume_plugins';
break;
case 'resume_theme':
$caps[] = 'resume_themes';
break;
case 'delete_user':
case 'delete_users':
// If multisite only super admins can delete users.
if ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'delete_users'; // delete_user maps to delete_users.
}
break;
case 'create_users':
if ( ! is_multisite() ) {
$caps[] = $cap;
} elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) ) {
$caps[] = $cap;
} else {
$caps[] = 'do_not_allow';
}
break;
case 'manage_links':
if ( get_option( 'link_manager_enabled' ) ) {
$caps[] = $cap;
} else {
$caps[] = 'do_not_allow';
}
break;
case 'customize':
$caps[] = 'edit_theme_options';
break;
case 'delete_site':
if ( is_multisite() ) {
$caps[] = 'manage_options';
} else {
$caps[] = 'do_not_allow';
}
break;
case 'edit_term':
case 'delete_term':
case 'assign_term':
if ( ! isset( $args[0] ) ) {
/* translators: %s: Capability name. */
$message = __( 'When checking for the %s capability, you must always check it against a specific term.' );
_doing_it_wrong(
__FUNCTION__,
sprintf( $message, '' . $cap . '' ),
'6.1.0'
);
$caps[] = 'do_not_allow';
break;
}
$term_id = (int) $args[0];
$term = get_term( $term_id );
if ( ! $term || is_wp_error( $term ) ) {
$caps[] = 'do_not_allow';
break;
}
$tax = get_taxonomy( $term->taxonomy );
if ( ! $tax ) {
$caps[] = 'do_not_allow';
break;
}
if ( 'delete_term' === $cap
&& ( get_option( 'default_' . $term->taxonomy ) == $term->term_id
|| get_option( 'default_term_' . $term->taxonomy ) == $term->term_id )
) {
$caps[] = 'do_not_allow';
break;
}
$taxo_cap = $cap . 's';
$caps = map_meta_cap( $tax->cap->$taxo_cap, $user_id, $term_id );
break;
case 'manage_post_tags':
case 'edit_categories':
case 'edit_post_tags':
case 'delete_categories':
case 'delete_post_tags':
$caps[] = 'manage_categories';
break;
case 'assign_categories':
case 'assign_post_tags':
$caps[] = 'edit_posts';
break;
case 'create_sites':
case 'delete_sites':
case 'manage_network':
case 'manage_sites':
case 'manage_network_users':
case 'manage_network_plugins':
case 'manage_network_themes':
case 'manage_network_options':
case 'upgrade_network':
$caps[] = $cap;
break;
case 'setup_network':
if ( is_multisite() ) {
$caps[] = 'manage_network_options';
} else {
$caps[] = 'manage_options';
}
break;
case 'update_php':
if ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'update_core';
}
break;
case 'update_https':
if ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'manage_options';
$caps[] = 'update_core';
}
break;
case 'export_others_personal_data':
case 'erase_others_personal_data':
case 'manage_privacy_options':
$caps[] = is_multisite() ? 'manage_network' : 'manage_options';
break;
case 'create_app_password':
case 'list_app_passwords':
case 'read_app_password':
case 'edit_app_password':
case 'delete_app_passwords':
case 'delete_app_password':
$caps = map_meta_cap( 'edit_user', $user_id, $args[0] );
break;
default:
// Handle meta capabilities for custom post types.
global $post_type_meta_caps;
if ( isset( $post_type_meta_caps[ $cap ] ) ) {
return map_meta_cap( $post_type_meta_caps[ $cap ], $user_id, ...$args );
}
// Block capabilities map to their post equivalent.
$block_caps = array(
'edit_blocks',
'edit_others_blocks',
'publish_blocks',
'read_private_blocks',
'delete_blocks',
'delete_private_blocks',
'delete_published_blocks',
'delete_others_blocks',
'edit_private_blocks',
'edit_published_blocks',
);
if ( in_array( $cap, $block_caps, true ) ) {
$cap = str_replace( '_blocks', '_posts', $cap );
}
// If no meta caps match, return the original cap.
$caps[] = $cap;
}
/**
* Filters the primitive capabilities required of the given user to satisfy the
* capability being checked.
*
* @since 2.8.0
*
* @param string[] $caps Primitive capabilities required of the user.
* @param string $cap Capability being checked.
* @param int $user_id The user ID.
* @param array $args Adds context to the capability check, typically
* starting with an object ID.
*/
return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
}
/**
* Returns whether the current user has the specified capability.
*
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
* capabilities such as `edit_post` and `edit_user` are capabilities used 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`.
*
* Example usage:
*
* current_user_can( 'edit_posts' );
* current_user_can( 'edit_post', $post->ID );
* current_user_can( 'edit_post_meta', $post->ID, $meta_key );
*
* While checking against particular roles in place of a capability is supported
* in part, this practice is discouraged as it may produce unreliable results.
*
* Note: Will always return true if the current user is a super admin, unless specifically denied.
*
* @since 2.0.0
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
* @since 5.8.0 Converted to wrapper for the user_can() function.
*
* @see WP_User::has_cap()
* @see map_meta_cap()
*
* @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
* passed, whether the current user has the given meta capability for the given object.
*/
function current_user_can( $capability, ...$args ) {
return user_can( wp_get_current_user(), $capability, ...$args );
}
/**
* Returns whether the current user has the specified capability for a given site.
*
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
* capabilities such as `edit_post` and `edit_user` are capabilities used 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`.
*
* Example usage:
*
* current_user_can_for_blog( $blog_id, 'edit_posts' );
* current_user_can_for_blog( $blog_id, 'edit_post', $post->ID );
* current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key );
*
* @since 3.0.0
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
* @since 5.8.0 Wraps current_user_can() after switching to blog.
*
* @param int $blog_id Site ID.
* @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability.
*/
function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
$can = current_user_can( $capability, ...$args );
if ( $switched ) {
restore_current_blog();
}
return $can;
}
/**
* Returns whether the author of the supplied post has the specified capability.
*
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
* capabilities such as `edit_post` and `edit_user` are capabilities used 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`.
*
* Example usage:
*
* author_can( $post, 'edit_posts' );
* author_can( $post, 'edit_post', $post->ID );
* author_can( $post, 'edit_post_meta', $post->ID, $meta_key );
*
* @since 2.9.0
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
*
* @param int|WP_Post $post Post ID or post object.
* @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the post author has the given capability.
*/
function author_can( $post, $capability, ...$args ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$author = get_userdata( $post->post_author );
if ( ! $author ) {
return false;
}
return $author->has_cap( $capability, ...$args );
}
/**
* Returns whether a particular user has the specified capability.
*
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
* capabilities such as `edit_post` and `edit_user` are capabilities used 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`.
*
* Example usage:
*
* user_can( $user->ID, 'edit_posts' );
* user_can( $user->ID, 'edit_post', $post->ID );
* user_can( $user->ID, 'edit_post_meta', $post->ID, $meta_key );
*
* @since 3.1.0
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
*
* @param int|WP_User $user User ID or object.
* @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability.
*/
function user_can( $user, $capability, ...$args ) {
if ( ! is_object( $user ) ) {
$user = get_userdata( $user );
}
if ( empty( $user ) ) {
// User is logged out, create anonymous user object.
$user = new WP_User( 0 );
$user->init( new stdClass() );
}
return $user->has_cap( $capability, ...$args );
}
/**
* Retrieves the global WP_Roles instance and instantiates it if necessary.
*
* @since 4.3.0
*
* @global WP_Roles $wp_roles WordPress role management object.
*
* @return WP_Roles WP_Roles global instance if not already instantiated.
*/
function wp_roles() {
global $wp_roles;
if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
return $wp_roles;
}
/**
* Retrieves role object.
*
* @since 2.0.0
*
* @param string $role Role name.
* @return WP_Role|null WP_Role object if found, null if the role does not exist.
*/
function get_role( $role ) {
return wp_roles()->get_role( $role );
}
/**
* Adds a role, if it does not exist.
*
* @since 2.0.0
*
* @param string $role Role name.
* @param string $display_name Display name for role.
* @param bool[] $capabilities List of capabilities keyed by the capability name,
* e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
* @return WP_Role|void WP_Role object, if the role is added.
*/
function add_role( $role, $display_name, $capabilities = array() ) {
if ( empty( $role ) ) {
return;
}
return wp_roles()->add_role( $role, $display_name, $capabilities );
}
/**
* Removes a role, if it exists.
*
* @since 2.0.0
*
* @param string $role Role name.
*/
function remove_role( $role ) {
wp_roles()->remove_role( $role );
}
/**
* Retrieves a list of super admins.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @return string[] List of super admin logins.
*/
function get_super_admins() {
global $super_admins;
if ( isset( $super_admins ) ) {
return $super_admins;
} else {
return get_site_option( 'site_admins', array( 'admin' ) );
}
}
/**
* Determines whether user is a site admin.
*
* @since 3.0.0
*
* @param int|false $user_id Optional. The ID of a user. Defaults to false, to check the current user.
* @return bool Whether the user is a site admin.
*/
function is_super_admin( $user_id = false ) {
if ( ! $user_id ) {
$user = wp_get_current_user();
} else {
$user = get_userdata( $user_id );
}
if ( ! $user || ! $user->exists() ) {
return false;
}
if ( is_multisite() ) {
$super_admins = get_super_admins();
if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins, true ) ) {
return true;
}
} else {
if ( $user->has_cap( 'delete_users' ) ) {
return true;
}
}
return false;
}
/**
* Grants Super Admin privileges.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @param int $user_id ID of the user to be granted Super Admin privileges.
* @return bool True on success, false on failure. This can fail when the user is
* already a super admin or when the `$super_admins` global is defined.
*/
function grant_super_admin( $user_id ) {
// If global super_admins override is defined, there is nothing to do here.
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
return false;
}
/**
* Fires before the user is granted Super Admin privileges.
*
* @since 3.0.0
*
* @param int $user_id ID of the user that is about to be granted Super Admin privileges.
*/
do_action( 'grant_super_admin', $user_id );
// Directly fetch site_admins instead of using get_super_admins().
$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
$user = get_userdata( $user_id );
if ( $user && ! in_array( $user->user_login, $super_admins, true ) ) {
$super_admins[] = $user->user_login;
update_site_option( 'site_admins', $super_admins );
/**
* Fires after the user is granted Super Admin privileges.
*
* @since 3.0.0
*
* @param int $user_id ID of the user that was granted Super Admin privileges.
*/
do_action( 'granted_super_admin', $user_id );
return true;
}
return false;
}
/**
* Revokes Super Admin privileges.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @param int $user_id ID of the user Super Admin privileges to be revoked from.
* @return bool True on success, false on failure. This can fail when the user's email
* is the network admin email or when the `$super_admins` global is defined.
*/
function revoke_super_admin( $user_id ) {
// If global super_admins override is defined, there is nothing to do here.
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
return false;
}
/**
* Fires before the user's Super Admin privileges are revoked.
*
* @since 3.0.0
*
* @param int $user_id ID of the user Super Admin privileges are being revoked from.
*/
do_action( 'revoke_super_admin', $user_id );
// Directly fetch site_admins instead of using get_super_admins().
$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
$user = get_userdata( $user_id );
if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
$key = array_search( $user->user_login, $super_admins, true );
if ( false !== $key ) {
unset( $super_admins[ $key ] );
update_site_option( 'site_admins', $super_admins );
/**
* Fires after the user's Super Admin privileges are revoked.
*
* @since 3.0.0
*
* @param int $user_id ID of the user Super Admin privileges were revoked from.
*/
do_action( 'revoked_super_admin', $user_id );
return true;
}
}
return false;
}
/**
* Filters the user capabilities to grant the 'install_languages' capability as necessary.
*
* A user must have at least one out of the 'update_core', 'install_plugins', and
* 'install_themes' capabilities to qualify for 'install_languages'.
*
* @since 4.9.0
*
* @param bool[] $allcaps An array of all the user's capabilities.
* @return bool[] Filtered array of the user's capabilities.
*/
function wp_maybe_grant_install_languages_cap( $allcaps ) {
if ( ! empty( $allcaps['update_core'] ) || ! empty( $allcaps['install_plugins'] ) || ! empty( $allcaps['install_themes'] ) ) {
$allcaps['install_languages'] = true;
}
return $allcaps;
}
/**
* Filters the user capabilities to grant the 'resume_plugins' and 'resume_themes' capabilities as necessary.
*
* @since 5.2.0
*
* @param bool[] $allcaps An array of all the user's capabilities.
* @return bool[] Filtered array of the user's capabilities.
*/
function wp_maybe_grant_resume_extensions_caps( $allcaps ) {
// Even in a multisite, regular administrators should be able to resume plugins.
if ( ! empty( $allcaps['activate_plugins'] ) ) {
$allcaps['resume_plugins'] = true;
}
// Even in a multisite, regular administrators should be able to resume themes.
if ( ! empty( $allcaps['switch_themes'] ) ) {
$allcaps['resume_themes'] = true;
}
return $allcaps;
}
/**
* Filters the user capabilities to grant the 'view_site_health_checks' capabilities as necessary.
*
* @since 5.2.2
*
* @param bool[] $allcaps An array of all the user's capabilities.
* @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.
* @return bool[] Filtered array of the user's capabilities.
*/
function wp_maybe_grant_site_health_caps( $allcaps, $caps, $args, $user ) {
if ( ! empty( $allcaps['install_plugins'] ) && ( ! is_multisite() || is_super_admin( $user->ID ) ) ) {
$allcaps['view_site_health_checks'] = true;
}
return $allcaps;
}
return;
// Dummy gettext calls to get strings in the catalog.
/* translators: User role for administrators. */
_x( 'Administrator', 'User role' );
/* translators: User role for editors. */
_x( 'Editor', 'User role' );
/* translators: User role for authors. */
_x( 'Author', 'User role' );
/* translators: User role for contributors. */
_x( 'Contributor', 'User role' );
/* translators: User role for subscribers. */
_x( 'Subscriber', 'User role' );
/**
* APIs to interact with global settings & styles.
*
* @package WordPress
*/
/**
* Gets the settings resulting of merging core, theme, and user data.
*
* @since 5.9.0
*
* @param array $path Path to the specific setting to retrieve. Optional.
* If empty, will return all settings.
* @param array $context {
* Metadata to know where to retrieve the $path from. Optional.
*
* @type string $block_name Which block to retrieve the settings from.
* If empty, it'll return the settings for the global context.
* @type string $origin Which origin to take data from.
* Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
* If empty or unknown, 'all' is used.
* }
* @return mixed The settings array or individual setting value to retrieve.
*/
function wp_get_global_settings( $path = array(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$new_path = array( 'blocks', $context['block_name'] );
foreach ( $path as $subpath ) {
$new_path[] = $subpath;
}
$path = $new_path;
}
/*
* This is the default value when no origin is provided or when it is 'all'.
*
* The $origin is used as part of the cache key. Changes here need to account
* for clearing the cache appropriately.
*/
$origin = 'custom';
if (
! wp_theme_has_theme_json() ||
( isset( $context['origin'] ) && 'base' === $context['origin'] )
) {
$origin = 'theme';
}
/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* See some of the existing hooks to modify theme.json behavior:
* https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* See https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_settings_' . $origin;
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = ! wp_is_development_mode( 'theme' );
$settings = false;
if ( $can_use_cached ) {
$settings = wp_cache_get( $cache_key, $cache_group );
}
if ( false === $settings ) {
$settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $settings, $cache_group );
}
}
return _wp_array_get( $settings, $path, $settings );
}
/**
* Gets the styles resulting of merging core, theme, and user data.
*
* @since 5.9.0
* @since 6.3.0 the internal link format "var:preset|color|secondary" is resolved
* to "var(--wp--preset--font-size--small)" so consumers don't have to.
* @since 6.3.0 `transforms` is now usable in the `context` parameter. In case [`transforms`]['resolve_variables']
* is defined, variables are resolved to their value in the styles.
*
* @param array $path Path to the specific style to retrieve. Optional.
* If empty, will return all styles.
* @param array $context {
* Metadata to know where to retrieve the $path from. Optional.
*
* @type string $block_name Which block to retrieve the styles from.
* If empty, it'll return the styles for the global context.
* @type string $origin Which origin to take data from.
* Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
* If empty or unknown, 'all' is used.
* @type array $transforms Which transformation(s) to apply.
* Valid value is array( 'resolve-variables' ).
* If defined, variables are resolved to their value in the styles.
* }
* @return mixed The styles array or individual style value to retrieve.
*/
function wp_get_global_styles( $path = array(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}
$origin = 'custom';
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
$origin = 'theme';
}
$resolve_variables = isset( $context['transforms'] )
&& is_array( $context['transforms'] )
&& in_array( 'resolve-variables', $context['transforms'], true );
$merged_data = WP_Theme_JSON_Resolver::get_merged_data( $origin );
if ( $resolve_variables ) {
$merged_data = WP_Theme_JSON::resolve_variables( $merged_data );
}
$styles = $merged_data->get_raw_data()['styles'];
return _wp_array_get( $styles, $path, $styles );
}
/**
* Returns the stylesheet resulting of merging core, theme, and user data.
*
* @since 5.9.0
* @since 6.1.0 Added 'base-layout-styles' support.
*
* @param array $types Optional. Types of styles to load.
* It accepts as values 'variables', 'presets', 'styles', 'base-layout-styles'.
* If empty, it'll load the following:
* - for themes without theme.json: 'variables', 'presets', 'base-layout-styles'.
* - for themes with theme.json: 'variables', 'presets', 'styles'.
* @return string Stylesheet.
*/
function wp_get_global_stylesheet( $types = array() ) {
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = empty( $types ) && ! wp_is_development_mode( 'theme' );
/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* @see `wp_cache_add_non_persistent_groups()`.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* See some of the existing hooks to modify theme.json behavior:
* @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* @see https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_stylesheet';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$supports_theme_json = wp_theme_has_theme_json();
if ( empty( $types ) && ! $supports_theme_json ) {
$types = array( 'variables', 'presets', 'base-layout-styles' );
} elseif ( empty( $types ) ) {
$types = array( 'variables', 'styles', 'presets' );
}
/*
* If variables are part of the stylesheet, then add them.
* This is so themes without a theme.json still work as before 5.9:
* they can override the default presets.
* See https://core.trac.wordpress.org/ticket/54782
*/
$styles_variables = '';
if ( in_array( 'variables', $types, true ) ) {
/*
* Only use the default, theme, and custom origins. Why?
* Because styles for `blocks` origin are added at a later phase
* (i.e. in the render cycle). Here, only the ones in use are rendered.
* @see wp_add_global_styles_for_blocks
*/
$origins = array( 'default', 'theme', 'custom' );
$styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins );
$types = array_diff( $types, array( 'variables' ) );
}
/*
* For the remaining types (presets, styles), we do consider origins:
*
* - themes without theme.json: only the classes for the presets defined by core
* - themes with theme.json: the presets and styles classes, both from core and the theme
*/
$styles_rest = '';
if ( ! empty( $types ) ) {
/*
* Only use the default, theme, and custom origins. Why?
* Because styles for `blocks` origin are added at a later phase
* (i.e. in the render cycle). Here, only the ones in use are rendered.
* @see wp_add_global_styles_for_blocks
*/
$origins = array( 'default', 'theme', 'custom' );
/*
* If the theme doesn't have theme.json but supports both appearance tools and color palette,
* the 'theme' origin should be included so color palette presets are also output.
*/
if ( ! $supports_theme_json && ( current_theme_supports( 'appearance-tools' ) || current_theme_supports( 'border' ) ) && current_theme_supports( 'editor-color-palette' ) ) {
$origins = array( 'default', 'theme' );
} elseif ( ! $supports_theme_json ) {
$origins = array( 'default' );
}
$styles_rest = $tree->get_stylesheet( $types, $origins );
}
$stylesheet = $styles_variables . $styles_rest;
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $stylesheet, $cache_group );
}
return $stylesheet;
}
/**
* Gets the global styles custom CSS from theme.json.
*
* @since 6.2.0
*
* @return string The global styles custom CSS.
*/
function wp_get_global_styles_custom_css() {
if ( ! wp_theme_has_theme_json() ) {
return '';
}
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = ! wp_is_development_mode( 'theme' );
/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* @see `wp_cache_add_non_persistent_groups()`.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* See some of the existing hooks to modify theme.json behavior:
* @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* @see https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_key = 'wp_get_global_styles_custom_css';
$cache_group = 'theme_json';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$stylesheet = $tree->get_custom_css();
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $stylesheet, $cache_group );
}
return $stylesheet;
}
/**
* Adds global style rules to the inline style for each block.
*
* @since 6.1.0
*
* @global WP_Styles $wp_styles
*/
function wp_add_global_styles_for_blocks() {
global $wp_styles;
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$block_nodes = $tree->get_styles_block_nodes();
foreach ( $block_nodes as $metadata ) {
$block_css = $tree->get_styles_for_block( $metadata );
if ( ! wp_should_load_separate_core_block_assets() ) {
wp_add_inline_style( 'global-styles', $block_css );
continue;
}
$stylesheet_handle = 'global-styles';
/*
* When `wp_should_load_separate_core_block_assets()` is true, block styles are
* enqueued for each block on the page in class WP_Block's render function.
* This means there will be a handle in the styles queue for each of those blocks.
* Block-specific global styles should be attached to the global-styles handle, but
* only for blocks on the page, thus we check if the block's handle is in the queue
* before adding the inline style.
* This conditional loading only applies to core blocks.
*/
if ( isset( $metadata['name'] ) ) {
if ( str_starts_with( $metadata['name'], 'core/' ) ) {
$block_name = str_replace( 'core/', '', $metadata['name'] );
$block_handle = 'wp-block-' . $block_name;
if ( in_array( $block_handle, $wp_styles->queue ) ) {
wp_add_inline_style( $stylesheet_handle, $block_css );
}
} else {
wp_add_inline_style( $stylesheet_handle, $block_css );
}
}
// The likes of block element styles from theme.json do not have $metadata['name'] set.
if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
$block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] );
if ( $block_name ) {
if ( str_starts_with( $block_name, 'core/' ) ) {
$block_name = str_replace( 'core/', '', $block_name );
$block_handle = 'wp-block-' . $block_name;
if ( in_array( $block_handle, $wp_styles->queue ) ) {
wp_add_inline_style( $stylesheet_handle, $block_css );
}
} else {
wp_add_inline_style( $stylesheet_handle, $block_css );
}
}
}
}
}
/**
* Gets the block name from a given theme.json path.
*
* @since 6.3.0
* @access private
*
* @param array $path An array of keys describing the path to a property in theme.json.
* @return string Identified block name, or empty string if none found.
*/
function wp_get_block_name_from_theme_json_path( $path ) {
// Block name is expected to be the third item after 'styles' and 'blocks'.
if (
count( $path ) >= 3
&& 'styles' === $path[0]
&& 'blocks' === $path[1]
&& str_contains( $path[2], '/' )
) {
return $path[2];
}
/*
* As fallback and for backward compatibility, allow any core block to be
* at any position.
*/
$result = array_values(
array_filter(
$path,
static function ( $item ) {
if ( str_contains( $item, 'core/' ) ) {
return true;
}
return false;
}
)
);
if ( isset( $result[0] ) ) {
return $result[0];
}
return '';
}
/**
* Checks whether a theme or its parent has a theme.json file.
*
* @since 6.2.0
*
* @return bool Returns true if theme or its parent has a theme.json file, false otherwise.
*/
function wp_theme_has_theme_json() {
static $theme_has_support = array();
$stylesheet = get_stylesheet();
if (
isset( $theme_has_support[ $stylesheet ] ) &&
/*
* Ignore static cache when the development mode is set to 'theme', to avoid interfering with
* the theme developer's workflow.
*/
! wp_is_development_mode( 'theme' )
) {
return $theme_has_support[ $stylesheet ];
}
$stylesheet_directory = get_stylesheet_directory();
$template_directory = get_template_directory();
// This is the same as get_theme_file_path(), which isn't available in load-styles.php context
if ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/theme.json' ) ) {
$path = $stylesheet_directory . '/theme.json';
} else {
$path = $template_directory . '/theme.json';
}
/** This filter is documented in wp-includes/link-template.php */
$path = apply_filters( 'theme_file_path', $path, 'theme.json' );
$theme_has_support[ $stylesheet ] = file_exists( $path );
return $theme_has_support[ $stylesheet ];
}
/**
* Cleans the caches under the theme_json group.
*
* @since 6.2.0
*/
function wp_clean_theme_json_cache() {
wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' );
wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' );
wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' );
wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' );
wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' );
WP_Theme_JSON_Resolver::clean_cached_data();
}
/**
* Returns the current theme's wanted patterns (slugs) to be
* registered from Pattern Directory.
*
* @since 6.3.0
*
* @return string[]
*/
function wp_get_theme_directory_pattern_slugs() {
return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_patterns();
}
/**
* Returns the metadata for the custom templates defined by the theme via theme.json.
*
* @since 6.4.0
*
* @return array Associative array of `$template_name => $template_data` pairs,
* with `$template_data` having "title" and "postTypes" fields.
*/
function wp_get_theme_data_custom_templates() {
return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_custom_templates();
}
/**
* Returns the metadata for the template parts defined by the theme.
*
* @since 6.4.0
*
* @return array Associative array of `$part_name => $part_data` pairs,
* with `$part_data` having "title" and "area" fields.
*/
function wp_get_theme_data_template_parts() {
$cache_group = 'theme_json';
$cache_key = 'wp_get_theme_data_template_parts';
$can_use_cached = ! wp_is_development_mode( 'theme' );
$metadata = false;
if ( $can_use_cached ) {
$metadata = wp_cache_get( $cache_key, $cache_group );
if ( false !== $metadata ) {
return $metadata;
}
}
if ( false === $metadata ) {
$metadata = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts();
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $metadata, $cache_group );
}
}
return $metadata;
}
/**
* Determines the CSS selector for the block type and property provided,
* returning it if available.
*
* @since 6.3.0
*
* @param WP_Block_Type $block_type The block's type.
* @param string|array $target The desired selector's target, `root` or array path.
* @param boolean $fallback Whether to fall back to broader selector.
*
* @return string|null CSS selector or `null` if no selector available.
*/
function wp_get_block_css_selector( $block_type, $target = 'root', $fallback = false ) {
if ( empty( $target ) ) {
return null;
}
$has_selectors = ! empty( $block_type->selectors );
// Root Selector.
// Calculated before returning as it can be used as fallback for
// feature selectors later on.
$root_selector = null;
if ( $has_selectors && isset( $block_type->selectors['root'] ) ) {
// Use the selectors API if available.
$root_selector = $block_type->selectors['root'];
} elseif ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) {
// Use the old experimental selector supports property if set.
$root_selector = $block_type->supports['__experimentalSelector'];
} else {
// If no root selector found, generate default block class selector.
$block_name = str_replace( '/', '-', str_replace( 'core/', '', $block_type->name ) );
$root_selector = ".wp-block-{$block_name}";
}
// Return selector if it's the root target we are looking for.
if ( 'root' === $target ) {
return $root_selector;
}
// If target is not `root` we have a feature or subfeature as the target.
// If the target is a string convert to an array.
if ( is_string( $target ) ) {
$target = explode( '.', $target );
}
// Feature Selectors ( May fallback to root selector ).
if ( 1 === count( $target ) ) {
$fallback_selector = $fallback ? $root_selector : null;
// Prefer the selectors API if available.
if ( $has_selectors ) {
// Look for selector under `feature.root`.
$path = array( current( $target ), 'root' );
$feature_selector = _wp_array_get( $block_type->selectors, $path, null );
if ( $feature_selector ) {
return $feature_selector;
}
// Check if feature selector is set via shorthand.
$feature_selector = _wp_array_get( $block_type->selectors, $target, null );
return is_string( $feature_selector ) ? $feature_selector : $fallback_selector;
}
// Try getting old experimental supports selector value.
$path = array( current( $target ), '__experimentalSelector' );
$feature_selector = _wp_array_get( $block_type->supports, $path, null );
// Nothing to work with, provide fallback or null.
if ( null === $feature_selector ) {
return $fallback_selector;
}
// Scope the feature selector by the block's root selector.
return WP_Theme_JSON::scope_selector( $root_selector, $feature_selector );
}
// Subfeature selector
// This may fallback either to parent feature or root selector.
$subfeature_selector = null;
// Use selectors API if available.
if ( $has_selectors ) {
$subfeature_selector = _wp_array_get( $block_type->selectors, $target, null );
}
// Only return if we have a subfeature selector.
if ( $subfeature_selector ) {
return $subfeature_selector;
}
// To this point we don't have a subfeature selector. If a fallback
// has been requested, remove subfeature from target path and return
// results of a call for the parent feature's selector.
if ( $fallback ) {
return wp_get_block_css_selector( $block_type, $target[0], $fallback );
}
return null;
}
/**
* Dependencies API: WP_Styles class
*
* @since 2.6.0
*
* @package WordPress
* @subpackage Dependencies
*/
/**
* Core class used to register styles.
*
* @since 2.6.0
*
* @see WP_Dependencies
*/
class WP_Styles extends WP_Dependencies {
/**
* Base URL for styles.
*
* Full URL with trailing slash.
*
* @since 2.6.0
* @var string
*/
public $base_url;
/**
* URL of the content directory.
*
* @since 2.8.0
* @var string
*/
public $content_url;
/**
* Default version string for stylesheets.
*
* @since 2.6.0
* @var string
*/
public $default_version;
/**
* The current text direction.
*
* @since 2.6.0
* @var string
*/
public $text_direction = 'ltr';
/**
* Holds a list of style handles which will be concatenated.
*
* @since 2.8.0
* @var string
*/
public $concat = '';
/**
* Holds a string which contains style handles and their version.
*
* @since 2.8.0
* @deprecated 3.4.0
* @var string
*/
public $concat_version = '';
/**
* Whether to perform concatenation.
*
* @since 2.8.0
* @var bool
*/
public $do_concat = false;
/**
* Holds HTML markup of styles and additional data if concatenation
* is enabled.
*
* @since 2.8.0
* @var string
*/
public $print_html = '';
/**
* Holds inline styles if concatenation is enabled.
*
* @since 3.3.0
* @var string
*/
public $print_code = '';
/**
* List of default directories.
*
* @since 2.8.0
* @var array
*/
public $default_dirs;
/**
* Holds a string which contains the type attribute for style tag.
*
* If the active theme does not declare HTML5 support for 'style',
* then it initializes as `type='text/css'`.
*
* @since 5.3.0
* @var string
*/
private $type_attr = '';
/**
* Constructor.
*
* @since 2.6.0
*/
public function __construct() {
if (
function_exists( 'is_admin' ) && ! is_admin()
&&
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'style' )
) {
$this->type_attr = " type='text/css'";
}
/**
* Fires when the WP_Styles instance is initialized.
*
* @since 2.6.0
*
* @param WP_Styles $wp_styles WP_Styles instance (passed by reference).
*/
do_action_ref_array( 'wp_default_styles', array( &$this ) );
}
/**
* Processes a style dependency.
*
* @since 2.6.0
* @since 5.5.0 Added the `$group` parameter.
*
* @see WP_Dependencies::do_item()
*
* @param string $handle The style's registered handle.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* Default false.
* @return bool True on success, false on failure.
*/
public function do_item( $handle, $group = false ) {
if ( ! parent::do_item( $handle ) ) {
return false;
}
$obj = $this->registered[ $handle ];
if ( null === $obj->ver ) {
$ver = '';
} else {
$ver = $obj->ver ? $obj->ver : $this->default_version;
}
if ( isset( $this->args[ $handle ] ) ) {
$ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ];
}
$src = $obj->src;
$cond_before = '';
$cond_after = '';
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
if ( $conditional ) {
$cond_before = "\n";
}
$inline_style = $this->print_inline_style( $handle, false );
if ( $inline_style ) {
$inline_style_tag = sprintf(
"\n",
esc_attr( $handle ),
$this->type_attr,
$inline_style
);
} else {
$inline_style_tag = '';
}
if ( $this->do_concat ) {
if ( $this->in_default_dir( $src ) && ! $conditional && ! isset( $obj->extra['alt'] ) ) {
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
$this->print_code .= $inline_style;
return true;
}
}
if ( isset( $obj->args ) ) {
$media = esc_attr( $obj->args );
} else {
$media = 'all';
}
// A single item may alias a set of items, by having dependencies, but no source.
if ( ! $src ) {
if ( $inline_style_tag ) {
if ( $this->do_concat ) {
$this->print_html .= $inline_style_tag;
} else {
echo $inline_style_tag;
}
}
return true;
}
$href = $this->_css_href( $src, $ver, $handle );
if ( ! $href ) {
return true;
}
$rel = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
$title = isset( $obj->extra['title'] ) ? sprintf( " title='%s'", esc_attr( $obj->extra['title'] ) ) : '';
$tag = sprintf(
"\n",
$rel,
$handle,
$title,
$href,
$this->type_attr,
$media
);
/**
* Filters the HTML link tag of an enqueued style.
*
* @since 2.6.0
* @since 4.3.0 Introduced the `$href` parameter.
* @since 4.5.0 Introduced the `$media` parameter.
*
* @param string $tag The link tag for the enqueued style.
* @param string $handle The style's registered handle.
* @param string $href The stylesheet's source URL.
* @param string $media The stylesheet's media attribute.
*/
$tag = apply_filters( 'style_loader_tag', $tag, $handle, $href, $media );
if ( 'rtl' === $this->text_direction && isset( $obj->extra['rtl'] ) && $obj->extra['rtl'] ) {
if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
$suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
$rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $src, $ver, "$handle-rtl" ) );
} else {
$rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
}
$rtl_tag = sprintf(
"\n",
$rel,
$handle,
$title,
$rtl_href,
$this->type_attr,
$media
);
/** This filter is documented in wp-includes/class-wp-styles.php */
$rtl_tag = apply_filters( 'style_loader_tag', $rtl_tag, $handle, $rtl_href, $media );
if ( 'replace' === $obj->extra['rtl'] ) {
$tag = $rtl_tag;
} else {
$tag .= $rtl_tag;
}
}
if ( $this->do_concat ) {
$this->print_html .= $cond_before;
$this->print_html .= $tag;
if ( $inline_style_tag ) {
$this->print_html .= $inline_style_tag;
}
$this->print_html .= $cond_after;
} else {
echo $cond_before;
echo $tag;
$this->print_inline_style( $handle );
echo $cond_after;
}
return true;
}
/**
* Adds extra CSS styles to a registered stylesheet.
*
* @since 3.3.0
*
* @param string $handle The style's registered handle.
* @param string $code String containing the CSS styles to be added.
* @return bool True on success, false on failure.
*/
public function add_inline_style( $handle, $code ) {
if ( ! $code ) {
return false;
}
$after = $this->get_data( $handle, 'after' );
if ( ! $after ) {
$after = array();
}
$after[] = $code;
return $this->add_data( $handle, 'after', $after );
}
/**
* Prints extra CSS styles of a registered stylesheet.
*
* @since 3.3.0
*
* @param string $handle The style's registered handle.
* @param bool $display Optional. Whether to print the inline style
* instead of just returning it. Default true.
* @return string|bool False if no data exists, inline styles if `$display` is true,
* true otherwise.
*/
public function print_inline_style( $handle, $display = true ) {
$output = $this->get_data( $handle, 'after' );
if ( empty( $output ) ) {
return false;
}
$output = implode( "\n", $output );
if ( ! $display ) {
return $output;
}
printf(
"\n",
esc_attr( $handle ),
$this->type_attr,
$output
);
return true;
}
/**
* Determines style dependencies.
*
* @since 2.6.0
*
* @see WP_Dependencies::all_deps()
*
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
* @param bool $recursion Optional. Internal flag that function is calling itself.
* Default false.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* Default false.
* @return bool True on success, false on failure.
*/
public function all_deps( $handles, $recursion = false, $group = false ) {
$r = parent::all_deps( $handles, $recursion, $group );
if ( ! $recursion ) {
/**
* Filters the array of enqueued styles before processing for output.
*
* @since 2.6.0
*
* @param string[] $to_do The list of enqueued style handles about to be processed.
*/
$this->to_do = apply_filters( 'print_styles_array', $this->to_do );
}
return $r;
}
/**
* Generates an enqueued style's fully-qualified URL.
*
* @since 2.6.0
*
* @param string $src The source of the enqueued style.
* @param string $ver The version of the enqueued style.
* @param string $handle The style's registered handle.
* @return string Style's fully-qualified URL.
*/
public function _css_href( $src, $ver, $handle ) {
if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
}
if ( ! empty( $ver ) ) {
$src = add_query_arg( 'ver', $ver, $src );
}
/**
* Filters an enqueued style's fully-qualified URL.
*
* @since 2.6.0
*
* @param string $src The source URL of the enqueued style.
* @param string $handle The style's registered handle.
*/
$src = apply_filters( 'style_loader_src', $src, $handle );
return esc_url( $src );
}
/**
* Whether a handle's source is in a default directory.
*
* @since 2.8.0
*
* @param string $src The source of the enqueued style.
* @return bool True if found, false if not.
*/
public function in_default_dir( $src ) {
if ( ! $this->default_dirs ) {
return true;
}
foreach ( (array) $this->default_dirs as $test ) {
if ( str_starts_with( $src, $test ) ) {
return true;
}
}
return false;
}
/**
* Processes items and dependencies for the footer group.
*
* HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
*
* @since 3.3.0
*
* @see WP_Dependencies::do_items()
*
* @return string[] Handles of items that have been processed.
*/
public function do_footer_items() {
$this->do_items( false, 1 );
return $this->done;
}
/**
* Resets class properties.
*
* @since 3.3.0
*/
public function reset() {
$this->do_concat = false;
$this->concat = '';
$this->concat_version = '';
$this->print_html = '';
}
}
/**
* Blocks API: WP_Block_Type class
*
* @package WordPress
* @subpackage Blocks
* @since 5.0.0
*/
/**
* Core class representing a block type.
*
* @since 5.0.0
*
* @see register_block_type()
*/
#[AllowDynamicProperties]
class WP_Block_Type {
/**
* Block API version.
*
* @since 5.6.0
* @var int
*/
public $api_version = 1;
/**
* Block type key.
*
* @since 5.0.0
* @var string
*/
public $name;
/**
* Human-readable block type label.
*
* @since 5.5.0
* @var string
*/
public $title = '';
/**
* Block type category classification, used in search interfaces
* to arrange block types by category.
*
* @since 5.5.0
* @var string|null
*/
public $category = null;
/**
* Setting parent lets a block require that it is only available
* when nested within the specified blocks.
*
* @since 5.5.0
* @var string[]|null
*/
public $parent = null;
/**
* Setting ancestor makes a block available only inside the specified
* block types at any position of the ancestor's block subtree.
*
* @since 6.0.0
* @var string[]|null
*/
public $ancestor = null;
/**
* Limits which block types can be inserted as children of this block type.
*
* @since 6.5.0
* @var string[]|null
*/
public $allowed_blocks = null;
/**
* Block type icon.
*
* @since 5.5.0
* @var string|null
*/
public $icon = null;
/**
* A detailed block type description.
*
* @since 5.5.0
* @var string
*/
public $description = '';
/**
* Additional keywords to produce block type as result
* in search interfaces.
*
* @since 5.5.0
* @var string[]
*/
public $keywords = array();
/**
* The translation textdomain.
*
* @since 5.5.0
* @var string|null
*/
public $textdomain = null;
/**
* Alternative block styles.
*
* @since 5.5.0
* @var array
*/
public $styles = array();
/**
* Block variations.
*
* @since 5.8.0
* @since 6.5.0 Only accessible through magic getter. null by default.
* @var array[]|null
*/
private $variations = null;
/**
* Block variations callback.
*
* @since 6.5.0
* @var callable|null
*/
public $variation_callback = null;
/**
* Custom CSS selectors for theme.json style generation.
*
* @since 6.3.0
* @var array
*/
public $selectors = array();
/**
* Supported features.
*
* @since 5.5.0
* @var array|null
*/
public $supports = null;
/**
* Structured data for the block preview.
*
* @since 5.5.0
* @var array|null
*/
public $example = null;
/**
* Block type render callback.
*
* @since 5.0.0
* @var callable
*/
public $render_callback = null;
/**
* Block type attributes property schemas.
*
* @since 5.0.0
* @var array|null
*/
public $attributes = null;
/**
* Context values inherited by blocks of this type.
*
* @since 5.5.0
* @var string[]
*/
private $uses_context = array();
/**
* Context provided by blocks of this type.
*
* @since 5.5.0
* @var string[]|null
*/
public $provides_context = null;
/**
* Block hooks for this block type.
*
* A block hook is specified by a block type and a relative position.
* The hooked block will be automatically inserted in the given position
* next to the "anchor" block whenever the latter is encountered.
*
* @since 6.4.0
* @var string[]
*/
public $block_hooks = array();
/**
* Block type editor only script handles.
*
* @since 6.1.0
* @var string[]
*/
public $editor_script_handles = array();
/**
* Block type front end and editor script handles.
*
* @since 6.1.0
* @var string[]
*/
public $script_handles = array();
/**
* Block type front end only script handles.
*
* @since 6.1.0
* @var string[]
*/
public $view_script_handles = array();
/**
* Block type front end only script module IDs.
*
* @since 6.5.0
* @var string[]
*/
public $view_script_module_ids = array();
/**
* Block type editor only style handles.
*
* @since 6.1.0
* @var string[]
*/
public $editor_style_handles = array();
/**
* Block type front end and editor style handles.
*
* @since 6.1.0
* @var string[]
*/
public $style_handles = array();
/**
* Block type front end only style handles.
*
* @since 6.5.0
* @var string[]
*/
public $view_style_handles = array();
/**
* Deprecated block type properties for script and style handles.
*
* @since 6.1.0
* @var string[]
*/
private $deprecated_properties = array(
'editor_script',
'script',
'view_script',
'editor_style',
'style',
);
/**
* Attributes supported by every block.
*
* @since 6.0.0 Added `lock`.
* @since 6.5.0 Added `metadata`.
* @var array
*/
const GLOBAL_ATTRIBUTES = array(
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
);
/**
* Constructor.
*
* Will populate object properties from the provided arguments.
*
* @since 5.0.0
* @since 5.5.0 Added the `title`, `category`, `parent`, `icon`, `description`,
* `keywords`, `textdomain`, `styles`, `supports`, `example`,
* `uses_context`, and `provides_context` properties.
* @since 5.6.0 Added the `api_version` property.
* @since 5.8.0 Added the `variations` property.
* @since 5.9.0 Added the `view_script` property.
* @since 6.0.0 Added the `ancestor` property.
* @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles,
* `editor_style_handles`, and `style_handles` properties.
* Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
* @since 6.3.0 Added the `selectors` property.
* @since 6.4.0 Added the `block_hooks` property.
* @since 6.5.0 Added the `view_style_handles` property.
*
* @see register_block_type()
*
* @param string $block_type Block type name including namespace.
* @param array|string $args {
* Optional. Array or string of arguments for registering a block type. Any arguments may be defined,
* however the ones described below are supported by default. Default empty array.
*
* @type string $api_version Block API version.
* @type string $title Human-readable block type label.
* @type string|null $category Block type category classification, used in
* search interfaces to arrange block types by category.
* @type string[]|null $parent Setting parent lets a block require that it is only
* available when nested within the specified blocks.
* @type string[]|null $ancestor Setting ancestor makes a block available only inside the specified
* block types at any position of the ancestor's block subtree.
* @type string[]|null $allowed_blocks Limits which block types can be inserted as children of this block type.
* @type string|null $icon Block type icon.
* @type string $description A detailed block type description.
* @type string[] $keywords Additional keywords to produce block type as
* result in search interfaces.
* @type string|null $textdomain The translation textdomain.
* @type array[] $styles Alternative block styles.
* @type array[] $variations Block variations.
* @type array $selectors Custom CSS selectors for theme.json style generation.
* @type array|null $supports Supported features.
* @type array|null $example Structured data for the block preview.
* @type callable|null $render_callback Block type render callback.
* @type callable|null $variation_callback Block type variations callback.
* @type array|null $attributes Block type attributes property schemas.
* @type string[] $uses_context Context values inherited by blocks of this type.
* @type string[]|null $provides_context Context provided by blocks of this type.
* @type string[] $block_hooks Block hooks.
* @type string[] $editor_script_handles Block type editor only script handles.
* @type string[] $script_handles Block type front end and editor script handles.
* @type string[] $view_script_handles Block type front end only script handles.
* @type string[] $editor_style_handles Block type editor only style handles.
* @type string[] $style_handles Block type front end and editor style handles.
* @type string[] $view_style_handles Block type front end only style handles.
* }
*/
public function __construct( $block_type, $args = array() ) {
$this->name = $block_type;
$this->set_props( $args );
}
/**
* Proxies getting values for deprecated properties for script and style handles for backward compatibility.
* Gets the value for the corresponding new property if the first item in the array provided.
*
* @since 6.1.0
*
* @param string $name Deprecated property name.
*
* @return string|string[]|null|void The value read from the new property if the first item in the array provided,
* null when value not found, or void when unknown property name provided.
*/
public function __get( $name ) {
if ( 'variations' === $name ) {
return $this->get_variations();
}
if ( 'uses_context' === $name ) {
return $this->get_uses_context();
}
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
return;
}
$new_name = $name . '_handles';
if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) {
return null;
}
if ( count( $this->{$new_name} ) > 1 ) {
return $this->{$new_name};
}
return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
}
/**
* Proxies checking for deprecated properties for script and style handles for backward compatibility.
* Checks whether the corresponding new property has the first item in the array provided.
*
* @since 6.1.0
*
* @param string $name Deprecated property name.
*
* @return bool Returns true when for the new property the first item in the array exists,
* or false otherwise.
*/
public function __isset( $name ) {
if ( in_array( $name, array( 'variations', 'uses_context' ), true ) ) {
return true;
}
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
return false;
}
$new_name = $name . '_handles';
return isset( $this->{$new_name}[0] );
}
/**
* Proxies setting values for deprecated properties for script and style handles for backward compatibility.
* Sets the value for the corresponding new property as the first item in the array.
* It also allows setting custom properties for backward compatibility.
*
* @since 6.1.0
*
* @param string $name Property name.
* @param mixed $value Property value.
*/
public function __set( $name, $value ) {
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
$this->{$name} = $value;
return;
}
$new_name = $name . '_handles';
if ( is_array( $value ) ) {
$filtered = array_filter( $value, 'is_string' );
if ( count( $filtered ) !== count( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %s: The '$value' argument. */
__( 'The %s argument must be a string or a string array.' ),
'$value'
),
'6.1.0'
);
}
$this->{$new_name} = array_values( $filtered );
return;
}
if ( ! is_string( $value ) ) {
return;
}
$this->{$new_name} = array( $value );
}
/**
* Renders the block type output for given attributes.
*
* @since 5.0.0
*
* @param array $attributes Optional. Block attributes. Default empty array.
* @param string $content Optional. Block content. Default empty string.
* @return string Rendered block type output.
*/
public function render( $attributes = array(), $content = '' ) {
if ( ! $this->is_dynamic() ) {
return '';
}
$attributes = $this->prepare_attributes_for_render( $attributes );
return (string) call_user_func( $this->render_callback, $attributes, $content );
}
/**
* Returns true if the block type is dynamic, or false otherwise. A dynamic
* block is one which defers its rendering to occur on-demand at runtime.
*
* @since 5.0.0
*
* @return bool Whether block type is dynamic.
*/
public function is_dynamic() {
return is_callable( $this->render_callback );
}
/**
* Validates attributes against the current block schema, populating
* defaulted and missing values.
*
* @since 5.0.0
*
* @param array $attributes Original block attributes.
* @return array Prepared block attributes.
*/
public function prepare_attributes_for_render( $attributes ) {
// If there are no attribute definitions for the block type, skip
// processing and return verbatim.
if ( ! isset( $this->attributes ) ) {
return $attributes;
}
foreach ( $attributes as $attribute_name => $value ) {
// If the attribute is not defined by the block type, it cannot be
// validated.
if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
continue;
}
$schema = $this->attributes[ $attribute_name ];
// Validate value by JSON schema. An invalid value should revert to
// its default, if one exists. This occurs by virtue of the missing
// attributes loop immediately following. If there is not a default
// assigned, the attribute value should remain unset.
$is_valid = rest_validate_value_from_schema( $value, $schema, $attribute_name );
if ( is_wp_error( $is_valid ) ) {
unset( $attributes[ $attribute_name ] );
}
}
// Populate values of any missing attributes for which the block type
// defines a default.
$missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
if ( isset( $schema['default'] ) ) {
$attributes[ $attribute_name ] = $schema['default'];
}
}
return $attributes;
}
/**
* Sets block type properties.
*
* @since 5.0.0
*
* @param array|string $args Array or string of arguments for registering a block type.
* See WP_Block_Type::__construct() for information on accepted arguments.
*/
public function set_props( $args ) {
$args = wp_parse_args(
$args,
array(
'render_callback' => null,
)
);
$args['name'] = $this->name;
// Setup attributes if needed.
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
$args['attributes'] = array();
}
// Register core attributes.
foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) {
if ( ! array_key_exists( $attr_key, $args['attributes'] ) ) {
$args['attributes'][ $attr_key ] = $attr_schema;
}
}
/**
* Filters the arguments for registering a block type.
*
* @since 5.5.0
*
* @param array $args Array of arguments for registering a block type.
* @param string $block_type Block type name including namespace.
*/
$args = apply_filters( 'register_block_type_args', $args, $this->name );
foreach ( $args as $property_name => $property_value ) {
$this->$property_name = $property_value;
}
}
/**
* Get all available block attributes including possible layout attribute from Columns block.
*
* @since 5.0.0
*
* @return array Array of attributes.
*/
public function get_attributes() {
return is_array( $this->attributes ) ?
$this->attributes :
array();
}
/**
* Get block variations.
*
* @since 6.5.0
*
* @return array[]
*/
public function get_variations() {
if ( ! isset( $this->variations ) ) {
$this->variations = array();
if ( is_callable( $this->variation_callback ) ) {
$this->variations = call_user_func( $this->variation_callback );
}
}
/**
* Filters the registered variations for a block type.
*
* @since 6.5.0
*
* @param array $variations Array of registered variations for a block type.
* @param WP_Block_Type $block_type The full block type object.
*/
return apply_filters( 'get_block_type_variations', $this->variations, $this );
}
/**
* Get block uses context.
*
* @since 6.5.0
*
* @return array[]
*/
public function get_uses_context() {
/**
* Filters the registered uses context for a block type.
*
* @since 6.5.0
*
* @param array $uses_context Array of registered uses context for a block type.
* @param WP_Block_Type $block_type The full block type object.
*/
return apply_filters( 'get_block_type_uses_context', $this->uses_context, $this );
}
}
Fatal error: Uncaught Error: Class 'WP_Block_Type' not found in /home/advibe/public_html/thedailybread/wp-includes/class-wp-block-type-registry.php:94
Stack trace:
#0 /home/advibe/public_html/thedailybread/wp-includes/blocks.php(707): WP_Block_Type_Registry->register('aioseo/breadcru...', Array)
#1 /home/advibe/public_html/thedailybread/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Blocks.php(75): register_block_type('aioseo/breadcru...', Array)
#2 /home/advibe/public_html/thedailybread/wp-content/plugins/all-in-one-seo-pack/app/Common/Breadcrumbs/Block.php(49): AIOSEO\Plugin\Common\Utils\Blocks->registerBlock('aioseo/breadcru...', Array)
#3 /home/advibe/public_html/thedailybread/wp-content/plugins/all-in-one-seo-pack/app/Common/Breadcrumbs/Block.php(30): AIOSEO\Plugin\Common\Breadcrumbs\Block->register()
#4 /home/advibe/public_html/thedailybread/wp-content/plugins/all-in-one-seo-pack/app/Common/Breadcrumbs/Breadcrumbs.php(66): AIOSEO\Plugin\Common\Breadcrumbs\Block->__construct()
#5 /home/advibe/public_html/the in /home/advibe/public_html/thedailybread/wp-includes/class-wp-block-type-registry.php on line 94