]> _ Git - physioassist-wordpress.git/blob
0a32acb4218ddd4ee0ae87f3cee1ad82d088b9a6
[physioassist-wordpress.git] /
1 <?php
2
3 use WPML\PB\Gutenberg\StringsInBlock\Base;
4
5 /**
6  * Class WPML_Compatibility_Theme_Enfold
7  */
8 class WPML_Compatibility_Theme_Enfold {
9         /** @var TranslationManagement */
10         private $translation_management;
11
12         /**
13          * @param TranslationManagement $translation_management
14          */
15         public function __construct( TranslationManagement $translation_management ) {
16                 $this->translation_management = $translation_management;
17         }
18
19
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 );
25         }
26
27         /**
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.
31          *
32          * "_aviaLayoutBuilder_active" and "_avia_builder_shortcode_tree" fields should be set to "copy" in wpml-config.xml.
33          *
34          * @param int     $post_id
35          * @param WP_Post $post
36          */
37         public function wp_insert_post_action( $post_id, $post ) {
38                 if ( $this->is_using_standard_wp_editor() ) {
39                         return;
40                 }
41
42                 $is_original = apply_filters( 'wpml_is_original_content', false, $post_id, 'post_' . $post->post_type );
43
44                 if ( ! $is_original && $this->is_active( $post_id ) ) {
45                         update_post_meta( $post_id, '_aviaLayoutBuilderCleanData', $post->post_content );
46                 }
47         }
48
49         /**
50          * @param string $content
51          * @param int    $post_id
52          *
53          * @return string
54          */
55         public function get_content_from_custom_field( $content, $post_id ) {
56
57                 if ( $this->is_active( $post_id ) ) {
58                         $content = str_replace( "\r\n", "\n", get_post_meta( $post_id, '_aviaLayoutBuilderCleanData', true ) );
59                 }
60
61                 if ( 'VISUAL' !== Base::get_string_type( $content ) ) {
62                         $content = html_entity_decode( $content );
63                 }
64
65                 return $content;
66         }
67
68         /**
69          * @param int    $master_post_id
70          * @param string $lang
71          * @param array  $post_array
72          * @param int    $id
73          */
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 );
78                 }
79         }
80
81         /**
82          * @param int $post_id
83          *
84          * @return bool
85          */
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 );
89
90                 return $page_builder_active && $page_builder_shortcode_tree !== '';
91         }
92
93         /**
94          * @return bool
95          */
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;
100
101                 return (string) ICL_TM_TMETHOD_MANUAL === (string) $doc_translation_method;
102         }
103
104         /**
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
107          *
108          * @param null|string $translation
109          * @param bool        $is_attribute
110          *
111          * @return null|string
112          */
113         public function replace_single_quotes( $translation, $is_attribute ) {
114                 if ( $translation && $is_attribute ) {
115                         $translation = preg_replace( "/'/", '’', $translation );
116                 }
117
118                 return $translation;
119         }
120 }