Make WordPress Core

Ticket #40364: 40364.5.diff

File 40364.5.diff, 24.7 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

     
    179179 * Update the details for a blog. Updates the blogs table for a given blog id.
    180180 *
    181181 * @since MU (3.0.0)
    182  *
    183  * @global wpdb $wpdb WordPress database abstraction object.
     182 * @since 4.9.0 Now wraps `wp_update_site()`.
    184183 *
    185184 * @param int   $blog_id Blog ID
    186185 * @param array $details Array of details keyed by blogs table field names.
    187186 * @return bool True if update succeeds, false otherwise.
    188187 */
    189188function update_blog_details( $blog_id, $details = array() ) {
    190         global $wpdb;
    191 
    192189        if ( empty($details) )
    193190                return false;
    194191
    195192        if ( is_object($details) )
    196193                $details = get_object_vars($details);
    197194
    198         $current_details = get_site( $blog_id );
    199         if ( empty($current_details) )
     195        $site = wp_update_site( $blog_id, $details );
     196        if ( is_wp_error( $site ) ) {
    200197                return false;
    201 
    202         $current_details = get_object_vars($current_details);
    203 
    204         $details = array_merge($current_details, $details);
    205         $details['last_updated'] = current_time('mysql', true);
    206 
    207         $update_details = array();
    208         $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    209         foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
    210                 if ( 'path' === $field ) {
    211                         $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
    212                 }
    213 
    214                 $update_details[ $field ] = $details[ $field ];
    215198        }
    216199
    217         $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
    218 
    219         if ( false === $result )
    220                 return false;
    221 
    222         // If spam status changed, issue actions.
    223         if ( $details['spam'] != $current_details['spam'] ) {
    224                 if ( $details['spam'] == 1 ) {
    225                         /**
    226                          * Fires when the 'spam' status is added to a blog.
    227                          *
    228                          * @since MU (3.0.0)
    229                          *
    230                          * @param int $blog_id Blog ID.
    231                          */
    232                         do_action( 'make_spam_blog', $blog_id );
    233                 } else {
    234                         /**
    235                          * Fires when the 'spam' status is removed from a blog.
    236                          *
    237                          * @since MU (3.0.0)
    238                          *
    239                          * @param int $blog_id Blog ID.
    240                          */
    241                         do_action( 'make_ham_blog', $blog_id );
    242                 }
    243         }
    244 
    245         // If mature status changed, issue actions.
    246         if ( $details['mature'] != $current_details['mature'] ) {
    247                 if ( $details['mature'] == 1 ) {
    248                         /**
    249                          * Fires when the 'mature' status is added to a blog.
    250                          *
    251                          * @since 3.1.0
    252                          *
    253                          * @param int $blog_id Blog ID.
    254                          */
    255                         do_action( 'mature_blog', $blog_id );
    256                 } else {
    257                         /**
    258                          * Fires when the 'mature' status is removed from a blog.
    259                          *
    260                          * @since 3.1.0
    261                          *
    262                          * @param int $blog_id Blog ID.
    263                          */
    264                         do_action( 'unmature_blog', $blog_id );
    265                 }
    266         }
    267 
    268         // If archived status changed, issue actions.
    269         if ( $details['archived'] != $current_details['archived'] ) {
    270                 if ( $details['archived'] == 1 ) {
    271                         /**
    272                          * Fires when the 'archived' status is added to a blog.
    273                          *
    274                          * @since MU (3.0.0)
    275                          *
    276                          * @param int $blog_id Blog ID.
    277                          */
    278                         do_action( 'archive_blog', $blog_id );
    279                 } else {
    280                         /**
    281                          * Fires when the 'archived' status is removed from a blog.
    282                          *
    283                          * @since MU (3.0.0)
    284                          *
    285                          * @param int $blog_id Blog ID.
    286                          */
    287                         do_action( 'unarchive_blog', $blog_id );
    288                 }
    289         }
    290 
    291         // If deleted status changed, issue actions.
    292         if ( $details['deleted'] != $current_details['deleted'] ) {
    293                 if ( $details['deleted'] == 1 ) {
    294                         /**
    295                          * Fires when the 'deleted' status is added to a blog.
    296                          *
    297                          * @since 3.5.0
    298                          *
    299                          * @param int $blog_id Blog ID.
    300                          */
    301                         do_action( 'make_delete_blog', $blog_id );
    302                 } else {
    303                         /**
    304                          * Fires when the 'deleted' status is removed from a blog.
    305                          *
    306                          * @since 3.5.0
    307                          *
    308                          * @param int $blog_id Blog ID.
    309                          */
    310                         do_action( 'make_undelete_blog', $blog_id );
    311                 }
    312         }
    313 
    314         if ( isset( $details['public'] ) ) {
    315                 switch_to_blog( $blog_id );
    316                 update_option( 'blog_public', $details['public'] );
    317                 restore_current_blog();
    318         }
    319 
    320         clean_blog_cache( $blog_id );
    321 
    322200        return true;
    323201}
    324202
     
    411289}
    412290
    413291/**
     292 * Inserts a new site into the database.
     293 *
     294 * @since 4.9.0
     295 *
     296 * @global wpdb $wpdb WordPress database abstraction object.
     297 *
     298 * @param array $data {
     299 *     Data for the new site that should be inserted.
     300 *
     301 *     @type string $domain       Site domain. Must always be provided.
     302 *     @type string $path         Site path. Default '/'.
     303 *     @type int    $site_id      The site's network ID. Default is the current network ID.
     304 *     @type string $registered   When the site was registered, in SQL datetime format. Default is
     305 *                                the current time.
     306 *     @type string $last_updated When the site was last updated, in SQL datetime format. Default is
     307 *                                the value of $registered.
     308 *     @type int    $public       Whether the site is public. Default 1.
     309 *     @type int    $archived     Whether the site is archived. Default 0.
     310 *     @type int    $mature       Whether the site is mature. Default 0.
     311 *     @type int    $spam         Whether the site is spam. Default 0.
     312 *     @type int    $deleted      Whether the site is deleted. Default 0.
     313 *     @type int    $lang_id      The site's language ID. Currently unused. Default 0.
     314 * }
     315 * @return int|WP_Error The new site's ID on success, or error object on failure.
     316 */
     317function wp_insert_site( $data ) {
     318        global $wpdb;
     319
     320        $now = current_time( 'mysql' );
     321
     322        $defaults = array(
     323                'domain'       => '',
     324                'path'         => '',
     325                'site_id'      => 0,
     326                'registered'   => $now,
     327                'last_updated' => $now,
     328                'public'       => 1,
     329                'archived'     => 0,
     330                'mature'       => 0,
     331                'spam'         => 0,
     332                'deleted'      => 0,
     333                'lang_id'      => 0,
     334        );
     335
     336        $compat_keys = array(
     337                'network_id' => 'site_id',
     338        );
     339
     340        foreach ( $compat_keys as $compat_key => $original_compat_key ) {
     341                if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) {
     342                        $data[ $original_compat_key ] = $data[ $compat_key ];
     343                }
     344        }
     345
     346        $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults );
     347
     348        $data['domain'] = trim( $data['domain'] );
     349
     350        // A domain must always be present.
     351        if ( empty( $data['domain'] ) ) {
     352                return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     353        }
     354
     355        $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
     356
     357        // Use the current network if none is set.
     358        if ( empty( $data['site_id'] ) ) {
     359                $data['site_id'] = get_current_network_id();
     360        }
     361
     362        if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) {
     363                return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
     364        }
     365
     366        $new_site = get_site( $wpdb->insert_id );
     367
     368        clean_blog_cache( $new_site );
     369
     370        /**
     371         * Fires once a site has been inserted into the database.
     372         *
     373         * @since 4.9.0
     374         *
     375         * @param WP_Site $new_site New site object.
     376         */
     377        do_action( 'wp_insert_site', $new_site );
     378
     379        return (int) $new_site->id;
     380}
     381
     382/**
     383 * Updates a site in the database.
     384 *
     385 * @since 4.9.0
     386 *
     387 * @global wpdb $wpdb WordPress database abstraction object.
     388 *
     389 * @param int   $site_id ID of the site that should be updated.
     390 * @param array $data    Site data to update. See wp_insert_site() for the list of supported keys.
     391 * @return int|WP_Error The updated site's ID on success, or error object on failure.
     392 */
     393function wp_update_site( $site_id, $data ) {
     394        global $wpdb;
     395
     396        $old_site = get_site( $site_id );
     397        if ( ! $old_site ) {
     398                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
     399        }
     400
     401        $defaults = $old_site->to_array();
     402        $defaults['last_updated'] = current_time( 'mysql' );
     403        unset( $defaults['blog_id'] );
     404
     405        $compat_keys = array(
     406                'network_id' => 'site_id',
     407        );
     408
     409        foreach ( $compat_keys as $compat_key => $original_compat_key ) {
     410                if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) {
     411                        $data[ $original_compat_key ] = $data[ $compat_key ];
     412                }
     413        }
     414
     415        $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults );
     416
     417        $data['domain'] = trim( $data['domain'] );
     418
     419        // A domain must always be present.
     420        if ( empty( $data['domain'] ) ) {
     421                return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     422        }
     423
     424        $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
     425
     426        // Use the previously set network if a falsy network ID has been passed.
     427        if ( empty( $data['site_id'] ) ) {
     428                $data['site_id'] = $defaults['site_id'];
     429        }
     430
     431        if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
     432                return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
     433        }
     434
     435        clean_blog_cache( $old_site );
     436
     437        $new_site = get_site( $old_site->id );
     438
     439        /**
     440         * Fires once a site has been updated in the database.
     441         *
     442         * @since 4.9.0
     443         *
     444         * @param WP_Site $new_site New site object.
     445         * @param WP_Site $old_site Old site object.
     446         */
     447        do_action( 'wp_update_site', $new_site, $old_site );
     448
     449        return (int) $new_site->id;
     450}
     451
     452/**
     453 * Deletes a site from the database.
     454 *
     455 * @since 4.9.0
     456 *
     457 * @global wpdb $wpdb WordPress database abstraction object.
     458 *
     459 * @param int $site_id ID of the site that should be deleted.
     460 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure.
     461 */
     462function wp_delete_site( $site_id ) {
     463        global $wpdb;
     464
     465        $old_site = get_site( $site_id );
     466        if ( ! $old_site ) {
     467                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
     468        }
     469
     470        if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
     471                return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
     472        }
     473
     474        clean_blog_cache( $old_site );
     475
     476        /**
     477         * Fires once a site has been deleted from the database.
     478         *
     479         * @since 4.9.0
     480         *
     481         * @param WP_Site $old_site Deleted site object.
     482         */
     483        do_action( 'wp_delete_site', $old_site );
     484
     485        return $old_site;
     486}
     487
     488/**
    414489 * Retrieves site data given a site ID or site object.
    415490 *
    416491 * Site data will be cached and returned after being passed through a filter.
     
    13461421
    13471422        update_posts_count();
    13481423}
     1424
     1425/**
     1426 * Updates the count of sites for a network based on a changed site.
     1427 *
     1428 * @since 4.9.0
     1429 *
     1430 * @param WP_Site      $new_site The site object that has been inserted, updated or deleted.
     1431 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
     1432 *                               state of that site. Default null.
     1433 */
     1434function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
     1435        if ( null === $old_site ) {
     1436                wp_maybe_update_network_site_counts( $new_site->network_id );
     1437                return;
     1438        }
     1439
     1440        if ( $new_site->network_id != $old_site->network_id ) {
     1441                wp_maybe_update_network_site_counts( $new_site->network_id );
     1442                wp_maybe_update_network_site_counts( $old_site->network_id );
     1443        }
     1444}
     1445
     1446/**
     1447 * Triggers actions on site status updates.
     1448 *
     1449 * @since 4.9.0
     1450 *
     1451 * @param WP_Site $new_site The site object after the update.
     1452 * @param WP_Site $old_site The site obejct prior to the update.
     1453 */
     1454function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) {
     1455        $site_id = $new_site->id;
     1456
     1457        if ( $new_site->spam != $old_site->spam ) {
     1458                if ( $new_site->spam == 1 ) {
     1459
     1460                        /**
     1461                         * Fires when the 'spam' status is added to a site.
     1462                         *
     1463                         * @since MU (3.0.0)
     1464                         *
     1465                         * @param int $site_id Site ID.
     1466                         */
     1467                        do_action( 'make_spam_blog', $site_id );
     1468                } else {
     1469
     1470                        /**
     1471                         * Fires when the 'spam' status is removed from a site.
     1472                         *
     1473                         * @since MU (3.0.0)
     1474                         *
     1475                         * @param int $site_id Site ID.
     1476                         */
     1477                        do_action( 'make_ham_blog', $site_id );
     1478                }
     1479        }
     1480
     1481        if ( $new_site->mature != $old_site->mature ) {
     1482                if ( $new_site->mature == 1 ) {
     1483
     1484                        /**
     1485                         * Fires when the 'mature' status is added to a site.
     1486                         *
     1487                         * @since 3.1.0
     1488                         *
     1489                         * @param int $site_id Site ID.
     1490                         */
     1491                        do_action( 'mature_blog', $site_id );
     1492                } else {
     1493
     1494                        /**
     1495                         * Fires when the 'mature' status is removed from a site.
     1496                         *
     1497                         * @since 3.1.0
     1498                         *
     1499                         * @param int $site_id Site ID.
     1500                         */
     1501                        do_action( 'unmature_blog', $site_id );
     1502                }
     1503        }
     1504
     1505        if ( $new_site->archived != $old_site->archived ) {
     1506                if ( $new_site->archived == 1 ) {
     1507
     1508                        /**
     1509                         * Fires when the 'archived' status is added to a site.
     1510                         *
     1511                         * @since MU (3.0.0)
     1512                         *
     1513                         * @param int $site_id Site ID.
     1514                         */
     1515                        do_action( 'archive_blog', $site_id );
     1516                } else {
     1517
     1518                        /**
     1519                         * Fires when the 'archived' status is removed from a site.
     1520                         *
     1521                         * @since MU (3.0.0)
     1522                         *
     1523                         * @param int $site_id Site ID.
     1524                         */
     1525                        do_action( 'unarchive_blog', $site_id );
     1526                }
     1527        }
     1528
     1529        if ( $new_site->deleted != $old_site->deleted ) {
     1530                if ( $new_site->deleted == 1 ) {
     1531
     1532                        /**
     1533                         * Fires when the 'deleted' status is added to a site.
     1534                         *
     1535                         * @since 3.5.0
     1536                         *
     1537                         * @param int $site_id Site ID.
     1538                         */
     1539                        do_action( 'make_delete_blog', $site_id );
     1540                } else {
     1541
     1542                        /**
     1543                         * Fires when the 'deleted' status is removed from a site.
     1544                         *
     1545                         * @since 3.5.0
     1546                         *
     1547                         * @param int $site_id Site ID.
     1548                         */
     1549                        do_action( 'make_undelete_blog', $site_id );
     1550                }
     1551        }
     1552
     1553        if ( $new_site->public != $old_site->public ) {
     1554                if ( $new_site->public == 1 ) {
     1555
     1556                        /**
     1557                         * Fires when the 'public' status is added to a site.
     1558                         *
     1559                         * @since 4.9.0
     1560                         *
     1561                         * @param int $site_id Site ID.
     1562                         */
     1563                        do_action( 'make_public_blog', $site_id );
     1564                } else {
     1565
     1566                        /**
     1567                         * Fires when the 'public' status is removed from a site.
     1568                         *
     1569                         * @since 4.9.0
     1570                         *
     1571                         * @param int $site_id Site ID.
     1572                         */
     1573                        do_action( 'make_private_blog', $site_id );
     1574                }
     1575        }
     1576}
     1577
     1578/**
     1579 * Sets the `blog_public` option to 1 for a given site ID.
     1580 *
     1581 * @since 4.9.0
     1582 *
     1583 * @param int $site_id Site ID.
     1584 */
     1585function wp_make_blog_public_on_site_update( $site_id ) {
     1586        update_blog_option( $site_id, 'blog_public', 1 );
     1587}
     1588
     1589/**
     1590 * Sets the `blog_public` option to 0 for a given site ID.
     1591 *
     1592 * @since 4.9.0
     1593 *
     1594 * @param int $site_id Site ID.
     1595 */
     1596function wp_make_blog_private_on_site_update( $site_id ) {
     1597        update_blog_option( $site_id, 'blog_public', 0 );
     1598}
  • src/wp-includes/ms-default-filters.php

     
    4141add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 );
    4242add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 );
    4343add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 );
     44add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
     45add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 );
     46add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
     47add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 );
     48add_action( 'make_public_blog', 'wp_make_blog_public_on_site_update', 10, 1 );
     49add_action( 'make_private_blog', 'wp_make_blog_private_on_site_update', 10, 1 );
    4450
    4551// Register Nonce
    4652add_action( 'signup_hidden_fields', 'signup_nonce_fields' );
  • src/wp-includes/ms-deprecated.php

     
    546546
    547547        return isset( $current_user->$local_key );
    548548}
     549
     550/**
     551 * Store basic site info in the blogs table.
     552 *
     553 * This function creates a row in the wp_blogs table and returns
     554 * the new blog's ID. It is the first step in creating a new blog.
     555 *
     556 * @since MU (3.0.0)
     557 * @deprecated 4.9.0 Use `wp_insert_site()`
     558 * @see wp_insert_site()
     559 *
     560 * @param string $domain  The domain of the new site.
     561 * @param string $path    The path of the new site.
     562 * @param int    $site_id Unless you're running a multi-network install, be sure to set this value to 1.
     563 * @return int|false The ID of the new row
     564 */
     565function insert_blog($domain, $path, $site_id) {
     566        _deprecated_function( __FUNCTION__, '4.9.0', 'wp_insert_site()' );
     567
     568        $data = array(
     569                'domain'  => $domain,
     570                'path'    => $path,
     571                'site_id' => $site_id,
     572        );
     573
     574        $site_id = wp_insert_site( $data );
     575        if ( is_wp_error( $site_id ) ) {
     576                return false;
     577        }
     578
     579        clean_blog_cache( $site_id );
     580
     581        return $site_id;
     582}
  • src/wp-includes/ms-functions.php

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

     
    12401240                        array( 'current_blog_%domain%%path%', 'site-options' ),
    12411241                );
    12421242        }
     1243
     1244        /**
     1245         * @ticket 40364
     1246         * @dataProvider data_wp_insert_site
     1247         */
     1248        function test_wp_insert_site( $site_data, $expected_data ) {
     1249                $site_id = wp_insert_site( $site_data );
     1250
     1251                $this->assertInternalType( 'integer', $site_id );
     1252
     1253                $site = get_site( $site_id );
     1254                foreach ( $expected_data as $key => $value ) {
     1255                        $this->assertEquals( $value, $site->$key );
     1256                }
     1257        }
     1258
     1259        function data_wp_insert_site() {
     1260                return array(
     1261                        array(
     1262                                array(
     1263                                        'domain' => 'example.com',
     1264                                ),
     1265                                array(
     1266                                        'domain'     => 'example.com',
     1267                                        'path'       => '/',
     1268                                        'network_id' => 1,
     1269                                        'public'     => 1,
     1270                                        'archived'   => 0,
     1271                                        'mature'     => 0,
     1272                                        'spam'       => 0,
     1273                                        'deleted'    => 0,
     1274                                        'lang_id'    => 0,
     1275                                ),
     1276                        ),
     1277                        array(
     1278                                array(
     1279                                        'domain'     => 'example.com',
     1280                                        'path'       => '/foo',
     1281                                        'network_id' => 2,
     1282                                ), array(
     1283                                        'domain'     => 'example.com',
     1284                                        'path'       => '/foo/',
     1285                                        'network_id' => 2,
     1286                                ),
     1287                        ),
     1288                        array(
     1289                                array(
     1290                                        'domain'  => 'example.com',
     1291                                        'path'    => '/bar/',
     1292                                        'site_id' => 2,
     1293                                        'public'  => 0,
     1294                                ), array(
     1295                                        'domain'     => 'example.com',
     1296                                        'path'       => '/bar/',
     1297                                        'network_id' => 2,
     1298                                        'public'     => 0,
     1299                                ),
     1300                        ),
     1301                        array(
     1302                                array(
     1303                                        'domain'   => 'example.com',
     1304                                        'path'     => 'foobar',
     1305                                        'public'   => 0,
     1306                                        'archived' => 1,
     1307                                        'mature'   => 1,
     1308                                        'spam'     => 1,
     1309                                        'deleted'  => 1,
     1310                                        'lang_id'  => 1,
     1311                                ), array(
     1312                                        'domain'   => 'example.com',
     1313                                        'path'     => '/foobar/',
     1314                                        'public'   => 0,
     1315                                        'archived' => 1,
     1316                                        'mature'   => 1,
     1317                                        'spam'     => 1,
     1318                                        'deleted'  => 1,
     1319                                        'lang_id'  => 1,
     1320                                ),
     1321                        ),
     1322                );
     1323        }
     1324
     1325        /**
     1326         * @ticket 40364
     1327         */
     1328        function test_wp_insert_site_empty_domain() {
     1329                $site_id = wp_insert_site( array( 'public' => 0 ) );
     1330
     1331                $this->assertWPError( $site_id );
     1332                $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
     1333        }
     1334
     1335        /**
     1336         * @ticket 40364
     1337         * @dataProvider data_wp_update_site
     1338         */
     1339        function test_wp_update_site( $site_data, $expected_data ) {
     1340                $site_id = self::factory()->blog->create();
     1341
     1342                $old_site = get_site( $site_id );
     1343
     1344                $result = wp_update_site( $site_id, $site_data );
     1345
     1346                $this->assertSame( $site_id, $result );
     1347
     1348                $new_site = get_site( $site_id );
     1349                foreach ( $new_site->to_array() as $key => $value ) {
     1350                        if ( isset( $expected_data[ $key ] ) ) {
     1351                                $this->assertEquals( $expected_data[ $key ], $value );
     1352                        } elseif ( 'last_updated' === $key ) {
     1353                                $this->assertTrue( $old_site->last_updated <= $value );
     1354                        } else {
     1355                                $this->assertEquals( $old_site->$key, $value );
     1356                        }
     1357                }
     1358        }
     1359
     1360        function data_wp_update_site() {
     1361                return array(
     1362                        array(
     1363                                array(
     1364                                        'domain'     => 'example.com',
     1365                                        'network_id' => 2,
     1366                                ),
     1367                                array(
     1368                                        'domain'  => 'example.com',
     1369                                        'site_id' => 2,
     1370                                ),
     1371                        ),
     1372                        array(
     1373                                array(
     1374                                        'path' => 'foo',
     1375                                ),
     1376                                array(
     1377                                        'path' => '/foo/'
     1378                                ),
     1379                        ),
     1380                        array(
     1381                                array(
     1382                                        'public'   => 0,
     1383                                        'archived' => 1,
     1384                                        'mature'   => 1,
     1385                                        'spam'     => 1,
     1386                                        'deleted'  => 1,
     1387                                        'lang_id'  => 1,
     1388                                ),
     1389                                array(
     1390                                        'public'   => 0,
     1391                                        'archived' => 1,
     1392                                        'mature'   => 1,
     1393                                        'spam'     => 1,
     1394                                        'deleted'  => 1,
     1395                                        'lang_id'  => 1,
     1396                                ),
     1397                        ),
     1398                );
     1399        }
     1400
     1401        /**
     1402         * @ticket 40364
     1403         */
     1404        function test_wp_update_site_empty_domain() {
     1405                $site_id = self::factory()->blog->create();
     1406
     1407                $result = wp_update_site( $site_id, array( 'domain' => '' ) );
     1408
     1409                $this->assertWPError( $result );
     1410                $this->assertSame( 'site_empty_domain', $result->get_error_code() );
     1411        }
     1412
     1413        /**
     1414         * @ticket 40364
     1415         */
     1416        function test_wp_update_site_invalid_id() {
     1417                $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
     1418
     1419                $this->assertWPError( $result );
     1420                $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1421        }
     1422
     1423        /**
     1424         * @ticket 40364
     1425         */
     1426        function test_wp_update_site_cleans_cache() {
     1427                $site_id = self::factory()->blog->create();
     1428                $site1 = get_site( $site_id );
     1429
     1430                $result = wp_update_site( $site_id, array( 'public' => 0 ) );
     1431                $site2 = get_site( $site_id );
     1432
     1433                $result = wp_update_site( $site_id, array( 'public' => 1 ) );
     1434                $site3 = get_site( $site_id );
     1435
     1436                $this->assertEquals( 1, $site1->public );
     1437                $this->assertEquals( 0, $site2->public );
     1438                $this->assertEquals( 1, $site3->public );
     1439        }
     1440
     1441        /**
     1442         * @ticket 40364
     1443         */
     1444        function test_wp_delete_site() {
     1445                $site_id = self::factory()->blog->create();
     1446
     1447                $site = get_site( $site_id );
     1448
     1449                $result = wp_delete_site( $site_id );
     1450
     1451                $this->assertInstanceOf( 'WP_Site', $result );
     1452                $this->assertEquals( $result->to_array(), $site->to_array() );
     1453        }
     1454
     1455        /**
     1456         * @ticket 40364
     1457         */
     1458        function test_wp_delete_site_invalid_id() {
     1459                $result = wp_delete_site( 444444 );
     1460
     1461                $this->assertWPError( $result );
     1462                $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1463        }
     1464
     1465        /**
     1466         * @ticket 40364
     1467         */
     1468        function test_wp_delete_site_cleans_cache() {
     1469                $site_id = self::factory()->blog->create();
     1470
     1471                get_site( $site_id );
     1472
     1473                wp_delete_site( $site_id );
     1474
     1475                $this->assertNull( get_site( $site_id ) );
     1476        }
    12431477}
    12441478
    12451479endif;