Make WordPress Core

Ticket #40364: 40364.2.diff

File 40364.2.diff, 24.3 KB (added by flixos90, 6 years ago)
  • src/wp-admin/includes/ms.php

     
    5353 * Delete a site.
    5454 *
    5555 * @since 3.0.0
     56 * @since 4.9.0 Now uses `wp_delete_site()` to delete the site row from the database.
    5657 *
    5758 * @global wpdb $wpdb WordPress database abstraction object.
    5859 *
     
    127128                        $wpdb->query( "DROP TABLE IF EXISTS `$table`" );
    128129                }
    129130
    130                 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
     131                wp_delete_site( $blog_id );
    131132
    132133                /**
    133134                 * Filters the upload base directory to delete when the site is deleted.
     
    169170                        if ( $dir != $top_dir)
    170171                        @rmdir( $dir );
    171172                }
    172 
    173                 clean_blog_cache( $blog );
    174173        }
    175174
    176175        /**
  • src/wp-includes/ms-blogs.php

     
    295295 * Update the details for a blog. Updates the blogs table for a given blog id.
    296296 *
    297297 * @since MU (3.0.0)
     298 * @since 4.9.0 Now wraps `wp_update_site()`.
    298299 *
    299300 * @global wpdb $wpdb WordPress database abstraction object.
    300301 *
     
    311312        if ( is_object($details) )
    312313                $details = get_object_vars($details);
    313314
    314         $current_details = get_site( $blog_id );
    315         if ( empty($current_details) )
     315        $site = wp_update_site( $blog_id, $details );
     316        if ( is_wp_error( $site ) ) {
    316317                return false;
    317 
    318         $current_details = get_object_vars($current_details);
    319 
    320         $details = array_merge($current_details, $details);
    321         $details['last_updated'] = current_time('mysql', true);
    322 
    323         $update_details = array();
    324         $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    325         foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
    326                 if ( 'path' === $field ) {
    327                         $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
    328                 }
    329 
    330                 $update_details[ $field ] = $details[ $field ];
    331         }
    332 
    333         $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
    334 
    335         if ( false === $result )
    336                 return false;
    337 
    338         // If spam status changed, issue actions.
    339         if ( $details['spam'] != $current_details['spam'] ) {
    340                 if ( $details['spam'] == 1 ) {
    341                         /**
    342                          * Fires when the 'spam' status is added to a blog.
    343                          *
    344                          * @since MU (3.0.0)
    345                          *
    346                          * @param int $blog_id Blog ID.
    347                          */
    348                         do_action( 'make_spam_blog', $blog_id );
    349                 } else {
    350                         /**
    351                          * Fires when the 'spam' status is removed from a blog.
    352                          *
    353                          * @since MU (3.0.0)
    354                          *
    355                          * @param int $blog_id Blog ID.
    356                          */
    357                         do_action( 'make_ham_blog', $blog_id );
    358                 }
    359         }
    360 
    361         // If mature status changed, issue actions.
    362         if ( $details['mature'] != $current_details['mature'] ) {
    363                 if ( $details['mature'] == 1 ) {
    364                         /**
    365                          * Fires when the 'mature' status is added to a blog.
    366                          *
    367                          * @since 3.1.0
    368                          *
    369                          * @param int $blog_id Blog ID.
    370                          */
    371                         do_action( 'mature_blog', $blog_id );
    372                 } else {
    373                         /**
    374                          * Fires when the 'mature' status is removed from a blog.
    375                          *
    376                          * @since 3.1.0
    377                          *
    378                          * @param int $blog_id Blog ID.
    379                          */
    380                         do_action( 'unmature_blog', $blog_id );
    381                 }
    382         }
    383 
    384         // If archived status changed, issue actions.
    385         if ( $details['archived'] != $current_details['archived'] ) {
    386                 if ( $details['archived'] == 1 ) {
    387                         /**
    388                          * Fires when the 'archived' status is added to a blog.
    389                          *
    390                          * @since MU (3.0.0)
    391                          *
    392                          * @param int $blog_id Blog ID.
    393                          */
    394                         do_action( 'archive_blog', $blog_id );
    395                 } else {
    396                         /**
    397                          * Fires when the 'archived' status is removed from a blog.
    398                          *
    399                          * @since MU (3.0.0)
    400                          *
    401                          * @param int $blog_id Blog ID.
    402                          */
    403                         do_action( 'unarchive_blog', $blog_id );
    404                 }
    405         }
    406 
    407         // If deleted status changed, issue actions.
    408         if ( $details['deleted'] != $current_details['deleted'] ) {
    409                 if ( $details['deleted'] == 1 ) {
    410                         /**
    411                          * Fires when the 'deleted' status is added to a blog.
    412                          *
    413                          * @since 3.5.0
    414                          *
    415                          * @param int $blog_id Blog ID.
    416                          */
    417                         do_action( 'make_delete_blog', $blog_id );
    418                 } else {
    419                         /**
    420                          * Fires when the 'deleted' status is removed from a blog.
    421                          *
    422                          * @since 3.5.0
    423                          *
    424                          * @param int $blog_id Blog ID.
    425                          */
    426                         do_action( 'make_undelete_blog', $blog_id );
    427                 }
    428         }
    429 
    430         if ( isset( $details['public'] ) ) {
    431                 switch_to_blog( $blog_id );
    432                 update_option( 'blog_public', $details['public'] );
    433                 restore_current_blog();
    434318        }
    435319
    436320        refresh_blog_details($blog_id);
     
    498382}
    499383
    500384/**
     385 * Inserts a new site into the database.
     386 *
     387 * @since 4.9.0
     388 *
     389 * @global wpdb $wpdb WordPress database abstraction object.
     390 *
     391 * @param array $data {
     392 *     Data for the new site that should be inserted.
     393 *
     394 *     @type string $domain       Site domain. Must always be provided.
     395 *     @type string $path         Site path. Default '/'.
     396 *     @type int    $site_id      The site's network ID. Default is the main network ID.
     397 *     @type string $registered   When the site was registered, in SQL datetime format. Default is
     398 *                                the current time.
     399 *     @type string $last_updated When the site was last updated, in SQL datetime format. Default is
     400 *                                the value of $registered.
     401 *     @type int    $public       Whether the site is public. Default 1.
     402 *     @type int    $archived     Whether the site is archived. Default 0.
     403 *     @type int    $mature       Whether the site is mature. Default 0.
     404 *     @type int    $spam         Whether the site is spam. Default 0.
     405 *     @type int    $deleted      Whether the site is deleted. Default 0.
     406 *     @type int    $lang_id      The site's language ID. Currently unused. Default 0.
     407 * }
     408 * @return int|WP_Error The new site's ID on success, or error object on failure.
     409 */
     410function wp_insert_site( $data ) {
     411        global $wpdb;
     412
     413        $now = current_time( 'mysql' );
     414
     415        $defaults = array(
     416                'domain'       => '',
     417                'path'         => '',
     418                'site_id'      => 0,
     419                'registered'   => $now,
     420                'last_updated' => $now,
     421                'public'       => 1,
     422                'archived'     => 0,
     423                'mature'       => 0,
     424                'spam'         => 0,
     425                'deleted'      => 0,
     426                'lang_id'      => 0,
     427        );
     428
     429        $compat_keys = array(
     430                'network_id' => 'site_id',
     431        );
     432
     433        foreach ( $compat_keys as $compat_key => $original_compat_key ) {
     434                if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) {
     435                        $data[ $original_compat_key ] = $data[ $compat_key ];
     436                }
     437        }
     438
     439        $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults );
     440
     441        // A domain must always be present.
     442        if ( empty( $data['domain'] ) ) {
     443                return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     444        }
     445
     446        $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
     447
     448        // Use the default network if none is set.
     449        if ( empty( $data['site_id'] ) ) {
     450                $data['site_id'] = get_main_network_id();
     451        }
     452
     453        if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) {
     454                return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
     455        }
     456
     457        $new_site = get_site( $wpdb->insert_id );
     458
     459        clean_blog_cache( $new_site );
     460
     461        /**
     462         * Fires once a site has been inserted into the database.
     463         *
     464         * @since 4.9.0
     465         *
     466         * @param WP_Site $new_site New site object.
     467         */
     468        do_action( 'wp_insert_site', $new_site );
     469
     470        return (int) $new_site->id;
     471}
     472
     473/**
     474 * Updates a site in the database.
     475 *
     476 * @since 4.9.0
     477 *
     478 * @global wpdb $wpdb WordPress database abstraction object.
     479 *
     480 * @param int   $site_id ID of the site that should be updated.
     481 * @param array $data    Site data to update. See wp_insert_site() for the list of supported keys.
     482 * @return int|WP_Error The updated site's ID on success, or error object on failure.
     483 */
     484function wp_update_site( $site_id, $data ) {
     485        global $wpdb;
     486
     487        $old_site = get_site( $site_id );
     488        if ( ! $old_site ) {
     489                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
     490        }
     491
     492        $defaults = $old_site->to_array();
     493        $defaults['last_updated'] = current_time( 'mysql' );
     494        unset( $defaults['blog_id'] );
     495
     496        $compat_keys = array(
     497                'network_id' => 'site_id',
     498        );
     499
     500        foreach ( $compat_keys as $compat_key => $original_compat_key ) {
     501                if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) {
     502                        $data[ $original_compat_key ] = $data[ $compat_key ];
     503                }
     504        }
     505
     506        $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults );
     507
     508        // A domain must always be present.
     509        if ( empty( $data['domain'] ) ) {
     510                return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     511        }
     512
     513        $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
     514
     515        // Use the default network if none is set.
     516        if ( empty( $data['site_id'] ) ) {
     517                $data['site_id'] = get_main_network_id();
     518        }
     519
     520        if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
     521                return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
     522        }
     523
     524        clean_blog_cache( $old_site );
     525
     526        $new_site = get_site( $old_site->id );
     527
     528        /**
     529         * Fires once a site has been updated in the database.
     530         *
     531         * @since 4.9.0
     532         *
     533         * @param WP_Site $new_site New site object.
     534         * @param WP_Site $old_site Old site object.
     535         */
     536        do_action( 'wp_update_site', $new_site, $old_site );
     537
     538        return (int) $new_site->id;
     539}
     540
     541/**
     542 * Deletes a site from the database.
     543 *
     544 * @since 4.9.0
     545 *
     546 * @global wpdb $wpdb WordPress database abstraction object.
     547 *
     548 * @param int $site_id ID of the site that should be deleted.
     549 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure.
     550 */
     551function wp_delete_site( $site_id ) {
     552        global $wpdb;
     553
     554        $old_site = get_site( $site_id );
     555        if ( ! $old_site ) {
     556                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
     557        }
     558
     559        if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
     560                return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
     561        }
     562
     563        clean_blog_cache( $old_site );
     564
     565        /**
     566         * Fires once a site has been deleted from the database.
     567         *
     568         * @since 4.9.0
     569         *
     570         * @param WP_Site $old_site Deleted site object.
     571         */
     572        do_action( 'wp_delete_site', $old_site );
     573
     574        return $old_site;
     575}
     576
     577/**
    501578 * Retrieves site data given a site ID or site object.
    502579 *
    503580 * Site data will be cached and returned after being passed through a filter.
     
    13051382
    13061383        update_posts_count();
    13071384}
     1385
     1386/**
     1387 * Updates the count of sites for a network based on a changed site.
     1388 *
     1389 * @since 4.9.0
     1390 *
     1391 * @param WP_Site      $new_site The site object that has been inserted, updated or deleted.
     1392 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
     1393 *                               state of that site. Default null.
     1394 */
     1395function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
     1396        if ( null === $old_site ) {
     1397                wp_maybe_update_network_site_counts( $new_site->network_id );
     1398                return;
     1399        }
     1400
     1401        if ( $new_site->network_id != $old_site->network_id ) {
     1402                wp_maybe_update_network_site_counts( $new_site->network_id );
     1403                wp_maybe_update_network_site_counts( $old_site->network_id );
     1404        }
     1405}
     1406
     1407/**
     1408 * Triggers actions on site status updates.
     1409 *
     1410 * @since 4.9.0
     1411 *
     1412 * @param WP_Site $new_site The site object after the update.
     1413 * @param WP_Site $old_site The site obejct prior to the update.
     1414 */
     1415function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) {
     1416        $site_id = $new_site->id;
     1417
     1418        if ( $new_site->spam != $old_site->spam ) {
     1419                if ( $new_site->spam == 1 ) {
     1420
     1421                        /**
     1422                         * Fires when the 'spam' status is added to a site.
     1423                         *
     1424                         * @since MU (3.0.0)
     1425                         *
     1426                         * @param int $site_id Site ID.
     1427                         */
     1428                        do_action( 'make_spam_blog', $site_id );
     1429                } else {
     1430
     1431                        /**
     1432                         * Fires when the 'spam' status is removed from a site.
     1433                         *
     1434                         * @since MU (3.0.0)
     1435                         *
     1436                         * @param int $site_id Site ID.
     1437                         */
     1438                        do_action( 'make_ham_blog', $site_id );
     1439                }
     1440        }
     1441
     1442        if ( $new_site->mature != $old_site->mature ) {
     1443                if ( $new_site->mature == 1 ) {
     1444
     1445                        /**
     1446                         * Fires when the 'mature' status is added to a site.
     1447                         *
     1448                         * @since 3.1.0
     1449                         *
     1450                         * @param int $site_id Site ID.
     1451                         */
     1452                        do_action( 'mature_blog', $site_id );
     1453                } else {
     1454
     1455                        /**
     1456                         * Fires when the 'mature' status is removed from a site.
     1457                         *
     1458                         * @since 3.1.0
     1459                         *
     1460                         * @param int $site_id Site ID.
     1461                         */
     1462                        do_action( 'unmature_blog', $site_id );
     1463                }
     1464        }
     1465
     1466        if ( $new_site->archived != $old_site->archived ) {
     1467                if ( $new_site->archived == 1 ) {
     1468
     1469                        /**
     1470                         * Fires when the 'archived' status is added to a site.
     1471                         *
     1472                         * @since MU (3.0.0)
     1473                         *
     1474                         * @param int $site_id Site ID.
     1475                         */
     1476                        do_action( 'archive_blog', $site_id );
     1477                } else {
     1478
     1479                        /**
     1480                         * Fires when the 'archived' status is removed from a site.
     1481                         *
     1482                         * @since MU (3.0.0)
     1483                         *
     1484                         * @param int $site_id Site ID.
     1485                         */
     1486                        do_action( 'unarchive_blog', $site_id );
     1487                }
     1488        }
     1489
     1490        if ( $new_site->deleted != $old_site->deleted ) {
     1491                if ( $new_site->deleted == 1 ) {
     1492
     1493                        /**
     1494                         * Fires when the 'deleted' status is added to a site.
     1495                         *
     1496                         * @since 3.5.0
     1497                         *
     1498                         * @param int $site_id Site ID.
     1499                         */
     1500                        do_action( 'make_delete_blog', $site_id );
     1501                } else {
     1502
     1503                        /**
     1504                         * Fires when the 'deleted' status is removed from a site.
     1505                         *
     1506                         * @since 3.5.0
     1507                         *
     1508                         * @param int $site_id Site ID.
     1509                         */
     1510                        do_action( 'make_undelete_blog', $site_id );
     1511                }
     1512        }
     1513
     1514        if ( $new_site->public != $old_site->public ) {
     1515                if ( $new_site->public == 1 ) {
     1516
     1517                        /**
     1518                         * Fires when the 'public' status is added to a site.
     1519                         *
     1520                         * @since 4.9.0
     1521                         *
     1522                         * @param int $site_id Site ID.
     1523                         */
     1524                        do_action( 'make_public_blog', $site_id );
     1525                } else {
     1526
     1527                        /**
     1528                         * Fires when the 'public' status is removed from a site.
     1529                         *
     1530                         * @since 4.9.0
     1531                         *
     1532                         * @param int $site_id Site ID.
     1533                         */
     1534                        do_action( 'make_private_blog', $site_id );
     1535                }
     1536        }
     1537}
     1538
     1539/**
     1540 * Sets the `blog_public` option to 1 for a given site ID.
     1541 *
     1542 * @since 4.9.0
     1543 *
     1544 * @param int $site_id Site ID.
     1545 */
     1546function wp_make_blog_public_on_site_update( $site_id ) {
     1547        update_blog_option( $site_id, 'blog_public', 1 );
     1548}
     1549
     1550/**
     1551 * Sets the `blog_public` option to 0 for a given site ID.
     1552 *
     1553 * @since 4.9.0
     1554 *
     1555 * @param int $site_id Site ID.
     1556 */
     1557function wp_make_blog_private_on_site_update( $site_id ) {
     1558        update_blog_option( $site_id, 'blog_public', 0 );
     1559}
  • src/wp-includes/ms-default-filters.php

     
    3838add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 );
    3939add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 );
    4040add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 );
     41add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
     42add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 );
     43add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
     44add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 );
     45add_action( 'make_public_blog', 'wp_make_blog_public_on_site_update', 10, 1 );
     46add_action( 'make_private_blog', 'wp_make_blog_private_on_site_update', 10, 1 );
    4147
    4248// Register Nonce
    4349add_action( 'signup_hidden_fields', 'signup_nonce_fields' );
  • src/wp-includes/ms-deprecated.php

     
    516516
    517517        return $results;
    518518}
     519
     520/**
     521 * Store basic site info in the blogs table.
     522 *
     523 * This function creates a row in the wp_blogs table and returns
     524 * the new blog's ID. It is the first step in creating a new blog.
     525 *
     526 * @since MU (3.0.0)
     527 * @deprecated 4.9.0 Use `wp_insert_site()`
     528 * @see wp_insert_site()
     529 *
     530 * @param string $domain  The domain of the new site.
     531 * @param string $path    The path of the new site.
     532 * @param int    $site_id Unless you're running a multi-network install, be sure to set this value to 1.
     533 * @return int|false The ID of the new row
     534 */
     535function insert_blog($domain, $path, $site_id) {
     536        _deprecated_function( __FUNCTION__, '4.9.0', 'wp_insert_site()' );
     537
     538        $data = array(
     539                'domain'  => $domain,
     540                'path'    => $path,
     541                'site_id' => $site_id,
     542        );
     543
     544        $site_id = wp_insert_site( $data );
     545        if ( is_wp_error( $site_id ) ) {
     546                return false;
     547        }
     548
     549        refresh_blog_details( $site_id );
     550
     551        return $site_id;
     552}
  • src/wp-includes/ms-functions.php

     
    12091209                wp_installing( true );
    12101210        }
    12111211
    1212         if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
     1212        $blog_id = wp_insert_site( array(
     1213                'domain'  => $domain,
     1214                'path'    => $path,
     1215                'site_id' => $site_id,
     1216        ) );
     1217
     1218        if ( is_wp_error( $blog_id ) ) {
    12131219                return new WP_Error('insert_blog', __('Could not create site.'));
     1220        }
    12141221
    12151222        switch_to_blog($blog_id);
    12161223        install_blog($blog_id, $title);
     
    13801387}
    13811388
    13821389/**
    1383  * Store basic site info in the blogs table.
    1384  *
    1385  * This function creates a row in the wp_blogs table and returns
    1386  * the new blog's ID. It is the first step in creating a new blog.
    1387  *
    1388  * @since MU (3.0.0)
    1389  *
    1390  * @global wpdb $wpdb WordPress database abstraction object.
    1391  *
    1392  * @param string $domain  The domain of the new site.
    1393  * @param string $path    The path of the new site.
    1394  * @param int    $site_id Unless you're running a multi-network install, be sure to set this value to 1.
    1395  * @return int|false The ID of the new row
    1396  */
    1397 function insert_blog($domain, $path, $site_id) {
    1398         global $wpdb;
    1399 
    1400         $path = trailingslashit($path);
    1401         $site_id = (int) $site_id;
    1402 
    1403         $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
    1404         if ( ! $result )
    1405                 return false;
    1406 
    1407         $blog_id = $wpdb->insert_id;
    1408         refresh_blog_details( $blog_id );
    1409 
    1410         wp_maybe_update_network_site_counts( $site_id );
    1411 
    1412         return $blog_id;
    1413 }
    1414 
    1415 /**
    14161390 * Install an empty blog.
    14171391 *
    14181392 * Creates the new blog tables and options. If calling this function
     
    14241398 * @global wpdb     $wpdb
    14251399 * @global WP_Roles $wp_roles
    14261400 *
    1427  * @param int    $blog_id    The value returned by insert_blog().
     1401 * @param int    $blog_id    The value returned by wp_insert_site().
    14281402 * @param string $blog_title The title of the new site.
    14291403 */
    14301404function install_blog( $blog_id, $blog_title = '' ) {
  • tests/phpunit/tests/multisite/site.php

     
    10281028        function filter_allow_unavailable_languages( $value, $option, $original_value ) {
    10291029                return $original_value;
    10301030        }
     1031
     1032        /**
     1033         * @ticket 40364
     1034         * @dataProvider data_wp_insert_site
     1035         */
     1036        function test_wp_insert_site( $site_data, $expected_data ) {
     1037                $site_id = wp_insert_site( $site_data );
     1038
     1039                $this->assertInternalType( 'integer', $site_id );
     1040
     1041                $site = get_site( $site_id );
     1042                foreach ( $expected_data as $key => $value ) {
     1043                        $this->assertEquals( $value, $site->$key );
     1044                }
     1045        }
     1046
     1047        function data_wp_insert_site() {
     1048                return array(
     1049                        array(
     1050                                array(
     1051                                        'domain' => 'example.com',
     1052                                ),
     1053                                array(
     1054                                        'domain'     => 'example.com',
     1055                                        'path'       => '/',
     1056                                        'network_id' => 1,
     1057                                        'public'     => 1,
     1058                                        'archived'   => 0,
     1059                                        'mature'     => 0,
     1060                                        'spam'       => 0,
     1061                                        'deleted'    => 0,
     1062                                        'lang_id'    => 0,
     1063                                ),
     1064                        ),
     1065                        array(
     1066                                array(
     1067                                        'domain'     => 'example.com',
     1068                                        'path'       => '/foo',
     1069                                        'network_id' => 2,
     1070                                ), array(
     1071                                        'domain'     => 'example.com',
     1072                                        'path'       => '/foo/',
     1073                                        'network_id' => 2,
     1074                                ),
     1075                        ),
     1076                        array(
     1077                                array(
     1078                                        'domain'  => 'example.com',
     1079                                        'path'    => '/bar/',
     1080                                        'site_id' => 2,
     1081                                        'public'  => 0,
     1082                                ), array(
     1083                                        'domain'     => 'example.com',
     1084                                        'path'       => '/bar/',
     1085                                        'network_id' => 2,
     1086                                        'public'     => 0,
     1087                                ),
     1088                        ),
     1089                        array(
     1090                                array(
     1091                                        'domain'   => 'example.com',
     1092                                        'path'     => 'foobar',
     1093                                        'public'   => 0,
     1094                                        'archived' => 1,
     1095                                        'mature'   => 1,
     1096                                        'spam'     => 1,
     1097                                        'deleted'  => 1,
     1098                                        'lang_id'  => 1,
     1099                                ), array(
     1100                                        'domain'   => 'example.com',
     1101                                        'path'     => '/foobar/',
     1102                                        'public'   => 0,
     1103                                        'archived' => 1,
     1104                                        'mature'   => 1,
     1105                                        'spam'     => 1,
     1106                                        'deleted'  => 1,
     1107                                        'lang_id'  => 1,
     1108                                ),
     1109                        ),
     1110                );
     1111        }
     1112
     1113        /**
     1114         * @ticket 40364
     1115         */
     1116        function test_wp_insert_site_empty_domain() {
     1117                $site_id = wp_insert_site( array( 'public' => 0 ) );
     1118
     1119                $this->assertWPError( $site_id );
     1120                $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
     1121        }
     1122
     1123        /**
     1124         * @ticket 40364
     1125         * @dataProvider data_wp_update_site
     1126         */
     1127        function test_wp_update_site( $site_data, $expected_data ) {
     1128                $site_id = self::factory()->blog->create();
     1129
     1130                $old_site = get_site( $site_id );
     1131
     1132                $result = wp_update_site( $site_id, $site_data );
     1133
     1134                $this->assertSame( $site_id, $result );
     1135
     1136                $new_site = get_site( $site_id );
     1137                foreach ( $new_site->to_array() as $key => $value ) {
     1138                        if ( isset( $expected_data[ $key ] ) ) {
     1139                                $this->assertEquals( $expected_data[ $key ], $value );
     1140                        } elseif ( 'last_updated' === $key ) {
     1141                                $this->assertTrue( $old_site->last_updated <= $value );
     1142                        } else {
     1143                                $this->assertEquals( $old_site->$key, $value );
     1144                        }
     1145                }
     1146        }
     1147
     1148        function data_wp_update_site() {
     1149                return array(
     1150                        array(
     1151                                array(
     1152                                        'domain'     => 'example.com',
     1153                                        'network_id' => 2,
     1154                                ),
     1155                                array(
     1156                                        'domain'  => 'example.com',
     1157                                        'site_id' => 2,
     1158                                ),
     1159                        ),
     1160                        array(
     1161                                array(
     1162                                        'path' => 'foo',
     1163                                ),
     1164                                array(
     1165                                        'path' => '/foo/'
     1166                                ),
     1167                        ),
     1168                        array(
     1169                                array(
     1170                                        'public'   => 0,
     1171                                        'archived' => 1,
     1172                                        'mature'   => 1,
     1173                                        'spam'     => 1,
     1174                                        'deleted'  => 1,
     1175                                        'lang_id'  => 1,
     1176                                ),
     1177                                array(
     1178                                        'public'   => 0,
     1179                                        'archived' => 1,
     1180                                        'mature'   => 1,
     1181                                        'spam'     => 1,
     1182                                        'deleted'  => 1,
     1183                                        'lang_id'  => 1,
     1184                                ),
     1185                        ),
     1186                );
     1187        }
     1188
     1189        /**
     1190         * @ticket 40364
     1191         */
     1192        function test_wp_update_site_empty_domain() {
     1193                $site_id = self::factory()->blog->create();
     1194
     1195                $result = wp_update_site( $site_id, array( 'domain' => '' ) );
     1196
     1197                $this->assertWPError( $result );
     1198                $this->assertSame( 'site_empty_domain', $result->get_error_code() );
     1199        }
     1200
     1201        /**
     1202         * @ticket 40364
     1203         */
     1204        function test_wp_update_site_invalid_id() {
     1205                $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
     1206
     1207                $this->assertWPError( $result );
     1208                $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1209        }
     1210
     1211        /**
     1212         * @ticket 40364
     1213         */
     1214        function test_wp_update_site_cleans_cache() {
     1215                $site_id = self::factory()->blog->create();
     1216                $site1 = get_site( $site_id );
     1217
     1218                $result = wp_update_site( $site_id, array( 'public' => 0 ) );
     1219                $site2 = get_site( $site_id );
     1220
     1221                $result = wp_update_site( $site_id, array( 'public' => 1 ) );
     1222                $site3 = get_site( $site_id );
     1223
     1224                $this->assertEquals( 1, $site1->public );
     1225                $this->assertEquals( 0, $site2->public );
     1226                $this->assertEquals( 1, $site3->public );
     1227        }
     1228
     1229        /**
     1230         * @ticket 40364
     1231         */
     1232        function test_wp_delete_site() {
     1233                $site_id = self::factory()->blog->create();
     1234
     1235                $site = get_site( $site_id );
     1236
     1237                $result = wp_delete_site( $site_id );
     1238
     1239                $this->assertInstanceOf( 'WP_Site', $result );
     1240                $this->assertEquals( $result->to_array(), $site->to_array() );
     1241        }
     1242
     1243        /**
     1244         * @ticket 40364
     1245         */
     1246        function test_wp_delete_site_invalid_id() {
     1247                $result = wp_delete_site( 444444 );
     1248
     1249                $this->assertWPError( $result );
     1250                $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1251        }
     1252
     1253        /**
     1254         * @ticket 40364
     1255         */
     1256        function test_wp_delete_site_cleans_cache() {
     1257                $site_id = self::factory()->blog->create();
     1258
     1259                get_site( $site_id );
     1260
     1261                wp_delete_site( $site_id );
     1262
     1263                $this->assertNull( get_site( $site_id ) );
     1264        }
    10311265}
    10321266
    10331267endif;