Meta: Remove object subtype handling from `register_meta()`.
Registration is now based solely on object type, which allows the code around this to be simplified significantly. In the process of making this adjustment: * `register_meta()`, `unregister_meta_key()`, `get_registered_metadata()`, and `registered_meta_key_exists()` no longer return `WP_Error` objects. * The recently introduced `wp_object_type_exists()` function and the restriction on object type has been removed. Note: No guarantee of uniqueness is made across object subtypes. Registered meta keys should be uniquely prefixed to avoid conflict. Fixes #35658. git-svn-id: https://develop.svn.wordpress.org/trunk@38095 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ff6d8819e4
commit
4f2008a104
|
@ -5357,31 +5357,6 @@ function mysql_to_rfc3339( $date_string ) {
|
|||
return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an object type exists. By default, these are `post`, `comment`, `user`, and `term`.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param string $object_type Object type to check.
|
||||
* @return bool True if the object type exists, false if not.
|
||||
*/
|
||||
function wp_object_type_exists( $object_type ) {
|
||||
/**
|
||||
* Filters WordPress object types.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param array $types Array of object types.
|
||||
*/
|
||||
$types = apply_filters( 'wp_object_types', array( 'post', 'comment', 'user', 'term' ) );
|
||||
|
||||
if ( in_array( $object_type, $types ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to raise the PHP memory limit for memory intensive processes.
|
||||
*
|
||||
|
|
|
@ -936,34 +936,14 @@ function is_protected_meta( $meta_key, $meta_type = null ) {
|
|||
* Sanitize meta value.
|
||||
*
|
||||
* @since 3.1.3
|
||||
* @since 4.6.0 Added the `$object_subtype` parameter.
|
||||
*
|
||||
* @param string $meta_key Meta key.
|
||||
* @param mixed $meta_value Meta value to sanitize.
|
||||
* @param string $object_type Type of object the meta is registered to.
|
||||
* @param string $object_subtype Optional. Subtype of object. Will inherit the object type by default.
|
||||
*
|
||||
* @return mixed Sanitized $meta_value.
|
||||
*/
|
||||
function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
|
||||
if ( ! empty( $object_subtype ) ) {
|
||||
/**
|
||||
* Filters the sanitization of a specific meta key of a specific meta type and subtype.
|
||||
*
|
||||
* The dynamic portions of the hook name, `$meta_type`, `$meta_subtype`,
|
||||
* and `$meta_key`, refer to the metadata object type (comment, post, or user)
|
||||
* the object subtype, and the meta key value, respectively.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param mixed $meta_value Meta value to sanitize.
|
||||
* @param string $meta_key Meta key.
|
||||
* @param string $object_type Object type.
|
||||
* @param string $object_subtype Object subtype.
|
||||
*/
|
||||
$meta_value = apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype );
|
||||
}
|
||||
|
||||
function sanitize_meta( $meta_key, $meta_value, $object_type ) {
|
||||
/**
|
||||
* Filters the sanitization of a specific meta key of a specific meta type.
|
||||
*
|
||||
|
@ -972,14 +952,12 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
|
|||
* key value, respectively.
|
||||
*
|
||||
* @since 3.3.0
|
||||
* @since 4.6.0 Added the `$object_subtype` parameter.
|
||||
*
|
||||
* @param mixed $meta_value Meta value to sanitize.
|
||||
* @param string $meta_key Meta key.
|
||||
* @param string $object_type Object type.
|
||||
* @param string $object_subtype Object subtype.
|
||||
*/
|
||||
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype );
|
||||
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -995,7 +973,6 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
|
|||
* @param array $args {
|
||||
* Data used to describe the meta key when registered.
|
||||
*
|
||||
* @type string $object_subtype A subtype; e.g. if the object type is "post", the post type.
|
||||
* @type string $type The type of data associated with this meta key.
|
||||
* @type string $description A description of the data attached to this meta key.
|
||||
* @type bool $single Whether the meta key has one value per object, or an array of values per object.
|
||||
|
@ -1005,9 +982,9 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
|
|||
* }
|
||||
* @param string|array $deprecated Deprecated. Use `$args` instead.
|
||||
*
|
||||
* @return bool|WP_error True if the meta key was successfully registered in the global array, WP_Error if not.
|
||||
* @return bool True if the meta key was successfully registered in the global array, false if not.
|
||||
* Registering a meta key with distinct sanitize and auth callbacks will fire those
|
||||
* callbacks, but will not add to the global registry as it requires a subtype.
|
||||
* callbacks, but will not add to the global registry.
|
||||
*/
|
||||
function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
|
||||
global $wp_meta_keys;
|
||||
|
@ -1016,12 +993,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
|
|||
$wp_meta_keys = array();
|
||||
}
|
||||
|
||||
if ( ! wp_object_type_exists( $object_type ) ) {
|
||||
return new WP_Error( 'register_meta_failed', __( 'Meta can only be registered against a core object type.' ) );
|
||||
}
|
||||
|
||||
$defaults = array(
|
||||
'object_subtype' => '',
|
||||
'type' => 'string',
|
||||
'description' => '',
|
||||
'single' => false,
|
||||
|
@ -1049,8 +1021,6 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
|
|||
$has_old_auth_cb = true;
|
||||
}
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
/**
|
||||
* Filters the registration arguments when registering meta.
|
||||
*
|
||||
|
@ -1062,11 +1032,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
|
|||
* @param string $meta_key Meta key.
|
||||
*/
|
||||
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
|
||||
|
||||
// Object subtype is required if using the args style of registration
|
||||
if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $args['object_subtype'] ) ) {
|
||||
return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) );
|
||||
}
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
|
||||
if ( empty( $args['auth_callback'] ) ) {
|
||||
|
@ -1077,39 +1043,23 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
|
|||
}
|
||||
}
|
||||
|
||||
$object_subtype = '';
|
||||
|
||||
if ( ! empty( $args['object_subtype'] ) ) {
|
||||
$object_subtype = $args['object_subtype'];
|
||||
}
|
||||
|
||||
// Back-compat: old sanitize and auth callbacks are applied to all of an object type.
|
||||
if ( $has_old_sanitize_cb && is_callable( $args['sanitize_callback'] ) ) {
|
||||
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
|
||||
}
|
||||
|
||||
if ( $has_old_auth_cb && is_callable( $args['auth_callback'] ) ) {
|
||||
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
|
||||
}
|
||||
|
||||
if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb) {
|
||||
if ( is_callable( $args['sanitize_callback'] ) ) {
|
||||
add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
|
||||
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
|
||||
}
|
||||
|
||||
if ( is_callable( $args['auth_callback'] ) ) {
|
||||
add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
|
||||
}
|
||||
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
|
||||
}
|
||||
|
||||
// Global registry only contains meta keys registered in the new way with a subtype.
|
||||
if ( ! empty( $object_subtype ) ) {
|
||||
$wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
|
||||
// Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
|
||||
if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
|
||||
$wp_meta_keys[ $object_type ][ $meta_key ] = $args;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return new WP_Error( 'register_meta_failed', __( 'Sanitize and auth callbacks registered; meta key not registered.' ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1118,33 +1068,22 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
|
|||
* @since 4.6.0
|
||||
*
|
||||
* @param string $object_type The type of object.
|
||||
* @param string $object_subtype The subtype of the object type.
|
||||
* @param string $meta_key The meta key.
|
||||
*
|
||||
* @return bool|WP_error True if the meta key is registered to the object type and subtype. False if not.
|
||||
* WP_Error if an invalid object type is passed.
|
||||
* @return bool True if the meta key is registered to the object type. False if not.
|
||||
*/
|
||||
function registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) {
|
||||
function registered_meta_key_exists( $object_type, $meta_key ) {
|
||||
global $wp_meta_keys;
|
||||
|
||||
if ( ! is_array( $wp_meta_keys ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only specific core object types are supported.
|
||||
if ( ! wp_object_type_exists( $object_type ) ) {
|
||||
return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( isset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ) ) {
|
||||
if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1157,35 +1096,30 @@ function registered_meta_key_exists( $object_type, $object_subtype, $meta_key )
|
|||
* @since 4.6.0
|
||||
*
|
||||
* @param string $object_type The type of object.
|
||||
* @param string $object_subtype The subtype of the object type.
|
||||
* @param string $meta_key The meta key.
|
||||
*
|
||||
* @return bool|WP_Error True if successful. WP_Error if the meta key is invalid.
|
||||
* @return bool True if successful. False if the meta key was not registered.
|
||||
*/
|
||||
function unregister_meta_key( $object_type, $object_subtype, $meta_key ) {
|
||||
function unregister_meta_key( $object_type, $meta_key ) {
|
||||
global $wp_meta_keys;
|
||||
|
||||
if ( ! registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) ) {
|
||||
return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) );
|
||||
if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
|
||||
$args = $wp_meta_keys[ $object_type ][ $meta_key ];
|
||||
|
||||
if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
|
||||
remove_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] );
|
||||
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
|
||||
}
|
||||
|
||||
if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
|
||||
remove_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'] );
|
||||
remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
|
||||
}
|
||||
|
||||
unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
|
||||
unset( $wp_meta_keys[ $object_type ][ $meta_key ] );
|
||||
|
||||
// Do some clean up
|
||||
if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
|
||||
unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
|
||||
}
|
||||
|
||||
if ( empty( $wp_meta_keys[ $object_type ] ) ) {
|
||||
unset( $wp_meta_keys[ $object_type ] );
|
||||
}
|
||||
|
@ -1194,62 +1128,43 @@ function unregister_meta_key( $object_type, $object_subtype, $meta_key ) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Retrieves a list of registered meta keys for an object type and optionally subtype.
|
||||
* Retrieves a list of registered meta keys for an object type.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param string $object_type The type of object. Post, comment, user, term.
|
||||
* @param string $object_subtype Optional. A subtype of the object (e.g. custom post type).
|
||||
*
|
||||
* @return array List of registered meta keys.
|
||||
*/
|
||||
function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
|
||||
function get_registered_meta_keys( $object_type ) {
|
||||
global $wp_meta_keys;
|
||||
|
||||
if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if ( empty( $object_subtype ) && isset( $wp_meta_keys[ $object_type ] ) ) {
|
||||
return $wp_meta_keys[ $object_type ];
|
||||
}
|
||||
|
||||
if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return $wp_meta_keys[ $object_type ][ $object_subtype ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves registered metadata for a specified object.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*
|
||||
* @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
|
||||
* @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type)
|
||||
* @param int $object_id ID of the object the metadata is for.
|
||||
* @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
|
||||
* metadata for the specified object.
|
||||
*
|
||||
* @return mixed|WP_Error
|
||||
* @return mixed A single value or array of values for a key if specified. An array of all registered keys
|
||||
* and values for an object ID if not.
|
||||
*/
|
||||
function get_registered_metadata( $object_type, $object_subtype, $object_id, $meta_key = '' ) {
|
||||
global $wp_meta_keys;
|
||||
|
||||
if ( ! is_array( $wp_meta_keys ) ) {
|
||||
return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );
|
||||
}
|
||||
|
||||
if ( ! wp_object_type_exists( $object_type ) ) {
|
||||
return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );
|
||||
}
|
||||
|
||||
function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
|
||||
if ( ! empty( $meta_key ) ) {
|
||||
if ( ! registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) ) {
|
||||
return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );
|
||||
if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
|
||||
return false;
|
||||
}
|
||||
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
|
||||
$meta_keys = get_registered_meta_keys( $object_type );
|
||||
$meta_key_data = $meta_keys[ $meta_key ];
|
||||
|
||||
$data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
|
||||
|
@ -1259,7 +1174,7 @@ function get_registered_metadata( $object_type, $object_subtype, $object_id, $me
|
|||
|
||||
$data = get_metadata( $object_type, $object_id );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
|
||||
$meta_keys = get_registered_meta_keys( $object_type );
|
||||
$registered_data = array();
|
||||
|
||||
// Someday, array_filter()
|
||||
|
|
|
@ -3,32 +3,21 @@
|
|||
* @group meta
|
||||
*/
|
||||
class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
||||
protected static $editor_id;
|
||||
protected static $post_id;
|
||||
protected static $comment_id;
|
||||
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
|
||||
self::$post_id = $factory->post->create();
|
||||
self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
self::delete_user( self::$editor_id );
|
||||
wp_delete_comment( self::$comment_id, true );
|
||||
wp_delete_post( self::$post_id, true );
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
wp_set_current_user( self::$editor_id );
|
||||
}
|
||||
|
||||
public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) {
|
||||
return $meta_key . ' old sanitized';
|
||||
}
|
||||
|
||||
public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type, $object_subtype ) {
|
||||
public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type ) {
|
||||
return $meta_key . ' new sanitized';
|
||||
}
|
||||
|
||||
|
@ -68,25 +57,23 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
|||
$this->assertEquals( array( 'auth' => 10, 'sanitize' => 10 ), $has_filters );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_valid_object_type_and_object_subtype_returns_true() {
|
||||
$result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
public function test_register_meta_with_post_object_type_returns_true() {
|
||||
$result = register_meta( 'post', 'flight_number', array() );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$this->assertTrue( $result );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_post_object_type_and_subtype_populates_wp_meta_keys() {
|
||||
public function test_register_meta_with_post_object_type_populates_wp_meta_keys() {
|
||||
global $wp_meta_keys;
|
||||
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'flight_number', array() );
|
||||
$actual = $wp_meta_keys;
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$expected = array(
|
||||
'post' => array(
|
||||
'post' => array(
|
||||
'flight_number' => array(
|
||||
'object_subtype' => 'post',
|
||||
'type' => 'string',
|
||||
'description' => '',
|
||||
'single' => false,
|
||||
|
@ -95,49 +82,20 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
|||
'show_in_rest' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( $actual, $expected );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_post_object_type_and_unregistered_subtype_populates_wp_meta_keys() {
|
||||
public function test_register_meta_with_term_object_type_populates_wp_meta_keys() {
|
||||
global $wp_meta_keys;
|
||||
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'not_a_post_type' ) );
|
||||
register_meta( 'term', 'category_icon', array() );
|
||||
$actual = $wp_meta_keys;
|
||||
unregister_meta_key( 'post', 'not_a_post_type', 'flight_number' );
|
||||
|
||||
$expected = array(
|
||||
'post' => array(
|
||||
'not_a_post_type' => array(
|
||||
'flight_number' => array(
|
||||
'object_subtype' => 'not_a_post_type',
|
||||
'type' => 'string',
|
||||
'description' => '',
|
||||
'single' => false,
|
||||
'sanitize_callback' => null,
|
||||
'auth_callback' => '__return_true',
|
||||
'show_in_rest' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( $actual, $expected );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_term_object_type_and_category_subtype_populates_wp_meta_keys() {
|
||||
global $wp_meta_keys;
|
||||
register_meta( 'term', 'category_icon', array( 'object_subtype' => 'category' ) );
|
||||
$actual = $wp_meta_keys;
|
||||
unregister_meta_key( 'term', 'category', 'category_icon' );
|
||||
unregister_meta_key( 'term', 'category_icon' );
|
||||
|
||||
$expected = array(
|
||||
'term' => array(
|
||||
'category' => array(
|
||||
'category_icon' => array(
|
||||
'object_subtype' => 'category',
|
||||
'type' => 'string',
|
||||
'description' => '',
|
||||
'single' => false,
|
||||
|
@ -146,32 +104,6 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
|||
'show_in_rest' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( $actual, $expected );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_comment_object_type_and_subtype_populates_wp_meta_keys() {
|
||||
global $wp_meta_keys;
|
||||
register_meta( 'comment', 'comment_rating', array( 'object_subtype' => 'comment' ) );
|
||||
$actual = $wp_meta_keys;
|
||||
unregister_meta_key( 'comment', 'comment', 'comment_rating' );
|
||||
|
||||
$expected = array(
|
||||
'comment' => array(
|
||||
'comment' => array(
|
||||
'comment_rating' => array(
|
||||
'object_subtype' => 'comment',
|
||||
'type' => 'string',
|
||||
'description' => '',
|
||||
'single' => false,
|
||||
'sanitize_callback' => null,
|
||||
'auth_callback' => '__return_true',
|
||||
'show_in_rest' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( $actual, $expected );
|
||||
|
@ -188,14 +120,13 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
|||
$this->assertEquals( array(), $actual );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_deprecated_sanitize_callback_param_returns_wp_error() {
|
||||
public function test_register_meta_with_deprecated_sanitize_callback_param_returns_false() {
|
||||
$actual = register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) );
|
||||
|
||||
remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb') );
|
||||
remove_filter( 'auth_post_meta_flight_number', '__return_true');
|
||||
|
||||
$this->assertWPError( $actual );
|
||||
$this->assertEquals( 'register_meta_failed', $actual->get_error_code() );
|
||||
$this->assertFalse( $actual );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_deprecated_sanitize_callback_parameter_passes_through_filter() {
|
||||
|
@ -210,15 +141,13 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
|||
|
||||
public function test_register_meta_with_current_sanitize_callback_populates_wp_meta_keys() {
|
||||
global $wp_meta_keys;
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
register_meta( 'post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
$actual = $wp_meta_keys;
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$expected = array(
|
||||
'post' => array(
|
||||
'post' => array(
|
||||
'flight_number' => array(
|
||||
'object_subtype' => 'post',
|
||||
'type' => 'string',
|
||||
'description' => '',
|
||||
'single' => false,
|
||||
|
@ -227,184 +156,148 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
|
|||
'show_in_rest' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertEquals( $actual, $expected );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_current_sanitize_callback_returns_true() {
|
||||
$result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
$result = register_meta( 'post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$this->assertTrue( $result );
|
||||
}
|
||||
|
||||
public function test_register_meta_with_new_sanitize_callback_parameter() {
|
||||
register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
$meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post', 'post' );
|
||||
register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
$meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
|
||||
unregister_meta_key( 'post', 'new_sanitized_key' );
|
||||
|
||||
$this->assertEquals( 'new_sanitized_key new sanitized', $meta );
|
||||
}
|
||||
|
||||
public function test_register_meta_unregistered_meta_key_removes_sanitize_filter() {
|
||||
register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
|
||||
register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
|
||||
unregister_meta_key( 'post', 'new_sanitized_key' );
|
||||
|
||||
$has_filter = has_filter( 'sanitize_post_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );
|
||||
$has_filter = has_filter( 'sanitize_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );
|
||||
|
||||
$this->assertFalse( $has_filter );
|
||||
}
|
||||
|
||||
public function test_register_meta_unregistered_meta_key_removes_auth_filter() {
|
||||
register_meta( 'post', 'new_auth_key', array( 'object_subtype' => 'post', 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) );
|
||||
unregister_meta_key( 'post', 'post', 'new_auth_key' );
|
||||
register_meta( 'post', 'new_auth_key', array( 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) );
|
||||
unregister_meta_key( 'post', 'new_auth_key' );
|
||||
|
||||
$has_filter = has_filter( 'auth_post_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );
|
||||
$has_filter = has_filter( 'auth_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );
|
||||
|
||||
$this->assertFalse( $has_filter );
|
||||
}
|
||||
|
||||
public function test_unregister_meta_key_clears_key_from_wp_meta_keys() {
|
||||
global $wp_meta_keys;
|
||||
register_meta( 'post', 'registered_key', array( 'object_subtype' => 'post' ) );
|
||||
unregister_meta_key( 'post', 'post', 'registered_key' );
|
||||
register_meta( 'post', 'registered_key', array() );
|
||||
unregister_meta_key( 'post', 'registered_key' );
|
||||
|
||||
$this->assertEquals( array(), $wp_meta_keys );
|
||||
}
|
||||
|
||||
public function test_unregister_meta_key_with_invalid_key_returns_wp_error() {
|
||||
$this->assertWPError( unregister_meta_key( 'post', 'post', 'not_a_registered_key' ) );
|
||||
public function test_unregister_meta_key_with_invalid_key_returns_false() {
|
||||
$this->assertFalse( unregister_meta_key( 'post', 'not_a_registered_key' ) );
|
||||
}
|
||||
|
||||
public function test_get_registered_meta_keys() {
|
||||
register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'registered_key1', array() );
|
||||
register_meta( 'post', 'registered_key2', array() );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( 'post', 'post' );
|
||||
$meta_keys = get_registered_meta_keys( 'post' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'post', 'registered_key2' );
|
||||
unregister_meta_key( 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'registered_key2' );
|
||||
|
||||
$this->assertArrayHasKey( 'registered_key1', $meta_keys );
|
||||
$this->assertArrayHasKey( 'registered_key2', $meta_keys );
|
||||
}
|
||||
|
||||
public function test_get_registered_meta_keys_with_subtype_without_registered_keys_is_empty() {
|
||||
register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( 'post', 'page' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'post', 'registered_key2' );
|
||||
|
||||
$this->assertEmpty( $meta_keys );
|
||||
}
|
||||
|
||||
public function test_get_registered_meta_keys_with_invalid_type_is_empty() {
|
||||
register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'registered_key1', array() );
|
||||
register_meta( 'post', 'registered_key2', array() );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( 'invalid-type' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'post', 'registered_key2' );
|
||||
unregister_meta_key( 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'registered_key2' );
|
||||
|
||||
$this->assertEmpty( $meta_keys );
|
||||
}
|
||||
|
||||
public function test_get_registered_meta_keys_has_count() {
|
||||
register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'page' ) );
|
||||
public function test_get_registered_meta_keys_description_arg() {
|
||||
register_meta( 'post', 'registered_key1', array( 'description' => 'I\'m just a field, take a good look at me' ) );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( 'post' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'page', 'registered_key2' );
|
||||
|
||||
$this->assertCount( 2, $meta_keys );
|
||||
}
|
||||
|
||||
public function test_get_registered_meta_keys_description_arg() {
|
||||
register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'description' => 'I\'m just a field, take a good look at me' ) );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( 'post', 'post' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'registered_key1' );
|
||||
|
||||
$this->assertEquals( 'I\'m just a field, take a good look at me', $meta_keys['registered_key1']['description'] );
|
||||
}
|
||||
|
||||
public function test_get_registered_meta_keys_invalid_arg() {
|
||||
register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'invalid_arg' => 'invalid' ) );
|
||||
register_meta( 'post', 'registered_key1', array( 'invalid_arg' => 'invalid' ) );
|
||||
|
||||
$meta_keys = get_registered_meta_keys( 'post', 'post' );
|
||||
$meta_keys = get_registered_meta_keys( 'post' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'registered_key1' );
|
||||
unregister_meta_key( 'post', 'registered_key1' );
|
||||
|
||||
$this->assertArrayNotHasKey( 'invalid_arg', $meta_keys['registered_key1'] );
|
||||
}
|
||||
|
||||
public function test_get_registered_metadata() {
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'flight_number', array() );
|
||||
add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
|
||||
|
||||
$meta = get_registered_metadata( 'post', 'post', self::$post_id );
|
||||
$meta = get_registered_metadata( 'post', self::$post_id );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$this->assertEquals( 'Oceanic 815', $meta['flight_number'][0] );
|
||||
}
|
||||
|
||||
public function test_get_registered_metadata_by_key() {
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'flight_number', array() );
|
||||
add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
|
||||
|
||||
$meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' );
|
||||
$meta = get_registered_metadata( 'post', self::$post_id, 'flight_number' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$this->assertEquals( 'Oceanic 815', $meta[0] );
|
||||
}
|
||||
|
||||
public function test_get_registered_metadata_by_key_single() {
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'single' => true ) );
|
||||
register_meta( 'post', 'flight_number', array( 'single' => true ) );
|
||||
add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
|
||||
|
||||
$meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' );
|
||||
$meta = get_registered_metadata( 'post', self::$post_id, 'flight_number' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$this->assertEquals( 'Oceanic 815', $meta );
|
||||
}
|
||||
|
||||
public function test_get_registered_metadata_by_invalid_key() {
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
|
||||
register_meta( 'post', 'flight_number', array() );
|
||||
add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
|
||||
|
||||
$meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_pilot' );
|
||||
$meta = get_registered_metadata( 'post', self::$post_id, 'flight_pilot' );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
unregister_meta_key( 'post', 'flight_number' );
|
||||
|
||||
$this->assertWPError( $meta );
|
||||
$this->assertFalse( $meta );
|
||||
}
|
||||
|
||||
public function test_get_registered_metadata_invalid_object_type() {
|
||||
register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
|
||||
add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
|
||||
public function test_get_registered_metadata_invalid_object_type_returns_empty_array() {
|
||||
$meta = get_registered_metadata( 'invalid-type', self::$post_id );
|
||||
|
||||
$meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );
|
||||
|
||||
unregister_meta_key( 'post', 'post', 'flight_number' );
|
||||
|
||||
$this->assertWPError( $meta );
|
||||
}
|
||||
|
||||
public function test_get_registered_metadata_invalid() {
|
||||
$meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );
|
||||
|
||||
$this->assertWPError( $meta );
|
||||
$this->assertEmpty( $meta );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue