4 * Class WPML_Elementor_Translate_IDs
6 class WPML_Elementor_Translate_IDs implements IWPML_Action {
8 /** @var \WPML\Utils\DebugBackTrace */
9 private $debug_backtrace;
12 * WPML_Elementor_Translate_IDs constructor.
14 * @param \WPML\Utils\DebugBackTrace $debug_backtrace
16 public function __construct( \WPML\Utils\DebugBackTrace $debug_backtrace ) {
17 $this->debug_backtrace = $debug_backtrace;
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(
25 'translate_template_id'
27 add_filter( 'elementor/frontend/builder_content_data', array( $this, 'translate_global_widget_ids' ), 10, 2 );
30 public function translate_theme_location_template_id( $template_id ) {
31 return $this->translate_id( $template_id );
35 * @param int|string $sub_id
36 * @param array $parsed_condition
40 public function translate_location_condition_sub_id( $sub_id, $parsed_condition ) {
42 * `$sub_name` gives a context for the `$sub_id`, it can be either:
48 $sub_name = isset( $parsed_condition['sub_name'] ) ? $parsed_condition['sub_name'] : null;
50 if ( (int) $sub_id > 0 && $sub_name ) {
51 $element_type = $sub_name;
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 );
59 $sub_id = $this->translate_id( $sub_id, $element_type );
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 );
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',
79 private function is_shortcode_call() {
80 return $this->debug_backtrace->is_class_function_in_call_stack(
81 'ElementorPro\Modules\Library\Classes\Shortcode',
85 private function is_template_widget_call() {
86 return $this->debug_backtrace->is_class_function_in_call_stack(
87 'ElementorPro\Modules\Library\Widgets\Template',
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'] );
100 $data['elements'] = $this->translate_global_widget_ids( $data['elements'], $post_id );
107 * @param int $element_id
108 * @param string $element_type
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 );
117 return apply_filters( 'wpml_object_id', $element_id, $element_type, true );