Skip to content

Commit 3fc6cc8

Browse files
authored
Merge pull request #1281 from cloudinary/bugfix/wpml-rest-loopback-url
Fix media uploaded in a non-default WPML language never finishing sync
2 parents e458e62 + b472240 commit 3fc6cc8

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎php/integrations/class-wpml.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function register_hooks() {
6464
add_filter( 'cloudinary_media_context_query', array( $this, 'filter_media_context_query' ) );
6565
add_filter( 'cloudinary_media_context_things', array( $this, 'filter_media_context_things' ) );
6666
add_filter( 'cloudinary_home_url', array( $this, 'home_url' ) );
67+
add_filter( 'cloudinary_rest_url', array( $this, 'rest_url' ), 10, 3 );
6768
add_action( 'cloudinary_edit_asset_permalink', array( $this, 'add_locale' ) );
6869
add_filter( 'cloudinary_contextualized_post_id', array( $this, 'contextualized_post_id' ) );
6970
add_filter( 'wpml_admin_language_switcher_items', array( $this, 'language_switcher_items' ) );
@@ -197,6 +198,41 @@ public function home_url() {
197198
return get_option( 'home' );
198199
}
199200

201+
/**
202+
* Rebuild the REST URL so it carries the current language correctly in every WPML
203+
* negotiation mode.
204+
*
205+
* WPML's `home_url` filter corrupts REST URLs for a non-default language (e.g.
206+
* `/wp-json/?lang=fr/cloudinary/v1/queue`), so `remove_global_hooks()` strips it before
207+
* rebuilding the URL, then `wpml_permalink` re-applies the language correctly - a no-op in
208+
* directory/domain mode, and required in parameter mode, which has no other way to do it.
209+
*
210+
* @param string $rest_url The REST url, already corrupted by WPML's `home_url` filter.
211+
* @param string $path The REST path that was requested.
212+
* @param string|null $scheme The scheme used for the REST url.
213+
*
214+
* @return string
215+
*/
216+
public function rest_url( $rest_url, $path, $scheme ) {
217+
if ( ! class_exists( 'WPML_URL_Filters' ) || ! function_exists( 'WPML\Container\make' ) ) {
218+
return $rest_url;
219+
}
220+
221+
$url_filters = make( 'WPML_URL_Filters' );
222+
if ( ! method_exists( $url_filters, 'remove_global_hooks' ) || ! method_exists( $url_filters, 'add_global_hooks' ) ) {
223+
return $rest_url;
224+
}
225+
226+
$url_filters->remove_global_hooks();
227+
try {
228+
$clean_rest_url = rest_url( $path, $scheme );
229+
} finally {
230+
$url_filters->add_global_hooks();
231+
}
232+
233+
return apply_filters( 'wpml_permalink', $clean_rest_url, apply_filters( 'wpml_current_language', null ) );
234+
}
235+
200236
/**
201237
* Add the locale to the edit asset link.
202238
* This will ensure that the asset is edited in the correct language.

0 commit comments

Comments
 (0)