Make WordPress Core

Ticket #40364: 40364.diff

File 40364.diff, 12.3 KB (added by flixos90, 8 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 );
    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
     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) )
    316                 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                 }
     315        $site = get_site( $blog_id );
     316        foreach ( $details as $field => $value ) {
     317                $site->$field = $value;
     318        }
    329319
    330                 $update_details[ $field ] = $details[ $field ];
     320        $site = wp_update_site( $site );
     321        if ( is_wp_error( $site ) ) {
     322                return false;
    331323        }
    332324
    333         $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
     325        //TODO: Do we need to run the 'refresh_blog_details' hook here for BC?
    334326
    335         if ( false === $result )
    336                 return false;
     327        return true;
     328}
     329
     330/**
     331 * Runs hooks when one or more site status fields have been updated.
     332 *
     333 * @since 4.9.0
     334 *
     335 * @param WP_Site $existing_site Site object prior to the update.
     336 * @param WP_Site $updated_site  Site object after the update.
     337 */
     338function maybe_transition_site_statuses( $existing_site, $updated_site ) {
     339        $site_id = $updated_site->id;
    337340
    338341        // If spam status changed, issue actions.
    339         if ( $details['spam'] != $current_details['spam'] ) {
    340                 if ( $details['spam'] == 1 ) {
     342        if ( $updated_site->spam != $existing_site->spam ) {
     343                if ( $updated_site->spam == 1 ) {
    341344                        /**
    342                          * Fires when the 'spam' status is added to a blog.
     345                         * Fires when the 'spam' status is added to a site.
    343346                         *
    344347                         * @since MU
    345348                         *
    346                          * @param int $blog_id Blog ID.
     349                         * @param int $site_id Site ID.
    347350                         */
    348                         do_action( 'make_spam_blog', $blog_id );
     351                        do_action( 'make_spam_blog', $site_id );
    349352                } else {
    350353                        /**
    351                          * Fires when the 'spam' status is removed from a blog.
     354                         * Fires when the 'spam' status is removed from a site.
    352355                         *
    353356                         * @since MU
    354357                         *
    355                          * @param int $blog_id Blog ID.
     358                         * @param int $site_id Site ID.
    356359                         */
    357                         do_action( 'make_ham_blog', $blog_id );
     360                        do_action( 'make_ham_blog', $site_id );
    358361                }
    359362        }
    360363
    361364        // If mature status changed, issue actions.
    362         if ( $details['mature'] != $current_details['mature'] ) {
    363                 if ( $details['mature'] == 1 ) {
     365        if ( $updated_site->mature != $existing_site->mature ) {
     366                if ( $updated_site->mature == 1 ) {
    364367                        /**
    365                          * Fires when the 'mature' status is added to a blog.
     368                         * Fires when the 'mature' status is added to a site.
    366369                         *
    367370                         * @since 3.1.0
    368371                         *
    369                          * @param int $blog_id Blog ID.
     372                         * @param int $site_id Site ID.
    370373                         */
    371                         do_action( 'mature_blog', $blog_id );
     374                        do_action( 'mature_blog', $site_id );
    372375                } else {
    373376                        /**
    374                          * Fires when the 'mature' status is removed from a blog.
     377                         * Fires when the 'mature' status is removed from a site.
    375378                         *
    376379                         * @since 3.1.0
    377380                         *
    378                          * @param int $blog_id Blog ID.
     381                         * @param int $site_id Site ID.
    379382                         */
    380                         do_action( 'unmature_blog', $blog_id );
     383                        do_action( 'unmature_blog', $site_id );
    381384                }
    382385        }
    383386
    384387        // If archived status changed, issue actions.
    385         if ( $details['archived'] != $current_details['archived'] ) {
    386                 if ( $details['archived'] == 1 ) {
     388        if ( $updated_site->archived != $existing_site->archived ) {
     389                if ( $updated_site->archived == 1 ) {
    387390                        /**
    388                          * Fires when the 'archived' status is added to a blog.
     391                         * Fires when the 'archived' status is added to a site.
    389392                         *
    390393                         * @since MU
    391394                         *
    392                          * @param int $blog_id Blog ID.
     395                         * @param int $site_id Site ID.
    393396                         */
    394                         do_action( 'archive_blog', $blog_id );
     397                        do_action( 'archive_blog', $site_id );
    395398                } else {
    396399                        /**
    397                          * Fires when the 'archived' status is removed from a blog.
     400                         * Fires when the 'archived' status is removed from a site.
    398401                         *
    399402                         * @since MU
    400403                         *
    401                          * @param int $blog_id Blog ID.
     404                         * @param int $site_id Site ID.
    402405                         */
    403                         do_action( 'unarchive_blog', $blog_id );
     406                        do_action( 'unarchive_blog', $site_id );
    404407                }
    405408        }
    406409
    407410        // If deleted status changed, issue actions.
    408         if ( $details['deleted'] != $current_details['deleted'] ) {
    409                 if ( $details['deleted'] == 1 ) {
     411        if ( $updated_site->deleted != $existing_site->deleted ) {
     412                if ( $updated_site->deleted == 1 ) {
    410413                        /**
    411                          * Fires when the 'deleted' status is added to a blog.
     414                         * Fires when the 'deleted' status is added to a site.
    412415                         *
    413416                         * @since 3.5.0
    414417                         *
    415                          * @param int $blog_id Blog ID.
     418                         * @param int $site_id Site ID.
    416419                         */
    417                         do_action( 'make_delete_blog', $blog_id );
     420                        do_action( 'make_delete_blog', $site_id );
    418421                } else {
    419422                        /**
    420                          * Fires when the 'deleted' status is removed from a blog.
     423                         * Fires when the 'deleted' status is removed from a site.
    421424                         *
    422425                         * @since 3.5.0
    423426                         *
    424                          * @param int $blog_id Blog ID.
     427                         * @param int $site_id Site ID.
    425428                         */
    426                         do_action( 'make_undelete_blog', $blog_id );
     429                        do_action( 'make_undelete_blog', $site_id );
    427430                }
    428431        }
    429432
    430         if ( isset( $details['public'] ) ) {
    431                 switch_to_blog( $blog_id );
    432                 update_option( 'blog_public', $details['public'] );
     433        //TODO: This should not be in here. It is an action in single-site context and database tables may not exist.
     434        if ( $updated_site->public != $existing_site->public ) {
     435                switch_to_blog( $site_id );
     436                update_option( 'blog_public', $updated_site->public );
    433437                restore_current_blog();
    434438        }
    435 
    436         refresh_blog_details($blog_id);
    437 
    438         return true;
    439439}
    440440
    441441/**
     
    498498}
    499499
    500500/**
     501 * Inserts a new site into the database.
     502 *
     503 * @since 4.9.0
     504 *
     505 * @global wpdb $wpdb WordPress database abstraction object.
     506 *
     507 * @param WP_Site $site Site object to insert its data. Its $id property must not be
     508 *                      set prior to inserting the site.
     509 * @return WP_Site|WP_Error Modified site object with $id property set on success, or
     510 *                          error object on failure.
     511 */
     512function wp_insert_site( $site ) {
     513        global $wpdb;
     514
     515        if ( ! empty( $site->id ) ) {
     516                return new WP_Error( 'site_already_exist', __( 'Cannot create existing site.' ) );
     517        }
     518
     519        if ( empty( $site->domain ) ) {
     520                return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     521        }
     522
     523        $site->path = trailingslashit( $site->path );
     524
     525        if ( empty( $site->network_id ) ) {
     526                $site->network_id = get_current_network_id();
     527        }
     528
     529        $site->registered = current_time( 'mysql' );
     530        $site->last_updated = $site->registered;
     531
     532        $args = $site->to_array();
     533
     534        $fields = array( 'domain', 'path', 'site_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
     535        $args = array_intersect_key( $args, array_flip( $fields ) );
     536
     537        $result = $wpdb->insert( $wpdb->blogs, $args );
     538        if ( ! $result ) {
     539                return new WP_Error( 'could_not_insert_site', __( 'Site could not be inserted into the database.' ) );
     540        }
     541
     542        $site->id = $wpdb->insert_id;
     543
     544        clean_blog_cache( $site );
     545
     546        //TODO: Pass $site->network_id to the function after #40384 is done.
     547        wp_maybe_update_network_site_counts();
     548
     549        return $site;
     550}
     551
     552/**
     553 * Updates an existing site in the database.
     554 *
     555 * @since 4.9.0
     556 *
     557 * @global wpdb $wpdb WordPress database abstraction object.
     558 *
     559 * @param WP_Site $site Site object to update its data.
     560 * @return WP_Site|WP_Error Modified site object on success, or
     561 *                          error object on failure.
     562 */
     563function wp_update_site( $site ) {
     564        global $wpdb;
     565
     566        $existing_site = ! empty( $site->id ) ? get_site( $site->id ) : null;
     567        if ( ! $existing_site ) {
     568                return new WP_Error( 'site_not_exist', __( 'Cannot update not-existing site.' ) );
     569        }
     570
     571        if ( empty( $site->domain ) ) {
     572                return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     573        }
     574
     575        $site->path = trailingslashit( $site->path );
     576
     577        if ( empty( $site->network_id ) ) {
     578                $site->network_id = get_current_network_id();
     579        }
     580
     581        $site->last_updated = current_time( 'mysql' );
     582
     583        $args = $site->to_array();
     584
     585        $fields = array( 'domain', 'path', 'site_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
     586        $args = array_intersect_key( $args, array_flip( $fields ) );
     587
     588        $result = $wpdb->update( $wpdb->blogs, $args, array( 'blog_id' => $site->id ) );
     589        if ( ! $result ) {
     590                return new WP_Error( 'could_not_update_site', __( 'Site could not be updated in the database.' ) );
     591        }
     592
     593        maybe_transition_site_statuses( $existing_site, $site );
     594
     595        clean_blog_cache( $site );
     596
     597        return $site;
     598}
     599
     600/**
     601 * Deletes an existing site from the database.
     602 *
     603 * @since 4.9.0
     604 *
     605 * @global wpdb $wpdb WordPress database abstraction object.
     606 *
     607 * @param WP_Site $site Site object to delete its data.
     608 * @return WP_Site|WP_Error Modified site object with $id property removed on success, or
     609 *                          error object on failure.
     610 */
     611function wp_delete_site( $site ) {
     612        global $wpdb;
     613
     614        $existing_site = ! empty( $site->id ) ? get_site( $site->id ) : null;
     615        if ( ! $existing_site ) {
     616                return new WP_Error( 'site_not_exist', __( 'Cannot update non-existing site.' ) );
     617        }
     618
     619        $result = $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $site->id ) );
     620        if ( ! $result ) {
     621                return new WP_Error( 'could_not_delete_site', __( 'Site could not be deleted from the database.' ) );
     622        }
     623
     624        clean_blog_cache( $site );
     625
     626        //TODO: Pass $site->network_id to the function after #40384 is done.
     627        wp_maybe_update_network_site_counts();
     628
     629        $site->id = 0;
     630
     631        return $site;
     632}
     633
     634/**
    501635 * Retrieves site data given a site ID or site object.
    502636 *
    503637 * Site data will be cached and returned after being passed through a filter.
  • src/wp-includes/ms-functions.php

     
    13441344 * the new blog's ID. It is the first step in creating a new blog.
    13451345 *
    13461346 * @since MU
    1347  *
    1348  * @global wpdb $wpdb WordPress database abstraction object.
     1347 * @since 4.9.0 Now wraps `wp_insert_site()`.
    13491348 *
    13501349 * @param string $domain  The domain of the new site.
    13511350 * @param string $path    The path of the new site.
     
    13531352 * @return int|false The ID of the new row
    13541353 */
    13551354function insert_blog($domain, $path, $site_id) {
    1356         global $wpdb;
     1355        $site = new WP_Site( new stdClass() );
     1356        $site->domain     = $domain;
     1357        $site->path       = $path;
     1358        $site->network_id = $site_id;
    13571359
    1358         $path = trailingslashit($path);
    1359         $site_id = (int) $site_id;
    1360 
    1361         $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
    1362         if ( ! $result )
     1360        $site = wp_insert_site( $site );
     1361        if ( is_wp_error( $site ) ) {
    13631362                return false;
     1363        }
    13641364
    1365         $blog_id = $wpdb->insert_id;
    1366         refresh_blog_details( $blog_id );
    1367 
    1368         wp_maybe_update_network_site_counts();
     1365        //TODO: Do we need to run the 'refresh_blog_details' hook here for BC?
    13691366
    1370         return $blog_id;
     1367        return $site->id;
    13711368}
    13721369
    13731370/**