[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> ms.php (source)

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


Generated : Fri Mar 29 08:20:02 2024 Cross-referenced by PHPXref