WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/01/2018 01:05:44 PM (14 months ago)
Author:
flixos90
Message:

Multisite: Complete the new CRUD API for managing sites.

New functions wp_insert_site( $data ), wp_update_site( $id, $data ) and wp_delete_site( $id ) are introduced to manage site rows in the wp_blogs table, forming the new CRUD API together with the existing get_site() / get_sites(). The new API provides various benefits over the previously existing API, fixing several cache invalidation issues and being hook-driven so that normalization and validation of the passed data can be fully customized.

New hooks introduced as part of this are the actions wp_insert_site, wp_update_site, wp_delete_site, wp_validate_site_data and the filter wp_normalize_site_data.

At this point, wp_insert_site() does not handle setting up the site's database tables, and wp_delete_site() does not handle dropping the site's database tables, so the two can not yet be used directly as full replacements of wpmu_create_blog() and wpmu_delete_blog(). Managing the site's database tables will be added via hooks as part of the follow-up ticket #41333.

The existing functions wpmu_create_blog(), update_blog_details(), and wpmu_delete_blog() make use of the respective new counterpart and will be obsolete once #41333 has been completed.

Props flixos90, jeremyfelt, spacedmonkey.
Fixes #40364.

File:
1 edited

Legend:

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

    r42976 r43548  
    12741274    $meta     = wp_parse_args( $meta, $defaults );
    12751275
    1276     $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
    1277 
    1278     if ( is_subdomain_install() ) {
    1279         $domain = str_replace( '@', '', $domain );
    1280     }
    1281 
    12821276    $title   = strip_tags( $title );
    12831277    $user_id = (int) $user_id;
    1284 
    1285     if ( empty( $path ) ) {
    1286         $path = '/';
    1287     }
    12881278
    12891279    // Check if the domain has been used already. We should return an error message.
     
    12961286    }
    12971287
    1298     if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) {
    1299         return new WP_Error( 'insert_blog', __( 'Could not create site.' ) );
     1288    $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
     1289
     1290    $site_data = array_merge(
     1291        array(
     1292            'domain'     => $domain,
     1293            'path'       => $path,
     1294            'network_id' => $network_id,
     1295        ),
     1296        array_intersect_key(
     1297            $meta,
     1298            array_flip( $site_data_whitelist )
     1299        )
     1300    );
     1301
     1302    $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) );
     1303
     1304    remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 );
     1305    $blog_id = wp_insert_site( $site_data );
     1306    add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
     1307
     1308    if ( is_wp_error( $blog_id ) ) {
     1309        return $blog_id;
    13001310    }
    13011311
     
    13071317
    13081318    foreach ( $meta as $key => $value ) {
    1309         if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
    1310             update_blog_status( $blog_id, $key, $value );
    1311         } else {
    1312             update_option( $key, $value );
    1313         }
    1314     }
    1315 
    1316     update_option( 'blog_public', (int) $meta['public'] );
     1319        update_option( $key, $value );
     1320    }
     1321
     1322    update_option( 'blog_public', (int) $site_data['public'] );
    13171323
    13181324    if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
     
    13211327
    13221328    restore_current_blog();
     1329
     1330    $site = get_site( $blog_id );
     1331
    13231332    /**
    13241333     * Fires immediately after a new site is created.
     
    13331342     * @param array  $meta       Meta data. Used to set initial site options.
    13341343     */
    1335     do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta );
     1344    do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
    13361345
    13371346    wp_cache_set( 'last_changed', microtime(), 'sites' );
     
    14871496
    14881497/**
    1489  * Store basic site info in the blogs table.
    1490  *
    1491  * This function creates a row in the wp_blogs table and returns
    1492  * the new blog's ID. It is the first step in creating a new blog.
    1493  *
    1494  * @since MU (3.0.0)
    1495  *
    1496  * @global wpdb $wpdb WordPress database abstraction object.
    1497  *
    1498  * @param string $domain     The domain of the new site.
    1499  * @param string $path       The path of the new site.
    1500  * @param int    $network_id Unless you're running a multi-network installation, be sure to set this value to 1.
    1501  * @return int|false The ID of the new row
    1502  */
    1503 function insert_blog( $domain, $path, $network_id ) {
    1504     global $wpdb;
    1505 
    1506     $path       = trailingslashit( $path );
    1507     $network_id = (int) $network_id;
    1508 
    1509     $result = $wpdb->insert(
    1510         $wpdb->blogs, array(
    1511             'site_id'    => $network_id,
    1512             'domain'     => $domain,
    1513             'path'       => $path,
    1514             'registered' => current_time( 'mysql' ),
    1515         )
    1516     );
    1517     if ( ! $result ) {
    1518         return false;
    1519     }
    1520 
    1521     $blog_id = $wpdb->insert_id;
    1522     clean_blog_cache( $blog_id );
    1523 
    1524     wp_maybe_update_network_site_counts( $network_id );
    1525 
    1526     return $blog_id;
    1527 }
    1528 
    1529 /**
    15301498 * Install an empty blog.
    15311499 *
     
    15391507 * @global WP_Roles $wp_roles
    15401508 *
    1541  * @param int    $blog_id    The value returned by insert_blog().
     1509 * @param int    $blog_id    The value returned by wp_insert_site().
    15421510 * @param string $blog_title The title of the new site.
    15431511 */
Note: See TracChangeset for help on using the changeset viewer.