translations_api

Retrieve translations from WordPress Translation API.

Description

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

Returns (object|WP_Error)

On success an object of translations, WP_Error on failure.

Parameters (2)

0. $type (string)
Type of translations. Accepts plugins,, themes., core.
1. $args — Optional. (null) => null
Translation API arguments. Optional.

Usage

  1. if ( !function_exists( 'translations_api' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/translation-install.php'; 
  3.  
  4. // Type of translations. Accepts 'plugins', 'themes', 'core'. 
  5. $type = ''; 
  6.  
  7. // Translation API arguments. Optional. 
  8. $args = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = translations_api($type, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/translation-install.php  
  1. function translations_api( $type, $args = null ) { 
  2. include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version 
  3.  
  4. if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ) ) ) { 
  5. return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) ); 
  6.  
  7. /** 
  8. * Allows a plugin to override the WordPress.org Translation Install API entirely. 
  9. * @since 4.0.0 
  10. * @param bool|array $result The result object. Default false. 
  11. * @param string $type The type oftranslationsbeing requested. 
  12. * @param object $args Translation API arguments. 
  13. */ 
  14. $res = apply_filters( 'translations_api', false, $type, $args ); 
  15.  
  16. if ( false === $res ) { 
  17. $url = $http_url = 'http://api.wordpress.org/translations/' . $type . '/1.0/'; 
  18. if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { 
  19. $url = set_url_scheme( $url, 'https' ); 
  20.  
  21. $options = array( 
  22. 'timeout' => 3,  
  23. 'body' => array( 
  24. 'wp_version' => $wp_version,  
  25. 'locale' => get_locale(),  
  26. 'version' => $args['version'], // Version of plugin, theme or core 
  27. ),  
  28. ); 
  29.  
  30. if ( 'core' !== $type ) { 
  31. $options['body']['slug'] = $args['slug']; // Plugin or theme slug 
  32.  
  33. $request = wp_remote_post( $url, $options ); 
  34.  
  35. if ( $ssl && is_wp_error( $request ) ) { 
  36. trigger_error( 
  37. sprintf( 
  38. /** translators: %s: support forums URL */ 
  39. __( 'An unexpectederroroccurred. 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>.' ),  
  40. __( 'https://wordpress.org/support/' ) 
  41. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),  
  42. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE 
  43. ); 
  44.  
  45. $request = wp_remote_post( $http_url, $options ); 
  46.  
  47. if ( is_wp_error( $request ) ) { 
  48. $res = new WP_Error( 'translations_api_failed',  
  49. sprintf( 
  50. /** translators: %s: support forums URL */ 
  51. __( 'An unexpectederroroccurred. 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>.' ),  
  52. __( 'https://wordpress.org/support/' ) 
  53. ),  
  54. $request->get_error_message() 
  55. ); 
  56. } else { 
  57. $res = json_decode( wp_remote_retrieve_body( $request ), true ); 
  58. if ( ! is_object( $res ) && ! is_array( $res ) ) { 
  59. $res = new WP_Error( 'translations_api_failed',  
  60. sprintf( 
  61. /** translators: %s: support forums URL */ 
  62. __( 'An unexpectederroroccurred. 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>.' ),  
  63. __( 'https://wordpress.org/support/' ) 
  64. ),  
  65. ); 
  66.  
  67. /** 
  68. * Filters the Translation Install API response results. 
  69. * @since 4.0.0 
  70. * @param object|WP_Error $res Response object or WP_Error. 
  71. * @param string $type The type oftranslationsbeing requested. 
  72. * @param object $args Translation API arguments. 
  73. */ 
  74. return apply_filters( 'translations_api_result', $res, $type, $args );