PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/ms.php

https://gitlab.com/geyson/geyson
PHP | 983 lines | 489 code | 121 blank | 373 comment | 123 complexity | ab2da0a25302e431caf035800482dbb1 MD5 | raw file
Possible License(s): 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'] ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
  34. wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
  35. }
  36. return $file;
  37. }
  38. /**
  39. * Delete a blog.
  40. *
  41. * @since 3.0.0
  42. *
  43. * @global wpdb $wpdb
  44. *
  45. * @param int $blog_id Blog ID.
  46. * @param bool $drop True if blog's table should be dropped. Default is false.
  47. */
  48. function wpmu_delete_blog( $blog_id, $drop = false ) {
  49. global $wpdb;
  50. $switch = false;
  51. if ( get_current_blog_id() != $blog_id ) {
  52. $switch = true;
  53. switch_to_blog( $blog_id );
  54. }
  55. $blog = get_blog_details( $blog_id );
  56. /**
  57. * Fires before a blog is deleted.
  58. *
  59. * @since MU
  60. *
  61. * @param int $blog_id The blog ID.
  62. * @param bool $drop True if blog's table should be dropped. Default is false.
  63. */
  64. do_action( 'delete_blog', $blog_id, $drop );
  65. $users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
  66. // Remove users from this blog.
  67. if ( ! empty( $users ) ) {
  68. foreach ( $users as $user_id ) {
  69. remove_user_from_blog( $user_id, $blog_id );
  70. }
  71. }
  72. update_blog_status( $blog_id, 'deleted', 1 );
  73. $current_site = get_current_site();
  74. // If a full blog object is not available, do not destroy anything.
  75. if ( $drop && ! $blog ) {
  76. $drop = false;
  77. }
  78. // Don't destroy the initial, main, or root blog.
  79. if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_site->path && $blog->domain == $current_site->domain ) ) ) {
  80. $drop = false;
  81. }
  82. $upload_path = trim( get_option( 'upload_path' ) );
  83. // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
  84. if ( $drop && get_site_option( 'ms_files_rewriting' ) && empty( $upload_path ) ) {
  85. $drop = false;
  86. }
  87. if ( $drop ) {
  88. $uploads = wp_upload_dir();
  89. $tables = $wpdb->tables( 'blog' );
  90. /**
  91. * Filter the tables to drop when the blog is deleted.
  92. *
  93. * @since MU
  94. *
  95. * @param array $tables The blog tables to be dropped.
  96. * @param int $blog_id The ID of the blog to drop tables for.
  97. */
  98. $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $blog_id );
  99. foreach ( (array) $drop_tables as $table ) {
  100. $wpdb->query( "DROP TABLE IF EXISTS `$table`" );
  101. }
  102. $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
  103. /**
  104. * Filter the upload base directory to delete when the blog is deleted.
  105. *
  106. * @since MU
  107. *
  108. * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
  109. * @param int $blog_id The blog ID.
  110. */
  111. $dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id );
  112. $dir = rtrim( $dir, DIRECTORY_SEPARATOR );
  113. $top_dir = $dir;
  114. $stack = array($dir);
  115. $index = 0;
  116. while ( $index < count( $stack ) ) {
  117. // Get indexed directory from stack
  118. $dir = $stack[$index];
  119. $dh = @opendir( $dir );
  120. if ( $dh ) {
  121. while ( ( $file = @readdir( $dh ) ) !== false ) {
  122. if ( $file == '.' || $file == '..' )
  123. continue;
  124. if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
  125. $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
  126. } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
  127. @unlink( $dir . DIRECTORY_SEPARATOR . $file );
  128. }
  129. }
  130. @closedir( $dh );
  131. }
  132. $index++;
  133. }
  134. $stack = array_reverse( $stack ); // Last added dirs are deepest
  135. foreach( (array) $stack as $dir ) {
  136. if ( $dir != $top_dir)
  137. @rmdir( $dir );
  138. }
  139. clean_blog_cache( $blog );
  140. }
  141. if ( $switch )
  142. restore_current_blog();
  143. }
  144. /**
  145. * Delete a user from the network and remove from all sites.
  146. *
  147. * @since 3.0.0
  148. *
  149. * @todo Merge with wp_delete_user() ?
  150. *
  151. * @global wpdb $wpdb
  152. *
  153. * @param int $id The user ID.
  154. * @return bool True if the user was deleted, otherwise false.
  155. */
  156. function wpmu_delete_user( $id ) {
  157. global $wpdb;
  158. $id = (int) $id;
  159. $user = new WP_User( $id );
  160. if ( !$user->exists() )
  161. return false;
  162. // Global super-administrators are protected, and cannot be deleted.
  163. $_super_admins = get_super_admins();
  164. if ( in_array( $user->user_login, $_super_admins, true ) ) {
  165. return false;
  166. }
  167. /**
  168. * Fires before a user is deleted from the network.
  169. *
  170. * @since MU
  171. *
  172. * @param int $id ID of the user about to be deleted from the network.
  173. */
  174. do_action( 'wpmu_delete_user', $id );
  175. $blogs = get_blogs_of_user( $id );
  176. if ( ! empty( $blogs ) ) {
  177. foreach ( $blogs as $blog ) {
  178. switch_to_blog( $blog->userblog_id );
  179. remove_user_from_blog( $id, $blog->userblog_id );
  180. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
  181. foreach ( (array) $post_ids as $post_id ) {
  182. wp_delete_post( $post_id );
  183. }
  184. // Clean links
  185. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
  186. if ( $link_ids ) {
  187. foreach ( $link_ids as $link_id )
  188. wp_delete_link( $link_id );
  189. }
  190. restore_current_blog();
  191. }
  192. }
  193. $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  194. foreach ( $meta as $mid )
  195. delete_metadata_by_mid( 'user', $mid );
  196. $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
  197. clean_user_cache( $user );
  198. /** This action is documented in wp-admin/includes/user.php */
  199. do_action( 'deleted_user', $id );
  200. return true;
  201. }
  202. /**
  203. * Sends an email when a site administrator email address is changed.
  204. *
  205. * @since 3.0.0
  206. *
  207. * @param string $old_value The old email address. Not currently used.
  208. * @param string $value The new email address.
  209. */
  210. function update_option_new_admin_email( $old_value, $value ) {
  211. if ( $value == get_option( 'admin_email' ) || !is_email( $value ) )
  212. return;
  213. $hash = md5( $value. time() .mt_rand() );
  214. $new_admin_email = array(
  215. 'hash' => $hash,
  216. 'newemail' => $value
  217. );
  218. update_option( 'adminhash', $new_admin_email );
  219. /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  220. $email_text = __( 'Howdy ###USERNAME###,
  221. You recently requested to have the administration email address on
  222. your site changed.
  223. If this is correct, please click on the following link to change it:
  224. ###ADMIN_URL###
  225. You can safely ignore and delete this email if you do not want to
  226. take this action.
  227. This email has been sent to ###EMAIL###
  228. Regards,
  229. All at ###SITENAME###
  230. ###SITEURL###' );
  231. /**
  232. * Filter the email text sent when the site admin email is changed.
  233. *
  234. * The following strings have a special meaning and will get replaced dynamically:
  235. * ###USERNAME### The current user's username.
  236. * ###ADMIN_URL### The link to click on to confirm the email change.
  237. * ###EMAIL### The new email.
  238. * ###SITENAME### The name of the site.
  239. * ###SITEURL### The URL to the site.
  240. *
  241. * @since MU
  242. *
  243. * @param string $email_text Text in the email.
  244. * @param string $new_admin_email New admin email that the current administration email was changed to.
  245. */
  246. $content = apply_filters( 'new_admin_email_content', $email_text, $new_admin_email );
  247. $current_user = wp_get_current_user();
  248. $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
  249. $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'options.php?adminhash='.$hash ) ), $content );
  250. $content = str_replace( '###EMAIL###', $value, $content );
  251. $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
  252. $content = str_replace( '###SITEURL###', network_home_url(), $content );
  253. wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
  254. }
  255. /**
  256. * Sends an email when an email address change is requested.
  257. *
  258. * @since 3.0.0
  259. *
  260. * @global object $errors WP_Error object.
  261. * @global object $wpdb WordPress database object.
  262. */
  263. function send_confirmation_on_profile_email() {
  264. global $errors, $wpdb;
  265. $current_user = wp_get_current_user();
  266. if ( ! is_object($errors) )
  267. $errors = new WP_Error();
  268. if ( $current_user->ID != $_POST['user_id'] )
  269. return false;
  270. if ( $current_user->user_email != $_POST['email'] ) {
  271. if ( !is_email( $_POST['email'] ) ) {
  272. $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address isn&#8217;t correct." ), array( 'form-field' => 'email' ) );
  273. return;
  274. }
  275. if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
  276. $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address is already used." ), array( 'form-field' => 'email' ) );
  277. delete_option( $current_user->ID . '_new_email' );
  278. return;
  279. }
  280. $hash = md5( $_POST['email'] . time() . mt_rand() );
  281. $new_user_email = array(
  282. 'hash' => $hash,
  283. 'newemail' => $_POST['email']
  284. );
  285. update_option( $current_user->ID . '_new_email', $new_user_email );
  286. /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  287. $email_text = __( 'Howdy ###USERNAME###,
  288. You recently requested to have the email address on your account changed.
  289. If this is correct, please click on the following link to change it:
  290. ###ADMIN_URL###
  291. You can safely ignore and delete this email if you do not want to
  292. take this action.
  293. This email has been sent to ###EMAIL###
  294. Regards,
  295. All at ###SITENAME###
  296. ###SITEURL###' );
  297. /**
  298. * Filter the email text sent when a user changes emails.
  299. *
  300. * The following strings have a special meaning and will get replaced dynamically:
  301. * ###USERNAME### The current user's username.
  302. * ###ADMIN_URL### The link to click on to confirm the email change.
  303. * ###EMAIL### The new email.
  304. * ###SITENAME### The name of the site.
  305. * ###SITEURL### The URL to the site.
  306. *
  307. * @since MU
  308. *
  309. * @param string $email_text Text in the email.
  310. * @param string $new_user_email New user email that the current user has changed to.
  311. */
  312. $content = apply_filters( 'new_user_email_content', $email_text, $new_user_email );
  313. $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
  314. $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
  315. $content = str_replace( '###EMAIL###', $_POST['email'], $content);
  316. $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
  317. $content = str_replace( '###SITEURL###', network_home_url(), $content );
  318. wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
  319. $_POST['email'] = $current_user->user_email;
  320. }
  321. }
  322. /**
  323. * Adds an admin notice alerting the user to check for confirmation email
  324. * after email address change.
  325. *
  326. * @since 3.0.0
  327. */
  328. function new_user_email_admin_notice() {
  329. if ( strpos( $_SERVER['PHP_SELF'], 'profile.php' ) && isset( $_GET['updated'] ) && $email = get_option( get_current_user_id() . '_new_email' ) )
  330. 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>";
  331. }
  332. /**
  333. * Check whether a blog has used its allotted upload space.
  334. *
  335. * @since MU
  336. *
  337. * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
  338. * @return bool True if user is over upload space quota, otherwise false.
  339. */
  340. function upload_is_user_over_quota( $echo = true ) {
  341. if ( get_site_option( 'upload_space_check_disabled' ) )
  342. return false;
  343. $space_allowed = get_space_allowed();
  344. if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
  345. $space_allowed = 10; // Default space allowed is 10 MB
  346. $space_used = get_space_used();
  347. if ( ( $space_allowed - $space_used ) < 0 ) {
  348. if ( $echo )
  349. _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' );
  350. return true;
  351. } else {
  352. return false;
  353. }
  354. }
  355. /**
  356. * Displays the amount of disk space used by the current blog. Not used in core.
  357. *
  358. * @since MU
  359. */
  360. function display_space_usage() {
  361. $space_allowed = get_space_allowed();
  362. $space_used = get_space_used();
  363. $percent_used = ( $space_used / $space_allowed ) * 100;
  364. if ( $space_allowed > 1000 ) {
  365. $space = number_format( $space_allowed / 1024 );
  366. /* translators: Gigabytes */
  367. $space .= __( 'GB' );
  368. } else {
  369. $space = number_format( $space_allowed );
  370. /* translators: Megabytes */
  371. $space .= __( 'MB' );
  372. }
  373. ?>
  374. <strong><?php printf( __( 'Used: %1$s%% of %2$s' ), number_format( $percent_used ), $space ); ?></strong>
  375. <?php
  376. }
  377. /**
  378. * Get the remaining upload space for this blog.
  379. *
  380. * @since MU
  381. *
  382. * @param int $size Current max size in bytes
  383. * @return int Max size in bytes
  384. */
  385. function fix_import_form_size( $size ) {
  386. if ( upload_is_user_over_quota( false ) ) {
  387. return 0;
  388. }
  389. $available = get_upload_space_available();
  390. return min( $size, $available );
  391. }
  392. /**
  393. * Displays the edit blog upload space setting form on the Edit Blog screen.
  394. *
  395. * @since 3.0.0
  396. *
  397. * @param int $id The ID of the blog to display the setting for.
  398. */
  399. function upload_space_setting( $id ) {
  400. switch_to_blog( $id );
  401. $quota = get_option( 'blog_upload_space' );
  402. restore_current_blog();
  403. if ( !$quota )
  404. $quota = '';
  405. ?>
  406. <tr>
  407. <th><label for="blog-upload-space-number"><?php _e( 'Site Upload Space Quota' ); ?></label></th>
  408. <td>
  409. <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; ?>" />
  410. <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>
  411. </td>
  412. </tr>
  413. <?php
  414. }
  415. /**
  416. * Update the status of a user in the database.
  417. *
  418. * Used in core to mark a user as spam or "ham" (not spam) in Multisite.
  419. *
  420. * @since 3.0.0
  421. *
  422. * @global wpdb $wpdb
  423. *
  424. * @param int $id The user ID.
  425. * @param string $pref The column in the wp_users table to update the user's status
  426. * in (presumably user_status, spam, or deleted).
  427. * @param int $value The new status for the user.
  428. * @param null $deprecated Deprecated as of 3.0.2 and should not be used.
  429. * @return int The initially passed $value.
  430. */
  431. function update_user_status( $id, $pref, $value, $deprecated = null ) {
  432. global $wpdb;
  433. if ( null !== $deprecated )
  434. _deprecated_argument( __FUNCTION__, '3.1' );
  435. $wpdb->update( $wpdb->users, array( sanitize_key( $pref ) => $value ), array( 'ID' => $id ) );
  436. $user = new WP_User( $id );
  437. clean_user_cache( $user );
  438. if ( $pref == 'spam' ) {
  439. if ( $value == 1 ) {
  440. /**
  441. * Fires after the user is marked as a SPAM user.
  442. *
  443. * @since 3.0.0
  444. *
  445. * @param int $id ID of the user marked as SPAM.
  446. */
  447. do_action( 'make_spam_user', $id );
  448. } else {
  449. /**
  450. * Fires after the user is marked as a HAM user. Opposite of SPAM.
  451. *
  452. * @since 3.0.0
  453. *
  454. * @param int $id ID of the user marked as HAM.
  455. */
  456. do_action( 'make_ham_user', $id );
  457. }
  458. }
  459. return $value;
  460. }
  461. /**
  462. * Cleans the user cache for a specific user.
  463. *
  464. * @since 3.0.0
  465. *
  466. * @param int $id The user ID.
  467. * @return bool|int The ID of the refreshed user or false if the user does not exist.
  468. */
  469. function refresh_user_details( $id ) {
  470. $id = (int) $id;
  471. if ( !$user = get_userdata( $id ) )
  472. return false;
  473. clean_user_cache( $user );
  474. return $id;
  475. }
  476. /**
  477. * Returns the language for a language code.
  478. *
  479. * @since 3.0.0
  480. *
  481. * @param string $code Optional. The two-letter language code. Default empty.
  482. * @return string The language corresponding to $code if it exists. If it does not exist,
  483. * then the first two letters of $code is returned.
  484. */
  485. function format_code_lang( $code = '' ) {
  486. $code = strtolower( substr( $code, 0, 2 ) );
  487. $lang_codes = array(
  488. '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',
  489. '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',
  490. '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',
  491. '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',
  492. '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',
  493. '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',
  494. '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',
  495. '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',
  496. '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',
  497. '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',
  498. 've' => 'Venda', 'vi' => 'Vietnamese', 'vo' => 'Volapük', 'cy' => 'Welsh','wa' => 'Walloon','wo' => 'Wolof', 'xh' => 'Xhosa', 'yi' => 'Yiddish', 'yo' => 'Yoruba', 'za' => 'Zhuang; Chuang', 'zu' => 'Zulu' );
  499. /**
  500. * Filter the language codes.
  501. *
  502. * @since MU
  503. *
  504. * @param array $lang_codes Key/value pair of language codes where key is the short version.
  505. * @param string $code A two-letter designation of the language.
  506. */
  507. $lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
  508. return strtr( $code, $lang_codes );
  509. }
  510. /**
  511. * Synchronize category and post tag slugs when global terms are enabled.
  512. *
  513. * @since 3.0.0
  514. *
  515. * @param object $term The term.
  516. * @param string $taxonomy The taxonomy for $term. Should be 'category' or 'post_tag', as these are
  517. * the only taxonomies which are processed by this function; anything else
  518. * will be returned untouched.
  519. * @return object|array Returns `$term`, after filtering the 'slug' field with {@see sanitize_title()}
  520. * if $taxonomy is 'category' or 'post_tag'.
  521. */
  522. function sync_category_tag_slugs( $term, $taxonomy ) {
  523. if ( global_terms_enabled() && ( $taxonomy == 'category' || $taxonomy == 'post_tag' ) ) {
  524. if ( is_object( $term ) ) {
  525. $term->slug = sanitize_title( $term->name );
  526. } else {
  527. $term['slug'] = sanitize_title( $term['name'] );
  528. }
  529. }
  530. return $term;
  531. }
  532. /**
  533. * Displays an access denied message when a user tries to view a site's dashboard they
  534. * do not have access to.
  535. *
  536. * @since 3.2.0
  537. * @access private
  538. */
  539. function _access_denied_splash() {
  540. if ( ! is_user_logged_in() || is_network_admin() )
  541. return;
  542. $blogs = get_blogs_of_user( get_current_user_id() );
  543. if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) )
  544. return;
  545. $blog_name = get_bloginfo( 'name' );
  546. if ( empty( $blogs ) )
  547. 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 ), 403 );
  548. $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>';
  549. $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>';
  550. $output .= '<h3>' . __('Your Sites') . '</h3>';
  551. $output .= '<table>';
  552. foreach ( $blogs as $blog ) {
  553. $output .= '<tr>';
  554. $output .= "<td>{$blog->blogname}</td>";
  555. $output .= '<td><a href="' . esc_url( get_admin_url( $blog->userblog_id ) ) . '">' . __( 'Visit Dashboard' ) . '</a> | ' .
  556. '<a href="' . esc_url( get_home_url( $blog->userblog_id ) ). '">' . __( 'View Site' ) . '</a></td>';
  557. $output .= '</tr>';
  558. }
  559. $output .= '</table>';
  560. wp_die( $output, 403 );
  561. }
  562. /**
  563. * Checks if the current user has permissions to import new users.
  564. *
  565. * @since 3.0.0
  566. *
  567. * @param string $permission A permission to be checked. Currently not used.
  568. * @return bool True if the user has proper permissions, false if they do not.
  569. */
  570. function check_import_new_users( $permission ) {
  571. if ( !is_super_admin() )
  572. return false;
  573. return true;
  574. }
  575. // See "import_allow_fetch_attachments" and "import_attachment_size_limit" filters too.
  576. /**
  577. * Generates and displays a drop-down of available languages.
  578. *
  579. * @since 3.0.0
  580. *
  581. * @param array $lang_files Optional. An array of the language files. Default empty array.
  582. * @param string $current Optional. The current language code. Default empty.
  583. */
  584. function mu_dropdown_languages( $lang_files = array(), $current = '' ) {
  585. $flag = false;
  586. $output = array();
  587. foreach ( (array) $lang_files as $val ) {
  588. $code_lang = basename( $val, '.mo' );
  589. if ( $code_lang == 'en_US' ) { // American English
  590. $flag = true;
  591. $ae = __( 'American English' );
  592. $output[$ae] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $ae . '</option>';
  593. } elseif ( $code_lang == 'en_GB' ) { // British English
  594. $flag = true;
  595. $be = __( 'British English' );
  596. $output[$be] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $be . '</option>';
  597. } else {
  598. $translated = format_code_lang( $code_lang );
  599. $output[$translated] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . esc_html ( $translated ) . '</option>';
  600. }
  601. }
  602. if ( $flag === false ) // WordPress english
  603. $output[] = '<option value=""' . selected( $current, '', false ) . '>' . __( 'English' ) . "</option>";
  604. // Order by name
  605. uksort( $output, 'strnatcasecmp' );
  606. /**
  607. * Filter the languages available in the dropdown.
  608. *
  609. * @since MU
  610. *
  611. * @param array $output HTML output of the dropdown.
  612. * @param array $lang_files Available language files.
  613. * @param string $current The current language code.
  614. */
  615. $output = apply_filters( 'mu_dropdown_languages', $output, $lang_files, $current );
  616. echo implode( "\n\t", $output );
  617. }
  618. /**
  619. * Displays an admin notice to upgrade all sites after a core upgrade.
  620. *
  621. * @since 3.0.0
  622. *
  623. * @global int $wp_db_version The version number of the database.
  624. *
  625. * @return false False if the current user is not a super admin.
  626. */
  627. function site_admin_notice() {
  628. global $wp_db_version;
  629. if ( !is_super_admin() )
  630. return false;
  631. if ( get_site_option( 'wpmu_upgrade_site' ) != $wp_db_version )
  632. echo "<div class='update-nag'>" . sprintf( __( 'Thank you for Updating! Please visit the <a href="%s">Upgrade Network</a> page to update all your sites.' ), esc_url( network_admin_url( 'upgrade.php' ) ) ) . "</div>";
  633. }
  634. /**
  635. * Avoids a collision between a site slug and a permalink slug.
  636. *
  637. * In a subdirectory install this will make sure that a site and a post do not use the
  638. * same subdirectory by checking for a site with the same name as a new post.
  639. *
  640. * @since 3.0.0
  641. *
  642. * @param array $data An array of post data.
  643. * @param array $postarr An array of posts. Not currently used.
  644. * @return array The new array of post data after checking for collisions.
  645. */
  646. function avoid_blog_page_permalink_collision( $data, $postarr ) {
  647. if ( is_subdomain_install() )
  648. return $data;
  649. if ( $data['post_type'] != 'page' )
  650. return $data;
  651. if ( !isset( $data['post_name'] ) || $data['post_name'] == '' )
  652. return $data;
  653. if ( !is_main_site() )
  654. return $data;
  655. $post_name = $data['post_name'];
  656. $c = 0;
  657. while( $c < 10 && get_id_from_blogname( $post_name ) ) {
  658. $post_name .= mt_rand( 1, 10 );
  659. $c ++;
  660. }
  661. if ( $post_name != $data['post_name'] ) {
  662. $data['post_name'] = $post_name;
  663. }
  664. return $data;
  665. }
  666. /**
  667. * Handles the display of choosing a user's primary site.
  668. *
  669. * This displays the user's primary site and allows the user to choose
  670. * which site is primary.
  671. *
  672. * @since 3.0.0
  673. */
  674. function choose_primary_blog() {
  675. ?>
  676. <table class="form-table">
  677. <tr>
  678. <?php /* translators: My sites label */ ?>
  679. <th scope="row"><label for="primary_blog"><?php _e( 'Primary Site' ); ?></label></th>
  680. <td>
  681. <?php
  682. $all_blogs = get_blogs_of_user( get_current_user_id() );
  683. $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
  684. if ( count( $all_blogs ) > 1 ) {
  685. $found = false;
  686. ?>
  687. <select name="primary_blog" id="primary_blog">
  688. <?php foreach( (array) $all_blogs as $blog ) {
  689. if ( $primary_blog == $blog->userblog_id )
  690. $found = true;
  691. ?><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
  692. } ?>
  693. </select>
  694. <?php
  695. if ( !$found ) {
  696. $blog = reset( $all_blogs );
  697. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  698. }
  699. } elseif ( count( $all_blogs ) == 1 ) {
  700. $blog = reset( $all_blogs );
  701. echo esc_url( get_home_url( $blog->userblog_id ) );
  702. if ( $primary_blog != $blog->userblog_id ) // Set the primary blog again if it's out of sync with blog list.
  703. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  704. } else {
  705. echo "N/A";
  706. }
  707. ?>
  708. </td>
  709. </tr>
  710. <?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
  711. <tr>
  712. <th scope="row" colspan="2" class="th-full">
  713. <?php
  714. /** This filter is documented in wp-login.php */
  715. $sign_up_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
  716. ?>
  717. <a href="<?php echo esc_url( $sign_up_url ); ?>"><?php _e( 'Create a New Site' ); ?></a>
  718. </th>
  719. </tr>
  720. <?php endif; ?>
  721. </table>
  722. <?php
  723. }
  724. /**
  725. * Grants Super Admin privileges.
  726. *
  727. * @since 3.0.0
  728. *
  729. * @global array $super_admins
  730. *
  731. * @param int $user_id ID of the user to be granted Super Admin privileges.
  732. * @return bool True on success, false on failure. This can fail when the user is
  733. * already a super admin or when the `$super_admins` global is defined.
  734. */
  735. function grant_super_admin( $user_id ) {
  736. // If global super_admins override is defined, there is nothing to do here.
  737. if ( isset( $GLOBALS['super_admins'] ) ) {
  738. return false;
  739. }
  740. /**
  741. * Fires before the user is granted Super Admin privileges.
  742. *
  743. * @since 3.0.0
  744. *
  745. * @param int $user_id ID of the user that is about to be granted Super Admin privileges.
  746. */
  747. do_action( 'grant_super_admin', $user_id );
  748. // Directly fetch site_admins instead of using get_super_admins()
  749. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  750. $user = get_userdata( $user_id );
  751. if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
  752. $super_admins[] = $user->user_login;
  753. update_site_option( 'site_admins' , $super_admins );
  754. /**
  755. * Fires after the user is granted Super Admin privileges.
  756. *
  757. * @since 3.0.0
  758. *
  759. * @param int $user_id ID of the user that was granted Super Admin privileges.
  760. */
  761. do_action( 'granted_super_admin', $user_id );
  762. return true;
  763. }
  764. return false;
  765. }
  766. /**
  767. * Revokes Super Admin privileges.
  768. *
  769. * @since 3.0.0
  770. *
  771. * @global array $super_admins
  772. *
  773. * @param int $user_id ID of the user Super Admin privileges to be revoked from.
  774. * @return bool True on success, false on failure. This can fail when the user's email
  775. * is the network admin email or when the `$super_admins` global is defined.
  776. */
  777. function revoke_super_admin( $user_id ) {
  778. // If global super_admins override is defined, there is nothing to do here.
  779. if ( isset( $GLOBALS['super_admins'] ) ) {
  780. return false;
  781. }
  782. /**
  783. * Fires before the user's Super Admin privileges are revoked.
  784. *
  785. * @since 3.0.0
  786. *
  787. * @param int $user_id ID of the user Super Admin privileges are being revoked from.
  788. */
  789. do_action( 'revoke_super_admin', $user_id );
  790. // Directly fetch site_admins instead of using get_super_admins()
  791. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  792. $user = get_userdata( $user_id );
  793. if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
  794. if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
  795. unset( $super_admins[$key] );
  796. update_site_option( 'site_admins', $super_admins );
  797. /**
  798. * Fires after the user's Super Admin privileges are revoked.
  799. *
  800. * @since 3.0.0
  801. *
  802. * @param int $user_id ID of the user Super Admin privileges were revoked from.
  803. */
  804. do_action( 'revoked_super_admin', $user_id );
  805. return true;
  806. }
  807. }
  808. return false;
  809. }
  810. /**
  811. * Whether or not we can edit this network from this page.
  812. *
  813. * By default editing of network is restricted to the Network Admin for that `$site_id`
  814. * this allows for this to be overridden.
  815. *
  816. * @since 3.1.0
  817. *
  818. * @global wpdb $wpdb
  819. *
  820. * @param int $site_id The network/site ID to check.
  821. * @return bool True if network can be edited, otherwise false.
  822. */
  823. function can_edit_network( $site_id ) {
  824. global $wpdb;
  825. if ( $site_id == $wpdb->siteid )
  826. $result = true;
  827. else
  828. $result = false;
  829. /**
  830. * Filter whether this network can be edited from this page.
  831. *
  832. * @since 3.1.0
  833. *
  834. * @param bool $result Whether the network can be edited from this page.
  835. * @param int $site_id The network/site ID to check.
  836. */
  837. return apply_filters( 'can_edit_network', $result, $site_id );
  838. }
  839. /**
  840. * Thickbox image paths for Network Admin.
  841. *
  842. * @since 3.1.0
  843. *
  844. * @access private
  845. */
  846. function _thickbox_path_admin_subfolder() {
  847. ?>
  848. <script type="text/javascript">
  849. var tb_pathToImage = "<?php echo includes_url( 'js/thickbox/loadingAnimation.gif', 'relative' ); ?>";
  850. </script>
  851. <?php
  852. }