class-wpml-compatibility-tiny-compress-images.php
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
class WPML_Compatibility_Tiny_Compress_Images {
/** @var \WPML_Translation_Element_Factory */
private $element_factory;
/**
* WPML_Compatibility_Tiny_Compress_Images constructor.
*
* @param \WPML_Translation_Element_Factory $element_factory
*/
function __construct( WPML_Translation_Element_Factory $element_factory ) {
$this->element_factory = $element_factory;
}
public function add_hooks() {
add_action( 'updated_tiny_postmeta', array( $this, 'updated_tiny_postmeta_action' ), 10, 3 );
}
/**
* @param int $post_id
* @param string $meta_key
* @param mixed $meta_value
*/
public function updated_tiny_postmeta_action( $post_id, $meta_key, $meta_value ) {
$attachment = $this->element_factory->create_post( $post_id );
$translations = $attachment->get_translations();
if ( ! $translations ) {
return;
}
$attached_file = get_attached_file( $post_id );
/** @var WPML_Translation_Element $translation */
foreach ( $translations as $translation ) {
$translation_id = $translation->get_id();
if ( $translation_id !== (int) $post_id && $this->source_and_translation_matches( $attached_file, $translation_id ) ) {
update_post_meta( $translation_id, $meta_key, $meta_value );
}
}
}
private function source_and_translation_matches( $source_attachment_file, $translated_attachment_id ) {
return get_attached_file( $translated_attachment_id ) === $source_attachment_file;
}
}