]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/ms.php
Wordpress 3.3
[autoinstalls/wordpress.git] / wp-admin / includes / ms.php
1 <?php
2 /**
3  * Multisite administration functions.
4  *
5  * @package WordPress
6  * @subpackage Multisite
7  * @since 3.0.0
8  */
9
10 /**
11  * Determine if uploaded file exceeds space quota.
12  *
13  * @since 3.0.0
14  *
15  * @param array $file $_FILES array for a given file.
16  * @return array $_FILES array with 'error' key set if file exceeds quota. 'error' is empty otherwise.
17  */
18 function check_upload_size( $file ) {
19         if ( get_site_option( 'upload_space_check_disabled' ) )
20                 return $file;
21
22         if ( $file['error'] != '0' ) // there's already an error
23                 return $file;
24
25         if ( defined( 'WP_IMPORTING' ) )
26                 return $file;
27
28         $space_allowed = 1048576 * get_space_allowed();
29         $space_used = get_dirsize( BLOGUPLOADDIR );
30         $space_left = $space_allowed - $space_used;
31         $file_size = filesize( $file['tmp_name'] );
32         if ( $space_left < $file_size )
33                 $file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) );
34         if ( $file_size > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
35                 $file['error'] = sprintf(__('This file is too big. Files must be less than %1$s KB in size.'), get_site_option( 'fileupload_maxk', 1500 ) );
36         if ( upload_is_user_over_quota( false ) ) {
37                 $file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
38         }
39         if ( $file['error'] != '0' && !isset($_POST['html-upload']) )
40                 wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
41
42         return $file;
43 }
44 add_filter( 'wp_handle_upload_prefilter', 'check_upload_size' );
45
46 /**
47  * Delete a blog
48  *
49  * @since 3.0.0
50  *
51  * @param int $blog_id Blog ID
52  * @param bool $drop True if blog's table should be dropped.  Default is false.
53  * @return void
54  */
55 function wpmu_delete_blog( $blog_id, $drop = false ) {
56         global $wpdb, $current_site;
57
58         $switch = false;
59         if ( $blog_id != $wpdb->blogid ) {
60                 $switch = true;
61                 switch_to_blog( $blog_id );
62                 $blog = get_blog_details( $blog_id );
63         } else {
64                 $blog = $GLOBALS['current_blog'];
65         }
66
67         do_action( 'delete_blog', $blog_id, $drop );
68
69         $users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
70
71         // Remove users from this blog.
72         if ( ! empty( $users ) ) {
73                 foreach ( $users as $user_id ) {
74                         remove_user_from_blog( $user_id, $blog_id );
75                 }
76         }
77
78         update_blog_status( $blog_id, 'deleted', 1 );
79
80         // Don't destroy the initial, main, or root blog.
81         if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_site->path && $blog->domain == $current_site->domain ) ) )
82                 $drop = false;
83
84         if ( $drop ) {
85
86                 $drop_tables = apply_filters( 'wpmu_drop_tables', $wpdb->tables( 'blog' ) );
87
88                 foreach ( (array) $drop_tables as $table ) {
89                         $wpdb->query( "DROP TABLE IF EXISTS `$table`" );
90                 }
91
92                 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
93                 $dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id );
94                 $dir = rtrim( $dir, DIRECTORY_SEPARATOR );
95                 $top_dir = $dir;
96                 $stack = array($dir);
97                 $index = 0;
98
99                 while ( $index < count( $stack ) ) {
100                         # Get indexed directory from stack
101                         $dir = $stack[$index];
102
103                         $dh = @opendir( $dir );
104                         if ( $dh ) {
105                                 while ( ( $file = @readdir( $dh ) ) !== false ) {
106                                         if ( $file == '.' || $file == '..' )
107                                                 continue;
108
109                                         if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) )
110                                                 $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
111                                         else if ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) )
112                                                 @unlink( $dir . DIRECTORY_SEPARATOR . $file );
113                                 }
114                         }
115                         $index++;
116                 }
117
118                 $stack = array_reverse( $stack );  // Last added dirs are deepest
119                 foreach( (array) $stack as $dir ) {
120                         if ( $dir != $top_dir)
121                         @rmdir( $dir );
122                 }
123         }
124
125         if ( $switch )
126                 restore_current_blog();
127 }
128
129 // @todo Merge with wp_delete_user() ?
130 function wpmu_delete_user( $id ) {
131         global $wpdb;
132
133         $id = (int) $id;
134
135         do_action( 'wpmu_delete_user', $id );
136
137         $blogs = get_blogs_of_user( $id );
138
139         if ( ! empty( $blogs ) ) {
140                 foreach ( $blogs as $blog ) {
141                         switch_to_blog( $blog->userblog_id );
142                         remove_user_from_blog( $id, $blog->userblog_id );
143
144                         $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
145                         foreach ( (array) $post_ids as $post_id ) {
146                                 wp_delete_post( $post_id );
147                         }
148
149                         // Clean links
150                         $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
151
152                         if ( $link_ids ) {
153                                 foreach ( $link_ids as $link_id )
154                                         wp_delete_link( $link_id );
155                         }
156
157                         restore_current_blog();
158                 }
159         }
160
161         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->users WHERE ID = %d", $id ) );
162         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
163
164         clean_user_cache( $id );
165
166         // allow for commit transaction
167         do_action( 'deleted_user', $id );
168
169         return true;
170 }
171
172 function wpmu_get_blog_allowedthemes( $blog_id = 0 ) {
173         $themes = get_themes();
174
175         if ( $blog_id != 0 )
176                 switch_to_blog( $blog_id );
177
178         $blog_allowed_themes = get_option( 'allowedthemes' );
179         if ( !is_array( $blog_allowed_themes ) || empty( $blog_allowed_themes ) ) { // convert old allowed_themes to new allowedthemes
180                 $blog_allowed_themes = get_option( 'allowed_themes' );
181
182                 if ( is_array( $blog_allowed_themes ) ) {
183                         foreach( (array) $themes as $key => $theme ) {
184                                 $theme_key = esc_html( $theme['Stylesheet'] );
185                                 if ( isset( $blog_allowed_themes[$key] ) == true ) {
186                                         $blog_allowedthemes[$theme_key] = 1;
187                                 }
188                         }
189                         $blog_allowed_themes = $blog_allowedthemes;
190                         add_option( 'allowedthemes', $blog_allowed_themes );
191                         delete_option( 'allowed_themes' );
192                 }
193         }
194
195         if ( $blog_id != 0 )
196                 restore_current_blog();
197
198         return $blog_allowed_themes;
199 }
200
201 function update_option_new_admin_email( $old_value, $value ) {
202         $email = get_option( 'admin_email' );
203         if ( $value == get_option( 'admin_email' ) || !is_email( $value ) )
204                 return;
205
206         $hash = md5( $value. time() .mt_rand() );
207         $new_admin_email = array(
208                 'hash' => $hash,
209                 'newemail' => $value
210         );
211         update_option( 'adminhash', $new_admin_email );
212
213         $content = apply_filters( 'new_admin_email_content', __( "Dear user,
214
215 You recently requested to have the administration email address on
216 your site changed.
217 If this is correct, please click on the following link to change it:
218 ###ADMIN_URL###
219
220 You can safely ignore and delete this email if you do not want to
221 take this action.
222
223 This email has been sent to ###EMAIL###
224
225 Regards,
226 All at ###SITENAME###
227 ###SITEURL### "), $new_admin_email );
228
229         $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'options.php?adminhash='.$hash ) ), $content );
230         $content = str_replace( '###EMAIL###', $value, $content );
231         $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
232         $content = str_replace( '###SITEURL###', network_home_url(), $content );
233
234         wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), get_option( 'blogname' ) ), $content );
235 }
236 add_action( 'update_option_new_admin_email', 'update_option_new_admin_email', 10, 2 );
237 add_action( 'add_option_new_admin_email', 'update_option_new_admin_email', 10, 2 );
238
239 function send_confirmation_on_profile_email() {
240         global $errors, $wpdb;
241         $current_user = wp_get_current_user();
242         if ( ! is_object($errors) )
243                 $errors = new WP_Error();
244
245         if ( $current_user->ID != $_POST['user_id'] )
246                 return false;
247
248         if ( $current_user->user_email != $_POST['email'] ) {
249                 if ( !is_email( $_POST['email'] ) ) {
250                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
251                         return;
252                 }
253
254                 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
255                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
256                         delete_option( $current_user->ID . '_new_email' );
257                         return;
258                 }
259
260                 $hash = md5( $_POST['email'] . time() . mt_rand() );
261                 $new_user_email = array(
262                                 'hash' => $hash,
263                                 'newemail' => $_POST['email']
264                                 );
265                 update_option( $current_user->ID . '_new_email', $new_user_email );
266
267                 $content = apply_filters( 'new_user_email_content', __( "Dear user,
268
269 You recently requested to have the email address on your account changed.
270 If this is correct, please click on the following link to change it:
271 ###ADMIN_URL###
272
273 You can safely ignore and delete this email if you do not want to
274 take this action.
275
276 This email has been sent to ###EMAIL###
277
278 Regards,
279 All at ###SITENAME###
280 ###SITEURL###" ), $new_user_email );
281
282                 $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
283                 $content = str_replace( '###EMAIL###', $_POST['email'], $content);
284                 $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
285                 $content = str_replace( '###SITEURL###', network_home_url(), $content );
286
287                 wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), get_option( 'blogname' ) ), $content );
288                 $_POST['email'] = $current_user->user_email;
289         }
290 }
291 add_action( 'personal_options_update', 'send_confirmation_on_profile_email' );
292
293 function new_user_email_admin_notice() {
294         if ( strpos( $_SERVER['PHP_SELF'], 'profile.php' ) && isset( $_GET['updated'] ) && $email = get_option( get_current_user_id() . '_new_email' ) )
295                 echo "<div class='update-nag'>" . sprintf( __( "Your email address has not been updated yet. Please check your inbox at %s for a confirmation email." ), $email['newemail'] ) . "</div>";
296 }
297 add_action( 'admin_notices', 'new_user_email_admin_notice' );
298
299 function get_site_allowed_themes() {
300         $themes = get_themes();
301         $allowed_themes = get_site_option( 'allowedthemes' );
302         if ( !is_array( $allowed_themes ) || empty( $allowed_themes ) ) {
303                 $allowed_themes = get_site_option( 'allowed_themes' ); // convert old allowed_themes format
304                 if ( !is_array( $allowed_themes ) ) {
305                         $allowed_themes = array();
306                 } else {
307                         foreach( (array) $themes as $key => $theme ) {
308                                 $theme_key = esc_html( $theme['Stylesheet'] );
309                                 if ( isset( $allowed_themes[ $key ] ) == true ) {
310                                         $allowedthemes[ $theme_key ] = 1;
311                                 }
312                         }
313                         $allowed_themes = $allowedthemes;
314                 }
315         }
316         return $allowed_themes;
317 }
318
319 /**
320  * Determines if there is any upload space left in the current blog's quota.
321  *
322  * @since 3.0.0
323  * @return bool True if space is available, false otherwise.
324  */
325 function is_upload_space_available() {
326         if ( get_site_option( 'upload_space_check_disabled' ) )
327                 return true;
328
329         if ( !( $space_allowed = get_upload_space_available() ) )
330                 return false;
331
332         return true;
333 }
334
335 /**
336  * @since 3.0.0
337  *
338  * @return int of upload size limit in bytes
339  */
340 function upload_size_limit_filter( $size ) {
341         $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
342         if ( get_site_option( 'upload_space_check_disabled' ) )
343                 return min( $size, $fileupload_maxk );
344
345         return min( $size, $fileupload_maxk, get_upload_space_available() );
346 }
347 /**
348  * Determines if there is any upload space left in the current blog's quota.
349  *
350  * @return int of upload space available in bytes
351  */
352 function get_upload_space_available() {
353         $space_allowed = get_space_allowed() * 1024 * 1024;
354         if ( get_site_option( 'upload_space_check_disabled' ) )
355                 return $space_allowed;
356
357         $dir_name = trailingslashit( BLOGUPLOADDIR );
358         if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) )
359                 return $space_allowed;
360
361         $dir = dir( $dir_name );
362         $size = 0;
363
364         while ( $file = $dir->read() ) {
365                 if ( $file != '.' && $file != '..' ) {
366                         if ( is_dir( $dir_name . $file) ) {
367                                 $size += get_dirsize( $dir_name . $file );
368                         } else {
369                                 $size += filesize( $dir_name . $file );
370                         }
371                 }
372         }
373         $dir->close();
374
375         if ( ( $space_allowed - $size ) <= 0 )
376                 return 0;
377
378         return $space_allowed - $size;
379 }
380
381 /**
382  * Returns the upload quota for the current blog.
383  *
384  * @return int Quota
385  */
386 function get_space_allowed() {
387         $space_allowed = get_option( 'blog_upload_space' );
388         if ( $space_allowed === false )
389                 $space_allowed = get_site_option( 'blog_upload_space' );
390         if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
391                 $space_allowed = 50;
392
393         return $space_allowed;
394 }
395
396 function display_space_usage() {
397         $space = get_space_allowed();
398         $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
399
400         $percentused = ( $used / $space ) * 100;
401
402         if ( $space > 1000 ) {
403                 $space = number_format( $space / 1024 );
404                 /* translators: Gigabytes */
405                 $space .= __( 'GB' );
406         } else {
407                 /* translators: Megabytes */
408                 $space .= __( 'MB' );
409         }
410         ?>
411         <strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percentused ), $space ); ?></strong>
412         <?php
413 }
414
415 // Edit blog upload space setting on Edit Blog page
416 function upload_space_setting( $id ) {
417         $quota = get_blog_option( $id, 'blog_upload_space' );
418         if ( !$quota )
419                 $quota = '';
420
421         ?>
422         <tr>
423                 <th><?php _e( 'Site Upload Space Quota '); ?></th>
424                 <td><input type="text" size="3" name="option[blog_upload_space]" value="<?php echo $quota; ?>" /> <?php _e( 'MB (Leave blank for network default)' ); ?></td>
425         </tr>
426         <?php
427 }
428 add_action( 'wpmueditblogaction', 'upload_space_setting' );
429
430 function update_user_status( $id, $pref, $value, $deprecated = null ) {
431         global $wpdb;
432
433         if ( null !== $deprecated  )
434                 _deprecated_argument( __FUNCTION__, '3.1' );
435
436         $wpdb->update( $wpdb->users, array( $pref => $value ), array( 'ID' => $id ) );
437
438         clean_user_cache( $id );
439
440         if ( $pref == 'spam' ) {
441                 if ( $value == 1 )
442                         do_action( 'make_spam_user', $id );
443                 else
444                         do_action( 'make_ham_user', $id );
445         }
446
447         return $value;
448 }
449
450 function refresh_user_details( $id ) {
451         $id = (int) $id;
452
453         if ( !$user = get_userdata( $id ) )
454                 return false;
455
456         clean_user_cache( $id );
457
458         return $id;
459 }
460
461 function format_code_lang( $code = '' ) {
462         $code = strtolower( substr( $code, 0, 2 ) );
463         $lang_codes = array(
464                 'aa' => 'Afar', 'ab' => 'Abkhazian', 'af' => 'Afrikaans', 'ak' => 'Akan', 'sq' => 'Albanian', 'am' => 'Amharic', 'ar' => 'Arabic', 'an' => 'Aragonese', 'hy' => 'Armenian', 'as' => 'Assamese', 'av' => 'Avaric', 'ae' => 'Avestan', 'ay' => 'Aymara', 'az' => 'Azerbaijani', 'ba' => 'Bashkir', 'bm' => 'Bambara', 'eu' => 'Basque', 'be' => 'Belarusian', 'bn' => 'Bengali',
465                 'bh' => 'Bihari', 'bi' => 'Bislama', 'bs' => 'Bosnian', 'br' => 'Breton', 'bg' => 'Bulgarian', 'my' => 'Burmese', 'ca' => 'Catalan; Valencian', 'ch' => 'Chamorro', 'ce' => 'Chechen', 'zh' => 'Chinese', 'cu' => 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic', 'cv' => 'Chuvash', 'kw' => 'Cornish', 'co' => 'Corsican', 'cr' => 'Cree',
466                 'cs' => 'Czech', 'da' => 'Danish', 'dv' => 'Divehi; Dhivehi; Maldivian', 'nl' => 'Dutch; Flemish', 'dz' => 'Dzongkha', 'en' => 'English', 'eo' => 'Esperanto', 'et' => 'Estonian', 'ee' => 'Ewe', 'fo' => 'Faroese', 'fj' => 'Fijjian', 'fi' => 'Finnish', 'fr' => 'French', 'fy' => 'Western Frisian', 'ff' => 'Fulah', 'ka' => 'Georgian', 'de' => 'German', 'gd' => 'Gaelic; Scottish Gaelic',
467                 'ga' => 'Irish', 'gl' => 'Galician', 'gv' => 'Manx', 'el' => 'Greek, Modern', 'gn' => 'Guarani', 'gu' => 'Gujarati', 'ht' => 'Haitian; Haitian Creole', 'ha' => 'Hausa', 'he' => 'Hebrew', 'hz' => 'Herero', 'hi' => 'Hindi', 'ho' => 'Hiri Motu', 'hu' => 'Hungarian', 'ig' => 'Igbo', 'is' => 'Icelandic', 'io' => 'Ido', 'ii' => 'Sichuan Yi', 'iu' => 'Inuktitut', 'ie' => 'Interlingue',
468                 'ia' => 'Interlingua (International Auxiliary Language Association)', 'id' => 'Indonesian', 'ik' => 'Inupiaq', 'it' => 'Italian', 'jv' => 'Javanese', 'ja' => 'Japanese', 'kl' => 'Kalaallisut; Greenlandic', 'kn' => 'Kannada', 'ks' => 'Kashmiri', 'kr' => 'Kanuri', 'kk' => 'Kazakh', 'km' => 'Central Khmer', 'ki' => 'Kikuyu; Gikuyu', 'rw' => 'Kinyarwanda', 'ky' => 'Kirghiz; Kyrgyz',
469                 'kv' => 'Komi', 'kg' => 'Kongo', 'ko' => 'Korean', 'kj' => 'Kuanyama; Kwanyama', 'ku' => 'Kurdish', 'lo' => 'Lao', 'la' => 'Latin', 'lv' => 'Latvian', 'li' => 'Limburgan; Limburger; Limburgish', 'ln' => 'Lingala', 'lt' => 'Lithuanian', 'lb' => 'Luxembourgish; Letzeburgesch', 'lu' => 'Luba-Katanga', 'lg' => 'Ganda', 'mk' => 'Macedonian', 'mh' => 'Marshallese', 'ml' => 'Malayalam',
470                 'mi' => 'Maori', 'mr' => 'Marathi', 'ms' => 'Malay', 'mg' => 'Malagasy', 'mt' => 'Maltese', 'mo' => 'Moldavian', 'mn' => 'Mongolian', 'na' => 'Nauru', 'nv' => 'Navajo; Navaho', 'nr' => 'Ndebele, South; South Ndebele', 'nd' => 'Ndebele, North; North Ndebele', 'ng' => 'Ndonga', 'ne' => 'Nepali', 'nn' => 'Norwegian Nynorsk; Nynorsk, Norwegian', 'nb' => 'BokmÃ¥l, Norwegian, Norwegian BokmÃ¥l',
471                 'no' => 'Norwegian', 'ny' => 'Chichewa; Chewa; Nyanja', 'oc' => 'Occitan, Provençal', 'oj' => 'Ojibwa', 'or' => 'Oriya', 'om' => 'Oromo', 'os' => 'Ossetian; Ossetic', 'pa' => 'Panjabi; Punjabi', 'fa' => 'Persian', 'pi' => 'Pali', 'pl' => 'Polish', 'pt' => 'Portuguese', 'ps' => 'Pushto', 'qu' => 'Quechua', 'rm' => 'Romansh', 'ro' => 'Romanian', 'rn' => 'Rundi', 'ru' => 'Russian',
472                 'sg' => 'Sango', 'sa' => 'Sanskrit', 'sr' => 'Serbian', 'hr' => 'Croatian', 'si' => 'Sinhala; Sinhalese', 'sk' => 'Slovak', 'sl' => 'Slovenian', 'se' => 'Northern Sami', 'sm' => 'Samoan', 'sn' => 'Shona', 'sd' => 'Sindhi', 'so' => 'Somali', 'st' => 'Sotho, Southern', 'es' => 'Spanish; Castilian', 'sc' => 'Sardinian', 'ss' => 'Swati', 'su' => 'Sundanese', 'sw' => 'Swahili',
473                 'sv' => 'Swedish', 'ty' => 'Tahitian', 'ta' => 'Tamil', 'tt' => 'Tatar', 'te' => 'Telugu', 'tg' => 'Tajik', 'tl' => 'Tagalog', 'th' => 'Thai', 'bo' => 'Tibetan', 'ti' => 'Tigrinya', 'to' => 'Tonga (Tonga Islands)', 'tn' => 'Tswana', 'ts' => 'Tsonga', 'tk' => 'Turkmen', 'tr' => 'Turkish', 'tw' => 'Twi', 'ug' => 'Uighur; Uyghur', 'uk' => 'Ukrainian', 'ur' => 'Urdu', 'uz' => 'Uzbek',
474                 've' => 'Venda', 'vi' => 'Vietnamese', 'vo' => 'Volapük', 'cy' => 'Welsh','wa' => 'Walloon','wo' => 'Wolof', 'xh' => 'Xhosa', 'yi' => 'Yiddish', 'yo' => 'Yoruba', 'za' => 'Zhuang; Chuang', 'zu' => 'Zulu' );
475         $lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
476         return strtr( $code, $lang_codes );
477 }
478
479 function sync_category_tag_slugs( $term, $taxonomy ) {
480         if ( global_terms_enabled() && ( $taxonomy == 'category' || $taxonomy == 'post_tag' ) ) {
481                 if ( is_object( $term ) ) {
482                         $term->slug = sanitize_title( $term->name );
483                 } else {
484                         $term['slug'] = sanitize_title( $term['name'] );
485                 }
486         }
487         return $term;
488 }
489 add_filter( 'get_term', 'sync_category_tag_slugs', 10, 2 );
490
491 function _access_denied_splash() {
492         if ( ! is_user_logged_in() || is_network_admin() )
493                 return;
494
495         $blogs = get_blogs_of_user( get_current_user_id() );
496
497         if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) )
498                 return;
499
500         $blog_name = get_bloginfo( 'name' );
501
502         if ( empty( $blogs ) )
503                 wp_die( sprintf( __( 'You attempted to access the "%1$s" dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "%1$s" dashboard, please contact your network administrator.' ), $blog_name ) );
504
505         $output = '<p>' . sprintf( __( 'You attempted to access the "%1$s" dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the "%1$s" dashboard, please contact your network administrator.' ), $blog_name ) . '</p>';
506         $output .= '<p>' . __( 'If you reached this screen by accident and meant to visit one of your own sites, here are some shortcuts to help you find your way.' ) . '</p>';
507
508         $output .= '<h3>' . __('Your Sites') . '</h3>';
509         $output .= '<table>';
510
511         foreach ( $blogs as $blog ) {
512                 $output .= "<tr>";
513                 $output .= "<td valign='top'>";
514                 $output .= "{$blog->blogname}";
515                 $output .= "</td>";
516                 $output .= "<td valign='top'>";
517                 $output .= "<a href='" . esc_url( get_admin_url( $blog->userblog_id ) ) . "'>" . __( 'Visit Dashboard' ) . "</a> | <a href='" . esc_url( get_home_url( $blog->userblog_id ) ). "'>" . __( 'View Site' ) . "</a>" ;
518                 $output .= "</td>";
519                 $output .= "</tr>";
520         }
521         $output .= '</table>';
522
523         wp_die( $output );
524 }
525 add_action( 'admin_page_access_denied', '_access_denied_splash', 99 );
526
527 function check_import_new_users( $permission ) {
528         if ( !is_super_admin() )
529                 return false;
530         return true;
531 }
532 add_filter( 'import_allow_create_users', 'check_import_new_users' );
533 // See "import_allow_fetch_attachments" and "import_attachment_size_limit" filters too.
534
535 function mu_dropdown_languages( $lang_files = array(), $current = '' ) {
536         $flag = false;
537         $output = array();
538
539         foreach ( (array) $lang_files as $val ) {
540                 $code_lang = basename( $val, '.mo' );
541
542                 if ( $code_lang == 'en_US' ) { // American English
543                         $flag = true;
544                         $ae = __( 'American English' );
545                         $output[$ae] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $ae . '</option>';
546                 } elseif ( $code_lang == 'en_GB' ) { // British English
547                         $flag = true;
548                         $be = __( 'British English' );
549                         $output[$be] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $be . '</option>';
550                 } else {
551                         $translated = format_code_lang( $code_lang );
552                         $output[$translated] =  '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . esc_html ( $translated ) . '</option>';
553                 }
554
555         }
556
557         if ( $flag === false ) // WordPress english
558                 $output[] = '<option value=""' . selected( $current, '', false ) . '>' . __( 'English' ) . "</option>";
559
560         // Order by name
561         uksort( $output, 'strnatcasecmp' );
562
563         $output = apply_filters( 'mu_dropdown_languages', $output, $lang_files, $current );
564         echo implode( "\n\t", $output );
565 }
566
567 /* Warn the admin if SECRET SALT information is missing from wp-config.php */
568 function secret_salt_warning() {
569         if ( !is_super_admin() )
570                 return;
571         $secret_keys = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT' );
572         $out = '';
573         foreach( $secret_keys as $key ) {
574                 if ( ! defined( $key ) )
575                         $out .= "define( '$key', '" . esc_html( wp_generate_password( 64, true, true ) ) . "' );<br />";
576         }
577         if ( $out != '' ) {
578                 $msg  = __( 'Warning! WordPress encrypts user cookies, but you must add the following lines to <strong>wp-config.php</strong> for it to be more secure.' );
579                 $msg .= '<br/>' . __( "Before the line <code>/* That's all, stop editing! Happy blogging. */</code> please add this code:" );
580                 $msg .= "<br/><br/><code>$out</code>";
581
582                 echo "<div class='update-nag'>$msg</div>";
583         }
584 }
585 add_action( 'network_admin_notices', 'secret_salt_warning' );
586
587 function site_admin_notice() {
588         global $wp_db_version;
589         if ( !is_super_admin() )
590                 return false;
591         if ( get_site_option( 'wpmu_upgrade_site' ) != $wp_db_version )
592                 echo "<div class='update-nag'>" . sprintf( __( 'Thank you for Updating! Please visit the <a href="%s">Update Network</a> page to update all your sites.' ), esc_url( network_admin_url( 'upgrade.php' ) ) ) . "</div>";
593 }
594 add_action( 'admin_notices', 'site_admin_notice' );
595 add_action( 'network_admin_notices', 'site_admin_notice' );
596
597 function avoid_blog_page_permalink_collision( $data, $postarr ) {
598         if ( is_subdomain_install() )
599                 return $data;
600         if ( $data['post_type'] != 'page' )
601                 return $data;
602         if ( !isset( $data['post_name'] ) || $data['post_name'] == '' )
603                 return $data;
604         if ( !is_main_site() )
605                 return $data;
606
607         $post_name = $data['post_name'];
608         $c = 0;
609         while( $c < 10 && get_id_from_blogname( $post_name ) ) {
610                 $post_name .= mt_rand( 1, 10 );
611                 $c ++;
612         }
613         if ( $post_name != $data['post_name'] ) {
614                 $data['post_name'] = $post_name;
615         }
616         return $data;
617 }
618 add_filter( 'wp_insert_post_data', 'avoid_blog_page_permalink_collision', 10, 2 );
619
620 function choose_primary_blog() {
621         ?>
622         <table class="form-table">
623         <tr>
624         <?php /* translators: My sites label */ ?>
625                 <th scope="row"><?php _e( 'Primary Site' ); ?></th>
626                 <td>
627                 <?php
628                 $all_blogs = get_blogs_of_user( get_current_user_id() );
629                 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
630                 if ( count( $all_blogs ) > 1 ) {
631                         $found = false;
632                         ?>
633                         <select name="primary_blog">
634                                 <?php foreach( (array) $all_blogs as $blog ) {
635                                         if ( $primary_blog == $blog->userblog_id )
636                                                 $found = true;
637                                         ?><option value="<?php echo $blog->userblog_id ?>"<?php selected( $primary_blog,  $blog->userblog_id ); ?>><?php echo esc_url( get_home_url( $blog->userblog_id ) ) ?></option><?php
638                                 } ?>
639                         </select>
640                         <?php
641                         if ( !$found ) {
642                                 $blog = array_shift( $all_blogs );
643                                 update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
644                         }
645                 } elseif ( count( $all_blogs ) == 1 ) {
646                         $blog = array_shift( $all_blogs );
647                         echo $blog->domain;
648                         if ( $primary_blog != $blog->userblog_id ) // Set the primary blog again if it's out of sync with blog list.
649                                 update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
650                 } else {
651                         echo "N/A";
652                 }
653                 ?>
654                 </td>
655         </tr>
656         <?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
657                 <tr>
658                         <th scope="row" colspan="2" class="th-full">
659                                 <a href="<?php echo apply_filters( 'wp_signup_location', network_home_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
660                         </th>
661                 </tr>
662         <?php endif; ?>
663         </table>
664         <?php
665 }
666
667 function ms_deprecated_blogs_file() {
668         if ( ! is_super_admin() )
669                 return;
670         if ( ! file_exists( WP_CONTENT_DIR . '/blogs.php' ) )
671                 return;
672         echo '<div class="update-nag">' . sprintf( __( 'The <code>%1$s</code> file is deprecated. Please remove it and update your server rewrite rules to use <code>%2$s</code> instead.' ), 'wp-content/blogs.php', 'wp-includes/ms-files.php' ) . '</div>';
673 }
674 add_action( 'network_admin_notices', 'ms_deprecated_blogs_file' );
675
676 /**
677  * Grants super admin privileges.
678  *
679  * @since 3.0.0
680  * @param int $user_id
681  */
682 function grant_super_admin( $user_id ) {
683         global $super_admins;
684
685         // If global super_admins override is defined, there is nothing to do here.
686         if ( isset($super_admins) )
687                 return false;
688
689         do_action( 'grant_super_admin', $user_id );
690
691         // Directly fetch site_admins instead of using get_super_admins()
692         $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
693
694         $user = new WP_User( $user_id );
695         if ( ! in_array( $user->user_login, $super_admins ) ) {
696                 $super_admins[] = $user->user_login;
697                 update_site_option( 'site_admins' , $super_admins );
698                 do_action( 'granted_super_admin', $user_id );
699                 return true;
700         }
701         return false;
702 }
703
704 /**
705  * Revokes super admin privileges.
706  *
707  * @since 3.0.0
708  * @param int $user_id
709  */
710 function revoke_super_admin( $user_id ) {
711         global $super_admins;
712
713         // If global super_admins override is defined, there is nothing to do here.
714         if ( isset($super_admins) )
715                 return false;
716
717         do_action( 'revoke_super_admin', $user_id );
718
719         // Directly fetch site_admins instead of using get_super_admins()
720         $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
721
722         $user = new WP_User( $user_id );
723         if ( $user->user_email != get_site_option( 'admin_email' ) ) {
724                 if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
725                         unset( $super_admins[$key] );
726                         update_site_option( 'site_admins', $super_admins );
727                         do_action( 'revoked_super_admin', $user_id );
728                         return true;
729                 }
730         }
731         return false;
732 }
733
734 /**
735  * Whether or not we can edit this network from this page
736  *
737  * By default editing of network is restricted to the Network Admin for that site_id this allows for this to be overridden
738  *
739  * @since 3.1.0
740  * @param integer $site_id The network/site id to check.
741  */
742 function can_edit_network( $site_id ) {
743         global $wpdb;
744
745         if ($site_id == $wpdb->siteid )
746                 $result = true;
747         else
748                 $result = false;
749
750         return apply_filters( 'can_edit_network', $result, $site_id );
751 }
752
753 /**
754  * Thickbox image paths for Network Admin.
755  *
756  * @since 3.1.0
757  * @access private
758  */
759 function _thickbox_path_admin_subfolder() {
760 ?>
761 <script type="text/javascript">
762 //<![CDATA[
763 var tb_pathToImage = "../../wp-includes/js/thickbox/loadingAnimation.gif";
764 var tb_closeImage = "../../wp-includes/js/thickbox/tb-close.png";
765 //]]>
766 </script>
767 <?php
768 }
769
770 /**
771  * Whether or not we have a large network.
772  *
773  * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
774  * Plugins can alter this criteria  using the 'wp_is_large_network' filter.
775  *
776  * @since 3.3.0
777  * @param string $using 'sites or 'users'.  Default is 'sites'.
778  * @return bool True if the network meets the criteria for large. False otherwise.
779  */
780 function wp_is_large_network( $using = 'sites' ) {
781         if ( 'users' == $using ) {
782                 $count = get_user_count();
783                 return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count );
784         }
785
786         $count = get_blog_count();
787         return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
788 }
789 ?>