[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> translation-install.php (source)

   1  <?php
   2  /**
   3   * WordPress Translation Installation Administration API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  
  10  /**
  11   * Retrieve translations from WordPress Translation API.
  12   *
  13   * @since 4.0.0
  14   *
  15   * @param string       $type Type of translations. Accepts 'plugins', 'themes', 'core'.
  16   * @param array|object $args Translation API arguments. Optional.
  17   * @return array|WP_Error {
  18   *     On success an associative array of translations, WP_Error on failure.
  19   *
  20   *     @type array $translations {
  21   *         List of translations, each an array of data.
  22   *
  23   *         @type array ...$0 {
  24   *             @type string   $language     Language code.
  25   *             @type string   $version      WordPress version.
  26   *             @type string   $updated      Date the translation was last updated, in MySQL datetime format.
  27   *             @type string   $english_name English name of the language.
  28   *             @type string   $native_name  Native name of the language.
  29   *             @type string   $package      URL to download the translation package.
  30   *             @type string[] $iso          Array of ISO language codes.
  31   *             @type array    $strings      Array of translated strings used in the installation process.
  32   *         }
  33   *     }
  34   * }
  35   */
  36  function translations_api( $type, $args = null ) {
  37      if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {
  38          return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
  39      }
  40  
  41      /**
  42       * Allows a plugin to override the WordPress.org Translation Installation API entirely.
  43       *
  44       * @since 4.0.0
  45       *
  46       * @param false|array $result The result array. Default false.
  47       * @param string      $type   The type of translations being requested.
  48       * @param object      $args   Translation API arguments.
  49       */
  50      $res = apply_filters( 'translations_api', false, $type, $args );
  51  
  52      if ( false === $res ) {
  53          $url      = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
  54          $http_url = $url;
  55          $ssl      = wp_http_supports( array( 'ssl' ) );
  56          if ( $ssl ) {
  57              $url = set_url_scheme( $url, 'https' );
  58          }
  59  
  60          $options = array(
  61              'timeout' => 3,
  62              'body'    => array(
  63                  'wp_version' => wp_get_wp_version(),
  64                  'locale'     => get_locale(),
  65                  'version'    => $args['version'], // Version of plugin, theme or core.
  66              ),
  67          );
  68  
  69          if ( 'core' !== $type ) {
  70              $options['body']['slug'] = $args['slug']; // Plugin or theme slug.
  71          }
  72  
  73          $request = wp_remote_post( $url, $options );
  74  
  75          if ( $ssl && is_wp_error( $request ) ) {
  76              wp_trigger_error(
  77                  __FUNCTION__,
  78                  sprintf(
  79                      /* translators: %s: Support forums URL. */
  80                      __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
  81                      __( 'https://wordpress.org/support/forums/' )
  82                  ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
  83                  headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
  84              );
  85  
  86              $request = wp_remote_post( $http_url, $options );
  87          }
  88  
  89          if ( is_wp_error( $request ) ) {
  90              $res = new WP_Error(
  91                  'translations_api_failed',
  92                  sprintf(
  93                      /* translators: %s: Support forums URL. */
  94                      __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
  95                      __( 'https://wordpress.org/support/forums/' )
  96                  ),
  97                  $request->get_error_message()
  98              );
  99          } else {
 100              $res = json_decode( wp_remote_retrieve_body( $request ), true );
 101              if ( ! is_object( $res ) && ! is_array( $res ) ) {
 102                  $res = new WP_Error(
 103                      'translations_api_failed',
 104                      sprintf(
 105                          /* translators: %s: Support forums URL. */
 106                          __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 107                          __( 'https://wordpress.org/support/forums/' )
 108                      ),
 109                      wp_remote_retrieve_body( $request )
 110                  );
 111              }
 112          }
 113      }
 114  
 115      /**
 116       * Filters the Translation Installation API response results.
 117       *
 118       * @since 4.0.0
 119       *
 120       * @param array|WP_Error $res  {
 121       *     On success an associative array of translations, WP_Error on failure.
 122       *
 123       *     @type array $translations {
 124       *         List of translations, each an array of data.
 125       *
 126       *         @type array ...$0 {
 127       *             @type string   $language     Language code.
 128       *             @type string   $version      WordPress version.
 129       *             @type string   $updated      Date the translation was last updated, in MySQL datetime format.
 130       *             @type string   $english_name English name of the language.
 131       *             @type string   $native_name  Native name of the language.
 132       *             @type string   $package      URL to download the translation package.
 133       *             @type string[] $iso          Array of ISO language codes.
 134       *             @type array    $strings      Array of translated strings used in the installation process.
 135       *         }
 136       *     }
 137       * }
 138       * @param string         $type The type of translations being requested.
 139       * @param object         $args Translation API arguments.
 140       */
 141      return apply_filters( 'translations_api_result', $res, $type, $args );
 142  }
 143  
 144  /**
 145   * Get available translations from the WordPress.org API.
 146   *
 147   * @since 4.0.0
 148   *
 149   * @see translations_api()
 150   *
 151   * @return array {
 152   *     Array of translations keyed by the language code, each an associative array of data.
 153   *     If the API response results in an error, an empty array will be returned.
 154   *
 155   *     @type array ...$0 {
 156   *         @type string   $language     Language code.
 157   *         @type string   $version      WordPress version.
 158   *         @type string   $updated      Date the translation was last updated, in MySQL datetime format.
 159   *         @type string   $english_name English name of the language.
 160   *         @type string   $native_name  Native name of the language.
 161   *         @type string   $package      URL to download the translation package.
 162   *         @type string[] $iso          Array of ISO language codes.
 163   *         @type array    $strings      Array of translated strings used in the installation process.
 164   *     }
 165   * }
 166   */
 167  function wp_get_available_translations() {
 168      if ( ! wp_installing() ) {
 169          $translations = get_site_transient( 'available_translations' );
 170          if ( false !== $translations ) {
 171              return $translations;
 172          }
 173      }
 174  
 175      $api = translations_api( 'core', array( 'version' => wp_get_wp_version() ) );
 176  
 177      if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
 178          return array();
 179      }
 180  
 181      $translations = array();
 182      // Key the array with the language code.
 183      foreach ( $api['translations'] as $translation ) {
 184          $translations[ $translation['language'] ] = $translation;
 185      }
 186  
 187      if ( ! defined( 'WP_INSTALLING' ) ) {
 188          set_site_transient( 'available_translations', $translations, 3 * HOUR_IN_SECONDS );
 189      }
 190  
 191      return $translations;
 192  }
 193  
 194  /**
 195   * Output the select form for the language selection on the installation screen.
 196   *
 197   * @since 4.0.0
 198   *
 199   * @global string $wp_local_package Locale code of the package.
 200   *
 201   * @param array[] $languages Array of available languages (populated via the Translation API).
 202   */
 203  function wp_install_language_form( $languages ) {
 204      global $wp_local_package;
 205  
 206      $installed_languages = get_available_languages();
 207  
 208      echo "<label class='screen-reader-text' for='language'>Select a default language</label>\n";
 209      echo "<select size='14' name='language' id='language'>\n";
 210      echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>';
 211      echo "\n";
 212  
 213      if ( ! empty( $wp_local_package ) && isset( $languages[ $wp_local_package ] ) ) {
 214          if ( isset( $languages[ $wp_local_package ] ) ) {
 215              $language = $languages[ $wp_local_package ];
 216              printf(
 217                  '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
 218                  esc_attr( $language['language'] ),
 219                  esc_attr( current( $language['iso'] ) ),
 220                  esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
 221                  in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
 222                  esc_html( $language['native_name'] )
 223              );
 224  
 225              unset( $languages[ $wp_local_package ] );
 226          }
 227      }
 228  
 229      foreach ( $languages as $language ) {
 230          printf(
 231              '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
 232              esc_attr( $language['language'] ),
 233              esc_attr( current( $language['iso'] ) ),
 234              esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
 235              in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
 236              esc_html( $language['native_name'] )
 237          );
 238      }
 239      echo "</select>\n";
 240      echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
 241  }
 242  
 243  /**
 244   * Download a language pack.
 245   *
 246   * @since 4.0.0
 247   *
 248   * @see wp_get_available_translations()
 249   *
 250   * @param string $download Language code to download.
 251   * @return string|false Returns the language code if successfully downloaded
 252   *                      (or already installed), or false on failure.
 253   */
 254  function wp_download_language_pack( $download ) {
 255      // Check if the translation is already installed.
 256      if ( in_array( $download, get_available_languages(), true ) ) {
 257          return $download;
 258      }
 259  
 260      if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) {
 261          return false;
 262      }
 263  
 264      // Confirm the translation is one we can download.
 265      $translations = wp_get_available_translations();
 266      if ( ! $translations ) {
 267          return false;
 268      }
 269      foreach ( $translations as $translation ) {
 270          if ( $translation['language'] === $download ) {
 271              $translation_to_load = true;
 272              break;
 273          }
 274      }
 275  
 276      if ( empty( $translation_to_load ) ) {
 277          return false;
 278      }
 279      $translation = (object) $translation;
 280  
 281      require_once  ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
 282      $skin              = new Automatic_Upgrader_Skin();
 283      $upgrader          = new Language_Pack_Upgrader( $skin );
 284      $translation->type = 'core';
 285      $result            = $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) );
 286  
 287      if ( ! $result || is_wp_error( $result ) ) {
 288          return false;
 289      }
 290  
 291      return $translation->language;
 292  }
 293  
 294  /**
 295   * Check if WordPress has access to the filesystem without asking for
 296   * credentials.
 297   *
 298   * @since 4.0.0
 299   *
 300   * @return bool Returns true on success, false on failure.
 301   */
 302  function wp_can_install_language_pack() {
 303      if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) {
 304          return false;
 305      }
 306  
 307      require_once  ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
 308      $skin     = new Automatic_Upgrader_Skin();
 309      $upgrader = new Language_Pack_Upgrader( $skin );
 310      $upgrader->init();
 311  
 312      $check = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
 313  
 314      if ( ! $check || is_wp_error( $check ) ) {
 315          return false;
 316      }
 317  
 318      return true;
 319  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref