PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/ms.php

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