PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/ms.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 895 lines | 682 code | 128 blank | 85 comment | 174 complexity | a423dc786f4569ee094020469581e22e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.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_allowed = 1048576 * get_space_allowed();
  25. $space_used = get_dirsize( BLOGUPLOADDIR );
  26. $space_left = $space_allowed - $space_used;
  27. $file_size = filesize( $file['tmp_name'] );
  28. if ( $space_left < $file_size )
  29. $file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) );
  30. if ( $file_size > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
  31. $file['error'] = sprintf(__('This file is too big. Files must be less than %1$s KB in size.'), get_site_option( 'fileupload_maxk', 1500 ) );
  32. if ( upload_is_user_over_quota( false ) ) {
  33. $file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
  34. }
  35. if ( $file['error'] != '0' && !isset($_POST['html-upload']) )
  36. wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
  37. return $file;
  38. }
  39. add_filter( 'wp_handle_upload_prefilter', 'check_upload_size' );
  40. /**
  41. * Delete a blog
  42. *
  43. * @since 3.0.0
  44. *
  45. * @param int $blog_id Blog ID
  46. * @param bool $drop True if blog's table should be dropped. Default is false.
  47. * @return void
  48. */
  49. function wpmu_delete_blog( $blog_id, $drop = false ) {
  50. global $wpdb;
  51. $switch = false;
  52. if ( $blog_id != $wpdb->blogid ) {
  53. $switch = true;
  54. switch_to_blog( $blog_id );
  55. }
  56. $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
  57. do_action( 'delete_blog', $blog_id, $drop );
  58. $users = get_users_of_blog( $blog_id );
  59. // Remove users from this blog.
  60. if ( ! empty( $users ) ) {
  61. foreach ( $users as $user ) {
  62. remove_user_from_blog( $user->user_id, $blog_id) ;
  63. }
  64. }
  65. update_blog_status( $blog_id, 'deleted', 1 );
  66. if ( $drop ) {
  67. if ( substr( $blog_prefix, -1 ) == '_' )
  68. $blog_prefix = substr( $blog_prefix, 0, -1 ) . '\_';
  69. $drop_tables = $wpdb->get_results( "SHOW TABLES LIKE '{$blog_prefix}%'", ARRAY_A );
  70. $drop_tables = apply_filters( 'wpmu_drop_tables', $drop_tables );
  71. reset( $drop_tables );
  72. foreach ( (array) $drop_tables as $drop_table) {
  73. $wpdb->query( "DROP TABLE IF EXISTS ". current( $drop_table ) ."" );
  74. }
  75. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
  76. $dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id );
  77. $dir = rtrim( $dir, DIRECTORY_SEPARATOR );
  78. $top_dir = $dir;
  79. $stack = array($dir);
  80. $index = 0;
  81. while ( $index < count( $stack ) ) {
  82. # Get indexed directory from stack
  83. $dir = $stack[$index];
  84. $dh = @opendir( $dir );
  85. if ( $dh ) {
  86. while ( ( $file = @readdir( $dh ) ) !== false ) {
  87. if ( $file == '.' || $file == '..' )
  88. continue;
  89. if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) )
  90. $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
  91. else if ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) )
  92. @unlink( $dir . DIRECTORY_SEPARATOR . $file );
  93. }
  94. }
  95. $index++;
  96. }
  97. $stack = array_reverse( $stack ); // Last added dirs are deepest
  98. foreach( (array) $stack as $dir ) {
  99. if ( $dir != $top_dir)
  100. @rmdir( $dir );
  101. }
  102. }
  103. $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = '{$blog_prefix}autosave_draft_ids'" );
  104. $blogs = get_site_option( 'blog_list' );
  105. if ( is_array( $blogs ) ) {
  106. foreach ( $blogs as $n => $blog ) {
  107. if ( $blog['blog_id'] == $blog_id )
  108. unset( $blogs[$n] );
  109. }
  110. update_site_option( 'blog_list', $blogs );
  111. }
  112. if ( $switch === true )
  113. restore_current_blog();
  114. }
  115. // @todo Merge with wp_delete_user() ?
  116. function wpmu_delete_user( $id ) {
  117. global $wpdb;
  118. $id = (int) $id;
  119. do_action( 'wpmu_delete_user', $id );
  120. $blogs = get_blogs_of_user( $id );
  121. if ( ! empty( $blogs ) ) {
  122. foreach ( $blogs as $blog ) {
  123. switch_to_blog( $blog->userblog_id );
  124. remove_user_from_blog( $id, $blog->userblog_id );
  125. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
  126. foreach ( (array) $post_ids as $post_id ) {
  127. wp_delete_post( $post_id );
  128. }
  129. // Clean links
  130. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
  131. if ( $link_ids ) {
  132. foreach ( $link_ids as $link_id )
  133. wp_delete_link( $link_id );
  134. }
  135. restore_current_blog();
  136. }
  137. }
  138. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->users WHERE ID = %d", $id ) );
  139. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  140. clean_user_cache( $id );
  141. // allow for commit transaction
  142. do_action( 'deleted_user', $id );
  143. return true;
  144. }
  145. function confirm_delete_users( $users ) {
  146. $current_user = wp_get_current_user();
  147. if ( !is_array( $users ) )
  148. return false;
  149. screen_icon();
  150. ?>
  151. <h2><?php esc_html_e( 'Users' ); ?></h2>
  152. <p><?php _e( 'Transfer or delete posts and links before deleting users.' ); ?></p>
  153. <form action="ms-edit.php?action=dodelete" method="post">
  154. <input type="hidden" name="dodelete" />
  155. <?php
  156. wp_nonce_field( 'ms-users-delete' );
  157. $site_admins = get_super_admins();
  158. $admin_out = "<option value='$current_user->ID'>$current_user->user_login</option>";
  159. foreach ( ( $allusers = (array) $_POST['allusers'] ) as $key => $val ) {
  160. if ( $val != '' && $val != '0' ) {
  161. $delete_user = new WP_User( $val );
  162. if ( in_array( $delete_user->user_login, $site_admins ) )
  163. wp_die( sprintf( __( 'Warning! User cannot be deleted. The user %s is a network admnistrator.' ), $delete_user->user_login ) );
  164. echo "<input type='hidden' name='user[]' value='{$val}'/>\n";
  165. $blogs = get_blogs_of_user( $val, true );
  166. if ( !empty( $blogs ) ) {
  167. ?>
  168. <br /><fieldset><p><legend><?php printf( __( "What should be done with posts and links owned by <em>%s</em>?" ), $delete_user->user_login ); ?></legend></p>
  169. <?php
  170. foreach ( (array) $blogs as $key => $details ) {
  171. $blog_users = get_users_of_blog( $details->userblog_id );
  172. if ( is_array( $blog_users ) && !empty( $blog_users ) ) {
  173. $user_site = "<a href='" . esc_url( get_home_url( $details->userblog_id ) ) . "'>{$details->blogname}</a>";
  174. $user_dropdown = "<select name='blog[$val][{$key}]'>";
  175. $user_list = '';
  176. foreach ( $blog_users as $user ) {
  177. if ( $user->user_id != $val && !in_array( $user->user_id, $allusers ) )
  178. $user_list .= "<option value='{$user->user_id}'>{$user->user_login}</option>";
  179. }
  180. if ( '' == $user_list )
  181. $user_list = $admin_out;
  182. $user_dropdown .= $user_list;
  183. $user_dropdown .= "</select>\n";
  184. ?>
  185. <ul style="list-style:none;">
  186. <li><?php printf( __( 'Site: %s' ), $user_site ); ?></li>
  187. <li><label><input type="radio" id="delete_option0" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID ?>]" value="delete" checked="checked" />
  188. <?php _e( 'Delete all posts and links.' ); ?></label></li>
  189. <li><label><input type="radio" id="delete_option1" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID ?>]" value="reassign" />
  190. <?php echo __( 'Attribute all posts and links to:' ) . '</label>' . $user_dropdown; ?></li>
  191. </ul>
  192. <?php
  193. }
  194. }
  195. echo "</fieldset>";
  196. }
  197. }
  198. }
  199. ?>
  200. <p class="submit"><input type="submit" class="button-secondary delete" value="<?php esc_attr_e( 'Confirm Deletion' ); ?>" /></p>
  201. </form>
  202. <?php
  203. return true;
  204. }
  205. function wpmu_get_blog_allowedthemes( $blog_id = 0 ) {
  206. $themes = get_themes();
  207. if ( $blog_id != 0 )
  208. switch_to_blog( $blog_id );
  209. $blog_allowed_themes = get_option( 'allowedthemes' );
  210. if ( !is_array( $blog_allowed_themes ) || empty( $blog_allowed_themes ) ) { // convert old allowed_themes to new allowedthemes
  211. $blog_allowed_themes = get_option( 'allowed_themes' );
  212. if ( is_array( $blog_allowed_themes ) ) {
  213. foreach( (array) $themes as $key => $theme ) {
  214. $theme_key = esc_html( $theme['Stylesheet'] );
  215. if ( isset( $blog_allowed_themes[$key] ) == true ) {
  216. $blog_allowedthemes[$theme_key] = 1;
  217. }
  218. }
  219. $blog_allowed_themes = $blog_allowedthemes;
  220. add_option( 'allowedthemes', $blog_allowed_themes );
  221. delete_option( 'allowed_themes' );
  222. }
  223. }
  224. if ( $blog_id != 0 )
  225. restore_current_blog();
  226. return $blog_allowed_themes;
  227. }
  228. function update_option_new_admin_email( $old_value, $value ) {
  229. $email = get_option( 'admin_email' );
  230. if ( $value == get_option( 'admin_email' ) || !is_email( $value ) )
  231. return;
  232. $hash = md5( $value. time() .mt_rand() );
  233. $new_admin_email = array(
  234. 'hash' => $hash,
  235. 'newemail' => $value
  236. );
  237. update_option( 'adminhash', $new_admin_email );
  238. $content = apply_filters( 'new_admin_email_content', __( "Dear user,
  239. You recently requested to have the administration email address on
  240. your site changed.
  241. If this is correct, please click on the following link to change it:
  242. ###ADMIN_URL###
  243. You can safely ignore and delete this email if you do not want to
  244. take this action.
  245. This email has been sent to ###EMAIL###
  246. Regards,
  247. All at ###SITENAME###
  248. ###SITEURL### "), $new_admin_email );
  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' ), get_option( 'blogname' ) ), $content );
  254. }
  255. add_action( 'update_option_new_admin_email', 'update_option_new_admin_email', 10, 2 );
  256. add_action( 'add_option_new_admin_email', 'update_option_new_admin_email', 10, 2 );
  257. function send_confirmation_on_profile_email() {
  258. global $errors, $wpdb;
  259. $current_user = wp_get_current_user();
  260. if ( ! is_object($errors) )
  261. $errors = new WP_Error();
  262. if ( $current_user->id != $_POST['user_id'] )
  263. return false;
  264. if ( $current_user->user_email != $_POST['email'] ) {
  265. if ( !is_email( $_POST['email'] ) ) {
  266. $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
  267. return;
  268. }
  269. if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
  270. $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
  271. delete_option( $current_user->ID . '_new_email' );
  272. return;
  273. }
  274. $hash = md5( $_POST['email'] . time() . mt_rand() );
  275. $new_user_email = array(
  276. 'hash' => $hash,
  277. 'newemail' => $_POST['email']
  278. );
  279. update_option( $current_user->ID . '_new_email', $new_user_email );
  280. $content = apply_filters( 'new_user_email_content', __( "Dear user,
  281. You recently requested to have the email address on your account changed.
  282. If this is correct, please click on the following link to change it:
  283. ###ADMIN_URL###
  284. You can safely ignore and delete this email if you do not want to
  285. take this action.
  286. This email has been sent to ###EMAIL###
  287. Regards,
  288. All at ###SITENAME###
  289. ###SITEURL###" ), $new_user_email );
  290. $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
  291. $content = str_replace( '###EMAIL###', $_POST['email'], $content);
  292. $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
  293. $content = str_replace( '###SITEURL###', network_home_url(), $content );
  294. wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), get_option( 'blogname' ) ), $content );
  295. $_POST['email'] = $current_user->user_email;
  296. }
  297. }
  298. add_action( 'personal_options_update', 'send_confirmation_on_profile_email' );
  299. function new_user_email_admin_notice() {
  300. if ( strpos( $_SERVER['PHP_SELF'], 'profile.php' ) && isset( $_GET['updated'] ) && $email = get_option( get_current_user_id() . '_new_email' ) )
  301. 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>";
  302. }
  303. add_action( 'admin_notices', 'new_user_email_admin_notice' );
  304. function get_site_allowed_themes() {
  305. $themes = get_themes();
  306. $allowed_themes = get_site_option( 'allowedthemes' );
  307. if ( !is_array( $allowed_themes ) || empty( $allowed_themes ) ) {
  308. $allowed_themes = get_site_option( 'allowed_themes' ); // convert old allowed_themes format
  309. if ( !is_array( $allowed_themes ) ) {
  310. $allowed_themes = array();
  311. } else {
  312. foreach( (array) $themes as $key => $theme ) {
  313. $theme_key = esc_html( $theme['Stylesheet'] );
  314. if ( isset( $allowed_themes[ $key ] ) == true ) {
  315. $allowedthemes[ $theme_key ] = 1;
  316. }
  317. }
  318. $allowed_themes = $allowedthemes;
  319. }
  320. }
  321. return $allowed_themes;
  322. }
  323. /**
  324. * Determines if there is any upload space left in the current blog's quota.
  325. *
  326. * @since 3.0.0
  327. * @return bool True if space is available, false otherwise.
  328. */
  329. function is_upload_space_available() {
  330. if ( get_site_option( 'upload_space_check_disabled' ) )
  331. return true;
  332. if ( !( $space_allowed = get_upload_space_available() ) )
  333. return false;
  334. return true;
  335. }
  336. /*
  337. * @since 3.0.0
  338. *
  339. * @return int of upload size limit in bytes
  340. */
  341. function upload_size_limit_filter( $size ) {
  342. $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
  343. return min( $size, $fileupload_maxk, get_upload_space_available() );
  344. }
  345. /**
  346. * Determines if there is any upload space left in the current blog's quota.
  347. *
  348. * @return int of upload space available in bytes
  349. */
  350. function get_upload_space_available() {
  351. $space_allowed = get_space_allowed() * 1024 * 1024;
  352. if ( get_site_option( 'upload_space_check_disabled' ) )
  353. return $space_allowed;
  354. $dir_name = trailingslashit( BLOGUPLOADDIR );
  355. if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) )
  356. return $space_allowed;
  357. $dir = dir( $dir_name );
  358. $size = 0;
  359. while ( $file = $dir->read() ) {
  360. if ( $file != '.' && $file != '..' ) {
  361. if ( is_dir( $dir_name . $file) ) {
  362. $size += get_dirsize( $dir_name . $file );
  363. } else {
  364. $size += filesize( $dir_name . $file );
  365. }
  366. }
  367. }
  368. $dir->close();
  369. if ( ( $space_allowed - $size ) <= 0 )
  370. return 0;
  371. return $space_allowed - $size;
  372. }
  373. /**
  374. * Returns the upload quota for the current blog.
  375. *
  376. * @return int Quota
  377. */
  378. function get_space_allowed() {
  379. $space_allowed = get_option( 'blog_upload_space' );
  380. if ( $space_allowed == false )
  381. $space_allowed = get_site_option( 'blog_upload_space' );
  382. if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
  383. $space_allowed = 50;
  384. return $space_allowed;
  385. }
  386. function display_space_usage() {
  387. $space = get_space_allowed();
  388. $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
  389. $percentused = ( $used / $space ) * 100;
  390. if ( $space > 1000 ) {
  391. $space = number_format( $space / 1024 );
  392. /* translators: Gigabytes */
  393. $space .= __( 'GB' );
  394. } else {
  395. /* translators: Megabytes */
  396. $space .= __( 'MB' );
  397. }
  398. ?>
  399. <strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percentused ), $space ); ?></strong>
  400. <?php
  401. }
  402. // Display File upload quota on dashboard
  403. function dashboard_quota() {
  404. if ( get_site_option( 'upload_space_check_disabled' ) )
  405. return true;
  406. $quota = get_space_allowed();
  407. $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
  408. if ( $used > $quota )
  409. $percentused = '100';
  410. else
  411. $percentused = ( $used / $quota ) * 100;
  412. $used_color = ( $percentused < 70 ) ? ( ( $percentused >= 40 ) ? 'waiting' : 'approved' ) : 'spam';
  413. $used = round( $used, 2 );
  414. $percentused = number_format( $percentused );
  415. ?>
  416. <p class="sub musub"><?php _e( 'Storage Space' ); ?></p>
  417. <div class="table table_content musubtable">
  418. <table>
  419. <tr class="first">
  420. <td class="first b b-posts"><?php printf( __( '<a href="%1$s" title="Manage Uploads" class="musublink">%2$sMB</a>' ), esc_url( admin_url( 'upload.php' ) ), $quota ); ?></td>
  421. <td class="t posts"><?php _e( 'Space Allowed' ); ?></td>
  422. </tr>
  423. </table>
  424. </div>
  425. <div class="table table_discussion musubtable">
  426. <table>
  427. <tr class="first">
  428. <td class="b b-comments"><?php printf( __( '<a href="%1$s" title="Manage Uploads" class="musublink">%2$sMB (%3$s%%)</a>' ), esc_url( admin_url( 'upload.php' ) ), $used, $percentused ); ?></td>
  429. <td class="last t comments <?php echo $used_color;?>"><?php _e( 'Space Used' );?></td>
  430. </tr>
  431. </table>
  432. </div>
  433. <br class="clear" />
  434. <?php
  435. }
  436. if ( current_user_can( 'edit_posts' ) )
  437. add_action( 'activity_box_end', 'dashboard_quota' );
  438. // Edit blog upload space setting on Edit Blog page
  439. function upload_space_setting( $id ) {
  440. $quota = get_blog_option( $id, 'blog_upload_space' );
  441. if ( !$quota )
  442. $quota = '';
  443. ?>
  444. <tr>
  445. <th><?php _e( 'Site Upload Space Quota '); ?></th>
  446. <td><input type="text" size="3" name="option[blog_upload_space]" value="<?php echo $quota; ?>" /> <?php _e( 'MB (Leave blank for network default)' ); ?></td>
  447. </tr>
  448. <?php
  449. }
  450. add_action( 'wpmueditblogaction', 'upload_space_setting' );
  451. function update_user_status( $id, $pref, $value, $refresh = 1 ) {
  452. global $wpdb;
  453. $wpdb->update( $wpdb->users, array( $pref => $value ), array( 'ID' => $id ) );
  454. if ( $refresh == 1 )
  455. refresh_user_details( $id );
  456. if ( $pref == 'spam' ) {
  457. if ( $value == 1 )
  458. do_action( 'make_spam_user', $id );
  459. else
  460. do_action( 'make_ham_user', $id );
  461. }
  462. return $value;
  463. }
  464. function refresh_user_details( $id ) {
  465. $id = (int) $id;
  466. if ( !$user = get_userdata( $id ) )
  467. return false;
  468. clean_user_cache( $id );
  469. return $id;
  470. }
  471. function format_code_lang( $code = '' ) {
  472. $code = strtolower( substr( $code, 0, 2 ) );
  473. $lang_codes = array(
  474. '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',
  475. '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',
  476. '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',
  477. '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',
  478. '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',
  479. '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',
  480. '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',
  481. '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',
  482. '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',
  483. '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',
  484. 've' => 'Venda', 'vi' => 'Vietnamese', 'vo' => 'Volapük', 'cy' => 'Welsh','wa' => 'Walloon','wo' => 'Wolof', 'xh' => 'Xhosa', 'yi' => 'Yiddish', 'yo' => 'Yoruba', 'za' => 'Zhuang; Chuang', 'zu' => 'Zulu' );
  485. $lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
  486. return strtr( $code, $lang_codes );
  487. }
  488. function sync_category_tag_slugs( $term, $taxonomy ) {
  489. if ( global_terms_enabled() && ( $taxonomy == 'category' || $taxonomy == 'post_tag' ) ) {
  490. if ( is_object( $term ) ) {
  491. $term->slug = sanitize_title( $term->name );
  492. } else {
  493. $term['slug'] = sanitize_title( $term['name'] );
  494. }
  495. }
  496. return $term;
  497. }
  498. add_filter( 'get_term', 'sync_category_tag_slugs', 10, 2 );
  499. function redirect_user_to_blog() {
  500. $c = 0;
  501. if ( isset( $_GET['c'] ) )
  502. $c = (int) $_GET['c'];
  503. if ( $c >= 5 ) {
  504. wp_die( __( "You don&#8217;t have permission to view this site. Please contact the system administrator." ) );
  505. }
  506. $c ++;
  507. $blog = get_active_blog_for_user( get_current_user_id() );
  508. $dashboard_blog = get_dashboard_blog();
  509. if ( is_object( $blog ) ) {
  510. wp_redirect( get_admin_url( $blog->blog_id, '?c=' . $c ) ); // redirect and count to 5, "just in case"
  511. exit;
  512. }
  513. /*
  514. If the user is a member of only 1 blog and the user's primary_blog isn't set to that blog,
  515. then update the primary_blog record to match the user's blog
  516. */
  517. $blogs = get_blogs_of_user( get_current_user_id() );
  518. if ( !empty( $blogs ) ) {
  519. foreach( $blogs as $blogid => $blog ) {
  520. if ( $blogid != $dashboard_blog->blog_id && get_user_meta( get_current_user_id() , 'primary_blog', true ) == $dashboard_blog->blog_id ) {
  521. update_user_meta( get_current_user_id(), 'primary_blog', $blogid );
  522. continue;
  523. }
  524. }
  525. $blog = get_blog_details( get_user_meta( get_current_user_id(), 'primary_blog', true ) );
  526. wp_redirect( get_admin_url( $blog->blog_id, '?c=' . $c ) );
  527. exit;
  528. }
  529. wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  530. }
  531. add_action( 'admin_page_access_denied', 'redirect_user_to_blog', 99 );
  532. function check_import_new_users( $permission ) {
  533. if ( !is_super_admin() )
  534. return false;
  535. return true;
  536. }
  537. add_filter( 'import_allow_create_users', 'check_import_new_users' );
  538. // See "import_allow_fetch_attachments" and "import_attachment_size_limit" filters too.
  539. function mu_dropdown_languages( $lang_files = array(), $current = '' ) {
  540. $flag = false;
  541. $output = array();
  542. foreach ( (array) $lang_files as $val ) {
  543. $code_lang = basename( $val, '.mo' );
  544. if ( $code_lang == 'en_US' ) { // American English
  545. $flag = true;
  546. $ae = __( 'American English' );
  547. $output[$ae] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $ae . '</option>';
  548. } elseif ( $code_lang == 'en_GB' ) { // British English
  549. $flag = true;
  550. $be = __( 'British English' );
  551. $output[$be] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . $be . '</option>';
  552. } else {
  553. $translated = format_code_lang( $code_lang );
  554. $output[$translated] = '<option value="' . esc_attr( $code_lang ) . '"' . selected( $current, $code_lang, false ) . '> ' . esc_html ( $translated ) . '</option>';
  555. }
  556. }
  557. if ( $flag === false ) // WordPress english
  558. $output[] = '<option value=""' . selected( $current, '', false ) . '>' . __( 'English' ) . "</option>";
  559. // Order by name
  560. uksort( $output, 'strnatcasecmp' );
  561. $output = apply_filters( 'mu_dropdown_languages', $output, $lang_files, $current );
  562. echo implode( "\n\t", $output );
  563. }
  564. /* Warn the admin if SECRET SALT information is missing from wp-config.php */
  565. function secret_salt_warning() {
  566. if ( !is_super_admin() )
  567. return;
  568. $secret_keys = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT' );
  569. $out = '';
  570. foreach( $secret_keys as $key ) {
  571. if ( ! defined( $key ) )
  572. $out .= "define( '$key', '" . esc_html( wp_generate_password( 64, true, true ) ) . "' );<br />";
  573. }
  574. if ( $out != '' ) {
  575. $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.' );
  576. $msg .= '<br/>' . __( "Before the line <code>/* That's all, stop editing! Happy blogging. */</code> please add this code:" );
  577. $msg .= "<br/><br/><code>$out</code>";
  578. echo "<div class='update-nag'>$msg</div>";
  579. }
  580. }
  581. add_action( 'admin_notices', 'secret_salt_warning' );
  582. function admin_notice_feed() {
  583. global $current_screen;
  584. if ( $current_screen->id != 'dashboard' )
  585. return;
  586. if ( !empty( $_GET['feed_dismiss'] ) ) {
  587. update_user_option( get_current_user_id(), 'admin_feed_dismiss', $_GET['feed_dismiss'], true );
  588. return;
  589. }
  590. $url = get_site_option( 'admin_notice_feed' );
  591. if ( empty( $url ) )
  592. return;
  593. $rss = fetch_feed( $url );
  594. if ( ! is_wp_error( $rss ) && $item = $rss->get_item() ) {
  595. $title = $item->get_title();
  596. if ( md5( $title ) == get_user_option( 'admin_feed_dismiss' ) )
  597. return;
  598. $msg = "<h3>" . esc_html( $title ) . "</h3>\n";
  599. $content = $item->get_description();
  600. $content = $content ? wp_html_excerpt( $content, 200 ) . ' &hellip; ' : '';
  601. $link = esc_url( strip_tags( $item->get_link() ) );
  602. $msg .= "<p>" . $content . "<a href='$link'>" . __( 'Read More' ) . "</a> <a href='index.php?feed_dismiss=" . md5( $title ) . "'>" . __( 'Dismiss' ) . "</a></p>";
  603. echo "<div class='updated'>$msg</div>";
  604. } elseif ( is_super_admin() ) {
  605. printf( '<div class="update-nag">' . __( 'Your feed at %s is empty.' ) . '</div>', esc_html( $url ) );
  606. }
  607. }
  608. add_action( 'admin_notices', 'admin_notice_feed' );
  609. function site_admin_notice() {
  610. global $wp_db_version;
  611. if ( !is_super_admin() )
  612. return false;
  613. if ( get_site_option( 'wpmu_upgrade_site' ) != $wp_db_version )
  614. 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( admin_url( 'ms-upgrade-network.php' ) ) ) . "</div>";
  615. }
  616. add_action( 'admin_notices', 'site_admin_notice' );
  617. function avoid_blog_page_permalink_collision( $data, $postarr ) {
  618. if ( is_subdomain_install() )
  619. return $data;
  620. if ( $data['post_type'] != 'page' )
  621. return $data;
  622. if ( !isset( $data['post_name'] ) || $data['post_name'] == '' )
  623. return $data;
  624. if ( !is_main_site() )
  625. return $data;
  626. $post_name = $data['post_name'];
  627. $c = 0;
  628. while( $c < 10 && get_id_from_blogname( $post_name ) ) {
  629. $post_name .= mt_rand( 1, 10 );
  630. $c ++;
  631. }
  632. if ( $post_name != $data['post_name'] ) {
  633. $data['post_name'] = $post_name;
  634. }
  635. return $data;
  636. }
  637. add_filter( 'wp_insert_post_data', 'avoid_blog_page_permalink_collision', 10, 2 );
  638. function choose_primary_blog() {
  639. ?>
  640. <table class="form-table">
  641. <tr>
  642. <?php /* translators: My sites label */ ?>
  643. <th scope="row"><?php _e( 'Primary Site' ); ?></th>
  644. <td>
  645. <?php
  646. $all_blogs = get_blogs_of_user( get_current_user_id() );
  647. $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
  648. if ( count( $all_blogs ) > 1 ) {
  649. $found = false;
  650. ?>
  651. <select name="primary_blog">
  652. <?php foreach( (array) $all_blogs as $blog ) {
  653. if ( $primary_blog == $blog->userblog_id )
  654. $found = true;
  655. ?><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
  656. } ?>
  657. </select>
  658. <?php
  659. if ( !$found ) {
  660. $blog = array_shift( $all_blogs );
  661. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  662. }
  663. } elseif ( count( $all_blogs ) == 1 ) {
  664. $blog = array_shift( $all_blogs );
  665. echo $blog->domain;
  666. if ( $primary_blog != $blog->userblog_id ) // Set the primary blog again if it's out of sync with blog list.
  667. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
  668. } else {
  669. echo "N/A";
  670. }
  671. ?>
  672. </td>
  673. </tr>
  674. <?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
  675. <tr>
  676. <th scope="row" colspan="2" class="th-full">
  677. <a href="<?php echo apply_filters( 'wp_signup_location', network_home_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
  678. </th>
  679. </tr>
  680. <?php endif; ?>
  681. </table>
  682. <?php
  683. }
  684. function show_post_thumbnail_warning() {
  685. if ( ! is_super_admin() )
  686. return;
  687. $mu_media_buttons = get_site_option( 'mu_media_buttons', array() );
  688. if ( empty($mu_media_buttons['image']) && current_theme_supports( 'post-thumbnails' ) ) {
  689. echo "<div class='update-nag'>" . sprintf( __( "Warning! The current theme supports Featured Images. You must enable image uploads on <a href='%s'>the options page</a> for it to work." ), esc_url( admin_url( 'ms-options.php' ) ) ) . "</div>";
  690. }
  691. }
  692. add_action( 'admin_notices', 'show_post_thumbnail_warning' );
  693. function ms_deprecated_blogs_file() {
  694. if ( ! is_super_admin() )
  695. return;
  696. if ( ! file_exists( WP_CONTENT_DIR . '/blogs.php' ) )
  697. return;
  698. echo '<div class="update-nag">' . sprintf( __( 'The <code>%1$s</code> file is deprecated. Please remove it and update your server rewrite rules to use <code>%2$s</code> instead.' ), 'wp-content/blogs.php', 'wp-includes/ms-files.php' ) . '</div>';
  699. }
  700. add_action( 'admin_notices', 'ms_deprecated_blogs_file' );
  701. /**
  702. * Outputs the notice message for multisite regarding activation of plugin page.
  703. *
  704. * @since 3.0.0
  705. * @return none
  706. */
  707. function _admin_notice_multisite_activate_plugins_page() {
  708. $message = sprintf( __( 'The plugins page is not visible to normal users. It must be activated first. %s' ), '<a href="' . esc_url( admin_url( 'ms-options.php#menu' ) ) . '">' . __( 'Activate' ) . '</a>' );
  709. echo "<div class='error'><p>$message</p></div>";
  710. }
  711. /**
  712. * Grants super admin privileges.
  713. *
  714. * @since 3.0.0
  715. * @param $user_id
  716. */
  717. function grant_super_admin( $user_id ) {
  718. global $super_admins;
  719. // If global super_admins override is defined, there is nothing to do here.
  720. if ( isset($super_admins) )
  721. return false;
  722. do_action( 'grant_super_admin', $user_id );
  723. // Directly fetch site_admins instead of using get_super_admins()
  724. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  725. $user = new WP_User( $user_id );
  726. if ( ! in_array( $user->user_login, $super_admins ) ) {
  727. $super_admins[] = $user->user_login;
  728. update_site_option( 'site_admins' , $super_admins );
  729. do_action( 'granted_super_admin', $user_id );
  730. return true;
  731. }
  732. return false;
  733. }
  734. /**
  735. * Revokes super admin privileges.
  736. *
  737. * @since 3.0.0
  738. * @param $user_id
  739. */
  740. function revoke_super_admin( $user_id ) {
  741. global $super_admins;
  742. // If global super_admins override is defined, there is nothing to do here.
  743. if ( isset($super_admins) )
  744. return false;
  745. do_action( 'revoke_super_admin', $user_id );
  746. // Directly fetch site_admins instead of using get_super_admins()
  747. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  748. $user = new WP_User( $user_id );
  749. if ( $user->user_email != get_site_option( 'admin_email' ) ) {
  750. if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
  751. unset( $super_admins[$key] );
  752. update_site_option( 'site_admins', $super_admins );
  753. do_action( 'revoked_super_admin', $user_id );
  754. return true;
  755. }
  756. }
  757. return false;
  758. }
  759. ?>