Make WordPress Core

Changeset 12806


Ignore:
Timestamp:
01/22/2010 11:35:59 PM (15 years ago)
Author:
ryan
Message:

Introduce update_blog_details(). Add some phpdoc. Fix some warnings. see #11644

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/ms-edit.php

    r12796 r12806  
    184184
    185185        // themes
    186         if ( is_array( $_POST[ 'theme' ] ) )
     186        if ( isset($_POST[ 'theme' ]) && is_array( $_POST[ 'theme' ] ) )
    187187            $_POST[ 'option' ][ 'allowedthemes' ] = $_POST[ 'theme' ];
    188188        else
     
    216216        // update blogs table
    217217        $blog_data = stripslashes_deep($_POST[ 'blog' ]);
    218         $result = $wpdb->update( $wpdb->blogs, $blog_data, array('blog_id' => $id) );
    219 
    220         update_blog_status( $id, 'spam', $_POST[ 'blog' ][ 'spam' ] );
    221         update_option( 'blog_public', $_POST[ 'blog' ][ 'public' ] );
     218        update_blog_details($id, $blog_data);
    222219
    223220        // get blog prefix
     
    238235
    239236        // remove user
    240         if ( is_array( $_POST[ 'blogusers' ] ) ) {
     237        if ( isset($_POST[ 'blogusers' ]) && is_array( $_POST[ 'blogusers' ] ) ) {
    241238            reset( $_POST[ 'blogusers' ] );
    242239            foreach ( (array) $_POST[ 'blogusers' ] as $key => $val )
  • trunk/wp-includes/ms-functions.php

    r12800 r12806  
    1818}
    1919
     20// @todo use update_blog_details
    2021function wpmu_update_blogs_date() {
    2122    global $wpdb;
     
    9394}
    9495
     96// @todo Use get_user_by() and deprecate.
    9597function get_user_details( $username ) {
    9698    global $wpdb;
     
    123125}
    124126
    125 function get_blog_details( $id, $getall = true ) {
    126     global $wpdb;
    127 
    128     if ( !is_numeric( $id ) )
    129         $id = get_id_from_blogname( $id );
    130 
    131     $id = (int) $id;
    132 
    133     $all = $getall == true ? '' : 'short';
    134     $details = wp_cache_get( $id . $all, 'blog-details' );
     127/**
     128 * Retrieve the details for a blog from the blogs table and blog options.
     129 *
     130 * @since 3.0
     131 * @param int $blog_id Blog ID
     132 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
     133 * @return object Blog details.
     134 */
     135function get_blog_details( $blog_id, $get_all = true ) {
     136    global $wpdb;
     137
     138    if ( !is_numeric( $blog_id ) )
     139        $blog_id = get_id_from_blogname( $blog_id );
     140
     141    $blog_id = (int) $blog_id;
     142
     143    $all = $get_all == true ? '' : 'short';
     144    $details = wp_cache_get( $blog_id . $all, 'blog-details' );
    135145
    136146    if ( $details ) {
     
    138148            return false;
    139149        elseif ( !is_object($details) ) // Clear old pre-serialized objects. Cache clients do better with that.
    140             wp_cache_delete( $id . $all, 'blog-details' );
     150            wp_cache_delete( $blog_id . $all, 'blog-details' );
    141151        else
    142152            return $details;
    143153    }
    144154
    145     $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $id) );
     155    $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id) );
    146156    if ( !$details ) {
    147         wp_cache_set( $id . $all, -1, 'blog-details' );
     157        wp_cache_set( $blog_id . $all, -1, 'blog-details' );
    148158        return false;
    149159    }
    150160
    151     if ( !$getall ) {
    152         wp_cache_set( $id . $all, $details, 'blog-details' );
     161    if ( !$get_all ) {
     162        wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    153163        return $details;
    154164    }
    155165
    156166    $wpdb->suppress_errors();
    157     switch_to_blog( $id );
     167    switch_to_blog( $blog_id );
    158168    $details->blogname      = get_option( 'blogname' );
    159169    $details->siteurl       = get_option( 'siteurl' );
     
    164174    $details = apply_filters('blog_details', $details);
    165175
    166     wp_cache_set( $id . $all, $details, 'blog-details' );
     176    wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    167177
    168178    $key = md5( $details->domain . $details->path );
     
    172182}
    173183
    174 function refresh_blog_details( $id ) {
    175     $id = (int) $id;
    176     $details = get_blog_details( $id, false );
    177 
    178     wp_cache_delete( $id , 'blog-details' );
    179     wp_cache_delete( $id . 'short' , 'blog-details' );
     184/**
     185 * Clear the blog details cache.
     186 *
     187 * @since 3.0
     188 *
     189 * @param int $blog_id Blog ID
     190 */
     191function refresh_blog_details( $blog_id ) {
     192    $blog_id = (int) $blog_id;
     193    $details = get_blog_details( $blog_id, false );
     194
     195    wp_cache_delete( $blog_id , 'blog-details' );
     196    wp_cache_delete( $blog_id . 'short' , 'blog-details' );
    180197    wp_cache_delete( md5( $details->domain . $details->path )  , 'blog-lookup' );
    181198    wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
     
    183200}
    184201
     202/**
     203 * Update the details for a blog. Updates the blogs table for a given blog id.
     204 *
     205 * @since 3.0
     206 *
     207 * @param int $blog_id Blog ID
     208 * @param array $details Array of details keyed by blogs table field names.
     209 * @return bool True if update succeeds, false otherwise.
     210 */
     211function update_blog_details( $blog_id, $details = array() ) {
     212    global $wpdb;
     213
     214    if ( empty($details) )
     215        return false;
     216
     217    if ( is_object($details) )
     218        $details = get_object_vars($details);
     219
     220    $current_details = get_blog_details($blog_id, false);
     221    if ( empty($current_details) )
     222        return false;
     223
     224    $current_details = get_object_vars($current_details);
     225
     226    $details = array_merge($current_details, $details);
     227    $details['last_updated'] = current_time('mysql', true);
     228
     229    $update_details = array();
     230    $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
     231    foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
     232        $update_details[$field] = $details[$field];
     233
     234    $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
     235
     236    // If spam status changed, issue actions.
     237    if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
     238        if ( $details[ 'spam' ] == 1 )
     239            do_action( "make_spam_blog", $blog_id );
     240        else
     241            do_action( "make_ham_blog", $blog_id );
     242    }
     243
     244    if ( isset($details[ 'public' ]) )
     245        update_blog_option( $blog_id, 'blog_public', $details[ 'public' ], false );
     246
     247    refresh_blog_details($blog_id);
     248
     249    return true;
     250}
     251
     252// @todo move to user.php
    185253function get_current_user_id() {
    186254    global $current_user;
     
    508576}
    509577
    510 function update_blog_status( $id, $pref, $value, $refresh = 1 ) {
     578/**
     579 * Update a blog details field.
     580 *
     581 * @since 3.0
     582 *
     583 * @param int $blog_id BLog ID
     584 * @param string $pref A field name
     585 * @param string $value Value for $pref
     586 * @param bool $refresh Whether to refresh the blog details cache. Default is true.
     587 */
     588function update_blog_status( $blog_id, $pref, $value, $refresh = true ) {
    511589    global $wpdb;
    512590
     
    514592        return $value;
    515593
    516     $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $id) );
    517     if ( $refresh == 1 )
     594    $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
     595
     596    if ( $refresh )
    518597        refresh_blog_details($id);
    519598
     
    535614        return $details->$pref;
    536615
    537     return $wpdb->get_var( $wpdb->prepare("SELECT $pref FROM {$wpdb->blogs} WHERE blog_id = %d", $id) );
     616    return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
    538617}
    539618
Note: See TracChangeset for help on using the changeset viewer.