Repository navigation
Fix WPML compatibility for the gallery block and category transformations - #1376
gabrielcld2 wants to merge 2 commits into
Conversation
…ions - The Cloudinary Gallery block now delivers the translated attachment for the current language, through the cloudinary_contextualized_post_id filter, when that attachment is synced. It keeps the stored asset otherwise. - Add a wpml-config.xml so WPML copies the category image and video transformation term meta to translated terms, and ship it in the build.
- Only swap a gallery asset for its contextualized attachment once that attachment is fully synced, and queue it for sync otherwise when auto sync is enabled, like attachments found in the post content. - Copy the "Disable Cloudinary global transformations" post option to translated posts through wpml-config.xml. - Cover the swap at render level, including the rebuilt transformations.
utkarshcloudinary
left a comment
There was a problem hiding this comment.
Left one blocker and some nits; otherwise this looks good!
| <custom-term-field action="copy">cloudinary_transformations_image_freeform</custom-term-field> | ||
| <custom-term-field action="copy">cloudinary_transformations_video_freeform</custom-term-field> |
There was a problem hiding this comment.
blocker: The term meta copy runs one save behind on edit.
WPML hooks SitePress::create_term on edit_term (priority 1). That calls WPML_Term_Actions::save_term_actions(), which adds sync_term_meta on the generic edited_term hook. sync_term_meta copies the original term meta to the translations.
Cloudinary saves this meta on edited_{$taxonomy} (class-global-transformations.php L718). In wp_update_term(), edited_term fires before edited_{$taxonomy}. So WPML copies the old freeform value, and then Cloudinary writes the new value on the original only. The translated term gets the new value only on the next save of the original.
Create is not affected: create_{$taxonomy} fires before created_term.
Suggested fix: in Integrations\WPML, hook edited_{$taxonomy} at a priority later than 10 and run ( new \WPML_Sync_Term_Meta_Action( $sitepress, $tt_id ) )->run() again. Another option: save the Cloudinary term meta on edit_term at priority 0 instead of edited_{$taxonomy}.
| } | ||
|
|
||
| $sync = $this->media->sync; | ||
| if ( ! $sync->is_synced( $contextualized_id ) ) { |
There was a problem hiding this comment.
warning: is_synced() is stricter than the normal delivery path.
is_synced() needs a full signature match. Media::cloudinary_id() (class-media.php L1790-1799) falls back to the local URL only when the missing sync type is required (for example file). When only a non-required item is stale (meta, breakpoints, delivery), it still delivers from Cloudinary.
So with this check, a translated attachment that has a valid public ID and only a stale non-required signature item keeps the original-language asset in the gallery. Core image blocks on the same page deliver the translated asset.
Suggested fix: use the same rule as cloudinary_id(). Swap when has_public_id() is true and is_required( maybe_prepare_sync( $id ), $id ) is false. Another option: swap when $this->media->cloudinary_id( $contextualized_id ) is not false.
| if ( $sync->is_auto_sync_enabled() ) { | ||
| $sync->add_to_sync( $contextualized_id ); | ||
| } |
There was a problem hiding this comment.
nit: This runs on each front-end render. add_to_sync() calls set_pending(), which is an update_post_meta() write. For a translated attachment that stays unsynced (sync error, or not uploadable), each page view writes post meta for each gallery item.
Delivery does the same, so this matches existing behavior. A ! $sync->is_pending( $contextualized_id ) guard before add_to_sync() removes the repeated writes.
| <custom-fields> | ||
| <custom-field action="copy">cloudinary_apply_type_terms</custom-field> | ||
| </custom-fields> |
There was a problem hiding this comment.
warning: cloudinary_transformations_terms (the term order post meta) is not declared.
If WPML does not copy it, the translated post falls back to wp_get_object_terms() order in Global_Transformations::get_terms(). The freeform values are joined in that order (L277), so the translation can apply the term transformations in a different order than the original. cloudinary_apply_type_terms is copied, so the two settings can disagree on the translation.
If it is copied as is, the stored values (category:12) are source-language term IDs.
Suggested fix: declare it explicitly. Either copy it and map each taxonomy:id through wpml_object_id in get_terms(), or set it to ignore and accept the default order on translations.
| @@ -0,0 +1,9 @@ | |||
| <wpml-config> | |||
| <custom-fields> | |||
| <custom-field action="copy">cloudinary_apply_type_terms</custom-field> | |||
There was a problem hiding this comment.
nit: copy applies only when the original is saved. Existing translations stay out of sync until someone saves the original again. A short note in the changelog will help.
Also, the Cloudinary fields stay editable on translations. For posts, WPML save_post_actions (priority 100) runs after save_taxonomy_ordering (priority 10), so an edit to the checkbox on a translation is lost when WPML copies the original value back. Think about disabling or hiding these fields on translations when WPML is active.
| </custom-fields> | ||
| <custom-term-fields> | ||
| <custom-term-field action="copy">cloudinary_transformations_image_freeform</custom-term-field> | ||
| <custom-term-field action="copy">cloudinary_transformations_video_freeform</custom-term-field> |
There was a problem hiding this comment.
nit (outside the diff): Global_Transformations::__construct() L85-86 reads $this->globals['image']->get_value() twice and never reads the video settings. The video freeform taxonomy field is defined in ui-definitions/settings-video.php, but it probably never registers, so cloudinary_transformations_video_freeform is never written. This entry does no harm. The constructor bug belongs in a follow-up.
Fixes the two WPML compatibility issues reported by the WPML compatibility team (tested with WPML 5.3.0 and WPML Media Translation).
Approach
1. The Cloudinary Gallery block shows the original-language images on translated pages
attachmentIdandpublicIdin its attributes. When the post was rendered, these were used as they are, so a translated page delivered the original-language image. This happened even when WPML Media Translation gave that page a different file.Gallery::prepare_block_render()now runs each image through the existingcloudinary_contextualized_post_idfilter. The WPML integration already hooks this filter to return the attachment for the current language.Sync::is_synced(), so a public ID copied from the original before a new file is synced doesn't count), the gallery uses its ID and public ID. The stored transformations of the original are dropped, so they get rebuilt for the new attachment.null), or when the translation isn't synced yet. In the second case the translation is queued for sync when auto sync is enabled, as Delivery does for attachments found in the post content.2. A category's image transformation doesn't apply to translated posts
cloudinary_transformations_image_freeformandcloudinary_transformations_video_freeformterm meta. Without awpml-config.xml, WPML never copied them to translated categories.wpml-config.xmlat the plugin root declares both fields ascopy. WPML then copies them when a category translation is created, and syncs them when the original category is saved.cloudinary_apply_type_termspost meta ascopy, so the "Disable Cloudinary global transformations" option of a post carries over to its translations. The term order meta (cloudinary_transformations_terms) is left out: it stores the original language's term IDs, and translated posts fall back to their own terms without it.wpml-config.xmlis added to thecopy:distlist ingruntfile.jsso it ships in the release ZIP.Out of scope
QA notes
npm run test:unit -- --filter Test_Gallery_Contextualize_Assetcovers the gallery swap:Issue 1: the Cloudinary Gallery block uses translated images
?lang=fr).res.cloudinary.com.Issue 2: category transformations apply to translated posts
e_grayscale. Save.e_grayscaletransformation was copied over.res.cloudinary.comwithe_grayscalein its URL.e_sepiaand save. The French post's image should switch to sepia.