class-wpml-compatibility-theme-enfold.php
4.14 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
use WPML\PB\Gutenberg\StringsInBlock\Base;
/**
* Class WPML_Compatibility_Theme_Enfold
*/
class WPML_Compatibility_Theme_Enfold {
/** @var TranslationManagement */
private $translation_management;
/**
* @param TranslationManagement $translation_management
*/
public function __construct( TranslationManagement $translation_management ) {
$this->translation_management = $translation_management;
}
public function init_hooks() {
add_action( 'wp_insert_post', array( $this, 'wp_insert_post_action' ), 10, 2 );
add_filter( 'wpml_pb_before_replace_string_with_translation', array( $this, 'replace_single_quotes' ), 10, 2 );
add_filter( 'wpml_pb_shortcode_content_for_translation', array( $this, 'get_content_from_custom_field' ), 10, 2 );
add_action( 'icl_make_duplicate', array( $this, 'sync_duplicate' ), 10, 4 );
add_filter( 'wpml_pb_is_post_built_with_shortcodes', [ $this, 'isPostBuiltWithShortcodes' ], 10, 2 );
}
/**
* Enfold's page builder is keeping the content in the custom field "_aviaLayoutBuilderCleanData" (maybe to prevent the content
* from being altered by another plugin). The standard post content will be displayed only if the field
* "_aviaLayoutBuilder_active" or "_avia_builder_shortcode_tree" does not exist.
*
* "_aviaLayoutBuilder_active" and "_avia_builder_shortcode_tree" fields should be set to "copy" in wpml-config.xml.
*
* @param int $post_id
* @param WP_Post $post
*/
public function wp_insert_post_action( $post_id, $post ) {
if ( $this->is_using_standard_wp_editor() ) {
return;
}
if ( wp_is_post_revision( $post_id ) ) {
return;
}
$is_original = apply_filters( 'wpml_is_original_content', false, $post_id, 'post_' . $post->post_type );
if ( ! $is_original && $this->is_active( $post_id ) ) {
update_post_meta( $post_id, '_aviaLayoutBuilderCleanData', $post->post_content );
}
}
/**
* @param string $content
* @param int $post_id
*
* @return string
*/
public function get_content_from_custom_field( $content, $post_id ) {
if ( $this->is_active( $post_id ) ) {
$content = str_replace( "\r\n", "\n", get_post_meta( $post_id, '_aviaLayoutBuilderCleanData', true ) );
}
if ( 'VISUAL' !== Base::get_string_type( $content ) ) {
$content = html_entity_decode( $content );
}
return $content;
}
/**
* @param int $master_post_id
* @param string $lang
* @param array $post_array
* @param int $id
*/
function sync_duplicate( $master_post_id, $lang, $post_array, $id ) {
if ( $this->is_active( $master_post_id ) ) {
$data = get_post_meta( $master_post_id, '_aviaLayoutBuilderCleanData', true );
update_post_meta( $id, '_aviaLayoutBuilderCleanData', $data );
}
}
/**
* @param int $post_id
*
* @return bool
*/
private function is_active( $post_id ) {
$page_builder_active = get_post_meta( $post_id, '_aviaLayoutBuilder_active', true );
$page_builder_shortcode_tree = get_post_meta( $post_id, '_avia_builder_shortcode_tree', true );
return $page_builder_active && $page_builder_shortcode_tree !== '';
}
/**
* @return bool
*/
private function is_using_standard_wp_editor() {
$doc_translation_method = isset( $this->translation_management->settings['doc_translation_method'] ) ?
$this->translation_management->settings['doc_translation_method'] :
ICL_TM_TMETHOD_MANUAL;
return (string) ICL_TM_TMETHOD_MANUAL === (string) $doc_translation_method;
}
/**
* Enfold/Avia replaces "'" with "’" in enfold/onfig-templatebuilder/avia-template-builder/assets/js/avia-builder.js:1312
* We just follow the same replacement pattern for string translations
*
* @param null|string $translation
* @param bool $is_attribute
*
* @return null|string
*/
public function replace_single_quotes( $translation, $is_attribute ) {
if ( $translation && $is_attribute ) {
$translation = preg_replace( "/'/", '’', $translation );
}
return $translation;
}
/**
* @param bool $isBuiltWithShortcodes
* @param WP_Post $post
*
* @return bool
*/
public function isPostBuiltWithShortcodes( $isBuiltWithShortcodes, \WP_Post $post ) {
return $isBuiltWithShortcodes || $this->is_active( $post->ID );
}
}