From 2e9956d92bba462d90cd87a9f7c7ba000c91108b Mon Sep 17 00:00:00 2001 From: Cristi Rusu Date: Tue, 24 Jun 2025 18:03:49 +0300 Subject: [PATCH] - fix: Editing a user triggers a call to api.wordpress.org/translations Use the `translations_api` filter to get the translations from a custom translations domain. This is a proof of concept, cannot be used in production until the FAIR translations API becomes operational. Signed-off-by: Cristi Rusu --- inc/namespace.php | 1 + inc/translations-api/namespace.php | 94 ++++++++++++++++++++++++++++++ plugin.php | 1 + 3 files changed, 96 insertions(+) create mode 100644 inc/translations-api/namespace.php diff --git a/inc/namespace.php b/inc/namespace.php index 8dae326..8cc08a9 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -32,6 +32,7 @@ function bootstrap() { Pings\bootstrap(); Salts\bootstrap(); Settings\bootstrap(); + Translations_Api\bootstrap(); User_Notification\bootstrap(); Version_Check\bootstrap(); diff --git a/inc/translations-api/namespace.php b/inc/translations-api/namespace.php new file mode 100644 index 0000000..1a6ca4a --- /dev/null +++ b/inc/translations-api/namespace.php @@ -0,0 +1,94 @@ + 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: FAIR Translations API URL. */ + __( 'Cannot establish a secure connection to %s. Please contact your server administrator.', 'fair' ), + esc_url( $url ) + ), + headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE + ); + + $request = wp_remote_post( $http_url, $options ); + } + + if ( is_wp_error( $request ) ) { + $result = new WP_Error( + 'translations_api_failed', + sprintf( + /* translators: %s: FAIR Translations API URL. */ + __( 'An unexpected error occurred while accessing the translations API at URL %s', 'fair' ), + esc_url( $http_url ) + ), + $request->get_error_message() + ); + } else { + $result = json_decode( wp_remote_retrieve_body( $request ), true ); + if ( ! is_object( $result ) && ! is_array( $result ) ) { + $result = new WP_Error( + 'translations_api_failed', + sprintf( + /* translators: %s: FAIR Translations API URL. */ + __( 'An unexpected error occurred while accessing the translations API at URL %s', 'fair' ), + esc_url( $http_url ) + ), + wp_remote_retrieve_body( $request ) + ); + } + } + return $result; +} diff --git a/plugin.php b/plugin.php index 97e434d..1bccdd4 100644 --- a/plugin.php +++ b/plugin.php @@ -30,6 +30,7 @@ require_once __DIR__ . '/inc/importers/namespace.php'; require_once __DIR__ . '/inc/pings/namespace.php'; require_once __DIR__ . '/inc/salts/namespace.php'; require_once __DIR__ . '/inc/settings/namespace.php'; +require_once __DIR__ . '/inc/translations-api/namespace.php'; require_once __DIR__ . '/inc/user-notification/namespace.php'; require_once __DIR__ . '/inc/version-check/namespace.php';