Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = function ( grunt ) {
'ui-definitions/**',
'*.php',
'readme.txt',
'wpml-config.xml',
'!**/src/**',
],
dest: '<%= dist_dir %>',
Expand Down
36 changes: 36 additions & 0 deletions php/integrations/class-wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

Expand Down Expand Up @@ -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.
*
Expand Down
42 changes: 42 additions & 0 deletions php/media/class-gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 25 additions & 4 deletions php/media/class-global-transformations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -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
);
Expand Down
Loading
Loading