3 use WPML\PB\Gutenberg\StringsInBlock\Base;
6 * Class WPML_Compatibility_Theme_Enfold
8 class WPML_Compatibility_Theme_Enfold {
9 /** @var TranslationManagement */
10 private $translation_management;
13 * @param TranslationManagement $translation_management
15 public function __construct( TranslationManagement $translation_management ) {
16 $this->translation_management = $translation_management;
20 public function init_hooks() {
21 add_action( 'wp_insert_post', array( $this, 'wp_insert_post_action' ), 10, 2 );
22 add_filter( 'wpml_pb_before_replace_string_with_translation', array( $this, 'replace_single_quotes' ), 10, 2 );
23 add_filter( 'wpml_pb_shortcode_content_for_translation', array( $this, 'get_content_from_custom_field' ), 10, 2 );
24 add_action( 'icl_make_duplicate', array( $this, 'sync_duplicate' ), 10, 4 );
28 * Enfold's page builder is keeping the content in the custom field "_aviaLayoutBuilderCleanData" (maybe to prevent the content
29 * from being altered by another plugin). The standard post content will be displayed only if the field
30 * "_aviaLayoutBuilder_active" or "_avia_builder_shortcode_tree" does not exist.
32 * "_aviaLayoutBuilder_active" and "_avia_builder_shortcode_tree" fields should be set to "copy" in wpml-config.xml.
35 * @param WP_Post $post
37 public function wp_insert_post_action( $post_id, $post ) {
38 if ( $this->is_using_standard_wp_editor() ) {
42 $is_original = apply_filters( 'wpml_is_original_content', false, $post_id, 'post_' . $post->post_type );
44 if ( ! $is_original && $this->is_active( $post_id ) ) {
45 update_post_meta( $post_id, '_aviaLayoutBuilderCleanData', $post->post_content );
50 * @param string $content
55 public function get_content_from_custom_field( $content, $post_id ) {
57 if ( $this->is_active( $post_id ) ) {
58 $content = str_replace( "\r\n", "\n", get_post_meta( $post_id, '_aviaLayoutBuilderCleanData', true ) );
61 if ( 'VISUAL' !== Base::get_string_type( $content ) ) {
62 $content = html_entity_decode( $content );
69 * @param int $master_post_id
71 * @param array $post_array
74 function sync_duplicate( $master_post_id, $lang, $post_array, $id ) {
75 if ( $this->is_active( $master_post_id ) ) {
76 $data = get_post_meta( $master_post_id, '_aviaLayoutBuilderCleanData', true );
77 update_post_meta( $id, '_aviaLayoutBuilderCleanData', $data );
86 private function is_active( $post_id ) {
87 $page_builder_active = get_post_meta( $post_id, '_aviaLayoutBuilder_active', true );
88 $page_builder_shortcode_tree = get_post_meta( $post_id, '_avia_builder_shortcode_tree', true );
90 return $page_builder_active && $page_builder_shortcode_tree !== '';
96 private function is_using_standard_wp_editor() {
97 $doc_translation_method = isset( $this->translation_management->settings['doc_translation_method'] ) ?
98 $this->translation_management->settings['doc_translation_method'] :
99 ICL_TM_TMETHOD_MANUAL;
101 return (string) ICL_TM_TMETHOD_MANUAL === (string) $doc_translation_method;
105 * Enfold/Avia replaces "'" with "’" in enfold/onfig-templatebuilder/avia-template-builder/assets/js/avia-builder.js:1312
106 * We just follow the same replacement pattern for string translations
108 * @param null|string $translation
109 * @param bool $is_attribute
111 * @return null|string
113 public function replace_single_quotes( $translation, $is_attribute ) {
114 if ( $translation && $is_attribute ) {
115 $translation = preg_replace( "/'/", '’', $translation );