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/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 a706b2b1d..2432ebe49 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,46 @@ 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 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. + * + * @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 ) { + return $attachment; + } + + if ( empty( $this->media->cloudinary_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/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 new file mode 100644 index 000000000..c0faf7b76 --- /dev/null +++ b/tests/phpunit/tests/test-gallery-contextualize-asset.php @@ -0,0 +1,343 @@ +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. + * + * @return array + */ + 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. + * + * @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 $deliverable, array $transformations = array() ) { + $media = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Media::class ) )->newInstanceWithoutConstructor(); + $media->public_ids = $deliverable; + $media->transformations = $transformations; + + $gallery = ( new ReflectionClass( Test_Gallery_Contextualize_Asset_Gallery::class ) )->newInstanceWithoutConstructor(); + $gallery->media = $media; + $gallery->set_plugin( \Cloudinary\get_plugin_instance() ); + + 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 ) { + $original_id = $this->original_id; + add_filter( + 'cloudinary_contextualized_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( $this->original_id => 'original' ) ); + + $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_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( $this->translated_id ); + $gallery = $this->get_gallery( + array( + $this->original_id => 'original', + $this->translated_id => 'translated', + ) + ); + + $this->assertSame( + array( + 'attachmentId' => $this->translated_id, + 'publicId' => 'translated', + ), + $gallery->contextualize_asset( $this->get_asset() ) + ); + } + + /** + * 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_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() ) ); + } + + /** + * 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( $this->original_id => 'original' ) ); + + $this->assertSame( $this->get_asset(), $gallery->contextualize_asset( $this->get_asset() ) ); + } + + /** + * 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', + ), + 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, 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. + * + * @param array $attachment The gallery asset. + * + * @return array + */ + 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 and transformations. + */ +class Test_Gallery_Contextualize_Asset_Media extends \Cloudinary\Media { + + /** + * Map of attachment ID => public ID. + * + * @var array + */ + public $public_ids = array(); + + /** + * Map of attachment ID => transformations. + * + * @var array + */ + public $transformations = 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; + } + + /** + * 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. + * + * @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(); + } +} + 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 new file mode 100644 index 000000000..5815672e4 --- /dev/null +++ b/wpml-config.xml @@ -0,0 +1,10 @@ + + + cloudinary_apply_type_terms + cloudinary_transformations_terms + + + cloudinary_transformations_image_freeform + cloudinary_transformations_video_freeform + +