wp/wp-admin/includes/translation-install.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    12  *
    12  *
    13  * @since 4.0.0
    13  * @since 4.0.0
    14  *
    14  *
    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 array|WP_Error On success an associative array of translations, WP_Error on failure.
    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  * }
    18  */
    35  */
    19 function translations_api( $type, $args = null ) {
    36 function translations_api( $type, $args = null ) {
    20 	// Include an unmodified $wp_version.
       
    21 	require ABSPATH . WPINC . '/version.php';
       
    22 
       
    23 	if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {
    37 	if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {
    24 		return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
    38 		return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
    25 	}
    39 	}
    26 
    40 
    27 	/**
    41 	/**
    44 		}
    58 		}
    45 
    59 
    46 		$options = array(
    60 		$options = array(
    47 			'timeout' => 3,
    61 			'timeout' => 3,
    48 			'body'    => array(
    62 			'body'    => array(
    49 				'wp_version' => $wp_version,
    63 				'wp_version' => wp_get_wp_version(),
    50 				'locale'     => get_locale(),
    64 				'locale'     => get_locale(),
    51 				'version'    => $args['version'], // Version of plugin, theme or core.
    65 				'version'    => $args['version'], // Version of plugin, theme or core.
    52 			),
    66 			),
    53 		);
    67 		);
    54 
    68 
   101 	/**
   115 	/**
   102 	 * Filters the Translation Installation API response results.
   116 	 * Filters the Translation Installation API response results.
   103 	 *
   117 	 *
   104 	 * @since 4.0.0
   118 	 * @since 4.0.0
   105 	 *
   119 	 *
   106 	 * @param array|WP_Error $res  Response as an associative array or WP_Error.
   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 	 * }
   107 	 * @param string         $type The type of translations being requested.
   138 	 * @param string         $type The type of translations being requested.
   108 	 * @param object         $args Translation API arguments.
   139 	 * @param object         $args Translation API arguments.
   109 	 */
   140 	 */
   110 	return apply_filters( 'translations_api_result', $res, $type, $args );
   141 	return apply_filters( 'translations_api_result', $res, $type, $args );
   111 }
   142 }
   115  *
   146  *
   116  * @since 4.0.0
   147  * @since 4.0.0
   117  *
   148  *
   118  * @see translations_api()
   149  * @see translations_api()
   119  *
   150  *
   120  * @return array[] Array of translations, each an array of data, keyed by the language. If the API response results
   151  * @return array {
   121  *                 in an error, an empty array will be returned.
   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  * }
   122  */
   166  */
   123 function wp_get_available_translations() {
   167 function wp_get_available_translations() {
   124 	if ( ! wp_installing() ) {
   168 	if ( ! wp_installing() ) {
   125 		$translations = get_site_transient( 'available_translations' );
   169 		$translations = get_site_transient( 'available_translations' );
   126 		if ( false !== $translations ) {
   170 		if ( false !== $translations ) {
   127 			return $translations;
   171 			return $translations;
   128 		}
   172 		}
   129 	}
   173 	}
   130 
   174 
   131 	// Include an unmodified $wp_version.
   175 	$api = translations_api( 'core', array( 'version' => wp_get_wp_version() ) );
   132 	require ABSPATH . WPINC . '/version.php';
       
   133 
       
   134 	$api = translations_api( 'core', array( 'version' => $wp_version ) );
       
   135 
   176 
   136 	if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
   177 	if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
   137 		return array();
   178 		return array();
   138 	}
   179 	}
   139 
   180 
   140 	$translations = array();
   181 	$translations = array();
   141 	// Key the array with the language code for now.
   182 	// Key the array with the language code.
   142 	foreach ( $api['translations'] as $translation ) {
   183 	foreach ( $api['translations'] as $translation ) {
   143 		$translations[ $translation['language'] ] = $translation;
   184 		$translations[ $translation['language'] ] = $translation;
   144 	}
   185 	}
   145 
   186 
   146 	if ( ! defined( 'WP_INSTALLING' ) ) {
   187 	if ( ! defined( 'WP_INSTALLING' ) ) {