wppaste
WordPress

translations_api( string $type, array|object $args = null ): array|WP_Error

Since
4.0.0
Source
wp-admin/includes/translation-install.php:36
Retrieve translations from WordPress Translation API.

Compatibility

WordPress
since 4.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$typestring
Type of translations. Accepts 'plugins', 'themes', 'core'.
$argsarray|objectoptional
Translation API arguments. Optional.Default: null

Return value

array|WP_Error
On success an associative array of translations, WP_Error on failure.
  • $translationsarray

    List of translations, each an array of data.
    • ...$0array

      @type string $language Language code.
      • $versionstring

        WordPress version.
      • $updatedstring

        Date the translation was last updated, in MySQL datetime format.
      • $english_namestring

        English name of the language.
      • $native_namestring

        Native name of the language.
      • $packagestring

        URL to download the translation package.
      • $isostring[]

        Array of ISO language codes.
      • $stringsarray

        Array of translated strings used in the installation process. }

Performance profile

How much work a call to translations_api() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Reaches an outbound HTTP request via wp_remote_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–127

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 155.

Plugin surface
2 hooks

Third-party callbacks on 'translations_api', 'translations_api_result' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • httpoutbound HTTP requestwp_remote_post()called directly
  • serializeserialisationjson_decode()called directly
  • optionnetwork optionget_site_option()one call below translations_api()

Further down the call graph this can also reach cache, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 20 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost translations_api() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$type12__()
$type && $res !== false20apply_filters(), apply_filters()
$type && $res === false && !is_wp_error()61–67apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), apply_filters()
$type && $res === false && !is_wp_error()65–71apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), apply_filters()
$type && $res === false && !is_wp_error()66–72apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), apply_filters()
$type && $res === false && !is_wp_error()70–76apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), apply_filters()
$type && $res === false && is_wp_error()70–74apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), ->get_error_message(), wp_version(), apply_filters()
$type && $res === false74–78apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), is_wp_error(), __(), __(), sprintf(), ->get_error_message(), wp_version(), apply_filters()
$type && $res === false && is_wp_error()75–79apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), ->get_error_message(), wp_version(), apply_filters()
$type && $res === false79–83apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), is_wp_error(), __(), __(), sprintf(), ->get_error_message(), wp_version(), apply_filters()
$type && $res === false && !is_wp_error() && !is_object($res) && !is_array($res)82–86apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf(), wp_remote_retrieve_body(), timeout(), apply_filters()
$type && $res === false && !is_wp_error() && !is_object($res) && !is_array($res)86–90apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf(), wp_remote_retrieve_body(), timeout(), apply_filters()
8 further outcomes, up to 127 instructions
$type && $res === false && !is_wp_error() && !is_object($res) && !is_array($res)87–91apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf(), wp_remote_retrieve_body(), timeout(), apply_filters()
$type && $res === false && !is_wp_error() && !is_object($res) && !is_array($res)91–95apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf(), wp_remote_retrieve_body(), timeout(), apply_filters()
$type && $res === false95–103apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), __(), headers_sent(), wp_trigger_error(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), apply_filters()
$type && $res === false100–108apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), __(), headers_sent(), wp_trigger_error(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), apply_filters()
$type && $res === false && is_wp_error()104–110apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), __(), headers_sent(), wp_trigger_error(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), ->get_error_message(), wp_version(), apply_filters()
$type && $res === false && is_wp_error()109–115apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), __(), headers_sent(), wp_trigger_error(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), ->get_error_message(), wp_version(), apply_filters()
$type && $res === false && !is_object($res) && !is_array($res)116–122apply_filters(), wp_http_supports(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), __(), headers_sent(), wp_trigger_error(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf(), wp_remote_retrieve_body(), timeout(), apply_filters()
$type && $res === false && !is_object($res) && !is_array($res)121–127apply_filters(), wp_http_supports(), set_url_scheme(), wp_get_wp_version(), get_locale(), wp_remote_post(), is_wp_error(), __(), __(), sprintf(), __(), headers_sent(), wp_trigger_error(), wp_remote_post(), is_wp_error(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf(), wp_remote_retrieve_body(), timeout(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 155 instructions, 12–127 executed per call, 11 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while translations_api() runs, in this order:

  1. apply_filters( translations_api )filterline 50 (+14 into the body)

    Allows a plugin to override the WordPress.org Translation Installation API entirely.

  2. apply_filters( translations_api_result )filterline 141 (+105 into the body)

    Filters the Translation Installation API response results.

Uses · 11

Used by · 1

Source code

function translations_api( $type, $args = null ) {	if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {		return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );	} 	/**	 * Allows a plugin to override the WordPress.org Translation Installation API entirely.	 *	 * @since 4.0.0	 *	 * @param false|array $result The result array. Default false.	 * @param string      $type   The type of translations being requested.	 * @param object      $args   Translation API arguments.	 */	$res = apply_filters( 'translations_api', false, $type, $args ); 	if ( false === $res ) {		$url      = 'http://api.wordpress.org/translations/' . $type . '/1.0/';		$http_url = $url;		$ssl      = wp_http_supports( array( 'ssl' ) );		if ( $ssl ) {			$url = set_url_scheme( $url, 'https' );		} 		$options = array(			'timeout' => 3,			'body'    => array(				'wp_version' => wp_get_wp_version(),				'locale'     => get_locale(),				'version'    => $args['version'], // Version of plugin, theme or core.			),		); 		if ( 'core' !== $type ) {			$options['body']['slug'] = $args['slug']; // Plugin or theme slug.		} 		$request = wp_remote_post( $url, $options ); 		if ( $ssl && is_wp_error( $request ) ) {			wp_trigger_error(				__FUNCTION__,				sprintf(					/* translators: %s: Support forums URL. */					__( '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>.' ),					__( 'https://wordpress.org/support/forums/' )				) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),				headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE			); 			$request = wp_remote_post( $http_url, $options );		} 		if ( is_wp_error( $request ) ) {			$res = new WP_Error(				'translations_api_failed',				sprintf(					/* translators: %s: Support forums URL. */					__( '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>.' ),					__( 'https://wordpress.org/support/forums/' )				),				$request->get_error_message()			);		} else {			$res = json_decode( wp_remote_retrieve_body( $request ), true );			if ( ! is_object( $res ) && ! is_array( $res ) ) {				$res = new WP_Error(					'translations_api_failed',					sprintf(						/* translators: %s: Support forums URL. */						__( '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>.' ),						__( 'https://wordpress.org/support/forums/' )					),					wp_remote_retrieve_body( $request )				);			}		}	} 	/**

Changelog

Introduced in 4.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/translation-install.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.