]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/ms.php
Wordpress 3.1.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;
57
58         $switch = false;
59         if ( $blog_id != $wpdb->blogid ) {
60                 $switch = true;
61                 switch_to_blog( $blog_id );
62         }
63
64         $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
65
66         do_action( 'delete_blog', $blog_id, $drop );
67
68         $users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
69
70         // Remove users from this blog.
71         if ( ! empty( $users ) ) {
72                 foreach ( $users as $user_id ) {
73                         remove_user_from_blog( $user_id, $blog_id) ;
74                 }
75         }
76
77         update_blog_status( $blog_id, 'deleted', 1 );
78
79         if ( $drop ) {
80                 if ( substr( $blog_prefix, -1 ) == '_' )
81                         $blog_prefix =  substr( $blog_prefix, 0, -1 ) . '\_';
82
83                 $drop_tables = $wpdb->get_results( "SHOW TABLES LIKE '{$blog_prefix}%'", ARRAY_A );
84                 $drop_tables = apply_filters( 'wpmu_drop_tables', $drop_tables );
85
86                 reset( $drop_tables );
87                 foreach ( (array) $drop_tables as $drop_table) {
88                         $wpdb->query( "DROP TABLE IF EXISTS ". current( $drop_table ) ."" );
89                 }
90                 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
91                 $dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id );
92                 $dir = rtrim( $dir, DIRECTORY_SEPARATOR );
93                 $top_dir = $dir;
94                 $stack = array($dir);
95                 $index = 0;
96
97                 while ( $index < count( $stack ) ) {
98                         # Get indexed directory from stack
99                         $dir = $stack[$index];
100
101                         $dh = @opendir( $dir );
102                         if ( $dh ) {
103                                 while ( ( $file = @readdir( $dh ) ) !== false ) {
104                                         if ( $file == '.' || $file == '..' )
105                                                 continue;
106
107                                         if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) )
108                                                 $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
109                                         else if ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) )
110                                                 @unlink( $dir . DIRECTORY_SEPARATOR . $file );
111                                 }
112                         }
113                         $index++;
114                 }
115
116                 $stack = array_reverse( $stack );  // Last added dirs are deepest
117                 foreach( (array) $stack as $dir ) {
118                         if ( $dir != $top_dir)
119                         @rmdir( $dir );
120                 }
121         }
122
123         $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = '{$blog_prefix}autosave_draft_ids'" );
124         $blogs = get_site_option( 'blog_list' );
125         if ( is_array( $blogs ) ) {
126                 foreach ( $blogs as $n => $blog ) {
127                         if ( $blog['blog_id'] == $blog_id )
128                                 unset( $blogs[$n] );
129                 }
130                 update_site_option( 'blog_list', $blogs );
131         }
132
133         if ( $switch === true )
134                 restore_current_blog();
135 }
136
137 // @todo Merge with wp_delete_user() ?
138 function wpmu_delete_user( $id ) {
139         global $wpdb;
140
141         $id = (int) $id;
142
143         do_action( 'wpmu_delete_user', $id );
144
145         $blogs = get_blogs_of_user( $id );
146
147         if ( ! empty( $blogs ) ) {
148                 foreach ( $blogs as $blog ) {
149                         switch_to_blog( $blog->userblog_id );
150                         remove_user_from_blog( $id, $blog->userblog_id );
151
152                         $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
153                         foreach ( (array) $post_ids as $post_id ) {
154                                 wp_delete_post( $post_id );
155                         }
156
157                         // Clean links
158                         $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
159
160                         if ( $link_ids ) {
161                                 foreach ( $link_ids as $link_id )
162                                         wp_delete_link( $link_id );
163                         }
164
165                         restore_current_blog();
166                 }
167         }
168
169         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->users WHERE ID = %d", $id ) );
170         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
171
172         clean_user_cache( $id );
173
174         // allow for commit transaction
175         do_action( 'deleted_user', $id );
176
177         return true;
178 }
179
180 function wpmu_get_blog_allowedthemes( $blog_id = 0 ) {
181         $themes = get_themes();
182
183         if ( $blog_id != 0 )
184                 switch_to_blog( $blog_id );
185
186         $blog_allowed_themes = get_option( 'allowedthemes' );
187         if ( !is_array( $blog_allowed_themes ) || empty( $blog_allowed_themes ) ) { // convert old allowed_themes to new allowedthemes
188                 $blog_allowed_themes = get_option( 'allowed_themes' );
189
190                 if ( is_array( $blog_allowed_themes ) ) {
191                         foreach( (array) $themes as $key => $theme ) {
192                                 $theme_key = esc_html( $theme['Stylesheet'] );
193                                 if ( isset( $blog_allowed_themes[$key] ) == true ) {
194                                         $blog_allowedthemes[$theme_key] = 1;
195                                 }
196                         }
197                         $blog_allowed_themes = $blog_allowedthemes;
198                         add_option( 'allowedthemes', $blog_allowed_themes );
199                         delete_option( 'allowed_themes' );
200                 }
201         }
202
203         if ( $blog_id != 0 )
204                 restore_current_blog();
205
206         return $blog_allowed_themes;
207 }
208
209 function update_option_new_admin_email( $old_value, $value ) {
210         $email = get_option( 'admin_email' );
211         if ( $value == get_option( 'admin_email' ) || !is_email( $value ) )
212                 return;
213
214         $hash = md5( $value. time() .mt_rand() );
215         $new_admin_email = array(
216                 'hash' => $hash,
217                 'newemail' => $value
218         );
219         update_option( 'adminhash', $new_admin_email );
220
221         $content = apply_filters( 'new_admin_email_content', __( "Dear user,
222
223 You recently requested to have the administration email address on
224 your site changed.
225 If this is correct, please click on the following link to change it:
226 ###ADMIN_URL###
227
228 You can safely ignore and delete this email if you do not want to
229 take this action.
230
231 This email has been sent to ###EMAIL###
232
233 Regards,
234 All at ###SITENAME###
235 ###SITEURL### "), $new_admin_email );
236
237         $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'options.php?adminhash='.$hash ) ), $content );
238         $content = str_replace( '###EMAIL###', $value, $content );
239         $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
240         $content = str_replace( '###SITEURL###', network_home_url(), $content );
241
242         wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), get_option( 'blogname' ) ), $content );
243 }
244 add_action( 'update_option_new_admin_email', 'update_option_new_admin_email', 10, 2 );
245 add_action( 'add_option_new_admin_email', 'update_option_new_admin_email', 10, 2 );
246
247 function send_confirmation_on_profile_email() {
248         global $errors, $wpdb;
249         $current_user = wp_get_current_user();
250         if ( ! is_object($errors) )
251                 $errors = new WP_Error();
252
253         if ( $current_user->id != $_POST['user_id'] )
254                 return false;
255
256         if ( $current_user->user_email != $_POST['email'] ) {
257                 if ( !is_email( $_POST['email'] ) ) {
258                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
259                         return;
260                 }
261
262                 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
263                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
264                         delete_option( $current_user->ID . '_new_email' );
265                         return;
266                 }
267
268                 $hash = md5( $_POST['email'] . time() . mt_rand() );
269                 $new_user_email = array(
270                                 'hash' => $hash,
271                                 'newemail' => $_POST['email']
272                                 );
273                 update_option( $current_user->ID . '_new_email', $new_user_email );
274
275                 $content = apply_filters( 'new_user_email_content', __( "Dear user,
276
277 You recently requested to have the email address on your account changed.
278 If this is correct, please click on the following link to change it:
279 ###ADMIN_URL###
280
281 You can safely ignore and delete this email if you do not want to
282 take this action.
283
284 This email has been sent to ###EMAIL###
285
286 Regards,
287 All at ###SITENAME###
288 ###SITEURL###" ), $new_user_email );
289
290                 $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
291                 $content = str_replace( '###EMAIL###', $_POST['email'], $content);
292                 $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
293                 $content = str_replace( '###SITEURL###', network_home_url(), $content );
294
295                 wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), get_option( 'blogname' ) ), $content );
296                 $_POST['email'] = $current_user->user_email;
297         }
298 }
299 add_action( 'personal_options_update', 'send_confirmation_on_profile_email' );
300
301 function new_user_email_admin_notice() {
302         if ( strpos( $_SERVER['PHP_SELF'], 'profile.php' ) && isset( $_GET['updated'] ) && $email = get_option( get_current_user_id() . '_new_email' ) )
303                 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>";
304 }
305 add_action( 'admin_notices', 'new_user_email_admin_notice' );
306
307 function get_site_allowed_themes() {
308         $themes = get_themes();
309         $allowed_themes = get_site_option( 'allowedthemes' );
310         if ( !is_array( $allowed_themes ) || empty( $allowed_themes ) ) {
311                 $allowed_themes = get_site_option( 'allowed_themes' ); // convert old allowed_themes format
312                 if ( !is_array( $allowed_themes ) ) {
313                         $allowed_themes = array();
314                 } else {
315                         foreach( (array) $themes as $key => $theme ) {
316                                 $theme_key = esc_html( $theme['Stylesheet'] );
317                                 if ( isset( $allowed_themes[ $key ] ) == true ) {
318                                         $allowedthemes[ $theme_key ] = 1;
319                                 }
320                         }
321                         $allowed_themes = $allowedthemes;
322                 }
323         }
324         return $allowed_themes;
325 }
326
327 /**
328  * Determines if there is any upload space left in the current blog's quota.
329  *
330  * @since 3.0.0
331  * @return bool True if space is available, false otherwise.
332  */
333 function is_upload_space_available() {
334         if ( get_site_option( 'upload_space_check_disabled' ) )
335                 return true;
336
337         if ( !( $space_allowed = get_upload_space_available() ) )
338                 return false;
339
340         return true;
341 }
342
343 /**
344  * @since 3.0.0
345  *
346  * @return int of upload size limit in bytes
347  */
348 function upload_size_limit_filter( $size ) {
349         $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
350         if ( get_site_option( 'upload_space_check_disabled' ) )
351                 return min( $size, $fileupload_maxk );
352
353         return min( $size, $fileupload_maxk, get_upload_space_available() );
354 }
355 /**
356  * Determines if there is any upload space left in the current blog's quota.
357  *
358  * @return int of upload space available in bytes
359  */
360 function get_upload_space_available() {
361         $space_allowed = get_space_allowed() * 1024 * 1024;
362         if ( get_site_option( 'upload_space_check_disabled' ) )
363                 return $space_allowed;
364
365         $dir_name = trailingslashit( BLOGUPLOADDIR );
366         if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) )
367                 return $space_allowed;
368
369         $dir = dir( $dir_name );
370         $size = 0;
371
372         while ( $file = $dir->read() ) {
373                 if ( $file != '.' && $file != '..' ) {
374                         if ( is_dir( $dir_name . $file) ) {
375                                 $size += get_dirsize( $dir_name . $file );
376                         } else {
377                                 $size += filesize( $dir_name . $file );
378                         }
379                 }
380         }
381         $dir->close();
382
383         if ( ( $space_allowed - $size ) <= 0 )
384                 return 0;
385
386         return $space_allowed - $size;
387 }
388
389 /**
390  * Returns the upload quota for the current blog.
391  *
392  * @return int Quota
393  */
394 function get_space_allowed() {
395         $space_allowed = get_option( 'blog_upload_space' );
396         if ( $space_allowed == false )
397                 $space_allowed = get_site_option( 'blog_upload_space' );
398         if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
399                 $space_allowed = 50;
400
401         return $space_allowed;
402 }
403
404 function display_space_usage() {
405         $space = get_space_allowed();
406         $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
407
408         $percentused = ( $used / $space ) * 100;
409
410         if ( $space > 1000 ) {
411                 $space = number_format( $space / 1024 );
412                 /* translators: Gigabytes */
413                 $space .= __( 'GB' );
414         } else {
415                 /* translators: Megabytes */
416                 $space .= __( 'MB' );
417         }
418         ?>
419         <strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percentused ), $space ); ?></strong>
420         <?php
421 }
422
423 // Edit blog upload space setting on Edit Blog page
424 function upload_space_setting( $id ) {
425         $quota = get_blog_option( $id, 'blog_upload_space' );
426         if ( !$quota )
427                 $quota = '';
428
429         ?>
430         <tr>
431                 <th><?php _e( 'Site Upload Space Quota '); ?></th>
432                 <td><input type="text" size="3" name="option[blog_upload_space]" value="<?php echo $quota; ?>" /> <?php _e( 'MB (Leave blank for network default)' ); ?></td>
433         </tr>
434         <?php
435 }
436 add_action( 'wpmueditblogaction', 'upload_space_setting' );
437
438 function update_user_status( $id, $pref, $value, $deprecated = null ) {
439         global $wpdb;
440
441         if ( null !== $deprecated  )
442                 _deprecated_argument( __FUNCTION__, '3.1' );
443
444         $wpdb->update( $wpdb->users, array( $pref => $value ), array( 'ID' => $id ) );
445
446         clean_user_cache( $id );
447
448         if ( $pref == 'spam' ) {
449                 if ( $value == 1 )
450                         do_action( 'make_spam_user', $id );
451                 else
452                         do_action( 'make_ham_user', $id );
453         }
454
455         return $value;
456 }
457
458 function refresh_user_details( $id ) {
459         $id = (int) $id;
460
461         if ( !$user = get_userdata( $id ) )
462                 return false;
463
464         clean_user_cache( $id );
465
466         return $id;
467 }
468
469 function format_code_lang( $code = '' ) {
470         $code = strtolower( substr( $code, 0, 2 ) );
471         $lang_codes = array(
472                 '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',
473                 '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',
474                 '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',
475                 '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',
476                 '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',
477                 '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',
478                 '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',
479                 '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',
480                 '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',
481                 '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',
482                 've' => 'Venda', 'vi' => 'Vietnamese', 'vo' => 'Volapük', 'cy' => 'Welsh','wa' => 'Walloon','wo' => 'Wolof', 'xh' => 'Xhosa', 'yi' => 'Yiddish', 'yo' => 'Yoruba', 'za' => 'Zhuang; Chuang', 'zu' => 'Zulu' );
483         $lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
484         return strtr( $code, $lang_codes );
485 }
486
487 function sync_category_tag_slugs( $term, $taxonomy ) {
488         if ( global_terms_enabled() && ( $taxonomy == 'category' || $taxonomy == 'post_tag' ) ) {
489                 if ( is_object( $term ) ) {
490                         $term->slug = sanitize_title( $term->name );
491                 } else {
492                         $term['slug'] = sanitize_title( $term['name'] );
493                 }
494         }
495         return $term;
496 }
497 add_filter( 'get_term', 'sync_category_tag_slugs', 10, 2 );
498
499 function redirect_user_to_blog() {
500         $c = 0;
501         if ( isset( $_GET['c'] ) )
502                 $c = (int) $_GET['c'];
503
504         if ( $c >= 5 ) {
505                 wp_die( __( "You don&#8217;t have permission to view this site. Please contact the system administrator." ) );
506         }
507         $c ++;
508
509         $blog = get_active_blog_for_user( get_current_user_id() );
510
511         if ( is_object( $blog ) ) {
512                 wp_redirect( get_admin_url( $blog->blog_id, '?c=' . $c ) ); // redirect and count to 5, "just in case"
513         } else {
514                 wp_redirect( user_admin_url( '?c=' . $c ) ); // redirect and count to 5, "just in case"
515         }
516         exit;
517 }
518 add_action( 'admin_page_access_denied', 'redirect_user_to_blog', 99 );
519
520 function check_import_new_users( $permission ) {
521         if ( !is_super_admin() )
522                 return false;
523         return true;
524 }
525 add_filter( 'import_allow_create_users', 'check_import_new_users' );
526 // See "import_allow_fetch_attachments" and "import_attachment_size_limit" filters too.
527
528 function mu_dropdown_languages( $lang_files = array(), $current = '' ) {
529         $flag = false;
530         $output = array();
531
532         foreach ( (array) $lang_files as $val ) {
533                 $code_lang = basename( $val, '.mo' );
534
535                 if ( $code_lang == 'en_US' ) { // American English
536                         $flag = true;
537                         $ae = __( 'American English' );
538                         $output[$ae] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $ae . '</option>';
539                 } elseif ( $code_lang == 'en_GB' ) { // British English
540                         $flag = true;
541                         $be = __( 'British English' );
542                         $output[$be] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $be . '</option>';
543                 } else {
544                         $translated = format_code_lang( $code_lang );
545                         $output[$translated] =  '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . esc_html ( $translated ) . '</option>';
546                 }
547
548         }
549
550         if ( $flag === false ) // WordPress english
551                 $output[] = '<option value=""' . selected( $current, '', false ) . '>' . __( 'English' ) . "</option>";
552
553         // Order by name
554         uksort( $output, 'strnatcasecmp' );
555
556         $output = apply_filters( 'mu_dropdown_languages', $output, $lang_files, $current );
557         echo implode( "\n\t", $output );
558 }
559
560 /* Warn the admin if SECRET SALT information is missing from wp-config.php */
561 function secret_salt_warning() {
562         if ( !is_super_admin() )
563                 return;
564         $secret_keys = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT' );
565         $out = '';
566         foreach( $secret_keys as $key ) {
567                 if ( ! defined( $key ) )
568                         $out .= "define( '$key', '" . esc_html( wp_generate_password( 64, true, true ) ) . "' );<br />";
569         }
570         if ( $out != '' ) {
571                 $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.' );
572                 $msg .= '<br/>' . __( "Before the line <code>/* That's all, stop editing! Happy blogging. */</code> please add this code:" );
573                 $msg .= "<br/><br/><code>$out</code>";
574
575                 echo "<div class='update-nag'>$msg</div>";
576         }
577 }
578 add_action( 'network_admin_notices', 'secret_salt_warning' );
579
580 function site_admin_notice() {
581         global $wp_db_version;
582         if ( !is_super_admin() )
583                 return false;
584         if ( get_site_option( 'wpmu_upgrade_site' ) != $wp_db_version )
585                 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>";
586 }
587 add_action( 'admin_notices', 'site_admin_notice' );
588 add_action( 'network_admin_notices', 'site_admin_notice' );
589
590 function avoid_blog_page_permalink_collision( $data, $postarr ) {
591         if ( is_subdomain_install() )
592                 return $data;
593         if ( $data['post_type'] != 'page' )
594                 return $data;
595         if ( !isset( $data['post_name'] ) || $data['post_name'] == '' )
596                 return $data;
597         if ( !is_main_site() )
598                 return $data;
599
600         $post_name = $data['post_name'];
601         $c = 0;
602         while( $c < 10 && get_id_from_blogname( $post_name ) ) {
603                 $post_name .= mt_rand( 1, 10 );
604                 $c ++;
605         }
606         if ( $post_name != $data['post_name'] ) {
607                 $data['post_name'] = $post_name;
608         }
609         return $data;
610 }
611 add_filter( 'wp_insert_post_data', 'avoid_blog_page_permalink_collision', 10, 2 );
612
613 function choose_primary_blog() {
614         ?>
615         <table class="form-table">
616         <tr>
617         <?php /* translators: My sites label */ ?>
618                 <th scope="row"><?php _e( 'Primary Site' ); ?></th>
619                 <td>
620                 <?php
621                 $all_blogs = get_blogs_of_user( get_current_user_id() );
622                 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
623                 if ( count( $all_blogs ) > 1 ) {
624                         $found = false;
625                         ?>
626                         <select name="primary_blog">
627                                 <?php foreach( (array) $all_blogs as $blog ) {
628                                         if ( $primary_blog == $blog->userblog_id )
629                                                 $found = true;
630                                         ?><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
631                                 } ?>
632                         </select>
633                         <?php
634                         if ( !$found ) {
635                                 $blog = array_shift( $all_blogs );
636                                 update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
637                         }
638                 } elseif ( count( $all_blogs ) == 1 ) {
639                         $blog = array_shift( $all_blogs );
640                         echo $blog->domain;
641                         if ( $primary_blog != $blog->userblog_id ) // Set the primary blog again if it's out of sync with blog list.
642                                 update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
643                 } else {
644                         echo "N/A";
645                 }
646                 ?>
647                 </td>
648         </tr>
649         <?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
650                 <tr>
651                         <th scope="row" colspan="2" class="th-full">
652                                 <a href="<?php echo apply_filters( 'wp_signup_location', network_home_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
653                         </th>
654                 </tr>
655         <?php endif; ?>
656         </table>
657         <?php
658 }
659
660 function ms_deprecated_blogs_file() {
661         if ( ! is_super_admin() )
662                 return;
663         if ( ! file_exists( WP_CONTENT_DIR . '/blogs.php' ) )
664                 return;
665         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>';
666 }
667 add_action( 'network_admin_notices', 'ms_deprecated_blogs_file' );
668
669 /**
670  * Grants super admin privileges.
671  *
672  * @since 3.0.0
673  * @param int $user_id
674  */
675 function grant_super_admin( $user_id ) {
676         global $super_admins;
677
678         // If global super_admins override is defined, there is nothing to do here.
679         if ( isset($super_admins) )
680                 return false;
681
682         do_action( 'grant_super_admin', $user_id );
683
684         // Directly fetch site_admins instead of using get_super_admins()
685         $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
686
687         $user = new WP_User( $user_id );
688         if ( ! in_array( $user->user_login, $super_admins ) ) {
689                 $super_admins[] = $user->user_login;
690                 update_site_option( 'site_admins' , $super_admins );
691                 do_action( 'granted_super_admin', $user_id );
692                 return true;
693         }
694         return false;
695 }
696
697 /**
698  * Revokes super admin privileges.
699  *
700  * @since 3.0.0
701  * @param int $user_id
702  */
703 function revoke_super_admin( $user_id ) {
704         global $super_admins;
705
706         // If global super_admins override is defined, there is nothing to do here.
707         if ( isset($super_admins) )
708                 return false;
709
710         do_action( 'revoke_super_admin', $user_id );
711
712         // Directly fetch site_admins instead of using get_super_admins()
713         $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
714
715         $user = new WP_User( $user_id );
716         if ( $user->user_email != get_site_option( 'admin_email' ) ) {
717                 if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
718                         unset( $super_admins[$key] );
719                         update_site_option( 'site_admins', $super_admins );
720                         do_action( 'revoked_super_admin', $user_id );
721                         return true;
722                 }
723         }
724         return false;
725 }
726
727 /**
728  * Whether or not we can edit this network from this page
729  *
730  * By default editing of network is restricted to the Network Admin for that site_id this allows for this to be overridden
731  *
732  * @since 3.1.0
733  * @param integer $site_id The network/site id to check.
734  */
735 function can_edit_network( $site_id ) {
736         global $wpdb;
737
738         if ($site_id == $wpdb->siteid )
739                 $result = true;
740         else
741                 $result = false;
742
743         return apply_filters( 'can_edit_network', $result, $site_id );
744 }
745
746 /**
747  * Thickbox image paths for Network Admin.
748  *
749  * @since 3.1.0
750  * @access private
751  */
752 function _thickbox_path_admin_subfolder() {
753 ?>
754 <script type="text/javascript">
755 //<![CDATA[
756 var tb_pathToImage = "../../wp-includes/js/thickbox/loadingAnimation.gif";
757 var tb_closeImage = "../../wp-includes/js/thickbox/tb-close.png";
758 //]]>
759 </script>
760 <?php
761 }
762
763 ?>