<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[12806] trunk: Introduce update_blog_details().</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12806">12806</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-01-22 23:35:59 +0000 (Fri, 22 Jan 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introduce update_blog_details(). Add some phpdoc. Fix some warnings. see <a href="http://trac.wordpress.org/ticket/11644">#11644</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminmseditphp">trunk/wp-admin/ms-edit.php</a></li>
<li><a href="#trunkwpincludesmsfunctionsphp">trunk/wp-includes/ms-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminmseditphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/ms-edit.php (12805 => 12806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/ms-edit.php        2010-01-22 22:38:43 UTC (rev 12805)
+++ trunk/wp-admin/ms-edit.php        2010-01-22 23:35:59 UTC (rev 12806)
</span><span class="lines">@@ -183,7 +183,7 @@
</span><span class="cx">                         wp_die( __('You probably need to go back to the <a href="ms-sites.php">sites page</a>') );
</span><span class="cx">
</span><span class="cx">                 // themes
</span><del>-                if ( is_array( $_POST[ 'theme' ] ) )
</del><ins>+                if ( isset($_POST[ 'theme' ]) && is_array( $_POST[ 'theme' ] ) )
</ins><span class="cx">                         $_POST[ 'option' ][ 'allowedthemes' ] = $_POST[ 'theme' ];
</span><span class="cx">                 else
</span><span class="cx">                         $_POST[ 'option' ][ 'allowedthemes' ] = '';
</span><span class="lines">@@ -215,11 +215,8 @@
</span><span class="cx">
</span><span class="cx">                 // update blogs table
</span><span class="cx">                 $blog_data = stripslashes_deep($_POST[ 'blog' ]);
</span><del>-                $result = $wpdb->update( $wpdb->blogs, $blog_data, array('blog_id' => $id) );
</del><ins>+                update_blog_details($id, $blog_data);
</ins><span class="cx">
</span><del>-                update_blog_status( $id, 'spam', $_POST[ 'blog' ][ 'spam' ] );
-                update_option( 'blog_public', $_POST[ 'blog' ][ 'public' ] );
-
</del><span class="cx">                 // get blog prefix
</span><span class="cx">                 $blog_prefix = $wpdb->get_blog_prefix( $id );
</span><span class="cx">
</span><span class="lines">@@ -237,7 +234,7 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 // remove user
</span><del>-                if ( is_array( $_POST[ 'blogusers' ] ) ) {
</del><ins>+                if ( isset($_POST[ 'blogusers' ]) && is_array( $_POST[ 'blogusers' ] ) ) {
</ins><span class="cx">                         reset( $_POST[ 'blogusers' ] );
</span><span class="cx">                         foreach ( (array) $_POST[ 'blogusers' ] as $key => $val )
</span><span class="cx">                                 remove_user_from_blog( $key, $id );
</span></span></pre></div>
<a id="trunkwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-functions.php (12805 => 12806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-functions.php        2010-01-22 22:38:43 UTC (rev 12805)
+++ trunk/wp-includes/ms-functions.php        2010-01-22 23:35:59 UTC (rev 12806)
</span><span class="lines">@@ -17,6 +17,7 @@
</span><span class="cx">         load_textdomain($domain, $mofile);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+// @todo use update_blog_details
</ins><span class="cx"> function wpmu_update_blogs_date() {
</span><span class="cx">         global $wpdb;
</span><span class="cx">
</span><span class="lines">@@ -92,6 +93,7 @@
</span><span class="cx">         return false;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+// @todo Use get_user_by() and deprecate.
</ins><span class="cx"> function get_user_details( $username ) {
</span><span class="cx">         global $wpdb;
</span><span class="cx">         return $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = %s", $username) );
</span><span class="lines">@@ -122,39 +124,47 @@
</span><span class="cx">         return $blog_id;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-function get_blog_details( $id, $getall = true ) {
</del><ins>+/**
+ * Retrieve the details for a blog from the blogs table and blog options.
+ *
+ * @since 3.0
+ * @param int $blog_id Blog ID
+ * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
+ * @return object Blog details.
+ */
+function get_blog_details( $blog_id, $get_all = true ) {
</ins><span class="cx">         global $wpdb;
</span><span class="cx">
</span><del>-        if ( !is_numeric( $id ) )
-                $id = get_id_from_blogname( $id );
</del><ins>+        if ( !is_numeric( $blog_id ) )
+                $blog_id = get_id_from_blogname( $blog_id );
</ins><span class="cx">
</span><del>-        $id = (int) $id;
</del><ins>+        $blog_id = (int) $blog_id;
</ins><span class="cx">
</span><del>-        $all = $getall == true ? '' : 'short';
-        $details = wp_cache_get( $id . $all, 'blog-details' );
</del><ins>+        $all = $get_all == true ? '' : 'short';
+        $details = wp_cache_get( $blog_id . $all, 'blog-details' );
</ins><span class="cx">
</span><span class="cx">         if ( $details ) {
</span><span class="cx">                 if ( !is_object($details) && $details == -1 )
</span><span class="cx">                         return false;
</span><span class="cx">                 elseif ( !is_object($details) ) // Clear old pre-serialized objects. Cache clients do better with that.
</span><del>-                        wp_cache_delete( $id . $all, 'blog-details' );
</del><ins>+                        wp_cache_delete( $blog_id . $all, 'blog-details' );
</ins><span class="cx">                 else
</span><span class="cx">                         return $details;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $id) );
</del><ins>+        $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id) );
</ins><span class="cx">         if ( !$details ) {
</span><del>-                wp_cache_set( $id . $all, -1, 'blog-details' );
</del><ins>+                wp_cache_set( $blog_id . $all, -1, 'blog-details' );
</ins><span class="cx">                 return false;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if ( !$getall ) {
-                wp_cache_set( $id . $all, $details, 'blog-details' );
</del><ins>+        if ( !$get_all ) {
+                wp_cache_set( $blog_id . $all, $details, 'blog-details' );
</ins><span class="cx">                 return $details;
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         $wpdb->suppress_errors();
</span><del>-        switch_to_blog( $id );
</del><ins>+        switch_to_blog( $blog_id );
</ins><span class="cx">         $details->blogname                = get_option( 'blogname' );
</span><span class="cx">         $details->siteurl                = get_option( 'siteurl' );
</span><span class="cx">         $details->post_count        = get_option( 'post_count' );
</span><span class="lines">@@ -163,7 +173,7 @@
</span><span class="cx">
</span><span class="cx">         $details = apply_filters('blog_details', $details);
</span><span class="cx">
</span><del>-        wp_cache_set( $id . $all, $details, 'blog-details' );
</del><ins>+        wp_cache_set( $blog_id . $all, $details, 'blog-details' );
</ins><span class="cx">
</span><span class="cx">         $key = md5( $details->domain . $details->path );
</span><span class="cx">         wp_cache_set( $key, $details, 'blog-lookup' );
</span><span class="lines">@@ -171,17 +181,75 @@
</span><span class="cx">         return $details;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-function refresh_blog_details( $id ) {
-        $id = (int) $id;
-        $details = get_blog_details( $id, false );
</del><ins>+/**
+ * Clear the blog details cache.
+ *
+ * @since 3.0
+ *
+ * @param int $blog_id Blog ID
+ */
+function refresh_blog_details( $blog_id ) {
+        $blog_id = (int) $blog_id;
+        $details = get_blog_details( $blog_id, false );
</ins><span class="cx">
</span><del>-        wp_cache_delete( $id , 'blog-details' );
-        wp_cache_delete( $id . 'short' , 'blog-details' );
</del><ins>+        wp_cache_delete( $blog_id , 'blog-details' );
+        wp_cache_delete( $blog_id . 'short' , 'blog-details' );
</ins><span class="cx">         wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
</span><span class="cx">         wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
</span><span class="cx">         wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Update the details for a blog. Updates the blogs table for a given blog id.
+ *
+ * @since 3.0
+ *
+ * @param int $blog_id Blog ID
+ * @param array $details Array of details keyed by blogs table field names.
+ * @return bool True if update succeeds, false otherwise.
+ */
+function update_blog_details( $blog_id, $details = array() ) {
+        global $wpdb;
+
+        if ( empty($details) )
+                return false;
+
+        if ( is_object($details) )
+                $details = get_object_vars($details);
+
+        $current_details = get_blog_details($blog_id, false);
+        if ( empty($current_details) )
+                return false;
+
+        $current_details = get_object_vars($current_details);
+
+        $details = array_merge($current_details, $details);
+        $details['last_updated'] = current_time('mysql', true);
+
+        $update_details = array();
+        $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
+        foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
+                $update_details[$field] = $details[$field];
+
+        $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
+
+        // If spam status changed, issue actions.
+        if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
+                if ( $details[ 'spam' ] == 1 )
+                        do_action( "make_spam_blog", $blog_id );
+                else
+                        do_action( "make_ham_blog", $blog_id );
+        }
+
+        if ( isset($details[ 'public' ]) )
+                update_blog_option( $blog_id, 'blog_public', $details[ 'public' ], false );
+
+        refresh_blog_details($blog_id);
+
+        return true;
+}
+
+// @todo move to user.php
</ins><span class="cx"> function get_current_user_id() {
</span><span class="cx">         global $current_user;
</span><span class="cx">         return $current_user->ID;
</span><span class="lines">@@ -507,14 +575,25 @@
</span><span class="cx">         return $archived;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-function update_blog_status( $id, $pref, $value, $refresh = 1 ) {
</del><ins>+/**
+ * Update a blog details field.
+ *
+ * @since 3.0
+ *
+ * @param int $blog_id BLog ID
+ * @param string $pref A field name
+ * @param string $value Value for $pref
+ * @param bool $refresh Whether to refresh the blog details cache. Default is true.
+ */
+function update_blog_status( $blog_id, $pref, $value, $refresh = true ) {
</ins><span class="cx">         global $wpdb;
</span><span class="cx">
</span><span class="cx">         if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
</span><span class="cx">                 return $value;
</span><span class="cx">
</span><del>-        $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $id) );
-        if ( $refresh == 1 )
</del><ins>+        $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
+
+        if ( $refresh )
</ins><span class="cx">                 refresh_blog_details($id);
</span><span class="cx">
</span><span class="cx">         if ( $pref == 'spam' ) {
</span><span class="lines">@@ -534,7 +613,7 @@
</span><span class="cx">         if ( $details )
</span><span class="cx">                 return $details->$pref;
</span><span class="cx">
</span><del>-        return $wpdb->get_var( $wpdb->prepare("SELECT $pref FROM {$wpdb->blogs} WHERE blog_id = %d", $id) );
</del><ins>+        return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
</span></span></pre>
</div>
</div>
</body>
</html>