Make WordPress Core


Ignore:
Timestamp:
09/24/2018 03:08:32 PM (7 years ago)
Author:
flixos90
Message:

Multisite: Introduce a site initialization and uninitialization API.

This changeset makes the new CRUD API for sites introduced in [43548] usable for real-world sites. A new function wp_initialize_site(), which takes care of creating a site's database tables and populating them with initial values, is hooked into the site insertion process that is initiated when calling wp_insert_site(). Similarly, a new function wp_uninitialize_site(), which takes care of dropping a site's database tables, is hooked into the site deletion process that is initiated when calling wp_delete_site().

A new function wp_is_site_initialized() completes the API, allowing to check whether a site is initialized. Since this function always makes a database request in its default behavior, it should be called with caution. Plugins that would like to use site initialization in special ways can leverage a pre_wp_is_site_initialized filter to alter that default behavior.

The separate handling of the site's row in the wp_blogs database table and the actual site setup allows for more flexibility in controlling whether or how a site's data is set up. For example, a unit test that only checks data from the site's database table row can unhook the site initialization process to improve performance. At the same time, developers consuming the new sites API only need to know about the CRUD functions, since the initialization and uninitialization processes happen internally.

With this changeset, the foundation for a sites REST API endpoint is fully available. The previously recommended functions wpmu_create_blog() and wpmu_delete_blog() now call the new respective function internally. Further follow-up work to this includes replacing calls to wpmu_create_blog() with wp_insert_site(), update_blog_details() with wp_update_site() and wpmu_delete_blog() with wp_delete_blog() throughout the codebase.

As a side-effect of this work, the wpmu_new_blog, delete_blog, and deleted_blog actions and the install_blog() function have been deprecated.

