Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,7 @@ function _wp_register_meta_args_allowed_list( $args, $default_args ) {
* Returns the object subtype for a given object ID of a specific type.
*
* @since 4.9.8
* @since 6.X.0 Added support for 'blog' object type in multisite.
*
Comment on lines 1789 to 1793
* @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
* 'user', or any other object type with an associated meta table.
Expand Down Expand Up @@ -1834,6 +1835,19 @@ function get_object_subtype( $object_type, $object_id ) {

$object_subtype = 'user';
break;

case 'blog':
if ( ! is_multisite() || $object_id <= 0 ) {
break;
}

$site = get_site( $object_id );
if ( ! $site ) {
break;
}

$object_subtype = 'blog';
break;
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/phpunit/tests/meta/registerMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
protected static $term_id;
protected static $comment_id;
protected static $user_id;
protected static $blog_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create( array( 'post_type' => 'page' ) );
self::$term_id = $factory->term->create( array( 'taxonomy' => 'category' ) );
self::$comment_id = $factory->comment->create();
self::$user_id = $factory->user->create();

if ( is_multisite() ) {
self::$blog_id = $factory->blog->create();
}
}

public static function wpTearDownAfterClass() {
wp_delete_post( self::$post_id, true );
wp_delete_term( self::$term_id, 'category' );
wp_delete_comment( self::$comment_id, true );
self::delete_user( self::$user_id );

if ( is_multisite() ) {
wp_delete_site( self::$blog_id );
wp_update_network_site_counts();
}
}

public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) {
Expand Down Expand Up @@ -1105,6 +1115,52 @@ public function data_get_types_and_subtypes() {
);
}

/**
* @ticket 44387
* @group ms-required
*/
public function test_get_object_subtype_for_blog_returns_blog_when_site_exists() {
$this->assertSame( 'blog', get_object_subtype( 'blog', self::$blog_id ) );
}

/**
* @ticket 44387
* @group ms-required
*/
public function test_get_object_subtype_for_blog_returns_empty_string_for_invalid_site() {
$this->assertSame( '', get_object_subtype( 'blog', 0 ) );
$this->assertSame( '', get_object_subtype( 'blog', 999999 ) );
}

/**
* @ticket 44387
* @group ms-required
*/
public function test_get_object_subtype_for_blog_with_registered_meta() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}

$this->assertSame( 'blog', get_object_subtype( 'blog', self::$blog_id ) );

register_meta(
'blog',
'site_rating',
array(
'single' => true,
'object_subtype' => 'blog',
)
);
add_site_meta( self::$blog_id, 'site_rating', '5' );

$meta = get_registered_metadata( 'blog', self::$blog_id, 'site_rating' );

unregister_meta_key( 'blog', 'site_rating', 'blog' );
delete_site_meta( self::$blog_id, 'site_rating' );

$this->assertSame( '5', $meta );
}

/**
* Test that attempting to register meta with revisions_enabled set to true on a
* post type that does not have revisions enabled fails and throws a `doing_it_wrong` notice.
Expand Down
Loading