Skip to content

Commit

Permalink
Various fixes and improvements with WPML
Browse files Browse the repository at this point in the history
  • Loading branch information
av3nger committed Mar 16, 2024
1 parent 18b2639 commit 7af8338
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Changed:

Fixed:
* Image size can now be changed in the Gutenberg image block for fully offloaded images
* Full size images not replaced in the gallery block on expand
* Full size images not replaced in the gallery block on expand
* Multiple fixes and improvements with the WPML integration

= 1.8.0 - 16.02.2024 =

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Changed:
Fixed:
* Image size can now be changed in the Gutenberg image block for fully offloaded images
* Full size images not replaced in the gallery block on expand
* Multiple fixes and improvements with the WPML integration

= 1.8.0 - 16.02.2024 =

Expand Down
5 changes: 5 additions & 0 deletions app/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
return $metadata;
}

// This is used with WPML integration.
$attachment_id = apply_filters( 'cf_images_media_post_id', $attachment_id );

$mime = get_post_mime_type( $attachment_id );

if ( ! wp_attachment_is_image( $attachment_id ) || false !== strpos( $mime, 'image/svg' ) ) {
Expand Down Expand Up @@ -433,6 +436,8 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
update_post_meta( $attachment_id, '_cloudflare_image_id', $results->id );
$this->maybe_save_hash( $results->variants );

do_action( 'cf_images_upload_success', $attachment_id, $results );

if ( doing_filter( 'wp_async_wp_generate_attachment_metadata' ) ) {
$this->fetch_stats( new Api\Image() );
}
Expand Down
37 changes: 37 additions & 0 deletions app/integrations/class-wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace CF_Images\App\Integrations;

use stdClass;

if ( ! defined( 'WPINC' ) ) {
die;
}
Expand All @@ -34,6 +36,7 @@ public function __construct() {
add_action( 'cf_images_before_wp_query', array( $this, 'remove_wpml_filters' ) );
add_action( 'wpml_after_duplicate_attachment', array( $this, 'ignore_attachment' ), 10, 2 );
add_action( 'wpml_after_copy_attached_file_postmeta', array( $this, 'ignore_attachment' ), 10, 2 );
add_action( 'cf_images_upload_success', array( $this, 'update_image_meta' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -85,6 +88,40 @@ public function remove_wpml_filters() {
* @since 1.4.0
*/
public function ignore_attachment( int $attachment_id, int $duplicated_attachment_id ) {
$original = $this->get_original_image_id( $attachment_id );

// When uploading an image from the "non-original" language, the parameters are swapped.
if ( $original === $duplicated_attachment_id ) {
$duplicated_attachment_id = $attachment_id;
}

update_post_meta( $duplicated_attachment_id, '_cloudflare_image_skip', true );
}

/**
* Update the meta for all images.
*
* @since 1.8.1
*
* @param int $attachment_id Original attachment ID.
* @param stdClass $results Upload results.
*/
public function update_image_meta( int $attachment_id, stdClass $results ) {
global $sitepress;

if ( ! $sitepress || ! method_exists( $sitepress, 'get_element_trid' ) ) {
return;
}

$translation_id = $sitepress->get_element_trid( $attachment_id, 'post_attachment' );
$translations = $sitepress->get_element_translations( $translation_id, 'post_attachment', true );

foreach ( $translations as $translation ) {
if ( $translation->original ) {
continue;
}

update_post_meta( $translation->element_id, '_cloudflare_image_id', $results->id );
}
}
}

0 comments on commit 7af8338

Please sign in to comment.