translations_api( string $type, array|object $args = null )
Retrieve translations from ClassicPress Translation API.
Parameters
- $type
-
(Required) Type of translations. Accepts 'plugins', 'themes', 'core'.
- $args
-
(Optional) Translation API arguments. Optional.
Default value: null
Return
(object|WP_Error) On success an object of translations, WP_Error on failure.
Source
File: wp-admin/includes/translation-install.php
function translations_api( $type, $args = null ) {
include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ) ) ) {
return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
}
/**
* Allows a plugin to override the ClassicPress.net Translation Installation API entirely.
*
* @since WP-4.0.0
*
* @param bool|array $result The result object. 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 ) {
$stats = array(
'locale' => get_locale(),
'version' => $args['version'], // Version of plugin, theme or core
);
$options = array(
'timeout' => 3,
'method' => 'POST',
);
if ( 'core' === $type ) {
// Get ClassicPress core translations from the ClassicPress.net API.
$stats['cp_version'] = $cp_version;
$options['method'] = 'GET';
$url = add_query_arg(
$stats,
'https://api-v1.classicpress.net/translations/core/1.0.0/translations.json'
);
$request = wp_remote_request( $url, $options );
} else {
// Get plugin and theme translations from the WP.org API.
$stats['wp_version'] = $wp_version;
$options['body'] = $stats;
$options['body']['slug'] = $args['slug']; // Plugin or theme slug
$url = 'https://api.wordpress.org/translations/' . $type . '/1.0/';
$request = wp_remote_request( $url, $options );
}
if ( is_wp_error( $request ) ) {
trigger_error(
sprintf(
/* translators: %s: support forums URL */
__( 'An unexpected error occurred. Something may be wrong with ClassicPress.net or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://forums.classicpress.net/c/support' )
) . ' ' . __( '(ClassicPress could not establish a secure connection to ClassicPress.net. Please contact your server administrator.)' ),
headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
);
// Retry request
$request = wp_remote_request( $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 ClassicPress.net or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://forums.classicpress.net/c/support' )
),
$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 ClassicPress.net or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://forums.classicpress.net/c/support' )
),
wp_remote_retrieve_body( $request )
);
}
}
}
/**
* Filters the Translation Installation API response results.
*
* @since WP-4.0.0
*
* @param object|WP_Error $res Response object or WP_Error.
* @param string $type The type of translations being requested.
* @param object $args Translation API arguments.
*/
return apply_filters( 'translations_api_result', $res, $type, $args );
}
Changelog
Version | Description |
---|---|
WP-4.0.0 | Introduced. |