From 487022fbfeffb735048f34fb5ed2dba86ced3546 Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Tue, 17 Nov 2026 10:25:54 +0100 Subject: [PATCH 1/3] Fix WPML compatibility for the gallery block and category transformations - 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. --- gruntfile.js | 1 + php/media/class-gallery.php | 41 ++++ .../test-gallery-contextualize-asset.php | 189 ++++++++++++++++++ wpml-config.xml | 6 + 4 files changed, 237 insertions(+) create mode 100644 tests/phpunit/tests/test-gallery-contextualize-asset.php create mode 100644 wpml-config.xml diff --git a/gruntfile.js b/gruntfile.js index 56e23f74b..ce45c24a9 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -38,6 +38,7 @@ module.exports = function ( grunt ) { 'ui-definitions/**', '*.php', 'readme.txt', + 'wpml-config.xml', '!**/src/**', ], dest: '<%= dist_dir %>', diff --git a/php/media/class-gallery.php b/php/media/class-gallery.php index a706b2b1d..e38e42c30 100644 --- a/php/media/class-gallery.php +++ b/php/media/class-gallery.php @@ -496,6 +496,8 @@ public function prepare_block_render( $content, $block ) { $attributes['mediaAssets'] = array(); foreach ( $attributes['selectedImages'] as $attachment ) { + $attachment = $this->contextualize_asset( $attachment ); + // Since the gallery widget uses public ID's, if an attachment is deleted, it can still remain working in a gallery. if ( ! wp_get_attachment_url( $attachment['attachmentId'] ) ) { continue; @@ -549,6 +551,45 @@ public function prepare_block_render( $content, $block ) { return $content . ob_get_clean(); } + /** + * Swap a gallery asset for its contextualized attachment (e.g. the translation of the + * attachment for the current language), so that the gallery delivers the same asset + * WordPress would deliver in that context. + * + * The asset is left untouched when there is no contextualized attachment, or when it + * isn't synced yet, since it would have no public ID to deliver. + * + * @param array $attachment The gallery asset, as stored in the block attributes. + * + * @return array + */ + protected function contextualize_asset( $attachment ) { + if ( empty( $attachment['attachmentId'] ) ) { + return $attachment; + } + + $attachment_id = (int) $attachment['attachmentId']; + + /** This filter is documented in php/class-delivery.php */ + $contextualized_id = (int) apply_filters( 'cloudinary_contextualized_post_id', $attachment_id ); + + if ( + empty( $contextualized_id ) + || $contextualized_id === $attachment_id + || ! $this->media->has_public_id( $contextualized_id ) + ) { + return $attachment; + } + + $attachment['attachmentId'] = $contextualized_id; + $attachment['publicId'] = $this->media->get_public_id( $contextualized_id, true ); + + // The stored transformations belong to the original attachment; they get rebuilt for the new one. + unset( $attachment['transformation'] ); + + return $attachment; + } + /** * Maybe decode the gallery configuration. * This has historical reasons, as it was used to be stored as encoded information. diff --git a/tests/phpunit/tests/test-gallery-contextualize-asset.php b/tests/phpunit/tests/test-gallery-contextualize-asset.php new file mode 100644 index 000000000..c1853dcfd --- /dev/null +++ b/tests/phpunit/tests/test-gallery-contextualize-asset.php @@ -0,0 +1,189 @@ + self::ORIGINAL_ID, + 'publicId' => 'original', + 'transformation' => array( 'transformation' => array( array( 'effect' => 'grayscale' ) ) ), + ); + + /** + * Build a gallery wired to a stubbed media component. + * + * @param array $public_ids Map of attachment ID => public ID for synced attachments. + * + * @return Test_Gallery_Contextualize_Asset_Gallery + */ + protected function get_gallery( array $public_ids ) { + $media = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Media::class ) )->newInstanceWithoutConstructor(); + $media->public_ids = $public_ids; + + $gallery = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Gallery::class ) )->newInstanceWithoutConstructor(); + $gallery->media = $media; + + return $gallery; + } + + /** + * Map the original attachment to the given ID through the contextualization filter. + * + * @param int|null $id The ID the filter returns for the original attachment. + * + * @return void + */ + protected function contextualize_to( $id ) { + add_filter( + 'cloudinary_contextualized_post_id', + static function ( $post_id ) use ( $id ) { + return self::ORIGINAL_ID === (int) $post_id ? $id : $post_id; + } + ); + } + + /** + * Without any contextualization, the asset is untouched. + * + * @return void + */ + public function test_asset_unchanged_without_contextualization() { + $gallery = $this->get_gallery( array( self::ORIGINAL_ID => 'original' ) ); + + $this->assertSame( $this->asset, $gallery->contextualize_asset( $this->asset ) ); + } + + /** + * A synced contextualized attachment replaces the stored one, and the stored transformations + * of the original are dropped so they get rebuilt for the new attachment. + * + * @return void + */ + public function test_asset_swapped_for_synced_contextualized_attachment() { + $this->contextualize_to( self::TRANSLATED_ID ); + $gallery = $this->get_gallery( + array( + self::ORIGINAL_ID => 'original', + self::TRANSLATED_ID => 'translated', + ) + ); + + $this->assertSame( + array( + 'attachmentId' => self::TRANSLATED_ID, + 'publicId' => 'translated', + ), + $gallery->contextualize_asset( $this->asset ) + ); + } + + /** + * A contextualized attachment that isn't synced yet has no public ID to deliver, so the + * original asset is kept. + * + * @return void + */ + public function test_asset_unchanged_when_contextualized_attachment_not_synced() { + $this->contextualize_to( self::TRANSLATED_ID ); + $gallery = $this->get_gallery( array( self::ORIGINAL_ID => 'original' ) ); + + $this->assertSame( $this->asset, $gallery->contextualize_asset( $this->asset ) ); + } + + /** + * WPML returns null for an attachment without a translation; the original asset is kept. + * + * @return void + */ + public function test_asset_unchanged_when_no_contextualized_attachment() { + $this->contextualize_to( null ); + $gallery = $this->get_gallery( array( self::ORIGINAL_ID => 'original' ) ); + + $this->assertSame( $this->asset, $gallery->contextualize_asset( $this->asset ) ); + } +} + +/** + * Exposes Gallery::contextualize_asset() for testing. + */ +class Test_Gallery_Contextualize_Asset_Gallery extends \Cloudinary\Media\Gallery { + + /** + * Expose the protected method. + * + * @param array $attachment The gallery asset. + * + * @return array + */ + public function contextualize_asset( $attachment ) { + return parent::contextualize_asset( $attachment ); + } +} + +/** + * Media stub with in-memory public IDs. + */ +class Test_Gallery_Contextualize_Asset_Media extends \Cloudinary\Media { + + /** + * Map of attachment ID => public ID. + * + * @var array + */ + public $public_ids = array(); + + /** + * Check if the attachment has a public ID. + * + * @param int $attachment_id The attachment ID. + * + * @return bool + */ + public function has_public_id( $attachment_id ) { + return isset( $this->public_ids[ $attachment_id ] ); + } + + /** + * Get the attachment public ID. + * + * @param int $attachment_id The attachment ID. + * @param bool $suffixed Unused. + * + * @return string|null + */ + public function get_public_id( $attachment_id, $suffixed = false ) { + return isset( $this->public_ids[ $attachment_id ] ) ? $this->public_ids[ $attachment_id ] : null; + } +} diff --git a/wpml-config.xml b/wpml-config.xml new file mode 100644 index 000000000..3c7c35e06 --- /dev/null +++ b/wpml-config.xml @@ -0,0 +1,6 @@ + + + cloudinary_transformations_image_freeform + cloudinary_transformations_video_freeform + + From 23a361295c91bec628f8e126a8996b2a3a811eea Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Tue, 17 Nov 2026 10:50:08 +0100 Subject: [PATCH 2/3] Address review feedback on WPML compatibility - 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. --- php/media/class-gallery.php | 18 +- .../test-gallery-contextualize-asset.php | 294 +++++++++++++++--- wpml-config.xml | 3 + 3 files changed, 274 insertions(+), 41 deletions(-) diff --git a/php/media/class-gallery.php b/php/media/class-gallery.php index e38e42c30..b66e38cf7 100644 --- a/php/media/class-gallery.php +++ b/php/media/class-gallery.php @@ -557,7 +557,8 @@ public function prepare_block_render( $content, $block ) { * WordPress would deliver in that context. * * The asset is left untouched when there is no contextualized attachment, or when it - * isn't synced yet, since it would have no public ID to deliver. + * isn't synced yet. In that case it gets queued for sync when auto sync is enabled, like + * attachments found in the post content. * * @param array $attachment The gallery asset, as stored in the block attributes. * @@ -573,11 +574,16 @@ protected function contextualize_asset( $attachment ) { /** This filter is documented in php/class-delivery.php */ $contextualized_id = (int) apply_filters( 'cloudinary_contextualized_post_id', $attachment_id ); - if ( - empty( $contextualized_id ) - || $contextualized_id === $attachment_id - || ! $this->media->has_public_id( $contextualized_id ) - ) { + if ( empty( $contextualized_id ) || $contextualized_id === $attachment_id ) { + return $attachment; + } + + $sync = $this->media->sync; + if ( ! $sync->is_synced( $contextualized_id ) ) { + if ( $sync->is_auto_sync_enabled() ) { + $sync->add_to_sync( $contextualized_id ); + } + return $attachment; } diff --git a/tests/phpunit/tests/test-gallery-contextualize-asset.php b/tests/phpunit/tests/test-gallery-contextualize-asset.php index c1853dcfd..7fc2b5cc3 100644 --- a/tests/phpunit/tests/test-gallery-contextualize-asset.php +++ b/tests/phpunit/tests/test-gallery-contextualize-asset.php @@ -1,18 +1,18 @@ original_id = self::factory()->attachment->create_object( 'original.jpg', 0, array( 'post_mime_type' => 'image/jpeg' ) ); + $this->translated_id = self::factory()->attachment->create_object( 'translated.jpg', 0, array( 'post_mime_type' => 'image/jpeg' ) ); + } /** * The gallery asset, as stored in the block attributes. * - * @var array + * @return array */ - protected $asset = array( - 'attachmentId' => self::ORIGINAL_ID, - 'publicId' => 'original', - 'transformation' => array( 'transformation' => array( array( 'effect' => 'grayscale' ) ) ), - ); + protected function get_asset() { + return array( + 'attachmentId' => $this->original_id, + 'publicId' => 'original', + 'transformation' => array( 'transformation' => array( array( 'effect' => 'grayscale' ) ) ), + ); + } /** - * Build a gallery wired to a stubbed media component. + * Build a gallery wired to stubbed media and sync components. * - * @param array $public_ids Map of attachment ID => public ID for synced attachments. + * @param array $synced Map of attachment ID => public ID for synced attachments. + * @param bool $auto_sync Whether auto sync is enabled. + * @param array $transformations Map of attachment ID => transformations. * * @return Test_Gallery_Contextualize_Asset_Gallery */ - protected function get_gallery( array $public_ids ) { - $media = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Media::class ) )->newInstanceWithoutConstructor(); - $media->public_ids = $public_ids; + protected function get_gallery( array $synced, $auto_sync = true, array $transformations = array() ) { + $sync = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Sync::class ) )->newInstanceWithoutConstructor(); + $sync->synced = array_keys( $synced ); + $sync->auto_sync = $auto_sync; + + $media = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Media::class ) )->newInstanceWithoutConstructor(); + $media->public_ids = $synced; + $media->transformations = $transformations; + $media->sync = $sync; $gallery = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Gallery::class ) )->newInstanceWithoutConstructor(); $gallery->media = $media; + $gallery->set_plugin( \Cloudinary\get_plugin_instance() ); return $gallery; } @@ -66,23 +89,46 @@ protected function get_gallery( array $public_ids ) { * @return void */ protected function contextualize_to( $id ) { + $original_id = $this->original_id; add_filter( 'cloudinary_contextualized_post_id', - static function ( $post_id ) use ( $id ) { - return self::ORIGINAL_ID === (int) $post_id ? $id : $post_id; + static function ( $post_id ) use ( $original_id, $id ) { + return $original_id === (int) $post_id ? $id : $post_id; } ); } + /** + * Render a gallery block with the stored asset, and return the assets passed to the widget. + * + * @param Test_Gallery_Contextualize_Asset_Gallery $gallery The gallery. + * + * @return array + */ + protected function render_media_assets( $gallery ) { + $block = array( + 'blockName' => 'cloudinary/gallery', + 'attrs' => array( 'selectedImages' => array( $this->get_asset() ) ), + ); + + $output = $gallery->prepare_block_render( '', $block ); + $this->assertSame( 1, preg_match( '/var attributes = (.*);/', $output, $matches ) ); + + $attributes = json_decode( $matches[1], true ); + + return $attributes['mediaAssets']; + } + /** * Without any contextualization, the asset is untouched. * * @return void */ public function test_asset_unchanged_without_contextualization() { - $gallery = $this->get_gallery( array( self::ORIGINAL_ID => 'original' ) ); + $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); - $this->assertSame( $this->asset, $gallery->contextualize_asset( $this->asset ) ); + $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); + $this->assertSame( array(), $gallery->media->sync->queued ); } /** @@ -92,34 +138,48 @@ public function test_asset_unchanged_without_contextualization() { * @return void */ public function test_asset_swapped_for_synced_contextualized_attachment() { - $this->contextualize_to( self::TRANSLATED_ID ); + $this->contextualize_to( $this->translated_id ); $gallery = $this->get_gallery( array( - self::ORIGINAL_ID => 'original', - self::TRANSLATED_ID => 'translated', + $this->original_id => 'original', + $this->translated_id => 'translated', ) ); $this->assertSame( array( - 'attachmentId' => self::TRANSLATED_ID, + 'attachmentId' => $this->translated_id, 'publicId' => 'translated', ), - $gallery->contextualize_asset( $this->asset ) + $gallery->contextualize_asset( $this->get_asset() ) ); } /** - * A contextualized attachment that isn't synced yet has no public ID to deliver, so the - * original asset is kept. + * A contextualized attachment that isn't synced has no up-to-date public ID to deliver, so the + * original asset is kept, and the attachment is queued for sync. + * + * @return void + */ + public function test_unsynced_contextualized_attachment_kept_and_queued() { + $this->contextualize_to( $this->translated_id ); + $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); + + $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); + $this->assertSame( array( $this->translated_id ), $gallery->media->sync->queued ); + } + + /** + * Without auto sync, an unsynced contextualized attachment isn't queued. * * @return void */ - public function test_asset_unchanged_when_contextualized_attachment_not_synced() { - $this->contextualize_to( self::TRANSLATED_ID ); - $gallery = $this->get_gallery( array( self::ORIGINAL_ID => 'original' ) ); + public function test_unsynced_contextualized_attachment_not_queued_without_auto_sync() { + $this->contextualize_to( $this->translated_id ); + $gallery = $this->get_gallery( array( $this->original_id => 'original' ), false ); - $this->assertSame( $this->asset, $gallery->contextualize_asset( $this->asset ) ); + $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); + $this->assertSame( array(), $gallery->media->sync->queued ); } /** @@ -129,17 +189,94 @@ public function test_asset_unchanged_when_contextualized_attachment_not_synced() */ public function test_asset_unchanged_when_no_contextualized_attachment() { $this->contextualize_to( null ); - $gallery = $this->get_gallery( array( self::ORIGINAL_ID => 'original' ) ); + $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); - $this->assertSame( $this->asset, $gallery->contextualize_asset( $this->asset ) ); + $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); + $this->assertSame( array(), $gallery->media->sync->queued ); + } + + /** + * The rendered gallery delivers the contextualized attachment with its own transformations. + * + * @return void + */ + public function test_render_delivers_contextualized_attachment_with_its_transformations() { + $this->contextualize_to( $this->translated_id ); + $sepia = array( array( 'effect' => 'sepia' ) ); + $gallery = $this->get_gallery( + array( + $this->original_id => 'original', + $this->translated_id => 'translated', + ), + true, + array( $this->translated_id => $sepia ) + ); + + $this->assertSame( + array( + array( + 'attachmentId' => $this->translated_id, + 'publicId' => 'translated', + 'transformation' => array( 'transformation' => $sepia ), + ), + ), + $this->render_media_assets( $gallery ) + ); + } + + /** + * The stored transformations of the original attachment don't leak to the contextualized one. + * + * @return void + */ + public function test_render_drops_original_transformations_for_contextualized_attachment() { + $this->contextualize_to( $this->translated_id ); + $gallery = $this->get_gallery( + array( + $this->original_id => 'original', + $this->translated_id => 'translated', + ) + ); + + $this->assertSame( + array( + array( + 'attachmentId' => $this->translated_id, + 'publicId' => 'translated', + ), + ), + $this->render_media_assets( $gallery ) + ); + } + + /** + * Without contextualization, the rendered gallery delivers the stored asset. + * + * @return void + */ + public function test_render_delivers_stored_asset_without_contextualization() { + $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); + + $this->assertSame( array( $this->get_asset() ), $this->render_media_assets( $gallery ) ); } } /** - * Exposes Gallery::contextualize_asset() for testing. + * Exposes Gallery::contextualize_asset() for testing, and skips enqueuing the gallery library. */ class Test_Gallery_Contextualize_Asset_Gallery extends \Cloudinary\Media\Gallery { + /** + * Set the plugin instance. + * + * @param \Cloudinary\Plugin $plugin The plugin instance. + * + * @return void + */ + public function set_plugin( $plugin ) { + $this->plugin = $plugin; + } + /** * Expose the protected method. * @@ -150,10 +287,18 @@ class Test_Gallery_Contextualize_Asset_Gallery extends \Cloudinary\Media\Gallery public function contextualize_asset( $attachment ) { return parent::contextualize_asset( $attachment ); } + + /** + * Don't enqueue the gallery library. + * + * @return void + */ + public function enqueue_gallery_library() { + } } /** - * Media stub with in-memory public IDs. + * Media stub with in-memory public IDs and transformations. */ class Test_Gallery_Contextualize_Asset_Media extends \Cloudinary\Media { @@ -164,6 +309,13 @@ class Test_Gallery_Contextualize_Asset_Media extends \Cloudinary\Media { */ public $public_ids = array(); + /** + * Map of attachment ID => transformations. + * + * @var array + */ + public $transformations = array(); + /** * Check if the attachment has a public ID. * @@ -186,4 +338,76 @@ public function has_public_id( $attachment_id ) { public function get_public_id( $attachment_id, $suffixed = false ) { return isset( $this->public_ids[ $attachment_id ] ) ? $this->public_ids[ $attachment_id ] : null; } + + /** + * Get the attachment transformations. + * + * @param int $attachment_id The attachment ID. + * @param array $transformations Unused. + * @param bool $overwrite_transformations Unused. + * + * @return array + */ + public function get_transformations( $attachment_id, $transformations = array(), $overwrite_transformations = false ) { + return isset( $this->transformations[ $attachment_id ] ) ? $this->transformations[ $attachment_id ] : array(); + } +} + +/** + * Sync stub with in-memory sync state, recording the attachments queued for sync. + */ +class Test_Gallery_Contextualize_Asset_Sync extends \Cloudinary\Sync { + + /** + * The synced attachment IDs. + * + * @var int[] + */ + public $synced = array(); + + /** + * Whether auto sync is enabled. + * + * @var bool + */ + public $auto_sync = true; + + /** + * The attachment IDs queued for sync. + * + * @var int[] + */ + public $queued = array(); + + /** + * Check if the attachment is synced. + * + * @param int $post_id The attachment ID. + * @param bool $re_check Unused. + * + * @return bool + */ + public function is_synced( $post_id, $re_check = false ) { + return in_array( (int) $post_id, $this->synced, true ); + } + + /** + * Check if auto sync is enabled. + * + * @return bool + */ + public function is_auto_sync_enabled() { + return $this->auto_sync; + } + + /** + * Record the attachment as queued for sync. + * + * @param int $attachment_id The attachment ID. + * + * @return void + */ + public function add_to_sync( $attachment_id ) { + $this->queued[] = (int) $attachment_id; + } } diff --git a/wpml-config.xml b/wpml-config.xml index 3c7c35e06..1736f4b46 100644 --- a/wpml-config.xml +++ b/wpml-config.xml @@ -1,4 +1,7 @@ + + cloudinary_apply_type_terms + cloudinary_transformations_image_freeform cloudinary_transformations_video_freeform From 1318792e56131843124ab03a6218f6499d61546b Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Wed, 18 Nov 2026 09:36:17 +0100 Subject: [PATCH 3/3] Address second round of review feedback on WPML compatibility - Save the term transformations on edit_{$taxonomy}, before edited_term and saved_term, so WPML copies the new values to translations on the same save regardless of the hook it syncs term meta on. - Swap a gallery asset for its contextualized attachment using the same rule as the rest of the delivery (Media::cloudinary_id()), which also handles queueing the sync without repeated writes. - Copy the term transformations ordering to translated posts, and map its terms to the post's language through a new cloudinary_contextualized_term filter. --- php/integrations/class-wpml.php | 36 ++++++ php/media/class-gallery.php | 13 +- php/media/class-global-transformations.php | 29 ++++- .../test-gallery-contextualize-asset.php | 108 +++------------- .../test-global-transformations-terms.php | 120 ++++++++++++++++++ wpml-config.xml | 1 + 6 files changed, 205 insertions(+), 102 deletions(-) create mode 100644 tests/phpunit/tests/test-global-transformations-terms.php diff --git a/php/integrations/class-wpml.php b/php/integrations/class-wpml.php index 7a19358ce..53973ccb7 100644 --- a/php/integrations/class-wpml.php +++ b/php/integrations/class-wpml.php @@ -67,6 +67,7 @@ public function register_hooks() { add_filter( 'cloudinary_rest_url', array( $this, 'rest_url' ), 10, 3 ); add_action( 'cloudinary_edit_asset_permalink', array( $this, 'add_locale' ) ); add_filter( 'cloudinary_contextualized_post_id', array( $this, 'contextualized_post_id' ) ); + add_filter( 'cloudinary_contextualized_term', array( $this, 'contextualized_term' ), 10, 2 ); add_filter( 'wpml_admin_language_switcher_items', array( $this, 'language_switcher_items' ) ); } @@ -260,6 +261,41 @@ public function contextualized_post_id( $post_id ) { return apply_filters( 'wpml_object_id', $post_id, 'attachment' ); } + /** + * Get the translation of a term in the language of a post. + * + * The post's term transformations ordering is copied to its translations as is, so it holds + * the terms of the original post's language. + * + * @param \WP_Term $term The term. + * @param int $post_id The post ID. + * + * @return \WP_Term + */ + public function contextualized_term( $term, $post_id ) { + $language = apply_filters( + 'wpml_element_language_code', + null, + array( + 'element_id' => $post_id, + 'element_type' => get_post_type( $post_id ), + ) + ); + + if ( empty( $language ) ) { + return $term; + } + + $term_id = (int) apply_filters( 'wpml_object_id', $term->term_id, $term->taxonomy, true, $language ); + if ( $term_id === $term->term_id ) { + return $term; + } + + $translated_term = get_term( $term_id, $term->taxonomy ); + + return $translated_term instanceof \WP_Term ? $translated_term : $term; + } + /** * Update the link for the Cloudinary Assets item on the admin bar language switcher. * diff --git a/php/media/class-gallery.php b/php/media/class-gallery.php index b66e38cf7..2432ebe49 100644 --- a/php/media/class-gallery.php +++ b/php/media/class-gallery.php @@ -556,9 +556,9 @@ public function prepare_block_render( $content, $block ) { * attachment for the current language), so that the gallery delivers the same asset * WordPress would deliver in that context. * - * The asset is left untouched when there is no contextualized attachment, or when it - * isn't synced yet. In that case it gets queued for sync when auto sync is enabled, like - * attachments found in the post content. + * The asset is left untouched when there is no contextualized attachment, or when it can't + * be delivered from Cloudinary yet. That follows the same rule as the rest of the delivery + * (Media::cloudinary_id()), which also queues the attachment for sync when needed. * * @param array $attachment The gallery asset, as stored in the block attributes. * @@ -578,12 +578,7 @@ protected function contextualize_asset( $attachment ) { return $attachment; } - $sync = $this->media->sync; - if ( ! $sync->is_synced( $contextualized_id ) ) { - if ( $sync->is_auto_sync_enabled() ) { - $sync->add_to_sync( $contextualized_id ); - } - + if ( empty( $this->media->cloudinary_id( $contextualized_id ) ) ) { return $attachment; } diff --git a/php/media/class-global-transformations.php b/php/media/class-global-transformations.php index 740dd6e8a..b2510eddf 100644 --- a/php/media/class-global-transformations.php +++ b/php/media/class-global-transformations.php @@ -368,7 +368,7 @@ public function get_terms( $post_id ) { $terms = array(); if ( ! empty( $items ) ) { $items = array_map( - function ( $item ) { + function ( $item ) use ( $post_id ) { // Get the id. if ( false !== strpos( $item, ':' ) ) { $parts = explode( ':', $item ); @@ -382,11 +382,30 @@ function ( $item ) { return null; } - // Return if term is valid. if ( $term instanceof \WP_Term ) { + /** + * Filter a term of the post's term transformations ordering. + * + * Allows mapping a stored term to the one that applies in the post's context, e.g. its + * translation in the post's language when the ordering was copied from another post. + * + * @hook cloudinary_contextualized_term + * @since 3.3.10 + * + * @param \WP_Term|null $term The term to use, initially the stored term. Null keeps the stored term. + * @param int $post_id The post ID. + * + * @return \WP_Term|null + */ + $contextualized_term = apply_filters( 'cloudinary_contextualized_term', $term, $post_id ); + + if ( $contextualized_term instanceof \WP_Term ) { + $term = $contextualized_term; + } + return array( 'term' => $term, - 'value' => $item, + 'value' => $term->taxonomy . ':' . $term->term_id, ); } @@ -715,7 +734,9 @@ function ( $taxonomy ) use ( $global ) { add_action( $taxonomy . '_add_form_fields', array( $global, 'add_taxonomy_fields' ) ); add_action( $taxonomy . '_edit_form_fields', array( $global, 'edit_taxonomy_fields' ) ); add_action( 'create_' . $taxonomy, array( $global, 'save_taxonomy_custom_meta' ) ); - add_action( 'edited_' . $taxonomy, array( $global, 'save_taxonomy_custom_meta' ) ); + // Save on edit, before the edited_term and saved_term hooks, so plugins syncing term meta + // on those hooks (e.g. WPML copying it to translations) get the new values. + add_action( 'edit_' . $taxonomy, array( $global, 'save_taxonomy_custom_meta' ) ); }, $taxonomies ); diff --git a/tests/phpunit/tests/test-gallery-contextualize-asset.php b/tests/phpunit/tests/test-gallery-contextualize-asset.php index 7fc2b5cc3..c0faf7b76 100644 --- a/tests/phpunit/tests/test-gallery-contextualize-asset.php +++ b/tests/phpunit/tests/test-gallery-contextualize-asset.php @@ -6,7 +6,7 @@ * a multilingual plugin (e.g. WPML Media Translation) maps an attachment to a different one for * the current language, the gallery must deliver that one instead, like core blocks do. * - * The Media and Sync dependencies are stubbed so no Cloudinary account is needed. + * The Media dependency is stubbed so no Cloudinary account is needed. * * @package Cloudinary */ @@ -56,23 +56,17 @@ protected function get_asset() { } /** - * Build a gallery wired to stubbed media and sync components. + * Build a gallery wired to a stubbed media component. * - * @param array $synced Map of attachment ID => public ID for synced attachments. - * @param bool $auto_sync Whether auto sync is enabled. + * @param array $deliverable Map of attachment ID => public ID for attachments that can be delivered from Cloudinary. * @param array $transformations Map of attachment ID => transformations. * * @return Test_Gallery_Contextualize_Asset_Gallery */ - protected function get_gallery( array $synced, $auto_sync = true, array $transformations = array() ) { - $sync = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Sync::class ) )->newInstanceWithoutConstructor(); - $sync->synced = array_keys( $synced ); - $sync->auto_sync = $auto_sync; - + protected function get_gallery( array $deliverable, array $transformations = array() ) { $media = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Media::class ) )->newInstanceWithoutConstructor(); - $media->public_ids = $synced; + $media->public_ids = $deliverable; $media->transformations = $transformations; - $media->sync = $sync; $gallery = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Gallery::class ) )->newInstanceWithoutConstructor(); $gallery->media = $media; @@ -128,7 +122,6 @@ public function test_asset_unchanged_without_contextualization() { $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); - $this->assertSame( array(), $gallery->media->sync->queued ); } /** @@ -156,30 +149,16 @@ public function test_asset_swapped_for_synced_contextualized_attachment() { } /** - * A contextualized attachment that isn't synced has no up-to-date public ID to deliver, so the - * original asset is kept, and the attachment is queued for sync. + * A contextualized attachment that can't be delivered from Cloudinary yet (e.g. its file isn't + * synced) keeps the original asset, like the rest of the delivery. * * @return void */ - public function test_unsynced_contextualized_attachment_kept_and_queued() { + public function test_asset_unchanged_when_contextualized_attachment_not_deliverable() { $this->contextualize_to( $this->translated_id ); $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); - $this->assertSame( array( $this->translated_id ), $gallery->media->sync->queued ); - } - - /** - * Without auto sync, an unsynced contextualized attachment isn't queued. - * - * @return void - */ - public function test_unsynced_contextualized_attachment_not_queued_without_auto_sync() { - $this->contextualize_to( $this->translated_id ); - $gallery = $this->get_gallery( array( $this->original_id => 'original' ), false ); - - $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); - $this->assertSame( array(), $gallery->media->sync->queued ); } /** @@ -192,7 +171,6 @@ public function test_asset_unchanged_when_no_contextualized_attachment() { $gallery = $this->get_gallery( array( $this->original_id => 'original' ) ); $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); - $this->assertSame( array(), $gallery->media->sync->queued ); } /** @@ -208,7 +186,6 @@ public function test_render_delivers_contextualized_attachment_with_its_transfor $this->original_id => 'original', $this->translated_id => 'translated', ), - true, array( $this->translated_id => $sepia ) ); @@ -339,6 +316,17 @@ public function get_public_id( $attachment_id, $suffixed = false ) { return isset( $this->public_ids[ $attachment_id ] ) ? $this->public_ids[ $attachment_id ] : null; } + /** + * Get the Cloudinary ID, if the attachment can be delivered from Cloudinary. + * + * @param int $attachment_id The attachment ID. + * + * @return string|false + */ + public function cloudinary_id( $attachment_id ) { + return isset( $this->public_ids[ $attachment_id ] ) ? $this->public_ids[ $attachment_id ] . '.jpg' : false; + } + /** * Get the attachment transformations. * @@ -353,61 +341,3 @@ public function get_transformations( $attachment_id, $transformations = array(), } } -/** - * Sync stub with in-memory sync state, recording the attachments queued for sync. - */ -class Test_Gallery_Contextualize_Asset_Sync extends \Cloudinary\Sync { - - /** - * The synced attachment IDs. - * - * @var int[] - */ - public $synced = array(); - - /** - * Whether auto sync is enabled. - * - * @var bool - */ - public $auto_sync = true; - - /** - * The attachment IDs queued for sync. - * - * @var int[] - */ - public $queued = array(); - - /** - * Check if the attachment is synced. - * - * @param int $post_id The attachment ID. - * @param bool $re_check Unused. - * - * @return bool - */ - public function is_synced( $post_id, $re_check = false ) { - return in_array( (int) $post_id, $this->synced, true ); - } - - /** - * Check if auto sync is enabled. - * - * @return bool - */ - public function is_auto_sync_enabled() { - return $this->auto_sync; - } - - /** - * Record the attachment as queued for sync. - * - * @param int $attachment_id The attachment ID. - * - * @return void - */ - public function add_to_sync( $attachment_id ) { - $this->queued[] = (int) $attachment_id; - } -} diff --git a/tests/phpunit/tests/test-global-transformations-terms.php b/tests/phpunit/tests/test-global-transformations-terms.php new file mode 100644 index 000000000..c59c3e4e4 --- /dev/null +++ b/tests/phpunit/tests/test-global-transformations-terms.php @@ -0,0 +1,120 @@ +global_transformations = new Global_Transformations( \Cloudinary\get_plugin_instance()->get_component( 'media' ) ); + } + + /** + * Get the Global_Transformations instance under test. + * + * @return Global_Transformations + */ + protected function get_global_transformations() { + return $this->global_transformations; + } + + /** + * The term transformations are saved on edit_{$taxonomy}, before edited_term and saved_term, + * so plugins syncing term meta on those hooks (e.g. WPML) get the new values. + * + * @return void + */ + public function test_term_meta_saved_before_edited_term() { + $callback = array( $this->get_global_transformations(), 'save_taxonomy_custom_meta' ); + + $this->assertNotFalse( has_action( 'create_category', $callback ) ); + $this->assertNotFalse( has_action( 'edit_category', $callback ) ); + $this->assertFalse( has_action( 'edited_category', $callback ) ); + } + + /** + * The stored term ordering is returned as is without contextualization. + * + * @return void + */ + public function test_get_terms_returns_stored_terms() { + $post_id = self::factory()->post->create(); + $first = self::factory()->category->create(); + $second = self::factory()->category->create(); + update_post_meta( $post_id, Global_Transformations::META_ORDER_KEY . '_terms', array( 'category:' . $second, 'category:' . $first ) ); + + $terms = array_values( $this->get_global_transformations()->get_terms( $post_id ) ); + + $this->assertSame( array( $second, $first ), array_map( 'intval', wp_list_pluck( wp_list_pluck( $terms, 'term' ), 'term_id' ) ) ); + $this->assertSame( array( 'category:' . $second, 'category:' . $first ), wp_list_pluck( $terms, 'value' ) ); + } + + /** + * Stored terms are contextualized for the post, in the stored order, e.g. to their translations + * when the ordering was copied from the original post. + * + * @return void + */ + public function test_get_terms_contextualizes_stored_terms() { + $post_id = self::factory()->post->create(); + $first = self::factory()->category->create(); + $second = self::factory()->category->create(); + $second_translated = self::factory()->category->create(); + update_post_meta( $post_id, Global_Transformations::META_ORDER_KEY . '_terms', array( 'category:' . $second, 'category:' . $first ) ); + + $filter = static function ( $term, $filtered_post_id ) use ( $post_id, $second, $second_translated ) { + if ( $post_id === $filtered_post_id && $second === $term->term_id ) { + return get_term( $second_translated, 'category' ); + } + + return $term; + }; + add_filter( 'cloudinary_contextualized_term', $filter, 10, 2 ); + + $terms = array_values( $this->get_global_transformations()->get_terms( $post_id ) ); + + $this->assertSame( array( $second_translated, $first ), array_map( 'intval', wp_list_pluck( wp_list_pluck( $terms, 'term' ), 'term_id' ) ) ); + $this->assertSame( array( 'category:' . $second_translated, 'category:' . $first ), wp_list_pluck( $terms, 'value' ) ); + } + + /** + * A filter returning something other than a term keeps the stored term. + * + * @return void + */ + public function test_get_terms_ignores_invalid_contextualized_term() { + $post_id = self::factory()->post->create(); + $term_id = self::factory()->category->create(); + update_post_meta( $post_id, Global_Transformations::META_ORDER_KEY . '_terms', array( 'category:' . $term_id ) ); + + add_filter( 'cloudinary_contextualized_term', '__return_null' ); + + $terms = array_values( $this->get_global_transformations()->get_terms( $post_id ) ); + + $this->assertSame( $term_id, $terms[0]['term']->term_id ); + } +} diff --git a/wpml-config.xml b/wpml-config.xml index 1736f4b46..5815672e4 100644 --- a/wpml-config.xml +++ b/wpml-config.xml @@ -1,6 +1,7 @@ cloudinary_apply_type_terms + cloudinary_transformations_terms cloudinary_transformations_image_freeform