images_updater = $images_updater; $this->sitepress = $sitepress; $this->iclTranslationManagement = $iclTranslationManagement; } public function add_hooks() { add_action( 'updated_post_meta', array( $this, 'translate_images' ), PHP_INT_MAX, 4 ); add_action( 'added_post_meta', array( $this, 'translate_images' ), PHP_INT_MAX, 4 ); } /** * @param int $meta_id * @param int $object_id * @param string $meta_key * @param string $meta_value */ public function translate_images( $meta_id, $object_id, $meta_key, $meta_value ) { $settings_factory = new WPML_Custom_Field_Setting_Factory( $this->iclTranslationManagement ); $setting = $settings_factory->post_meta_setting( $meta_key ); $is_custom_field_translatable = $this->sitepress->get_wp_api() ->constant( 'WPML_TRANSLATE_CUSTOM_FIELD' ) === $setting->status(); $post_type = get_post_type( $object_id ); $is_post_translatable = $this->sitepress->is_translated_post_type( $post_type ); if ( is_string( $meta_value ) && $is_post_translatable && $is_custom_field_translatable ) { $post_element = new WPML_Post_Element( $object_id, $this->sitepress ); $source_language = $post_element->get_source_language_code(); if ( null !== $source_language ) { $this->filter_meta_value_and_update( $meta_value, $meta_key, $post_element->get_language_code(), $source_language, $object_id ); } else { foreach ( array_keys( $this->sitepress->get_active_languages() ) as $language ) { $translation = $post_element->get_translation( $language ); if( $translation ) { $this->filter_meta_value_and_update( $meta_value, $meta_key, $language, $source_language, $translation->get_id() ); } } } } } /** * @param string $meta_value * @param string $target_language * @param string $source_language * @param string $meta_key * @param int $post_id * * @return string */ private function filter_meta_value_and_update( $meta_value, $meta_key, $target_language, $source_language, $post_id ) { $meta_value_filtered = $this->images_updater->replace_images_with_translations( $meta_value, $target_language, $source_language ); remove_action( 'updated_post_meta', array( $this, 'translate_images' ), PHP_INT_MAX, 4 ); update_post_meta( $post_id, $meta_key, $meta_value_filtered ); add_action( 'updated_post_meta', array( $this, 'translate_images' ), PHP_INT_MAX, 4 ); return $meta_value_filtered; } }