PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/ms.php

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