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-admin/includes/ms.php

    r43571 r43654  
    7474
    7575    $blog = get_site( $blog_id );
    76     /**
    77      * Fires before a site is deleted.
    78      *
    79      * @since MU (3.0.0)
    80      *
    81      * @param int  $blog_id The site ID.
    82      * @param bool $drop    True if site's table should be dropped. Default is false.
    83      */
    84     do_action( 'delete_blog', $blog_id, $drop );
    85 
    86     $users = get_users(
    87         array(
    88             'blog_id' => $blog_id,
    89             'fields'  => 'ids',
    90         )
    91     );
    92 
    93     // Remove users from this blog.
    94     if ( ! empty( $users ) ) {
    95         foreach ( $users as $user_id ) {
    96             remove_user_from_blog( $user_id, $blog_id );
    97         }
    98     }
    99 
    100     update_blog_status( $blog_id, 'deleted', 1 );
    10176
    10277    $current_network = get_network();
     
    12095
    12196    if ( $drop ) {
    122         $uploads = wp_get_upload_dir();
    123 
    124         $tables = $wpdb->tables( 'blog' );
    125         /**
    126          * Filters the tables to drop when the site is deleted.
    127          *
    128          * @since MU (3.0.0)
    129          *
    130          * @param string[] $tables  Array of names of the site tables to be dropped.
    131          * @param int      $blog_id The ID of the site to drop tables for.
    132          */
    133         $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $blog_id );
    134 
    135         foreach ( (array) $drop_tables as $table ) {
    136             $wpdb->query( "DROP TABLE IF EXISTS `$table`" );
    137         }
    138 
    139         if ( is_site_meta_supported() ) {
    140             $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $blog_id ) );
    141             foreach ( $blog_meta_ids as $mid ) {
    142                 delete_metadata_by_mid( 'blog', $mid );
     97        wp_delete_site( $blog_id );
     98    } else {
     99        /** This action is documented in wp-includes/ms-blogs.php */
     100        do_action_deprecated( 'delete_blog', array( $blog_id, false ), '5.0.0' );
     101
     102        $users = get_users(
     103            array(
     104                'blog_id' => $blog_id,
     105                'fields'  => 'ids',
     106            )
     107        );
     108
     109        // Remove users from this blog.
     110        if ( ! empty( $users ) ) {
     111            foreach ( $users as $user_id ) {
     112                remove_user_from_blog( $user_id, $blog_id );
    143113            }
    144114        }
    145115
    146         wp_delete_site( $blog_id );
    147 
    148         /**
    149          * Filters the upload base directory to delete when the site is deleted.
    150          *
    151          * @since MU (3.0.0)
    152          *
    153          * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
    154          * @param int    $blog_id            The site ID.
    155          */
    156         $dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id );
    157         $dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
    158         $top_dir = $dir;
    159         $stack   = array( $dir );
    160         $index   = 0;
    161 
    162         while ( $index < count( $stack ) ) {
    163             // Get indexed directory from stack
    164             $dir = $stack[ $index ];
    165 
    166             $dh = @opendir( $dir );
    167             if ( $dh ) {
    168                 while ( ( $file = @readdir( $dh ) ) !== false ) {
    169                     if ( $file == '.' || $file == '..' ) {
    170                         continue;
    171                     }
    172 
    173                     if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
    174                         $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
    175                     } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
    176                         @unlink( $dir . DIRECTORY_SEPARATOR . $file );
    177                     }
    178                 }
    179                 @closedir( $dh );
    180             }
    181             $index++;
    182         }
    183 
    184         $stack = array_reverse( $stack ); // Last added dirs are deepest
    185         foreach ( (array) $stack as $dir ) {
    186             if ( $dir != $top_dir ) {
    187                 @rmdir( $dir );
    188             }
    189         }
    190 
    191         clean_blog_cache( $blog );
    192     }
    193 
    194     /**
    195      * Fires after the site is deleted from the network.
    196      *
    197      * @since 4.8.0
    198      *
    199      * @param int  $blog_id The site ID.
    200      * @param bool $drop    True if site's tables should be dropped. Default is false.
    201      */
    202     do_action( 'deleted_blog', $blog_id, $drop );
     116        update_blog_status( $blog_id, 'deleted', 1 );
     117
     118        /** This action is documented in wp-includes/ms-blogs.php */
     119        do_action_deprecated( 'deleted_blog', array( $blog_id, false ), '5.0.0' );
     120    }
    203121
    204122    if ( $switch ) {
Note: See TracChangeset for help on using the changeset viewer.