root/trunk/wp-includes/wpmu-functions.php

Revision 1528, 66.9 kB (checked in by donncha, 1 day ago)

Added login link to new user password email

  • Property svn:eol-style set to native
Line 
1 <?php
2 /*
3     Helper functions for WPMU
4 */
5 function load_muplugin_textdomain($domain, $path = false) {
6     $locale = get_locale();
7     if ( empty($locale) )
8         $locale = 'en_US';
9
10     if ( false === $path )
11         $path = WPMU_PLUGIN_DIR;
12
13     $mofile = WPMU_PLUGIN_DIR . "/$domain-$locale.mo";
14     load_textdomain($domain, $mofile);
15 }
16
17 function wpmu_update_blogs_date() {
18     global $wpdb;
19
20     $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql')), array('blog_id' => $wpdb->blogid) );
21     refresh_blog_details( $wpdb->blogid );
22
23     do_action( 'wpmu_blog_updated', $wpdb->blogid );
24 }
25
26 function get_blogaddress_by_id( $blog_id ) {
27     $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
28     return clean_url("http://" . $bloginfo->domain . $bloginfo->path);
29 }
30
31 function get_blogaddress_by_name( $blogname ) {
32     global $hostname, $domain, $base;
33
34     if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
35         if( $blogname == 'main' )
36             $blogname = 'www';
37         return clean_url("http://".$blogname.".".$domain.$base);
38     } else {
39         return clean_url("http://".$hostname.$base.$blogname);
40     }
41 }
42
43 function get_blogaddress_by_domain( $domain, $path ){
44     if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
45         $url = "http://".$domain.$path;
46     } else {
47         if( $domain != $_SERVER['HTTP_HOST'] ) {
48             $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
49             if( $blogname != 'www.' ) {
50                 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path . $blogname . '/';
51             } else { // we're installing the main blog
52                 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
53             }
54         } else { // main blog
55             $url = 'http://' . $domain . $path;
56         }
57     }
58     return clean_url($url);
59 }
60
61 function get_sitestats() {
62     global $wpdb;
63
64     $stats['blogs'] = get_blog_count();
65
66     $count_ts = get_site_option( "get_user_count_ts" );
67     if( time() - $count_ts > 3600 ) {
68         $count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users}" );
69         update_site_option( "user_count", $count );
70         update_site_option( "user_count_ts", time() );
71     } else {
72         $count = get_site_option( "user_count" );
73     }
74     $stats['users'] = $count;
75     return $stats;
76 }
77
78 function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
79     global $wpdb;
80     
81     if( $sitedomain == '' ) {
82         $site_id = $wpdb->siteid;
83     } else {
84         $site_id = $wpdb->get_var( $wpdb->prepare("SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
85     }
86     
87     if( $site_id != false ) {
88         return $wpdb->get_results( $wpdb->prepare("SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = %d AND sm.site_id = %d", $wpdb->sitemeta.'.meta_value', $site_id), ARRAY_A );
89     }
90     return false;
91 }
92
93 function get_user_details( $username ) {
94     global $wpdb;
95     return $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = %s", $username) );
96 }
97
98 function is_main_blog() {
99     global $current_blog, $current_site;
100     if( $current_blog->domain == $current_site->domain && $current_blog->path == $current_site->path )
101         return true;
102     return false;
103 }
104
105 function get_id_from_blogname( $name ) {
106     global $wpdb, $current_site;
107     if( constant( 'VHOST' ) == 'yes' ) {
108         $domain = $name . '.' . $current_site->domain;
109         $path = $current_site->path;
110     } else {
111         $domain = $current_site->domain;
112         $path = $current_site->path . $name;
113     }
114     return $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
115 }
116
117 function get_blog_details( $id, $getall = true ) {
118     global $wpdb;
119
120     if( !is_numeric( $id ) ) {
121         $id = get_id_from_blogname( $id );
122     }
123     $all = $getall == true ? '' : 'short';
124     $details = wp_cache_get( $id . $all, 'blog-details' );
125
126     if ( $details ) {
127         if ( $details == -1 )
128             return false;
129         elseif ( !is_object($details) ) // Clear old pre-serialized objects. Cache clients do better with that.
130             wp_cache_delete( $id . $all, 'blog-details' );
131         else
132             return $details;
133     }
134
135     $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $id) ); // get_blog_details ?
136     if ( !$details ) {
137         wp_cache_set( $id . $all, -1, 'blog-details' );
138         return false;
139     }
140
141     if ( !$getall ) {
142         wp_cache_add( $id . $all, $details, 'blog-details' );
143         return $details;
144     }
145
146     $wpdb->suppress_errors();
147     $details->blogname   = get_blog_option($id, 'blogname');
148     $details->siteurl    = get_blog_option($id, 'siteurl');
149     $details->post_count = get_blog_option($id, 'post_count');
150     $wpdb->suppress_errors( false );
151
152     $details = apply_filters('blog_details', $details);
153
154     wp_cache_set( $id . $all, $details, 'blog-details' );
155
156     $key = md5( $details->domain . $details->path );
157     wp_cache_set( $key, $details, 'blog-lookup' );
158
159     return $details;
160 }
161
162 function refresh_blog_details( $id ) {
163     $id = (int) $id;
164     $details = get_blog_details( $id, false );
165     
166     wp_cache_delete( $id , 'blog-details' );
167     wp_cache_delete( md5( $details->domain . $details->path )  , 'blog-lookup' );
168 }
169
170 function get_current_user_id() {
171     global $current_user;
172     return $current_user->ID;
173 }
174
175 function is_site_admin( $user_login = false ) {
176     global $current_user;
177
178     if ( !$current_user && !$user_login )
179         return false;
180
181     if ( $user_login )
182         $user_login = sanitize_user( $user_login );
183     else
184         $user_login = $current_user->user_login;
185
186     $site_admins = get_site_option( 'site_admins', array('admin') );
187     if( is_array( $site_admins ) && in_array( $user_login, $site_admins ) )
188         return true;
189
190     return false;
191 }
192
193 // expects key not to be SQL escaped
194 function get_site_option( $key, $default = false, $use_cache = true ) {
195     global $wpdb;
196
197     // Allow plugins to short-circuit site options.
198      $pre = apply_filters( 'pre_site_option_' . $key, false );
199      if ( false !== $pre )
200          return $pre;
201
202     if( $use_cache == true ) {
203         $value = wp_cache_get($wpdb->siteid . $key, 'site-options');
204     } else {
205         $value = false;
206     }
207
208     if ( false === $value ) {
209         $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) );
210         if ( ! is_null($value) ) {
211             wp_cache_add($wpdb->siteid . $key, $value, 'site-options');
212         } elseif ( $default ) {
213             wp_cache_add($wpdb->siteid . $key, addslashes( $default ), 'site-options');
214             return $default;
215         } else {
216             wp_cache_add($wpdb->siteid . $key, false, 'site-options');
217             return false;
218         }
219     }
220
221     if (! unserialize($value) )
222         $value = stripslashes( $value );
223
224     return apply_filters( 'site_option_' . $key, maybe_unserialize( $value ) );
225 }
226
227 // expects $key, $value not to be SQL escaped
228 function add_site_option( $key, $value ) {
229     global $wpdb;
230
231     $exists = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) );
232     if ( is_object( $exists ) ) {// If we already have it
233         update_site_option( $key, $value );
234         return false;
235     }
236
237     if ( is_array($value) || is_object($value) )
238         $value = serialize($value);
239     wp_cache_delete($wpdb->siteid . $key, 'site-options');
240
241     $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $key, 'meta_value' => $value) );
242     return $wpdb->insert_id;
243 }
244
245 // expects $key, $value not to be SQL escaped
246 function update_site_option( $key, $value ) {
247     global $wpdb;
248
249     if ( $value == get_site_option( $key ) )
250          return false;
251
252     $exists = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) );
253     if ( false == is_object( $exists ) ) // It's a new record
254         return add_site_option( $key, $value );
255
256     if ( is_array($value) || is_object($value) )
257         $value = serialize($value);
258
259     $wpdb->update( $wpdb->sitemeta, array('meta_value' => $value), array('site_id' => $wpdb->siteid, 'meta_key' => $key) );
260     wp_cache_delete( $wpdb->siteid . $key, 'site-options' );
261     return true;
262 }
263
264 /*
265 function get_blog_option( $id, $key, $default='na' ) {
266     switch_to_blog($id);
267     $option = get_option( $key );
268     restore_current_blog();
269     return $option;
270 }
271 */
272
273 function get_blog_option( $blog_id, $setting, $deprecated = '' ) {
274     global $wpdb;
275
276     $key = $blog_id."-".$setting."-blog_option";
277     $value = wp_cache_get( $key, "site-options" );
278     if( $value == null ) {
279         $row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->base_prefix}{$blog_id}_options WHERE option_name = %s", $setting) );
280         if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
281             $value = $row->option_value;
282             if( $value == false ) {
283                 wp_cache_set($key, 'falsevalue', 'site-options');
284                 return false;
285             } else {
286                 wp_cache_set($key, $value, 'site-options');
287             }
288         } else { // option does not exist, so we must cache its non-existence
289             wp_cache_set($key, 'noop', 'site-options');
290         }
291     } elseif( $value == 'noop' ) {
292         return false;
293     } elseif( $value == 'falsevalue' ) {
294         return false;
295     }
296     // If home is not set use siteurl.
297     if ( 'home' == $setting && '' == $value )
298         return get_blog_option($blog_id, 'siteurl');
299
300     if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
301         $value = preg_replace('|/+$|', '', $value);
302
303     if (! unserialize($value) )
304         $value = stripslashes( $value );
305
306     return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
307 }
308
309 function add_blog_option( $id, $key, $value ) {
310     $id = (int) $id;
311     
312     switch_to_blog($id);
313     add_option( $key, $value );
314     restore_current_blog();
315     wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
316 }
317
318 function delete_blog_option( $id, $key ) {
319     $id = (int) $id;
320     
321     switch_to_blog($id);
322     delete_option( $key );
323     restore_current_blog();
324     wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
325 }
326
327 function update_blog_option( $id, $key, $value, $refresh = true ) {
328     $id = (int) $id;
329     
330     switch_to_blog($id);
331     update_option( $key, $value );
332     restore_current_blog();
333     
334     if( $refresh == true ) {
335         refresh_blog_details( $id );
336     }
337     wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
338 }
339
340 function switch_to_blog( $new_blog ) {
341     global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
342
343     if ( empty($new_blog) )
344         return false;
345
346     if ( empty($switched_stack) )
347         $switched_stack = array();
348
349     $switched_stack[] = $blog_id;
350
351     if ( $blog_id == $new_blog )
352         return false;
353
354     $wp_object_cache->switched_cache[ $blog_id ] = $wp_object_cache->cache;
355     unset( $wp_object_cache->cache );
356
357     $wpdb->set_blog_id($new_blog);
358     $table_prefix = $wpdb->prefix;
359     $prev_blog_id = $blog_id;
360     $blog_id = $new_blog;
361
362     if( is_object( $wp_roles ) ) {
363         $wpdb->suppress_errors();
364         $wp_roles->_init();
365         $wpdb->suppress_errors( false );
366     }
367
368     if ( is_object( $current_user ) )
369         $current_user->_init_caps();
370
371     do_action('switch_blog', $blog_id, $prev_blog_id);
372     $switched = true;
373     return true;
374 }
375
376 function restore_current_blog() {
377     global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
378
379     if ( !$switched )
380         return false;
381
382     $blog = array_pop($switched_stack);
383     if ( $blog_id == $blog )
384         return false;
385
386     $wp_object_cache->cache = $wp_object_cache->switched_cache[ $blog ];
387     unset( $wp_object_cache->switched_cache[ $blog ] );
388
389     $wpdb->set_blog_id($blog);
390     $prev_blog_id = $blog_id;
391     $blog_id = $blog;
392     $table_prefix = $wpdb->prefix;
393
394     if( is_object( $wp_roles ) ) {
395         $wpdb->suppress_errors();
396         $wp_roles->_init();
397         $wpdb->suppress_errors( false );
398     }
399
400     if ( is_object( $current_user ) )
401         $current_user->_init_caps();
402
403     do_action('switch_blog', $blog_id, $prev_blog_id);
404
405     $switched = false;
406     return true;
407 }
408
409 function get_blogs_of_user( $id, $all = false ) {
410     global $wpdb;
411
412     $user = get_userdata( (int) $id );
413     if ( !$user )
414         return false;
415
416     $blogs = $match = array();
417     foreach ( (array) $user as $key => $value ) {
418         if ( strstr( $key, '_capabilities') && strstr( $key, $wpdb->base_prefix) ) {
419             preg_match('/' . $wpdb->base_prefix . '(\d+)_capabilities/', $key, $match);
420             $blog = get_blog_details( $match[1] );
421             if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
422                 $blogs[$match[1]]->userblog_id = $match[1];
423                 $blogs[$match[1]]->blogname    = $blog->blogname;
424                 $blogs[$match[1]]->domain      = $blog->domain;
425                 $blogs[$match[1]]->path        = $blog->path;
426                 $blogs[$match[1]]->site_id     = $blog->site_id;
427                 $blogs[$match[1]]->siteurl     = $blog->siteurl;
428             }
429         }
430     }
431
432     return $blogs;
433 }
434
435 function get_active_blog_for_user( $user_id ) { // get an active blog for user - either primary blog or from blogs list
436     $primary_blog = get_usermeta( $user_id, "primary_blog" );
437     if( $primary_blog == false ) {
438         $details = false;
439     } else {
440         $details = get_blog_details( $primary_blog );
441     }
442
443     if( ( is_object( $details ) == false ) || ( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) ) {
444         $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
445         $ret = false;
446         if( is_array( $blogs ) && count( $blogs ) > 0 ) {
447             foreach( (array) $blogs as $blog_id => $blog ) {
448                 $details = get_blog_details( $blog_id );
449                 if( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
450                     $ret = $blog;
451                     break;
452                 }
453             }
454         } else {
455             $ret = "username only"; // user has no blogs. We can add details for dashboard.wordpress.com here.
456         }
457         return $ret;
458     } else {