PageRenderTime 70ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/ms.php

http://github.com/markjaquith/WordPress
PHP | 1158 lines | 754 code | 115 blank | 289 comment | 106 complexity | 9baa459da19e0274e5dd6379269f7c20 MD5 | raw file
Possible License(s): 0BSD
  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. }
  21. if ( '0' != $file['error'] ) { // There's already an error.
  22. return $file;
  23. }
  24. if ( defined( 'WP_IMPORTING' ) ) {
  25. return $file;
  26. }
  27. $space_left = get_upload_space_available();
  28. $file_size = filesize( $file['tmp_name'] );
  29. if ( $space_left < $file_size ) {
  30. /* translators: %s: Required disk space in kilobytes. */
  31. $file['error'] = sprintf( __( 'Not enough space to upload. %s KB needed.' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) );
  32. }
  33. if ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
  34. /* translators: %s: Maximum allowed file size in kilobytes. */
  35. $file['error'] = sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) );
  36. }
  37. if ( upload_is_user_over_quota( false ) ) {
  38. $file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
  39. }
  40. if ( '0' != $file['error'] && ! isset( $_POST['html-upload'] ) && ! wp_doing_ajax() ) {
  41. wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
  42. }
  43. return $file;
  44. }
  45. /**
  46. * Delete a site.
  47. *
  48. * @since 3.0.0
  49. * @since 5.1.0 Use wp_delete_site() internally to delete the site row from the database.
  50. *
  51. * @global wpdb $wpdb WordPress database abstraction object.
  52. *
  53. * @param int $blog_id Site ID.
  54. * @param bool $drop True if site's database tables should be dropped. Default is false.
  55. */
  56. function wpmu_delete_blog( $blog_id, $drop = false ) {
  57. global $wpdb;
  58. $switch = false;
  59. if ( get_current_blog_id() != $blog_id ) {
  60. $switch = true;
  61. switch_to_blog( $blog_id );
  62. }
  63. $blog = get_site( $blog_id );
  64. $current_network = get_network();
  65. // If a full blog object is not available, do not destroy anything.
  66. if ( $drop && ! $blog ) {
  67. $drop = false;
  68. }
  69. // Don't destroy the initial, main, or root blog.
  70. if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_network->path && $blog->domain == $current_network->domain ) ) ) {
  71. $drop = false;
  72. }
  73. $upload_path = trim( get_option( 'upload_path' ) );
  74. // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
  75. if ( $drop && get_site_option( 'ms_files_rewriting' ) && empty( $upload_path ) ) {
  76. $drop = false;
  77. }
  78. if ( $drop ) {
  79. wp_delete_site( $blog_id );
  80. } else {
  81. /** This action is documented in wp-includes/ms-blogs.php */
  82. do_action_deprecated( 'delete_blog', array( $blog_id, false ), '5.1.0' );
  83. $users = get_users(
  84. array(
  85. 'blog_id' => $blog_id,
  86. 'fields' => 'ids',
  87. )
  88. );
  89. // Remove users from this blog.
  90. if ( ! empty( $users ) ) {
  91. foreach ( $users as $user_id ) {
  92. remove_user_from_blog( $user_id, $blog_id );
  93. }
  94. }
  95. update_blog_status( $blog_id, 'deleted', 1 );
  96. /** This action is documented in wp-includes/ms-blogs.php */
  97. do_action_deprecated( 'deleted_blog', array( $blog_id, false ), '5.1.0' );
  98. }
  99. if ( $switch ) {
  100. restore_current_blog();
  101. }
  102. }
  103. /**
  104. * Delete a user from the network and remove from all sites.
  105. *
  106. * @since 3.0.0
  107. *
  108. * @todo Merge with wp_delete_user()?
  109. *
  110. * @global wpdb $wpdb WordPress database abstraction object.
  111. *
  112. * @param int $id The user ID.
  113. * @return bool True if the user was deleted, otherwise false.
  114. */
  115. function wpmu_delete_user( $id ) {
  116. global $wpdb;
  117. if ( ! is_numeric( $id ) ) {
  118. return false;
  119. }
  120. $id = (int) $id;
  121. $user = new WP_User( $id );
  122. if ( ! $user->exists() ) {
  123. return false;
  124. }
  125. // Global super-administrators are protected, and cannot be deleted.
  126. $_super_admins = get_super_admins();
  127. if ( in_array( $user->user_login, $_super_admins, true ) ) {
  128. return false;
  129. }
  130. /**
  131. * Fires before a user is deleted from the network.
  132. *
  133. * @since MU (3.0.0)
  134. *
  135. * @param int $id ID of the user about to be deleted from the network.
  136. */
  137. do_action( 'wpmu_delete_user', $id );
  138. $blogs = get_blogs_of_user( $id );
  139. if ( ! empty( $blogs ) ) {
  140. foreach ( $blogs as $blog ) {
  141. switch_to_blog( $blog->userblog_id );
  142. remove_user_from_blog( $id, $blog->userblog_id );
  143. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
  144. foreach ( (array) $post_ids as $post_id ) {
  145. wp_delete_post( $post_id );
  146. }
  147. // Clean links.
  148. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
  149. if ( $link_ids ) {
  150. foreach ( $link_ids as $link_id ) {
  151. wp_delete_link( $link_id );
  152. }
  153. }
  154. restore_current_blog();
  155. }
  156. }
  157. $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  158. foreach ( $meta as $mid ) {
  159. delete_metadata_by_mid( 'user', $mid );
  160. }
  161. $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
  162. clean_user_cache( $user );
  163. /** This action is documented in wp-admin/includes/user.php */
  164. do_action( 'deleted_user', $id, null );
  165. return true;
  166. }
  167. /**
  168. * Check whether a site has used its allotted upload space.
  169. *
  170. * @since MU (3.0.0)
  171. *
  172. * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
  173. * @return bool True if user is over upload space quota, otherwise false.
  174. */
  175. function upload_is_user_over_quota( $echo = true ) {
  176. if ( get_site_option( 'upload_space_check_disabled' ) ) {
  177. return false;
  178. }
  179. $space_allowed = get_space_allowed();
  180. if ( ! is_numeric( $space_allowed ) ) {
  181. $space_allowed = 10; // Default space allowed is 10 MB.
  182. }
  183. $space_used = get_space_used();
  184. if ( ( $space_allowed - $space_used ) < 0 ) {
  185. if ( $echo ) {
  186. printf(
  187. /* translators: %s: Allowed space allocation. */
  188. __( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ),
  189. size_format( $space_allowed * MB_IN_BYTES )
  190. );
  191. }
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. /**
  198. * Displays the amount of disk space used by the current site. Not used in core.
  199. *
  200. * @since MU (3.0.0)
  201. */
  202. function display_space_usage() {
  203. $space_allowed = get_space_allowed();
  204. $space_used = get_space_used();
  205. $percent_used = ( $space_used / $space_allowed ) * 100;
  206. $space = size_format( $space_allowed * MB_IN_BYTES );
  207. ?>
  208. <strong>
  209. <?php
  210. /* translators: Storage space that's been used. 1: Percentage of used space, 2: Total space allowed in megabytes or gigabytes. */
  211. printf( __( 'Used: %1$s%% of %2$s' ), number_format( $percent_used ), $space );
  212. ?>
  213. </strong>
  214. <?php
  215. }
  216. /**
  217. * Get the remaining upload space for this site.
  218. *
  219. * @since MU (3.0.0)
  220. *
  221. * @param int $size Current max size in bytes
  222. * @return int Max size in bytes
  223. */
  224. function fix_import_form_size( $size ) {
  225. if ( upload_is_user_over_quota( false ) ) {
  226. return 0;
  227. }
  228. $available = get_upload_space_available();
  229. return min( $size, $available );
  230. }
  231. /**
  232. * Displays the site upload space quota setting form on the Edit Site Settings screen.
  233. *
  234. * @since 3.0.0
  235. *
  236. * @param int $id The ID of the site to display the setting for.
  237. */
  238. function upload_space_setting( $id ) {
  239. switch_to_blog( $id );
  240. $quota = get_option( 'blog_upload_space' );
  241. restore_current_blog();
  242. if ( ! $quota ) {
  243. $quota = '';
  244. }
  245. ?>
  246. <tr>
  247. <th><label for="blog-upload-space-number"><?php _e( 'Site Upload Space Quota' ); ?></label></th>
  248. <td>
  249. <input type="number" step="1" min="0" style="width: 100px" name="option[blog_upload_space]" id="blog-upload-space-number" aria-describedby="blog-upload-space-desc" value="<?php echo $quota; ?>" />
  250. <span id="blog-upload-space-desc"><span class="screen-reader-text"><?php _e( 'Size in megabytes' ); ?></span> <?php _e( 'MB (Leave blank for network default)' ); ?></span>
  251. </td>
  252. </tr>
  253. <?php
  254. }
  255. /**
  256. * Cleans the user cache for a specific user.
  257. *
  258. * @since 3.0.0
  259. *
  260. * @param int $id The user ID.
  261. * @return bool|int The ID of the refreshed user or false if the user does not exist.
  262. */
  263. function refresh_user_details( $id ) {
  264. $id = (int) $id;
  265. $user = get_userdata( $id );
  266. if ( ! $user ) {
  267. return false;
  268. }
  269. clean_user_cache( $user );
  270. return $id;
  271. }
  272. /**
  273. * Returns the language for a language code.
  274. *
  275. * @since 3.0.0
  276. *
  277. * @param string $code Optional. The two-letter language code. Default empty.
  278. * @return string The language corresponding to $code if it exists. If it does not exist,
  279. * then the first two letters of $code is returned.
  280. */
  281. function format_code_lang( $code = '' ) {
  282. $code = strtolower( substr( $code, 0, 2 ) );
  283. $lang_codes = array(
  284. 'aa' => 'Afar',
  285. 'ab' => 'Abkhazian',
  286. 'af' => 'Afrikaans',
  287. 'ak' => 'Akan',
  288. 'sq' => 'Albanian',
  289. 'am' => 'Amharic',
  290. 'ar' => 'Arabic',
  291. 'an' => 'Aragonese',
  292. 'hy' => 'Armenian',
  293. 'as' => 'Assamese',
  294. 'av' => 'Avaric',
  295. 'ae' => 'Avestan',
  296. 'ay' => 'Aymara',
  297. 'az' => 'Azerbaijani',
  298. 'ba' => 'Bashkir',
  299. 'bm' => 'Bambara',
  300. 'eu' => 'Basque',
  301. 'be' => 'Belarusian',
  302. 'bn' => 'Bengali',
  303. 'bh' => 'Bihari',
  304. 'bi' => 'Bislama',
  305. 'bs' => 'Bosnian',
  306. 'br' => 'Breton',
  307. 'bg' => 'Bulgarian',
  308. 'my' => 'Burmese',
  309. 'ca' => 'Catalan; Valencian',
  310. 'ch' => 'Chamorro',
  311. 'ce' => 'Chechen',
  312. 'zh' => 'Chinese',
  313. 'cu' => 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic',
  314. 'cv' => 'Chuvash',
  315. 'kw' => 'Cornish',
  316. 'co' => 'Corsican',
  317. 'cr' => 'Cree',
  318. 'cs' => 'Czech',
  319. 'da' => 'Danish',
  320. 'dv' => 'Divehi; Dhivehi; Maldivian',
  321. 'nl' => 'Dutch; Flemish',
  322. 'dz' => 'Dzongkha',
  323. 'en' => 'English',
  324. 'eo' => 'Esperanto',
  325. 'et' => 'Estonian',
  326. 'ee' => 'Ewe',
  327. 'fo' => 'Faroese',
  328. 'fj' => 'Fijjian',
  329. 'fi' => 'Finnish',
  330. 'fr' => 'French',
  331. 'fy' => 'Western Frisian',
  332. 'ff' => 'Fulah',
  333. 'ka' => 'Georgian',
  334. 'de' => 'German',
  335. 'gd' => 'Gaelic; Scottish Gaelic',
  336. 'ga' => 'Irish',
  337. 'gl' => 'Galician',
  338. 'gv' => 'Manx',
  339. 'el' => 'Greek, Modern',
  340. 'gn' => 'Guarani',
  341. 'gu' => 'Gujarati',
  342. 'ht' => 'Haitian; Haitian Creole',
  343. 'ha' => 'Hausa',
  344. 'he' => 'Hebrew',
  345. 'hz' => 'Herero',
  346. 'hi' => 'Hindi',
  347. 'ho' => 'Hiri Motu',
  348. 'hu' => 'Hungarian',
  349. 'ig' => 'Igbo',
  350. 'is' => 'Icelandic',
  351. 'io' => 'Ido',
  352. 'ii' => 'Sichuan Yi',
  353. 'iu' => 'Inuktitut',
  354. 'ie' => 'Interlingue',
  355. 'ia' => 'Interlingua (International Auxiliary Language Association)',
  356. 'id' => 'Indonesian',
  357. 'ik' => 'Inupiaq',
  358. 'it' => 'Italian',
  359. 'jv' => 'Javanese',
  360. 'ja' => 'Japanese',
  361. 'kl' => 'Kalaallisut; Greenlandic',
  362. 'kn' => 'Kannada',
  363. 'ks' => 'Kashmiri',
  364. 'kr' => 'Kanuri',
  365. 'kk' => 'Kazakh',
  366. 'km' => 'Central Khmer',
  367. 'ki' => 'Kikuyu; Gikuyu',
  368. 'rw' => 'Kinyarwanda',
  369. 'ky' => 'Kirghiz; Kyrgyz',
  370. 'kv' => 'Komi',
  371. 'kg' => 'Kongo',
  372. 'ko' => 'Korean',
  373. 'kj' => 'Kuanyama; Kwanyama',
  374. 'ku' => 'Kurdish',
  375. 'lo' => 'Lao',
  376. 'la' => 'Latin',
  377. 'lv' => 'Latvian',
  378. 'li' => 'Limburgan; Limburger; Limburgish',
  379. 'ln' => 'Lingala',
  380. 'lt' => 'Lithuanian',
  381. 'lb' => 'Luxembourgish; Letzeburgesch',
  382. 'lu' => 'Luba-Katanga',
  383. 'lg' => 'Ganda',
  384. 'mk' => 'Macedonian',
  385. 'mh' => 'Marshallese',
  386. 'ml' => 'Malayalam',
  387. 'mi' => 'Maori',
  388. 'mr' => 'Marathi',
  389. 'ms' => 'Malay',
  390. 'mg' => 'Malagasy',
  391. 'mt' => 'Maltese',
  392. 'mo' => 'Moldavian',
  393. 'mn' => 'Mongolian',
  394. 'na' => 'Nauru',
  395. 'nv' => 'Navajo; Navaho',
  396. 'nr' => 'Ndebele, South; South Ndebele',
  397. 'nd' => 'Ndebele, North; North Ndebele',
  398. 'ng' => 'Ndonga',
  399. 'ne' => 'Nepali',
  400. 'nn' => 'Norwegian Nynorsk; Nynorsk, Norwegian',
  401. 'nb' => 'Bokmål, Norwegian, Norwegian Bokmål',
  402. 'no' => 'Norwegian',
  403. 'ny' => 'Chichewa; Chewa; Nyanja',
  404. 'oc' => 'Occitan, Provençal',
  405. 'oj' => 'Ojibwa',
  406. 'or' => 'Oriya',
  407. 'om' => 'Oromo',
  408. 'os' => 'Ossetian; Ossetic',
  409. 'pa' => 'Panjabi; Punjabi',
  410. 'fa' => 'Persian',
  411. 'pi' => 'Pali',
  412. 'pl' => 'Polish',
  413. 'pt' => 'Portuguese',
  414. 'ps' => 'Pushto',
  415. 'qu' => 'Quechua',
  416. 'rm' => 'Romansh',
  417. 'ro' => 'Romanian',
  418. 'rn' => 'Rundi',
  419. 'ru' => 'Russian',
  420. 'sg' => 'Sango',
  421. 'sa' => 'Sanskrit',
  422. 'sr' => 'Serbian',
  423. 'hr' => 'Croatian',
  424. 'si' => 'Sinhala; Sinhalese',
  425. 'sk' => 'Slovak',
  426. 'sl' => 'Slovenian',
  427. 'se' => 'Northern Sami',
  428. 'sm' => 'Samoan',
  429. 'sn' => 'Shona',
  430. 'sd' => 'Sindhi',
  431. 'so' => 'Somali',
  432. 'st' => 'Sotho, Southern',
  433. 'es' => 'Spanish; Castilian',
  434. 'sc' => 'Sardinian',
  435. 'ss' => 'Swati',
  436. 'su' => 'Sundanese',
  437. 'sw' => 'Swahili',
  438. 'sv' => 'Swedish',
  439. 'ty' => 'Tahitian',
  440. 'ta' => 'Tamil',
  441. 'tt' => 'Tatar',
  442. 'te' => 'Telugu',
  443. 'tg' => 'Tajik',
  444. 'tl' => 'Tagalog',
  445. 'th' => 'Thai',
  446. 'bo' => 'Tibetan',
  447. 'ti' => 'Tigrinya',
  448. 'to' => 'Tonga (Tonga Islands)',
  449. 'tn' => 'Tswana',
  450. 'ts' => 'Tsonga',
  451. 'tk' => 'Turkmen',
  452. 'tr' => 'Turkish',
  453. 'tw' => 'Twi',
  454. 'ug' => 'Uighur; Uyghur',
  455. 'uk' => 'Ukrainian',
  456. 'ur' => 'Urdu',
  457. 'uz' => 'Uzbek',
  458. 've' => 'Venda',
  459. 'vi' => 'Vietnamese',
  460. 'vo' => 'Volapük',
  461. 'cy' => 'Welsh',
  462. 'wa' => 'Walloon',
  463. 'wo' => 'Wolof',
  464. 'xh' => 'Xhosa',
  465. 'yi' => 'Yiddish',
  466. 'yo' => 'Yoruba',
  467. 'za' => 'Zhuang; Chuang',
  468. 'zu' => 'Zulu',
  469. );
  470. /**
  471. * Filters the language codes.
  472. *
  473. * @since MU (3.0.0)
  474. *
  475. * @param string[] $lang_codes Array of key/value pairs of language codes where key is the short version.
  476. * @param string $code A two-letter designation of the language.
  477. */
  478. $lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
  479. return strtr( $code, $lang_codes );
  480. }
  481. /**
  482. * Synchronizes category and post tag slugs when global terms are enabled.
  483. *
  484. * @since 3.0.0
  485. *
  486. * @param WP_Term|array $term The term.
  487. * @param string $taxonomy The taxonomy for `$term`. Should be 'category' or 'post_tag', as these are
  488. * the only taxonomies which are processed by this function; anything else
  489. * will be returned untouched.
  490. * @return WP_Term|array Returns `$term`, after filtering the 'slug' field with `sanitize_title()`
  491. * if `$taxonomy` is 'category' or 'post_tag'.
  492. */
  493. function sync_category_tag_slugs( $term, $taxonomy ) {
  494. if ( global_terms_enabled() && ( 'category' === $taxonomy || 'post_tag' === $taxonomy ) ) {
  495. if ( is_object( $term ) ) {
  496. $term->slug = sanitize_title( $term->name );
  497. } else {
  498. $term['slug'] = sanitize_title( $term['name'] );
  499. }
  500. }
  501. return $term;
  502. }
  503. /**
  504. * Displays an access denied message when a user tries to view a site's dashboard they
  505. * do not have access to.
  506. *
  507. * @since 3.2.0
  508. * @access private
  509. */
  510. function _access_denied_splash() {
  511. if ( ! is_user_logged_in() || is_network_admin() ) {
  512. return;
  513. }
  514. $blogs = get_blogs_of_user( get_current_user_id() );
  515. if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ) {
  516. return;
  517. }
  518. $blog_name = get_bloginfo( 'name' );
  519. if ( empty( $blogs ) ) {
  520. wp_die(
  521. sprintf(
  522. /* translators: 1: Site title. */
  523. __( '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.' ),
  524. $blog_name
  525. ),
  526. 403
  527. );
  528. }
  529. $output = '<p>' . sprintf(
  530. /* translators: 1: Site title. */
  531. __( '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.' ),
  532. $blog_name
  533. ) . '</p>';
  534. $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>';
  535. $output .= '<h3>' . __( 'Your Sites' ) . '</h3>';
  536. $output .= '<table>';
  537. foreach ( $blogs as $blog ) {
  538. $output .= '<tr>';
  539. $output .= "<td>{$blog->blogname}</td>";
  540. $output .= '<td><a href="' . esc_url( get_admin_url( $blog->userblog_id ) ) . '">' . __( 'Visit Dashboard' ) . '</a> | ' .
  541. '<a href="' . esc_url( get_home_url( $blog->userblog_id ) ) . '">' . __( 'View Site' ) . '</a></td>';
  542. $output .= '</tr>';
  543. }
  544. $output .= '</table>';
  545. wp_die( $output, 403 );
  546. }
  547. /**
  548. * Checks if the current user has permissions to import new users.
  549. *
  550. * @since 3.0.0
  551. *
  552. * @param string $permission A permission to be checked. Currently not used.
  553. * @return bool True if the user has proper permissions, false if they do not.
  554. */
  555. function check_import_new_users( $permission ) {
  556. if ( ! current_user_can( 'manage_network_users' ) ) {
  557. return false;
  558. }
  559. return true;
  560. }
  561. // See "import_allow_fetch_attachments" and "import_attachment_size_limit" filters too.
  562. /**
  563. * Generates and displays a drop-down of available languages.
  564. *
  565. * @since 3.0.0
  566. *
  567. * @param string[] $lang_files Optional. An array of the language files. Default empty array.
  568. * @param string $current Optional. The current language code. Default empty.
  569. */
  570. function mu_dropdown_languages( $lang_files = array(), $current = '' ) {
  571. $flag = false;
  572. $output = array();
  573. foreach ( (array) $lang_files as $val ) {
  574. $code_lang = basename( $val, '.mo' );
  575. if ( 'en_US' === $code_lang ) { // American English.
  576. $flag = true;
  577. $ae = __( 'American English' );
  578. $output[ $ae ] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $ae . '</option>';
  579. } elseif ( 'en_GB' === $code_lang ) { // British English.
  580. $flag = true;
  581. $be = __( 'British English' );
  582. $output[ $be ] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $be . '</option>';
  583. } else {
  584. $translated = format_code_lang( $code_lang );
  585. $output[ $translated ] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . esc_html( $translated ) . '</option>';
  586. }
  587. }
  588. if ( false === $flag ) { // WordPress English.
  589. $output[] = '<option value=""' . selected( $current, '', false ) . '>' . __( 'English' ) . '</option>';
  590. }
  591. // Order by name.
  592. uksort( $output, 'strnatcasecmp' );
  593. /**
  594. * Filters the languages available in the dropdown.
  595. *
  596. * @since MU (3.0.0)
  597. *
  598. * @param string[] $output Array of HTML output for the dropdown.
  599. * @param string[] $lang_files Array of available language files.
  600. * @param string $current The current language code.
  601. */
  602. $output = apply_filters( 'mu_dropdown_languages', $output, $lang_files, $current );
  603. echo implode( "\n\t", $output );
  604. }
  605. /**
  606. * Displays an admin notice to upgrade all sites after a core upgrade.
  607. *
  608. * @since 3.0.0
  609. *
  610. * @global int $wp_db_version WordPress database version.
  611. * @global string $pagenow
  612. *
  613. * @return false False if the current user is not a super admin.
  614. */
  615. function site_admin_notice() {
  616. global $wp_db_version, $pagenow;
  617. if ( ! current_user_can( 'upgrade_network' ) ) {
  618. return false;
  619. }
  620. if ( 'upgrade.php' == $pagenow ) {
  621. return;
  622. }
  623. if ( get_site_option( 'wpmu_upgrade_site' ) != $wp_db_version ) {
  624. echo "<div class='update-nag'>" . sprintf(
  625. /* translators: %s: URL to Upgrade Network screen. */
  626. __( 'Thank you for Updating! Please visit the <a href="%s">Upgrade Network</a> page to update all your sites.' ),
  627. esc_url( network_admin_url( 'upgrade.php' ) )
  628. ) . '</div>';
  629. }
  630. }
  631. /**
  632. * Avoids a collision between a site slug and a permalink slug.
  633. *
  634. * In a subdirectory installation this will make sure that a site and a post do not use the
  635. * same subdirectory by checking for a site with the same name as a new post.
  636. *
  637. * @since 3.0.0
  638. *
  639. * @param array $data An array of post data.
  640. * @param array $postarr An array of posts. Not currently used.
  641. * @return array The new array of post data after checking for collisions.
  642. */
  643. function avoid_blog_page_permalink_collision( $data, $postarr ) {
  644. if ( is_subdomain_install() ) {
  645. return $data;
  646. }
  647. if ( 'page' !== $data['post_type'] ) {
  648. return $data;
  649. }
  650. if ( ! isset( $data['post_name'] ) || '' === $data['post_name'] ) {
  651. return $data;
  652. }
  653. if ( ! is_main_site() ) {
  654. return $data;
  655. }
  656. $post_name = $data['post_name'];
  657. $c = 0;
  658. while ( $c < 10 && get_id_from_blogname( $post_name ) ) {
  659. $post_name .= mt_rand( 1, 10 );
  660. $c ++;
  661. }
  662. if ( $post_name != $data['post_name'] ) {
  663. $data['post_name'] = $post_name;
  664. }
  665. return $data;
  666. }
  667. /**
  668. * Handles the display of choosing a user's primary site.
  669. *
  670. * This displays the user's primary site and allows the user to choose
  671. * which site is primary.
  672. *
  673. * @since 3.0.0
  674. */
  675. function choose_primary_blog() {
  676. ?>
  677. <table class="form-table" role="presentation">
  678. <tr>
  679. <?php /* translators: My Sites label. */ ?>
  680. <th scope="row"><label for="primary_blog"><?php _e( 'Primary Site' ); ?></label></th>
  681. <td>
  682. <?php
  683. $all_blogs = get_blogs_of_user( get_current_user_id() );
  684. $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
  685. if ( count( $all_blogs ) > 1 ) {
  686. $found = false;
  687. ?>
  688. <select name="primary_blog" id="primary_blog">
  689. <?php
  690. foreach ( (array) $all_blogs as $blog ) {
  691. if ( $primary_blog == $blog->userblog_id ) {
  692. $found = true;
  693. }
  694. ?>
  695. <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>
  696. <?php
  697. }
  698. ?>
  699. </select>
  700. <?php
  701. if ( ! $found ) {
  702. $blog = reset( $all_blogs );
  703. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  704. }
  705. } elseif ( count( $all_blogs ) == 1 ) {
  706. $blog = reset( $all_blogs );
  707. echo esc_url( get_home_url( $blog->userblog_id ) );
  708. if ( $primary_blog != $blog->userblog_id ) { // Set the primary blog again if it's out of sync with blog list.
  709. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  710. }
  711. } else {
  712. echo 'N/A';
  713. }
  714. ?>
  715. </td>
  716. </tr>
  717. </table>
  718. <?php
  719. }
  720. /**
  721. * Whether or not we can edit this network from this page.
  722. *
  723. * By default editing of network is restricted to the Network Admin for that `$network_id`.
  724. * This function allows for this to be overridden.
  725. *
  726. * @since 3.1.0
  727. *
  728. * @param int $network_id The network ID to check.
  729. * @return bool True if network can be edited, otherwise false.
  730. */
  731. function can_edit_network( $network_id ) {
  732. if ( get_current_network_id() == $network_id ) {
  733. $result = true;
  734. } else {
  735. $result = false;
  736. }
  737. /**
  738. * Filters whether this network can be edited from this page.
  739. *
  740. * @since 3.1.0
  741. *
  742. * @param bool $result Whether the network can be edited from this page.
  743. * @param int $network_id The network ID to check.
  744. */
  745. return apply_filters( 'can_edit_network', $result, $network_id );
  746. }
  747. /**
  748. * Thickbox image paths for Network Admin.
  749. *
  750. * @since 3.1.0
  751. *
  752. * @access private
  753. */
  754. function _thickbox_path_admin_subfolder() {
  755. ?>
  756. <script type="text/javascript">
  757. var tb_pathToImage = "<?php echo includes_url( 'js/thickbox/loadingAnimation.gif', 'relative' ); ?>";
  758. </script>
  759. <?php
  760. }
  761. /**
  762. * @param array $users
  763. */
  764. function confirm_delete_users( $users ) {
  765. $current_user = wp_get_current_user();
  766. if ( ! is_array( $users ) || empty( $users ) ) {
  767. return false;
  768. }
  769. ?>
  770. <h1><?php esc_html_e( 'Users' ); ?></h1>
  771. <?php if ( 1 == count( $users ) ) : ?>
  772. <p><?php _e( 'You have chosen to delete the user from all networks and sites.' ); ?></p>
  773. <?php else : ?>
  774. <p><?php _e( 'You have chosen to delete the following users from all networks and sites.' ); ?></p>
  775. <?php endif; ?>
  776. <form action="users.php?action=dodelete" method="post">
  777. <input type="hidden" name="dodelete" />
  778. <?php
  779. wp_nonce_field( 'ms-users-delete' );
  780. $site_admins = get_super_admins();
  781. $admin_out = '<option value="' . esc_attr( $current_user->ID ) . '">' . $current_user->user_login . '</option>';
  782. ?>
  783. <table class="form-table" role="presentation">
  784. <?php
  785. $allusers = (array) $_POST['allusers'];
  786. foreach ( $allusers as $user_id ) {
  787. if ( '' != $user_id && '0' != $user_id ) {
  788. $delete_user = get_userdata( $user_id );
  789. if ( ! current_user_can( 'delete_user', $delete_user->ID ) ) {
  790. wp_die(
  791. sprintf(
  792. /* translators: %s: User login. */
  793. __( 'Warning! User %s cannot be deleted.' ),
  794. $delete_user->user_login
  795. )
  796. );
  797. }
  798. if ( in_array( $delete_user->user_login, $site_admins, true ) ) {
  799. wp_die(
  800. sprintf(
  801. /* translators: %s: User login. */
  802. __( 'Warning! User cannot be deleted. The user %s is a network administrator.' ),
  803. '<em>' . $delete_user->user_login . '</em>'
  804. )
  805. );
  806. }
  807. ?>
  808. <tr>
  809. <th scope="row"><?php echo $delete_user->user_login; ?>
  810. <?php echo '<input type="hidden" name="user[]" value="' . esc_attr( $user_id ) . '" />' . "\n"; ?>
  811. </th>
  812. <?php
  813. $blogs = get_blogs_of_user( $user_id, true );
  814. if ( ! empty( $blogs ) ) {
  815. ?>
  816. <td><fieldset><p><legend>
  817. <?php
  818. printf(
  819. /* translators: %s: User login. */
  820. __( 'What should be done with content owned by %s?' ),
  821. '<em>' . $delete_user->user_login . '</em>'
  822. );
  823. ?>
  824. </legend></p>
  825. <?php
  826. foreach ( (array) $blogs as $key => $details ) {
  827. $blog_users = get_users(
  828. array(
  829. 'blog_id' => $details->userblog_id,
  830. 'fields' => array( 'ID', 'user_login' ),
  831. )
  832. );
  833. if ( is_array( $blog_users ) && ! empty( $blog_users ) ) {
  834. $user_site = "<a href='" . esc_url( get_home_url( $details->userblog_id ) ) . "'>{$details->blogname}</a>";
  835. $user_dropdown = '<label for="reassign_user" class="screen-reader-text">' . __( 'Select a user' ) . '</label>';
  836. $user_dropdown .= "<select name='blog[$user_id][$key]' id='reassign_user'>";
  837. $user_list = '';
  838. foreach ( $blog_users as $user ) {
  839. if ( ! in_array( (int) $user->ID, $allusers, true ) ) {
  840. $user_list .= "<option value='{$user->ID}'>{$user->user_login}</option>";
  841. }
  842. }
  843. if ( '' == $user_list ) {
  844. $user_list = $admin_out;
  845. }
  846. $user_dropdown .= $user_list;
  847. $user_dropdown .= "</select>\n";
  848. ?>
  849. <ul style="list-style:none;">
  850. <li>
  851. <?php
  852. /* translators: %s: Link to user's site. */
  853. printf( __( 'Site: %s' ), $user_site );
  854. ?>
  855. </li>
  856. <li><label><input type="radio" id="delete_option0" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="delete" checked="checked" />
  857. <?php _e( 'Delete all content.' ); ?></label></li>
  858. <li><label><input type="radio" id="delete_option1" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="reassign" />
  859. <?php _e( 'Attribute all content to:' ); ?></label>
  860. <?php echo $user_dropdown; ?></li>
  861. </ul>
  862. <?php
  863. }
  864. }
  865. echo '</fieldset></td></tr>';
  866. } else {
  867. ?>
  868. <td><p><?php _e( 'User has no sites or content and will be deleted.' ); ?></p></td>
  869. <?php } ?>
  870. </tr>
  871. <?php
  872. }
  873. }
  874. ?>
  875. </table>
  876. <?php
  877. /** This action is documented in wp-admin/users.php */
  878. do_action( 'delete_user_form', $current_user, $allusers );
  879. if ( 1 == count( $users ) ) :
  880. ?>
  881. <p><?php _e( 'Once you hit &#8220;Confirm Deletion&#8221;, the user will be permanently removed.' ); ?></p>
  882. <?php else : ?>
  883. <p><?php _e( 'Once you hit &#8220;Confirm Deletion&#8221;, these users will be permanently removed.' ); ?></p>
  884. <?php
  885. endif;
  886. submit_button( __( 'Confirm Deletion' ), 'primary' );
  887. ?>
  888. </form>
  889. <?php
  890. return true;
  891. }
  892. /**
  893. * Print JavaScript in the header on the Network Settings screen.
  894. *
  895. * @since 4.1.0
  896. */
  897. function network_settings_add_js() {
  898. ?>
  899. <script type="text/javascript">
  900. jQuery(document).ready( function($) {
  901. var languageSelect = $( '#WPLANG' );
  902. $( 'form' ).submit( function() {
  903. // Don't show a spinner for English and installed languages,
  904. // as there is nothing to download.
  905. if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
  906. $( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' );
  907. }
  908. });
  909. });
  910. </script>
  911. <?php
  912. }
  913. /**
  914. * Outputs the HTML for a network's "Edit Site" tabular interface.
  915. *
  916. * @since 4.6.0
  917. *
  918. * @param $args {
  919. * Optional. Array or string of Query parameters. Default empty array.
  920. *
  921. * @type int $blog_id The site ID. Default is the current site.
  922. * @type array $links The tabs to include with (label|url|cap) keys.
  923. * @type string $selected The ID of the selected link.
  924. * }
  925. */
  926. function network_edit_site_nav( $args = array() ) {
  927. /**
  928. * Filters the links that appear on site-editing network pages.
  929. *
  930. * Default links: 'site-info', 'site-users', 'site-themes', and 'site-settings'.
  931. *
  932. * @since 4.6.0
  933. *
  934. * @param array $links {
  935. * An array of link data representing individual network admin pages.
  936. *
  937. * @type array $link_slug {
  938. * An array of information about the individual link to a page.
  939. *
  940. * $type string $label Label to use for the link.
  941. * $type string $url URL, relative to `network_admin_url()` to use for the link.
  942. * $type string $cap Capability required to see the link.
  943. * }
  944. * }
  945. */
  946. $links = apply_filters(
  947. 'network_edit_site_nav_links',
  948. array(
  949. 'site-info' => array(
  950. 'label' => __( 'Info' ),
  951. 'url' => 'site-info.php',
  952. 'cap' => 'manage_sites',
  953. ),
  954. 'site-users' => array(
  955. 'label' => __( 'Users' ),
  956. 'url' => 'site-users.php',
  957. 'cap' => 'manage_sites',
  958. ),
  959. 'site-themes' => array(
  960. 'label' => __( 'Themes' ),
  961. 'url' => 'site-themes.php',
  962. 'cap' => 'manage_sites',
  963. ),
  964. 'site-settings' => array(
  965. 'label' => __( 'Settings' ),
  966. 'url' => 'site-settings.php',
  967. 'cap' => 'manage_sites',
  968. ),
  969. )
  970. );
  971. // Parse arguments.
  972. $parsed_args = wp_parse_args(
  973. $args,
  974. array(
  975. 'blog_id' => isset( $_GET['blog_id'] ) ? (int) $_GET['blog_id'] : 0,
  976. 'links' => $links,
  977. 'selected' => 'site-info',
  978. )
  979. );
  980. // Setup the links array.
  981. $screen_links = array();
  982. // Loop through tabs.
  983. foreach ( $parsed_args['links'] as $link_id => $link ) {
  984. // Skip link if user can't access.
  985. if ( ! current_user_can( $link['cap'], $parsed_args['blog_id'] ) ) {
  986. continue;
  987. }
  988. // Link classes.
  989. $classes = array( 'nav-tab' );
  990. // Aria-current attribute.
  991. $aria_current = '';
  992. // Selected is set by the parent OR assumed by the $pagenow global.
  993. if ( $parsed_args['selected'] === $link_id || $link['url'] === $GLOBALS['pagenow'] ) {
  994. $classes[] = 'nav-tab-active';
  995. $aria_current = ' aria-current="page"';
  996. }
  997. // Escape each class.
  998. $esc_classes = implode( ' ', $classes );
  999. // Get the URL for this link.
  1000. $url = add_query_arg( array( 'id' => $parsed_args['blog_id'] ), network_admin_url( $link['url'] ) );
  1001. // Add link to nav links.
  1002. $screen_links[ $link_id ] = '<a href="' . esc_url( $url ) . '" id="' . esc_attr( $link_id ) . '" class="' . $esc_classes . '"' . $aria_current . '>' . esc_html( $link['label'] ) . '</a>';
  1003. }
  1004. // All done!
  1005. echo '<nav class="nav-tab-wrapper wp-clearfix" aria-label="' . esc_attr__( 'Secondary menu' ) . '">';
  1006. echo implode( '', $screen_links );
  1007. echo '</nav>';
  1008. }
  1009. /**
  1010. * Returns the arguments for the help tab on the Edit Site screens.
  1011. *
  1012. * @since 4.9.0
  1013. *
  1014. * @return array Help tab arguments.
  1015. */
  1016. function get_site_screen_help_tab_args() {
  1017. return array(
  1018. 'id' => 'overview',
  1019. 'title' => __( 'Overview' ),
  1020. 'content' =>
  1021. '<p>' . __( 'The menu is for editing information specific to individual sites, particularly if the admin area of a site is unavailable.' ) . '</p>' .
  1022. '<p>' . __( '<strong>Info</strong> &mdash; The site URL is rarely edited as this can cause the site to not work properly. The Registered date and Last Updated date are displayed. Network admins can mark a site as archived, spam, deleted and mature, to remove from public listings or disable.' ) . '</p>' .
  1023. '<p>' . __( '<strong>Users</strong> &mdash; This displays the users associated with this site. You can also change their role, reset their password, or remove them from the site. Removing the user from the site does not remove the user from the network.' ) . '</p>' .
  1024. '<p>' . sprintf(
  1025. /* translators: %s: URL to Network Themes screen. */
  1026. __( '<strong>Themes</strong> &mdash; This area shows themes that are not already enabled across the network. Enabling a theme in this menu makes it accessible to this site. It does not activate the theme, but allows it to show in the site&#8217;s Appearance menu. To enable a theme for the entire network, see the <a href="%s">Network Themes</a> screen.' ),
  1027. network_admin_url( 'themes.php' )
  1028. ) . '</p>' .
  1029. '<p>' . __( '<strong>Settings</strong> &mdash; This page shows a list of all settings associated with this site. Some are created by WordPress and others are created by plugins you activate. Note that some fields are grayed out and say Serialized Data. You cannot modify these values due to the way the setting is stored in the database.' ) . '</p>',
  1030. );
  1031. }
  1032. /**
  1033. * Returns the content for the help sidebar on the Edit Site screens.
  1034. *
  1035. * @since 4.9.0
  1036. *
  1037. * @return string Help sidebar content.
  1038. */
  1039. function get_site_screen_help_sidebar_content() {
  1040. return '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  1041. '<p>' . __( '<a href="https://wordpress.org/support/article/network-admin-sites-screen/">Documentation on Site Management</a>' ) . '</p>' .
  1042. '<p>' . __( '<a href="https://wordpress.org/support/forum/multisite/">Support Forums</a>' ) . '</p>';
  1043. }