wp/wp-admin/includes/translation-install.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    15  * @param string       $type Type of translations. Accepts 'plugins', 'themes', 'core'.
    15  * @param string       $type Type of translations. Accepts 'plugins', 'themes', 'core'.
    16  * @param array|object $args Translation API arguments. Optional.
    16  * @param array|object $args Translation API arguments. Optional.
    17  * @return object|WP_Error On success an object of translations, WP_Error on failure.
    17  * @return object|WP_Error On success an object of translations, WP_Error on failure.
    18  */
    18  */
    19 function translations_api( $type, $args = null ) {
    19 function translations_api( $type, $args = null ) {
    20 	include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
    20 	// Include an unmodified $wp_version.
    21 
    21 	require ABSPATH . WPINC . '/version.php';
    22 	if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ) ) ) {
    22 
    23 		return  new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
    23 	if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {
       
    24 		return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
    24 	}
    25 	}
    25 
    26 
    26 	/**
    27 	/**
    27 	 * Allows a plugin to override the WordPress.org Translation Installation API entirely.
    28 	 * Allows a plugin to override the WordPress.org Translation Installation API entirely.
    28 	 *
    29 	 *
    33 	 * @param object      $args   Translation API arguments.
    34 	 * @param object      $args   Translation API arguments.
    34 	 */
    35 	 */
    35 	$res = apply_filters( 'translations_api', false, $type, $args );
    36 	$res = apply_filters( 'translations_api', false, $type, $args );
    36 
    37 
    37 	if ( false === $res ) {
    38 	if ( false === $res ) {
    38 		$url = $http_url = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
    39 		$url      = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
    39 		if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
    40 		$http_url = $url;
       
    41 		$ssl      = wp_http_supports( array( 'ssl' ) );
       
    42 		if ( $ssl ) {
    40 			$url = set_url_scheme( $url, 'https' );
    43 			$url = set_url_scheme( $url, 'https' );
    41 		}
    44 		}
    42 
    45 
    43 		$options = array(
    46 		$options = array(
    44 			'timeout' => 3,
    47 			'timeout' => 3,
    45 			'body'    => array(
    48 			'body'    => array(
    46 				'wp_version' => $wp_version,
    49 				'wp_version' => $wp_version,
    47 				'locale'     => get_locale(),
    50 				'locale'     => get_locale(),
    48 				'version'    => $args['version'], // Version of plugin, theme or core
    51 				'version'    => $args['version'], // Version of plugin, theme or core.
    49 			),
    52 			),
    50 		);
    53 		);
    51 
    54 
    52 		if ( 'core' !== $type ) {
    55 		if ( 'core' !== $type ) {
    53 			$options['body']['slug'] = $args['slug']; // Plugin or theme slug
    56 			$options['body']['slug'] = $args['slug']; // Plugin or theme slug.
    54 		}
    57 		}
    55 
    58 
    56 		$request = wp_remote_post( $url, $options );
    59 		$request = wp_remote_post( $url, $options );
    57 
    60 
    58 		if ( $ssl && is_wp_error( $request ) ) {
    61 		if ( $ssl && is_wp_error( $request ) ) {
    59 			trigger_error(
    62 			trigger_error(
    60 				sprintf(
    63 				sprintf(
    61 					/* translators: %s: support forums URL */
    64 					/* translators: %s: Support forums URL. */
    62 					__( '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 					__( '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>.' ),
    63 					__( 'https://wordpress.org/support/' )
    66 					__( 'https://wordpress.org/support/forums/' )
    64 				) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
    67 				) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
    65 				headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
    68 				headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
    66 			);
    69 			);
    67 
    70 
    68 			$request = wp_remote_post( $http_url, $options );
    71 			$request = wp_remote_post( $http_url, $options );
    70 
    73 
    71 		if ( is_wp_error( $request ) ) {
    74 		if ( is_wp_error( $request ) ) {
    72 			$res = new WP_Error(
    75 			$res = new WP_Error(
    73 				'translations_api_failed',
    76 				'translations_api_failed',
    74 				sprintf(
    77 				sprintf(
    75 					/* translators: %s: support forums URL */
    78 					/* translators: %s: Support forums URL. */
    76 					__( '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 					__( '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>.' ),
    77 					__( 'https://wordpress.org/support/' )
    80 					__( 'https://wordpress.org/support/forums/' )
    78 				),
    81 				),
    79 				$request->get_error_message()
    82 				$request->get_error_message()
    80 			);
    83 			);
    81 		} else {
    84 		} else {
    82 			$res = json_decode( wp_remote_retrieve_body( $request ), true );
    85 			$res = json_decode( wp_remote_retrieve_body( $request ), true );
    83 			if ( ! is_object( $res ) && ! is_array( $res ) ) {
    86 			if ( ! is_object( $res ) && ! is_array( $res ) ) {
    84 				$res = new WP_Error(
    87 				$res = new WP_Error(
    85 					'translations_api_failed',
    88 					'translations_api_failed',
    86 					sprintf(
    89 					sprintf(
    87 						/* translators: %s: support forums URL */
    90 						/* translators: %s: Support forums URL. */
    88 						__( '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 						__( '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>.' ),
    89 						__( 'https://wordpress.org/support/' )
    92 						__( 'https://wordpress.org/support/forums/' )
    90 					),
    93 					),
    91 					wp_remote_retrieve_body( $request )
    94 					wp_remote_retrieve_body( $request )
    92 				);
    95 				);
    93 			}
    96 			}
    94 		}
    97 		}
   111  *
   114  *
   112  * @since 4.0.0
   115  * @since 4.0.0
   113  *
   116  *
   114  * @see translations_api()
   117  * @see translations_api()
   115  *
   118  *
   116  * @return array Array of translations, each an array of data. If the API response results
   119  * @return array[] Array of translations, each an array of data, keyed by the language. If the API response results
   117  *               in an error, an empty array will be returned.
   120  *                 in an error, an empty array will be returned.
   118  */
   121  */
   119 function wp_get_available_translations() {
   122 function wp_get_available_translations() {
   120 	if ( ! wp_installing() && false !== ( $translations = get_site_transient( 'available_translations' ) ) ) {
   123 	if ( ! wp_installing() ) {
   121 		return $translations;
   124 		$translations = get_site_transient( 'available_translations' );
   122 	}
   125 		if ( false !== $translations ) {
   123 
   126 			return $translations;
   124 	include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
   127 		}
       
   128 	}
       
   129 
       
   130 	// Include an unmodified $wp_version.
       
   131 	require ABSPATH . WPINC . '/version.php';
   125 
   132 
   126 	$api = translations_api( 'core', array( 'version' => $wp_version ) );
   133 	$api = translations_api( 'core', array( 'version' => $wp_version ) );
   127 
   134 
   128 	if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
   135 	if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
   129 		return array();
   136 		return array();
   145 /**
   152 /**
   146  * Output the select form for the language selection on the installation screen.
   153  * Output the select form for the language selection on the installation screen.
   147  *
   154  *
   148  * @since 4.0.0
   155  * @since 4.0.0
   149  *
   156  *
   150  * @global string $wp_local_package
   157  * @global string $wp_local_package Locale code of the package.
   151  *
   158  *
   152  * @param array $languages Array of available languages (populated via the Translation API).
   159  * @param array[] $languages Array of available languages (populated via the Translation API).
   153  */
   160  */
   154 function wp_install_language_form( $languages ) {
   161 function wp_install_language_form( $languages ) {
   155 	global $wp_local_package;
   162 	global $wp_local_package;
   156 
   163 
   157 	$installed_languages = get_available_languages();
   164 	$installed_languages = get_available_languages();
   166 			$language = $languages[ $wp_local_package ];
   173 			$language = $languages[ $wp_local_package ];
   167 			printf(
   174 			printf(
   168 				'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
   175 				'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
   169 				esc_attr( $language['language'] ),
   176 				esc_attr( $language['language'] ),
   170 				esc_attr( current( $language['iso'] ) ),
   177 				esc_attr( current( $language['iso'] ) ),
   171 				esc_attr( $language['strings']['continue'] ),
   178 				esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
   172 				in_array( $language['language'], $installed_languages ) ? ' data-installed="1"' : '',
   179 				in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
   173 				esc_html( $language['native_name'] )
   180 				esc_html( $language['native_name'] )
   174 			);
   181 			);
   175 
   182 
   176 			unset( $languages[ $wp_local_package ] );
   183 			unset( $languages[ $wp_local_package ] );
   177 		}
   184 		}
   180 	foreach ( $languages as $language ) {
   187 	foreach ( $languages as $language ) {
   181 		printf(
   188 		printf(
   182 			'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
   189 			'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
   183 			esc_attr( $language['language'] ),
   190 			esc_attr( $language['language'] ),
   184 			esc_attr( current( $language['iso'] ) ),
   191 			esc_attr( current( $language['iso'] ) ),
   185 			esc_attr( $language['strings']['continue'] ),
   192 			esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
   186 			in_array( $language['language'], $installed_languages ) ? ' data-installed="1"' : '',
   193 			in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
   187 			esc_html( $language['native_name'] )
   194 			esc_html( $language['native_name'] )
   188 		);
   195 		);
   189 	}
   196 	}
   190 	echo "</select>\n";
   197 	echo "</select>\n";
   191 	echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
   198 	echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
   202  * @return string|bool Returns the language code if successfully downloaded
   209  * @return string|bool Returns the language code if successfully downloaded
   203  *                     (or already installed), or false on failure.
   210  *                     (or already installed), or false on failure.
   204  */
   211  */
   205 function wp_download_language_pack( $download ) {
   212 function wp_download_language_pack( $download ) {
   206 	// Check if the translation is already installed.
   213 	// Check if the translation is already installed.
   207 	if ( in_array( $download, get_available_languages() ) ) {
   214 	if ( in_array( $download, get_available_languages(), true ) ) {
   208 		return $download;
   215 		return $download;
   209 	}
   216 	}
   210 
   217 
   211 	if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) {
   218 	if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) {
   212 		return false;
   219 		return false;