translations_api( string $type, array|object $args = null ): array|WP_Error
- Since
- 4.0.0
- Source
wp-admin/includes/translation-install.php:36
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.
$translationsarrayList of translations, each an array of data....$0array@type string $language Language code.$versionstringWordPress version.$updatedstringDate the translation was last updated, in MySQL datetime format.$english_namestringEnglish name of the language.$native_namestringNative name of the language.$packagestringURL to download the translation package.$isostring[]Array of ISO language codes.$stringsarrayArray 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
- Scaling
- Constant
- Instructions
- 12–127
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches an outbound HTTP request via wp_remote_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 155.
Third-party callbacks on 'translations_api', 'translations_api_result' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - httpoutbound HTTP request
wp_remote_post()called directly - serializeserialisation
json_decode()called directly - optionnetwork option
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$type | 12 | __() |
$type && $res !== false | 20 | apply_filters(), apply_filters() |
$type && $res === false && !is_wp_error() | 61–67 | apply_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–71 | apply_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–72 | apply_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–76 | apply_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–74 | apply_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 === false | 74–78 | apply_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–79 | apply_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 === false | 79–83 | apply_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–86 | apply_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–90 | apply_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–91 | apply_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–95 | apply_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 === false | 95–103 | apply_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 === false | 100–108 | apply_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–110 | apply_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–115 | apply_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–122 | apply_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–127 | apply_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:
- apply_filters( translations_api )filterline 50 (+14 into the body)
Allows a plugin to override the WordPress.org Translation Installation API entirely.
- apply_filters( translations_api_result )filterline 141 (+105 into the body)
Filters the Translation Installation API response results.
Uses · 11
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_http_supports()Determines if there is an HTTP Transport that can process this request.
- set_url_scheme()Sets the scheme for a URL.
- wp_get_wp_version()Returns the current WordPress version.
- get_locale()Retrieves the current locale.
- wp_remote_post()Performs an HTTP request using the POST method and returns its response.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.
- wp_remote_retrieve_body()Retrieves only the body from the raw response.
- WP_Error::__construct()Initializes the error.
Used by · 1
- wp_get_available_translations()Get available translations from the WordPress.org API.
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’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’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’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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.