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/src/wp-includes/ms-deprecated.php

    r43548 r43654  
    581581    return $site_id;
    582582}
     583
     584/**
     585 * Install an empty blog.
     586 *
     587 * Creates the new blog tables and options. If calling this function
     588 * directly, be sure to use switch_to_blog() first, so that $wpdb
     589 * points to the new blog.
     590 *
     591 * @since MU (3.0.0)
     592 * @deprecated 5.0.0
     593 *
     594 * @global wpdb     $wpdb
     595 * @global WP_Roles $wp_roles
     596 *
     597 * @param int    $blog_id    The value returned by wp_insert_site().
     598 * @param string $blog_title The title of the new site.
     599 */
     600function install_blog( $blog_id, $blog_title = '' ) {
     601    global $wpdb, $wp_roles;
     602
     603    _deprecated_function( __FUNCTION__, '5.0.0' );
     604
     605    // Cast for security
     606    $blog_id = (int) $blog_id;
     607
     608    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     609
     610    $suppress = $wpdb->suppress_errors();
     611    if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
     612        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>' );
     613    }
     614    $wpdb->suppress_errors( $suppress );
     615
     616    $url = get_blogaddress_by_id( $blog_id );
     617
     618    // Set everything up
     619    make_db_current_silent( 'blog' );
     620    populate_options();
     621    populate_roles();
     622
     623    // populate_roles() clears previous role definitions so we start over.
     624    $wp_roles = new WP_Roles();
     625
     626    $siteurl = $home = untrailingslashit( $url );
     627
     628    if ( ! is_subdomain_install() ) {
     629
     630        if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
     631            $siteurl = set_url_scheme( $siteurl, 'https' );
     632        }
     633        if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
     634            $home = set_url_scheme( $home, 'https' );
     635        }
     636    }
     637
     638    update_option( 'siteurl', $siteurl );
     639    update_option( 'home', $home );
     640
     641    if ( get_site_option( 'ms_files_rewriting' ) ) {
     642        update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
     643    } else {
     644        update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
     645    }
     646
     647    update_option( 'blogname', wp_unslash( $blog_title ) );
     648    update_option( 'admin_email', '' );
     649
     650    // remove all perms
     651    $table_prefix = $wpdb->get_blog_prefix();
     652    delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
     653    delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
     654}
     655
     656/**
     657 * Set blog defaults.
     658 *
     659 * This function creates a row in the wp_blogs table.
     660 *
     661 * @since MU (3.0.0)
     662 * @deprecated MU
     663 * @deprecated Use wp_install_defaults()
     664 *
     665 * @global wpdb $wpdb WordPress database abstraction object.
     666 *
     667 * @param int $blog_id Ignored in this function.
     668 * @param int $user_id
     669 */
     670function install_blog_defaults( $blog_id, $user_id ) {
     671    global $wpdb;
     672
     673    _deprecated_function( __FUNCTION__, 'MU' );
     674
     675    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     676
     677    $suppress = $wpdb->suppress_errors();
     678
     679    wp_install_defaults( $user_id );
     680
     681    $wpdb->suppress_errors( $suppress );
     682}
Note: See TracChangeset for help on using the changeset viewer.