PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/ms.php

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