Make WordPress Core

Changeset 63603


Ignore:
Timestamp:
09/11/2026 04:50:51 PM (14 hours ago)
Author:
spacedmonkey
Message:

Meta: Support site meta subtype in get_object_subtype().

Add a 'blog' case to get_object_subtype() so register_meta() can resolve
subtypes for site meta, matching the support already in place for posts,
terms, comments, and users.

Introduce register_site_meta() and unregister_site_meta() wrapper
functions for registering and unregistering meta for sites, consistent
with the existing site meta wrapper functions.

Props sainathpoojary, spacedmonkey, flixos90, johnbillion.
Fixes #44387.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/meta.php

    r63497 r63603  
    17901790 *
    17911791 * @since 4.9.8
     1792 * @since 7.2.0 Added support for 'blog' object type in multisite.
    17921793 *
    17931794 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
     
    18351836                        $object_subtype = 'user';
    18361837                        break;
     1838
     1839                case 'blog':
     1840                        if ( ! is_multisite() || $object_id <= 0 ) {
     1841                                break;
     1842                        }
     1843
     1844                        $site = get_site( $object_id );
     1845                        if ( ! $site ) {
     1846                                break;
     1847                        }
     1848
     1849                        $object_subtype = 'blog';
     1850                        break;
    18371851        }
    18381852
  • trunk/src/wp-includes/ms-site.php

    r61615 r63603  
    11311131
    11321132/**
     1133 * Registers a meta key for sites.
     1134 *
     1135 * @since 7.2.0
     1136 *
     1137 * @param string $meta_key The meta key to register.
     1138 * @param array  $args     Data used to describe the meta key when registered. See
     1139 *                         {@see register_meta()} for a list of supported arguments.
     1140 * @return bool True if the meta key was successfully registered, false if not.
     1141 */
     1142function register_site_meta( $meta_key, array $args ) {
     1143        return register_meta( 'blog', $meta_key, $args );
     1144}
     1145
     1146/**
     1147 * Unregisters a meta key for sites.
     1148 *
     1149 * @since 7.2.0
     1150 *
     1151 * @param string $meta_key The meta key to unregister.
     1152 * @return bool True on success, false if the meta key was not previously registered.
     1153 */
     1154function unregister_site_meta( $meta_key ) {
     1155        return unregister_meta_key( 'blog', $meta_key );
     1156}
     1157
     1158/**
    11331159 * Updates the count of sites for a network based on a changed site.
    11341160 *
  • trunk/tests/phpunit/tests/meta/registerMeta.php

    r59023 r63603  
    99        protected static $comment_id;
    1010        protected static $user_id;
     11        protected static $blog_id;
    1112
    1213        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    1516                self::$comment_id = $factory->comment->create();
    1617                self::$user_id    = $factory->user->create();
     18
     19                if ( is_multisite() ) {
     20                        self::$blog_id = $factory->blog->create();
     21                }
    1722        }
    1823
     
    2227                wp_delete_comment( self::$comment_id, true );
    2328                self::delete_user( self::$user_id );
     29
     30                if ( is_multisite() ) {
     31                        wp_delete_site( self::$blog_id );
     32                        wp_update_network_site_counts();
     33                }
    2434        }
    2535
     
    11071117
    11081118        /**
     1119         * @ticket 44387
     1120         * @group ms-required
     1121         */
     1122        public function test_get_object_subtype_for_blog_returns_blog_when_site_exists() {
     1123                $this->assertSame( 'blog', get_object_subtype( 'blog', self::$blog_id ) );
     1124        }
     1125
     1126        /**
     1127         * @ticket 44387
     1128         * @group ms-required
     1129         */
     1130        public function test_get_object_subtype_for_blog_returns_empty_string_for_invalid_site() {
     1131                $this->assertSame( '', get_object_subtype( 'blog', 0 ) );
     1132                $this->assertSame( '', get_object_subtype( 'blog', 999999 ) );
     1133        }
     1134
     1135        /**
     1136         * @ticket 44387
     1137         * @group ms-excluded
     1138         */
     1139        public function test_get_object_subtype_for_blog_returns_empty_string_no_multisite() {
     1140                $this->assertSame( '', get_object_subtype( 'blog', 0 ) );
     1141        }
     1142
     1143        /**
     1144         * @ticket 44387
     1145         * @group ms-required
     1146         */
     1147        public function test_get_object_subtype_for_blog_with_registered_meta() {
     1148                if ( ! is_site_meta_supported() ) {
     1149                        $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     1150                }
     1151
     1152                $this->assertSame( 'blog', get_object_subtype( 'blog', self::$blog_id ) );
     1153
     1154                register_meta(
     1155                        'blog',
     1156                        'site_rating',
     1157                        array(
     1158                                'single'         => true,
     1159                                'object_subtype' => 'blog',
     1160                        )
     1161                );
     1162                add_site_meta( self::$blog_id, 'site_rating', '5' );
     1163
     1164                $meta = get_registered_metadata( 'blog', self::$blog_id, 'site_rating' );
     1165
     1166                unregister_meta_key( 'blog', 'site_rating', 'blog' );
     1167                delete_site_meta( self::$blog_id, 'site_rating' );
     1168
     1169                $this->assertSame( '5', $meta );
     1170        }
     1171
     1172        /**
    11091173         * Test that attempting to register meta with revisions_enabled set to true on a
    11101174         * post type that does not have revisions enabled fails and throws a `doing_it_wrong` notice.
     
    11361200                $this->assertFalse( $register );
    11371201        }
     1202
     1203        /**
     1204         * @ticket 44387
     1205         * @group ms-required
     1206         */
     1207        public function test_register_site_meta_returns_true_on_success() {
     1208                if ( ! is_site_meta_supported() ) {
     1209                        $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     1210                }
     1211
     1212                $result = register_site_meta( 'test_site_meta_key', array( 'single' => true ) );
     1213                unregister_site_meta( 'test_site_meta_key' );
     1214
     1215                $this->assertTrue( $result );
     1216        }
     1217
     1218        /**
     1219         * @ticket 44387
     1220         * @group ms-required
     1221         */
     1222        public function test_unregister_site_meta_returns_true_on_success() {
     1223                if ( ! is_site_meta_supported() ) {
     1224                        $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     1225                }
     1226
     1227                register_site_meta( 'test_site_meta_key', array( 'single' => true ) );
     1228                $result = unregister_site_meta( 'test_site_meta_key' );
     1229
     1230                $this->assertTrue( $result );
     1231        }
    11381232}
Note: See TracChangeset for help on using the changeset viewer.