[ Index ]

PHP Cross Reference of WordPress

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


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1