PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/ms.php

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 714 lines | 485 code | 123 blank | 106 comment | 125 complexity | c1368b420131c2571d61429e13ed233b MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  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. * Check whether a blog has used its allotted upload space.
  217. *
  218. * @since MU
  219. *
  220. * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
  221. * @return int
  222. */
  223. function upload_is_user_over_quota( $echo = true ) {
  224. if ( get_site_option( 'upload_space_check_disabled' ) )
  225. return false;
  226. $space_allowed = get_space_allowed();
  227. if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
  228. $space_allowed = 10; // Default space allowed is 10 MB
  229. $space_used = get_space_used();
  230. if ( ( $space_allowed - $space_used ) < 0 ) {
  231. if ( $echo )
  232. _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' );
  233. return true;
  234. } else {
  235. return false;
  236. }
  237. }
  238. /**
  239. * Displays the amount of disk space used by the current blog. Not used in core.
  240. *
  241. * @since MU
  242. */
  243. function display_space_usage() {
  244. $space_allowed = get_space_allowed();
  245. $space_used = get_space_used();
  246. $percent_used = ( $space_used / $space_allowed ) * 100;
  247. if ( $space_allowed > 1000 ) {
  248. $space = number_format( $space_allowed / 1024 );
  249. /* translators: Gigabytes */
  250. $space .= __( 'GB' );
  251. } else {
  252. $space = number_format( $space_allowed );
  253. /* translators: Megabytes */
  254. $space .= __( 'MB' );
  255. }
  256. ?>
  257. <strong><?php printf( __( 'Used: %1$s%% of %2$s' ), number_format( $percent_used ), $space ); ?></strong>
  258. <?php
  259. }
  260. /**
  261. * Get the remaining upload space for this blog.
  262. *
  263. * @since MU
  264. * @uses upload_is_user_over_quota()
  265. * @uses get_space_allowed()
  266. * @uses get_upload_space_available()
  267. *
  268. * @param int $size Current max size in bytes
  269. * @return int Max size in bytes
  270. */
  271. function fix_import_form_size( $size ) {
  272. if ( upload_is_user_over_quota( false ) == true )
  273. return 0;
  274. $available = get_upload_space_available();
  275. return min( $size, $available );
  276. }
  277. // Edit blog upload space setting on Edit Blog page
  278. function upload_space_setting( $id ) {
  279. switch_to_blog( $id );
  280. $quota = get_option( 'blog_upload_space' );
  281. restore_current_blog();
  282. if ( !$quota )
  283. $quota = '';
  284. ?>
  285. <tr>
  286. <th><?php _e( 'Site Upload Space Quota '); ?></th>
  287. <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>
  288. </tr>
  289. <?php
  290. }
  291. add_action( 'wpmueditblogaction', 'upload_space_setting' );
  292. function update_user_status( $id, $pref, $value, $deprecated = null ) {
  293. global $wpdb;
  294. if ( null !== $deprecated )
  295. _deprecated_argument( __FUNCTION__, '3.1' );
  296. $wpdb->update( $wpdb->users, array( $pref => $value ), array( 'ID' => $id ) );
  297. $user = new WP_User( $id );
  298. clean_user_cache( $user );
  299. if ( $pref == 'spam' ) {
  300. if ( $value == 1 )
  301. do_action( 'make_spam_user', $id );
  302. else
  303. do_action( 'make_ham_user', $id );
  304. }
  305. return $value;
  306. }
  307. function refresh_user_details( $id ) {
  308. $id = (int) $id;
  309. if ( !$user = get_userdata( $id ) )
  310. return false;
  311. clean_user_cache( $user );
  312. return $id;
  313. }
  314. function format_code_lang( $code = '' ) {
  315. $code = strtolower( substr( $code, 0, 2 ) );
  316. $lang_codes = array(
  317. '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',
  318. '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',
  319. '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',
  320. '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',
  321. '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',
  322. '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',
  323. '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',
  324. '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',
  325. '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',
  326. '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',
  327. 've' => 'Venda', 'vi' => 'Vietnamese', 'vo' => 'Volapük', 'cy' => 'Welsh','wa' => 'Walloon','wo' => 'Wolof', 'xh' => 'Xhosa', 'yi' => 'Yiddish', 'yo' => 'Yoruba', 'za' => 'Zhuang; Chuang', 'zu' => 'Zulu' );
  328. $lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
  329. return strtr( $code, $lang_codes );
  330. }
  331. function sync_category_tag_slugs( $term, $taxonomy ) {
  332. if ( global_terms_enabled() && ( $taxonomy == 'category' || $taxonomy == 'post_tag' ) ) {
  333. if ( is_object( $term ) ) {
  334. $term->slug = sanitize_title( $term->name );
  335. } else {
  336. $term['slug'] = sanitize_title( $term['name'] );
  337. }
  338. }
  339. return $term;
  340. }
  341. add_filter( 'get_term', 'sync_category_tag_slugs', 10, 2 );
  342. function _access_denied_splash() {
  343. if ( ! is_user_logged_in() || is_network_admin() )
  344. return;
  345. $blogs = get_blogs_of_user( get_current_user_id() );
  346. if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) )
  347. return;
  348. $blog_name = get_bloginfo( 'name' );
  349. if ( empty( $blogs ) )
  350. 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 ) );
  351. $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>';
  352. $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>';
  353. $output .= '<h3>' . __('Your Sites') . '</h3>';
  354. $output .= '<table>';
  355. foreach ( $blogs as $blog ) {
  356. $output .= "<tr>";
  357. $output .= "<td valign='top'>";
  358. $output .= "{$blog->blogname}";
  359. $output .= "</td>";
  360. $output .= "<td valign='top'>";
  361. $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>" ;
  362. $output .= "</td>";
  363. $output .= "</tr>";
  364. }
  365. $output .= '</table>';
  366. wp_die( $output );
  367. }
  368. add_action( 'admin_page_access_denied', '_access_denied_splash', 99 );
  369. function check_import_new_users( $permission ) {
  370. if ( !is_super_admin() )
  371. return false;
  372. return true;
  373. }
  374. add_filter( 'import_allow_create_users', 'check_import_new_users' );
  375. // See "import_allow_fetch_attachments" and "import_attachment_size_limit" filters too.
  376. function mu_dropdown_languages( $lang_files = array(), $current = '' ) {
  377. $flag = false;
  378. $output = array();
  379. foreach ( (array) $lang_files as $val ) {
  380. $code_lang = basename( $val, '.mo' );
  381. if ( $code_lang == 'en_US' ) { // American English
  382. $flag = true;
  383. $ae = __( 'American English' );
  384. $output[$ae] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $ae . '</option>';
  385. } elseif ( $code_lang == 'en_GB' ) { // British English
  386. $flag = true;
  387. $be = __( 'British English' );
  388. $output[$be] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $be . '</option>';
  389. } else {
  390. $translated = format_code_lang( $code_lang );
  391. $output[$translated] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . esc_html ( $translated ) . '</option>';
  392. }
  393. }
  394. if ( $flag === false ) // WordPress english
  395. $output[] = '<option value=""' . selected( $current, '', false ) . '>' . __( 'English' ) . "</option>";
  396. // Order by name
  397. uksort( $output, 'strnatcasecmp' );
  398. $output = apply_filters( 'mu_dropdown_languages', $output, $lang_files, $current );
  399. echo implode( "\n\t", $output );
  400. }
  401. /* Warn the admin if SECRET SALT information is missing from wp-config.php */
  402. function secret_salt_warning() {
  403. if ( !is_super_admin() )
  404. return;
  405. $secret_keys = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT' );
  406. $out = '';
  407. foreach( $secret_keys as $key ) {
  408. if ( ! defined( $key ) )
  409. $out .= "define( '$key', '" . esc_html( wp_generate_password( 64, true, true ) ) . "' );<br />";
  410. }
  411. if ( $out != '' ) {
  412. $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.' );
  413. $msg .= '<br/>' . __( "Before the line <code>/* That's all, stop editing! Happy blogging. */</code> please add this code:" );
  414. $msg .= "<br/><br/><code>$out</code>";
  415. echo "<div class='update-nag'>$msg</div>";
  416. }
  417. }
  418. add_action( 'network_admin_notices', 'secret_salt_warning' );
  419. function site_admin_notice() {
  420. global $wp_db_version;
  421. if ( !is_super_admin() )
  422. return false;
  423. if ( get_site_option( 'wpmu_upgrade_site' ) != $wp_db_version )
  424. 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>";
  425. }
  426. add_action( 'admin_notices', 'site_admin_notice' );
  427. add_action( 'network_admin_notices', 'site_admin_notice' );
  428. function avoid_blog_page_permalink_collision( $data, $postarr ) {
  429. if ( is_subdomain_install() )
  430. return $data;
  431. if ( $data['post_type'] != 'page' )
  432. return $data;
  433. if ( !isset( $data['post_name'] ) || $data['post_name'] == '' )
  434. return $data;
  435. if ( !is_main_site() )
  436. return $data;
  437. $post_name = $data['post_name'];
  438. $c = 0;
  439. while( $c < 10 && get_id_from_blogname( $post_name ) ) {
  440. $post_name .= mt_rand( 1, 10 );
  441. $c ++;
  442. }
  443. if ( $post_name != $data['post_name'] ) {
  444. $data['post_name'] = $post_name;
  445. }
  446. return $data;
  447. }
  448. add_filter( 'wp_insert_post_data', 'avoid_blog_page_permalink_collision', 10, 2 );
  449. function choose_primary_blog() {
  450. ?>
  451. <table class="form-table">
  452. <tr>
  453. <?php /* translators: My sites label */ ?>
  454. <th scope="row"><?php _e( 'Primary Site' ); ?></th>
  455. <td>
  456. <?php
  457. $all_blogs = get_blogs_of_user( get_current_user_id() );
  458. $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
  459. if ( count( $all_blogs ) > 1 ) {
  460. $found = false;
  461. ?>
  462. <select name="primary_blog">
  463. <?php foreach( (array) $all_blogs as $blog ) {
  464. if ( $primary_blog == $blog->userblog_id )
  465. $found = true;
  466. ?><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
  467. } ?>
  468. </select>
  469. <?php
  470. if ( !$found ) {
  471. $blog = array_shift( $all_blogs );
  472. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  473. }
  474. } elseif ( count( $all_blogs ) == 1 ) {
  475. $blog = array_shift( $all_blogs );
  476. echo $blog->domain;
  477. if ( $primary_blog != $blog->userblog_id ) // Set the primary blog again if it's out of sync with blog list.
  478. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  479. } else {
  480. echo "N/A";
  481. }
  482. ?>
  483. </td>
  484. </tr>
  485. <?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
  486. <tr>
  487. <th scope="row" colspan="2" class="th-full">
  488. <a href="<?php echo apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
  489. </th>
  490. </tr>
  491. <?php endif; ?>
  492. </table>
  493. <?php
  494. }
  495. /**
  496. * Grants super admin privileges.
  497. *
  498. * @since 3.0.0
  499. * @param int $user_id
  500. */
  501. function grant_super_admin( $user_id ) {
  502. global $super_admins;
  503. // If global super_admins override is defined, there is nothing to do here.
  504. if ( isset($super_admins) )
  505. return false;
  506. do_action( 'grant_super_admin', $user_id );
  507. // Directly fetch site_admins instead of using get_super_admins()
  508. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  509. $user = get_userdata( $user_id );
  510. if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
  511. $super_admins[] = $user->user_login;
  512. update_site_option( 'site_admins' , $super_admins );
  513. do_action( 'granted_super_admin', $user_id );
  514. return true;
  515. }
  516. return false;
  517. }
  518. /**
  519. * Revokes super admin privileges.
  520. *
  521. * @since 3.0.0
  522. * @param int $user_id
  523. */
  524. function revoke_super_admin( $user_id ) {
  525. global $super_admins;
  526. // If global super_admins override is defined, there is nothing to do here.
  527. if ( isset($super_admins) )
  528. return false;
  529. do_action( 'revoke_super_admin', $user_id );
  530. // Directly fetch site_admins instead of using get_super_admins()
  531. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  532. $user = get_userdata( $user_id );
  533. if ( $user && $user->user_email != get_site_option( 'admin_email' ) ) {
  534. if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
  535. unset( $super_admins[$key] );
  536. update_site_option( 'site_admins', $super_admins );
  537. do_action( 'revoked_super_admin', $user_id );
  538. return true;
  539. }
  540. }
  541. return false;
  542. }
  543. /**
  544. * Whether or not we can edit this network from this page
  545. *
  546. * By default editing of network is restricted to the Network Admin for that site_id this allows for this to be overridden
  547. *
  548. * @since 3.1.0
  549. * @param integer $site_id The network/site id to check.
  550. */
  551. function can_edit_network( $site_id ) {
  552. global $wpdb;
  553. if ($site_id == $wpdb->siteid )
  554. $result = true;
  555. else
  556. $result = false;
  557. return apply_filters( 'can_edit_network', $result, $site_id );
  558. }
  559. /**
  560. * Thickbox image paths for Network Admin.
  561. *
  562. * @since 3.1.0
  563. * @access private
  564. */
  565. function _thickbox_path_admin_subfolder() {
  566. ?>
  567. <script type="text/javascript">
  568. //<![CDATA[
  569. var tb_pathToImage = "../../wp-includes/js/thickbox/loadingAnimation.gif";
  570. //]]>
  571. </script>
  572. <?php
  573. }
  574. /**
  575. * Whether or not we have a large network.
  576. *
  577. * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
  578. * Plugins can alter this criteria using the 'wp_is_large_network' filter.
  579. *
  580. * @since 3.3.0
  581. * @param string $using 'sites or 'users'. Default is 'sites'.
  582. * @return bool True if the network meets the criteria for large. False otherwise.
  583. */
  584. function wp_is_large_network( $using = 'sites' ) {
  585. if ( 'users' == $using ) {
  586. $count = get_user_count();
  587. return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count );
  588. }
  589. $count = get_blog_count();
  590. return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
  591. }