]> _ Git - physioassist-wordpress.git/blob
a4761d6602b4ea9d654c69ead84053408edec47e
[physioassist-wordpress.git] /
1 <?php
2
3 /**
4  * Class WPML_Elementor_Translate_IDs
5  */
6 class WPML_Elementor_Translate_IDs implements IWPML_Action {
7
8         /** @var \WPML\Utils\DebugBackTrace */
9         private $debug_backtrace;
10
11         /**
12          * WPML_Elementor_Translate_IDs constructor.
13          *
14          * @param \WPML\Utils\DebugBackTrace $debug_backtrace
15          */
16         public function __construct( \WPML\Utils\DebugBackTrace $debug_backtrace ) {
17                 $this->debug_backtrace = $debug_backtrace;
18         }
19
20         public function add_hooks() {
21                 add_filter( 'elementor/theme/get_location_templates/template_id', array( $this, 'translate_theme_location_template_id' ) );
22                 add_filter( 'elementor/theme/get_location_templates/condition_sub_id', array( $this, 'translate_location_condition_sub_id' ), 10, 2 );
23                 add_filter( 'elementor/documents/get/post_id', array(
24                         $this,
25                         'translate_template_id'
26                 ) );
27                 add_filter( 'elementor/frontend/builder_content_data', array( $this, 'translate_global_widget_ids' ), 10, 2 );
28         }
29
30         public function translate_theme_location_template_id( $template_id ) {
31                 return $this->translate_id( $template_id );
32         }
33
34         /**
35          * @param int|string $sub_id
36          * @param array      $parsed_condition
37          *
38          * @return int|string
39          */
40         public function translate_location_condition_sub_id( $sub_id, $parsed_condition ) {
41                 /**
42                  * `$sub_name` gives a context for the `$sub_id`, it can be either:
43                  * - `child_of`
44                  * - `in_{taxonomy}`
45                  * - `{post_type}`
46                  * - `{taxonomy}`
47                  */
48                 $sub_name = isset( $parsed_condition['sub_name'] ) ? $parsed_condition['sub_name'] : null;
49
50                 if ( (int) $sub_id > 0 && $sub_name ) {
51                         $element_type = $sub_name;
52
53                         if ( 'child_of' === $sub_name ) {
54                                 $element_type = get_post_type( $sub_id );
55                         } elseif ( 0 === strpos( $sub_name, 'in_' ) ) {
56                                 $element_type = preg_replace( '/^in_/', '', $sub_name );
57                         }
58
59                         $sub_id = $this->translate_id( $sub_id, $element_type );
60                 }
61
62                 return $sub_id;
63         }
64
65         public function translate_template_id( $template_id ) {
66                 if ( $this->is_WP_widget_call() || $this->is_shortcode_call() || $this->is_template_widget_call() ) {
67                         $template_id = $this->translate_id( $template_id );
68                 }
69
70                 return $template_id;
71         }
72
73         private function is_WP_widget_call() {
74                 return $this->debug_backtrace->is_class_function_in_call_stack(
75                         'ElementorPro\Modules\Library\WP_Widgets\Elementor_Library',
76                         'widget' );
77         }
78
79         private function is_shortcode_call() {
80                 return $this->debug_backtrace->is_class_function_in_call_stack(
81                         'ElementorPro\Modules\Library\Classes\Shortcode',
82                         'shortcode' );
83         }
84
85         private function is_template_widget_call() {
86                 return $this->debug_backtrace->is_class_function_in_call_stack(
87                         'ElementorPro\Modules\Library\Widgets\Template',
88                         'render' );
89         }
90
91         public function translate_global_widget_ids( $data_array, $post_id ) {
92                 foreach ( $data_array as &$data ) {
93                         if ( isset( $data['elType'] ) && 'widget' === $data['elType'] ) {
94                                 if ( 'global' === $data['widgetType'] ) {
95                                         $data['templateID'] = $this->translate_id( $data['templateID'] );
96                                 } elseif ( 'template' === $data['widgetType'] ) {
97                                         $data['settings']['template_id'] = $this->translate_id( $data['settings']['template_id'] );
98                                 }
99                         }
100                         $data['elements'] = $this->translate_global_widget_ids( $data['elements'], $post_id );
101                 }
102
103                 return $data_array;
104         }
105
106         /**
107          * @param int    $element_id
108          * @param string $element_type
109          *
110          * @return int
111          */
112         private function translate_id( $element_id, $element_type = null ) {
113                 if ( ! $element_type || $element_type === "any_child_of" ) {
114                         $element_type = get_post_type( $element_id );
115                 }
116
117                 return apply_filters( 'wpml_object_id', $element_id, $element_type, true );
118         }
119 }