Fixes #41333. See #40364.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/site.php

    r43571 r43654  
    1010     */
    1111    class Tests_Multisite_Site extends WP_UnitTestCase {
    12         protected $suppress          = false;
    13         protected $site_status_hooks = array();
     12        protected $suppress                = false;
     13        protected $site_status_hooks       = array();
     14        protected $wp_initialize_site_args = array();
    1415        protected static $network_ids;
    1516        protected static $site_ids;
     17        protected static $uninitialized_site_id;
    1618
    1719        function setUp() {
     
    5759            }
    5860            unset( $id );
     61
     62            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     63            self::$uninitialized_site_id = wp_insert_site( array(
     64                'domain'  => 'uninitialized.org',
     65                'path'    => '/',
     66                'site_id' => self::$network_ids['make.wordpress.org/'],
     67            ) );
     68            add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
    5969        }
    6070
    6171        public static function wpTearDownAfterClass() {
    6272            global $wpdb;
     73
     74            remove_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10 );
     75            wp_delete_site( self::$uninitialized_site_id );
     76            add_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10, 1 );
    6377
    6478            foreach ( self::$site_ids as $id ) {
     
    12671281         */
    12681282        public function test_wp_insert_site( $site_data, $expected_data ) {
     1283            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    12691284            $site_id = wp_insert_site( $site_data );
    12701285
     
    13611376         */
    13621377        public function test_wp_insert_site_empty_domain() {
     1378            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    13631379            $site_id = wp_insert_site( array( 'public' => 0 ) );
    13641380
     
    14981514
    14991515        /**
     1516         * @ticket 41333
     1517         */
     1518        public function test_wp_delete_site_validate_site_deletion_action() {
     1519            add_action( 'wp_validate_site_deletion', array( $this, 'action_wp_validate_site_deletion_prevent_deletion' ) );
     1520            $result = wp_delete_site( self::$site_ids['make.wordpress.org/'] );
     1521            $this->assertWPError( $result );
     1522            $this->assertSame( 'action_does_not_like_deletion', $result->get_error_code() );
     1523        }
     1524
     1525        public function action_wp_validate_site_deletion_prevent_deletion( $errors ) {
     1526            $errors->add( 'action_does_not_like_deletion', 'You cannot delete this site because the action does not like it.' );
     1527        }
     1528
     1529        /**
    15001530         * @ticket 40364
    15011531         * @dataProvider data_wp_normalize_site_data
     
    17101740        public function test_site_dates_are_gmt() {
    17111741            $first_date = current_time( 'mysql', true );
    1712             $site_id    = wp_insert_site(
     1742
     1743            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     1744            $site_id = wp_insert_site(
    17131745                array(
    17141746                    'domain'     => 'valid-domain.com',
     
    20162048            $this->site_status_hooks[ current_action() ] = $site_id;
    20172049        }
     2050
     2051        /**
     2052         * @ticket 41333
     2053         * @dataProvider data_wp_initialize_site
     2054         */
     2055        public function test_wp_initialize_site( $args, $expected_options, $expected_meta ) {
     2056            $result = wp_initialize_site( self::$uninitialized_site_id, $args );
     2057
     2058            switch_to_blog( self::$uninitialized_site_id );
     2059
     2060            $options = array();
     2061            foreach ( $expected_options as $option => $value ) {
     2062                $options[ $option ] = get_option( $option );
     2063            }
     2064
     2065            $meta = array();
     2066            foreach ( $expected_meta as $meta_key => $value ) {
     2067                $meta[ $meta_key ] = get_site_meta( self::$uninitialized_site_id, $meta_key, true );
     2068            }
     2069
     2070            restore_current_blog();
     2071
     2072            $initialized = wp_is_site_initialized( self::$uninitialized_site_id );
     2073
     2074            wp_uninitialize_site( self::$uninitialized_site_id );
     2075
     2076            $this->assertTrue( $result );
     2077            $this->assertTrue( $initialized );
     2078            $this->assertEquals( $expected_options, $options );
     2079            $this->assertEquals( $expected_meta, $meta );
     2080        }
     2081
     2082        public function data_wp_initialize_site() {
     2083            return array(
     2084                array(
     2085                    array(),
     2086                    array(
     2087                        'home'        => 'http://uninitialized.org',
     2088                        'siteurl'     => 'http://uninitialized.org',
     2089                        'admin_email' => '',
     2090                        'blog_public' => '1',
     2091                    ),
     2092                    array(),
     2093                ),
     2094                array(
     2095                    array(
     2096                        'options' => array(
     2097                            'home'    => 'https://uninitialized.org',
     2098                            'siteurl' => 'https://uninitialized.org',
     2099                            'key'     => 'value',
     2100                        ),
     2101                        'meta'    => array(
     2102                            'key1' => 'value1',
     2103                            'key2' => 'value2',
     2104                        ),
     2105                    ),
     2106                    array(
     2107                        'home'    => 'https://uninitialized.org',
     2108                        'siteurl' => 'https://uninitialized.org',
     2109                        'key'     => 'value',
     2110                    ),
     2111                    array(
     2112                        'key1' => 'value1',
     2113                        'key2' => 'value2',
     2114                        'key3' => '',
     2115                    ),
     2116                ),
     2117                array(
     2118                    array(
     2119                        'title'   => 'My New Site',
     2120                        'options' => array(
     2121                            'blogdescription' => 'Just My New Site',
     2122                        ),
     2123                    ),
     2124                    array(
     2125                        'blogname'        => 'My New Site',
     2126                        'blogdescription' => 'Just My New Site',
     2127                    ),
     2128                    array(),
     2129                ),
     2130            );
     2131        }
     2132
     2133        /**
     2134         * @ticket 41333
     2135         */
     2136        public function test_wp_initialize_site_user_roles() {
     2137            global $wpdb;
     2138
     2139            $result = wp_initialize_site( self::$uninitialized_site_id, array() );
     2140
     2141            switch_to_blog( self::$uninitialized_site_id );
     2142            $table_prefix = $wpdb->get_blog_prefix( self::$uninitialized_site_id );
     2143            $roles        = get_option( $table_prefix . 'user_roles' );
     2144            restore_current_blog();
     2145
     2146            wp_uninitialize_site( self::$uninitialized_site_id );
     2147
     2148            $this->assertTrue( $result );
     2149            $this->assertEqualSets(
     2150                array(
     2151                    'administrator',
     2152                    'editor',
     2153                    'author',
     2154                    'contributor',
     2155                    'subscriber',
     2156                ),
     2157                array_keys( $roles )
     2158            );
     2159        }
     2160
     2161        /**
     2162         * @ticket 41333
     2163         */
     2164        public function test_wp_initialize_site_user_is_admin() {
     2165            $result = wp_initialize_site( self::$uninitialized_site_id, array( 'user_id' => 1 ) );
     2166
     2167            switch_to_blog( self::$uninitialized_site_id );
     2168            $user_is_admin = user_can( 1, 'manage_options' );
     2169            $admin_email   = get_option( 'admin_email' );
     2170            restore_current_blog();
     2171
     2172            wp_uninitialize_site( self::$uninitialized_site_id );
     2173
     2174            $this->assertTrue( $result );
     2175            $this->assertTrue( $user_is_admin );
     2176            $this->assertEquals( get_userdata( 1 )->user_email, $admin_email );
     2177        }
     2178
     2179        /**
     2180         * @ticket 41333
     2181         */
     2182        public function test_wp_initialize_site_args_filter() {
     2183            add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args' ), 10, 3 );
     2184            $result = wp_initialize_site( self::$uninitialized_site_id, array( 'title' => 'My Site' ) );
     2185
     2186            switch_to_blog( self::$uninitialized_site_id );
     2187            $site_title = get_option( 'blogname' );
     2188            restore_current_blog();
     2189
     2190            wp_uninitialize_site( self::$uninitialized_site_id );
     2191
     2192            $this->assertSame(
     2193                sprintf( 'My Site %1$d in Network %2$d', self::$uninitialized_site_id, get_site( self::$uninitialized_site_id )->network_id ),
     2194                $site_title
     2195            );
     2196        }
     2197
     2198        public function filter_wp_initialize_site_args( $args, $site, $network ) {
     2199            $args['title'] = sprintf( 'My Site %1$d in Network %2$d', $site->id, $network->id );
     2200
     2201            return $args;
     2202        }
     2203
     2204        /**
     2205         * @ticket 41333
     2206         */
     2207        public function test_wp_initialize_site_empty_id() {
     2208            $result = wp_initialize_site( 0 );
     2209            $this->assertWPError( $result );
     2210            $this->assertSame( 'site_empty_id', $result->get_error_code() );
     2211        }
     2212
     2213        /**
     2214         * @ticket 41333
     2215         */
     2216        public function test_wp_initialize_site_invalid_id() {
     2217            $result = wp_initialize_site( 123 );
     2218            $this->assertWPError( $result );
     2219            $this->assertSame( 'site_invalid_id', $result->get_error_code() );
     2220        }
     2221
     2222        /**
     2223         * @ticket 41333
     2224         */
     2225        public function test_wp_initialize_site_already_initialized() {
     2226            $result = wp_initialize_site( get_current_blog_id() );
     2227            $this->assertWPError( $result );
     2228            $this->assertSame( 'site_already_initialized', $result->get_error_code() );
     2229        }
     2230
     2231        /**
     2232         * @ticket 41333
     2233         */
     2234        public function test_wp_uninitialize_site() {
     2235            $site_id = self::factory()->blog->create();
     2236
     2237            $result = wp_uninitialize_site( $site_id );
     2238            $this->assertTrue( $result );
     2239            $this->assertFalse( wp_is_site_initialized( $site_id ) );
     2240        }
     2241
     2242        /**
     2243         * @ticket 41333
     2244         */
     2245        public function test_wp_uninitialize_site_empty_id() {
     2246            $result = wp_uninitialize_site( 0 );
     2247            $this->assertWPError( $result );
     2248            $this->assertSame( 'site_empty_id', $result->get_error_code() );
     2249        }
     2250
     2251        /**
     2252         * @ticket 41333
     2253         */
     2254        public function test_wp_uninitialize_site_invalid_id() {
     2255            $result = wp_uninitialize_site( 123 );
     2256            $this->assertWPError( $result );
     2257            $this->assertSame( 'site_invalid_id', $result->get_error_code() );
     2258        }
     2259
     2260        /**
     2261         * @ticket 41333
     2262         */
     2263        public function test_wp_uninitialize_site_already_uninitialized() {
     2264            $result = wp_uninitialize_site( self::$uninitialized_site_id );
     2265            $this->assertWPError( $result );
     2266            $this->assertSame( 'site_already_uninitialized', $result->get_error_code() );
     2267        }
     2268
     2269        /**
     2270         * @ticket 41333
     2271         */
     2272        public function test_wp_is_site_initialized() {
     2273            $this->assertTrue( wp_is_site_initialized( get_current_blog_id() ) );
     2274            $this->assertFalse( wp_is_site_initialized( self::$uninitialized_site_id ) );
     2275        }
     2276
     2277        /**
     2278         * @ticket 41333
     2279         */
     2280        public function test_wp_is_site_initialized_prefilter() {
     2281            add_filter( 'pre_wp_is_site_initialized', '__return_false' );
     2282            $this->assertFalse( wp_is_site_initialized( get_current_blog_id() ) );
     2283
     2284            add_filter( 'pre_wp_is_site_initialized', '__return_true' );
     2285            $this->assertTrue( wp_is_site_initialized( self::$uninitialized_site_id ) );
     2286        }
     2287
     2288        /**
     2289         * @ticket 41333
     2290         */
     2291        public function test_wp_insert_site_forwards_args_to_wp_initialize_site() {
     2292            $args = array(
     2293                'user_id' => 1,
     2294                'title'   => 'My Site',
     2295                'options' => array( 'option1' => 'value1' ),
     2296                'meta'    => array( 'meta1' => 'value1' ),
     2297            );
     2298
     2299            add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args_catch_args' ) );
     2300            $site_id = wp_insert_site(
     2301                array_merge(
     2302                    array(
     2303                        'domain' => 'testsite.org',
     2304                        'path'   => '/',
     2305                    ),
     2306                    $args
     2307                )
     2308            );
     2309            $passed_args = $this->wp_initialize_site_args;
     2310
     2311            $this->wp_initialize_site_args = null;
     2312
     2313            $this->assertEqualSetsWithIndex( $args, $passed_args );
     2314        }
     2315
     2316        public function filter_wp_initialize_site_args_catch_args( $args ) {
     2317            $this->wp_initialize_site_args = $args;
     2318
     2319            return $args;
     2320        }
    20182321    }
    20192322
Note: See TracChangeset for help on using the changeset viewer.