Make WordPress Core


Ignore:
Timestamp:
09/24/2018 03:08:32 PM (6 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/src/wp-includes/ms-functions.php

    r43571 r43654  
    12861286 * @param string $title      The new site's title.
    12871287 * @param int    $user_id    The user ID of the new site's admin.
    1288  * @param array  $meta       Optional. Array of key=>value pairs used to set initial site options.
     1288 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
    12891289 *                           If valid status keys are included ('public', 'archived', 'mature',
    12901290 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
     
    12941294 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
    12951295 */
    1296 function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 ) {
     1296function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
    12971297    $defaults = array(
    12981298        'public' => 0,
    1299         'WPLANG' => get_network_option( $network_id, 'WPLANG' ),
    13001299    );
    1301     $meta     = wp_parse_args( $meta, $defaults );
     1300    $options  = wp_parse_args( $options, $defaults );
    13021301
    13031302    $title   = strip_tags( $title );
     
    13211320            'network_id' => $network_id,
    13221321        ),
    1323         array_intersect_key(
    1324             $meta,
    1325             array_flip( $site_data_whitelist )
    1326         )
     1322        array_intersect_key( $options, array_flip( $site_data_whitelist ) )
    13271323    );
    13281324
    1329     $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) );
    1330 
    1331     remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 );
    1332     $blog_id = wp_insert_site( $site_data );
    1333     add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
     1325    // Data to pass to wp_initialize_site().
     1326    $site_initialization_data = array(
     1327        'title'   => $title,
     1328        'user_id' => $user_id,
     1329        'options' => array_diff_key( $options, array_flip( $site_data_whitelist ) ),
     1330    );
     1331
     1332    $blog_id = wp_insert_site( array_merge( $site_data, $site_initialization_data ) );
    13341333
    13351334    if ( is_wp_error( $blog_id ) ) {
     
    13371336    }
    13381337
    1339     switch_to_blog( $blog_id );
    1340     install_blog( $blog_id, $title );
    1341     wp_install_defaults( $user_id );
    1342 
    1343     add_user_to_blog( $blog_id, $user_id, 'administrator' );
    1344 
    1345     foreach ( $meta as $key => $value ) {
    1346         update_option( $key, $value );
    1347     }
    1348 
    1349     update_option( 'blog_public', (int) $site_data['public'] );
    1350 
    1351     if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
    1352         update_user_meta( $user_id, 'primary_blog', $blog_id );
    1353     }
    1354 
    1355     restore_current_blog();
    1356 
    1357     $site = get_site( $blog_id );
    1358 
    1359     /**
    1360      * Fires immediately after a new site is created.
    1361      *
    1362      * @since MU (3.0.0)
    1363      *
    1364      * @param int    $blog_id    Site ID.
    1365      * @param int    $user_id    User ID.
    1366      * @param string $domain     Site domain.
    1367      * @param string $path       Site path.
    1368      * @param int    $network_id Network ID. Only relevant on multi-network installations.
    1369      * @param array  $meta       Meta data. Used to set initial site options.
    1370      */
    1371     do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
    1372 
    13731338    wp_cache_set( 'last_changed', microtime(), 'sites' );
    13741339
     
    13831348 *
    13841349 * @since MU (3.0.0)
    1385  *
    1386  * @param int    $blog_id    The new site's ID.
    1387  * @param string $deprecated Not used.
     1350 * @since 5.0.0 $blog_id now supports input from the {@see 'wp_initialize_site'} action.
     1351 *
     1352 * @param WP_Site|int $blog_id    The new site's object or ID.
     1353 * @param string      $deprecated Not used.
    13881354 * @return bool
    13891355 */
    13901356function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
     1357    if ( is_object( $blog_id ) ) {
     1358        $blog_id = $blog_id->blog_id;
     1359    }
     1360
    13911361    if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
    13921362        return false;
     
    15271497     */
    15281498    return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
    1529 }
    1530 
    1531 /**
    1532  * Install an empty blog.
    1533  *
    1534  * Creates the new blog tables and options. If calling this function
    1535  * directly, be sure to use switch_to_blog() first, so that $wpdb
    1536  * points to the new blog.
    1537  *
    1538  * @since MU (3.0.0)
    1539  *
    1540  * @global wpdb     $wpdb
    1541  * @global WP_Roles $wp_roles
    1542  *
    1543  * @param int    $blog_id    The value returned by wp_insert_site().
    1544  * @param string $blog_title The title of the new site.
    1545  */
    1546 function install_blog( $blog_id, $blog_title = '' ) {
    1547     global $wpdb, $wp_roles;
    1548 
    1549     // Cast for security
    1550     $blog_id = (int) $blog_id;
    1551 
    1552     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    1553 
    1554     $suppress = $wpdb->suppress_errors();
    1555     if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
    1556         die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
    1557     }
    1558     $wpdb->suppress_errors( $suppress );
    1559 
    1560     $url = get_blogaddress_by_id( $blog_id );
    1561 
    1562     // Set everything up
    1563     make_db_current_silent( 'blog' );
    1564     populate_options();
    1565     populate_roles();
    1566 
    1567     // populate_roles() clears previous role definitions so we start over.
    1568     $wp_roles = new WP_Roles();
    1569 
    1570     $siteurl = $home = untrailingslashit( $url );
    1571 
    1572     if ( ! is_subdomain_install() ) {
    1573 
    1574         if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
    1575             $siteurl = set_url_scheme( $siteurl, 'https' );
    1576         }
    1577         if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
    1578             $home = set_url_scheme( $home, 'https' );
    1579         }
    1580     }
    1581 
    1582     update_option( 'siteurl', $siteurl );
    1583     update_option( 'home', $home );
    1584 
    1585     if ( get_site_option( 'ms_files_rewriting' ) ) {
    1586         update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
    1587     } else {
    1588         update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
    1589     }
    1590 
    1591     update_option( 'blogname', wp_unslash( $blog_title ) );
    1592     update_option( 'admin_email', '' );
    1593 
    1594     // remove all perms
    1595     $table_prefix = $wpdb->get_blog_prefix();
    1596     delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
    1597     delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
    1598 }
    1599 
    1600 /**
    1601  * Set blog defaults.
    1602  *
    1603  * This function creates a row in the wp_blogs table.
    1604  *
    1605  * @since MU (3.0.0)
    1606  * @deprecated MU
    1607  * @deprecated Use wp_install_defaults()
    1608  *
    1609  * @global wpdb $wpdb WordPress database abstraction object.
    1610  *
    1611  * @param int $blog_id Ignored in this function.
    1612  * @param int $user_id
    1613  */
    1614 function install_blog_defaults( $blog_id, $user_id ) {
    1615     global $wpdb;
    1616 
    1617     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    1618 
    1619     $suppress = $wpdb->suppress_errors();
    1620 
    1621     wp_install_defaults( $user_id );
    1622 
    1623     $wpdb->suppress_errors( $suppress );
    16241499}
    16251500
     
    20251900 *
    20261901 * @since MU (3.0.0)
     1902 * @since 5.0.0 Parameters now support input from the {@see 'wp_initialize_site'} action.
    20271903 *
    20281904 * @global wpdb $wpdb WordPress database abstraction object.
    20291905 *
    2030  * @param int $blog_id
    2031  * @param int $user_id
     1906 * @param WP_Site|int $blog_id The new site's object or ID.
     1907 * @param int|array   $user_id User ID, or array of arguments including 'user_id'.
    20321908 */
    20331909function wpmu_log_new_registrations( $blog_id, $user_id ) {
    20341910    global $wpdb;
     1911
     1912    if ( is_object( $blog_id ) ) {
     1913        $blog_id = $blog_id->blog_id;
     1914    }
     1915
     1916    if ( is_array( $user_id ) ) {
     1917        $user_id = ! empty( $user_id['user_id'] ) ? $user_id['user_id'] : 0;
     1918    }
     1919
    20351920    $user = get_userdata( (int) $user_id );
    20361921    if ( $user ) {
Note: See TracChangeset for help on using the changeset viewer.