PluginProbe ʕ •ᴥ•ʔ
ElementsKit Elementor Addons – Advanced Widgets & Templates Addons for Elementor / 4.0.0
ElementsKit Elementor Addons – Advanced Widgets & Templates Addons for Elementor v4.0.0
4.0.2 4.0.1 4.0.0 3.10.02 3.10.01 3.9.10 3.9.9 3.9.8 3.9.7 3.9.5 3.9.6 3.9.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.3.1 2.3.1.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.4.0 2.5.0 2.5.1 2.5.10 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.2 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.6.0 3.6.1 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.8.1 3.8.2 3.9.0 3.9.1 3.9.2 trunk 1.2.6 1.2.7 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.0.9.1 2.0.9.2 2.0.9.3
elementskit-lite / compatibility / wpml / init.php
elementskit-lite / compatibility / wpml Last commit date
init.php 3 weeks ago
init.php
176 lines
1 <?php
2 namespace ElementsKit_Lite\Compatibility\Wpml;
3
4 defined( 'ABSPATH' ) || exit;
5
6 /**
7 * Init
8 * Initiate all necessary classes, hooks, configs.
9 *
10 * @since 1.2.6
11 */
12 class Init {
13
14 /**
15 * @var self
16 */
17 private static $instance;
18
19 /**
20 * Ensures only one instance of the plugin class is loaded or can be loaded.
21 *
22 * @since 1.2.6
23 * @return self
24 */
25 public static function instance() {
26 if ( is_null( self::$instance ) ) {
27 self::$instance = new self();
28 }
29
30 return self::$instance;
31 }
32
33 /**
34 * @since 1.2.6
35 */
36 public function __construct() {
37 if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
38 add_filter( 'elementor/documents/get/post_id', [ $this, 'wpml_template_translation' ] );
39 }
40
41 // Brace-to-span bridge for every ElementsKit field that uses the "{...}" focused-text
42 // syntax (\ElementsKit_Lite\Utils::kspan): Heading title, Heading shadow text and
43 // Testimonial designation. WPML registers these under generic page-builder keys
44 // (e.g. "package-string-3-810"), so the conversion is gated on the brace/span token
45 // in the value, not on the field key — which makes it cover every kspan field.
46 add_filter( 'wpml_tm_translation_job_data', [ $this, 'rewrite_job_data' ], 10, 2 );
47 add_filter( 'wpml_tm_job_fields', [ $this, 'rewrite_job_fields' ], 10, 2 );
48 }
49
50 /**
51 * Outbound: rewrite the field value sent to ATE when the job is created so the word
52 * inside "{...}" is exposed for translation instead of treated as a placeholder token.
53 */
54 public function rewrite_job_data( $package, $post ) {
55 if ( empty( $package['contents'] ) || ! is_array( $package['contents'] ) ) {
56 return $package;
57 }
58
59 foreach ( $package['contents'] as &$field ) {
60 if ( empty( $field['data'] ) ) {
61 continue;
62 }
63
64 $is_base64 = isset( $field['format'] ) && 'base64' === $field['format'];
65 $value = $is_base64 ? base64_decode( $field['data'] ) : $field['data'];
66
67 if ( false === strpos( $value, '{' ) ) {
68 continue;
69 }
70
71 $value = $this->braces_to_span( $value );
72 $field['data'] = $is_base64 ? base64_encode( $value ) : $value;
73 }
74 unset( $field );
75
76 return $package;
77 }
78
79 /**
80 * Inbound: rewrite the translated value back to braces before it is applied.
81 */
82 public function rewrite_job_fields( $fields, $job ) {
83 if ( empty( $fields ) ) {
84 return $fields;
85 }
86
87 foreach ( $fields as &$field ) {
88 $translated = $this->get_field_prop( $field, 'field_data_translated' );
89
90 if ( '' === $translated || false === strpos( $translated, '<span>' ) ) {
91 continue;
92 }
93
94 $is_base64 = 'base64' === $this->get_field_prop( $field, 'field_format' );
95 $decoded = $is_base64 ? base64_decode( $translated ) : $translated;
96 $decoded = $this->span_to_braces( $decoded );
97 $encoded = $is_base64 ? base64_encode( $decoded ) : $decoded;
98
99 $this->set_field_prop( $field, 'field_data_translated', $encoded );
100 }
101 unset( $field );
102
103 return $fields;
104 }
105
106 /**
107 * Read a property from a job field, whether it's an object or an array.
108 *
109 * @param object|array $field
110 * @param string $key
111 * @return mixed
112 */
113 private function get_field_prop( $field, $key ) {
114 if ( is_object( $field ) ) {
115 return $field->$key ?? '';
116 }
117
118 return $field[ $key ] ?? '';
119 }
120
121 /**
122 * Write a property on a job field, whether it's an object or an array.
123 *
124 * @param object|array $field
125 * @param string $key
126 * @param mixed $value
127 */
128 private function set_field_prop( &$field, $key, $value ) {
129 if ( is_object( $field ) ) {
130 $field->$key = $value;
131 } else {
132 $field[ $key ] = $value;
133 }
134 }
135
136 /**
137 * Collapse "{{report}}" or "{report}" into a single <span>report</span> (mirrors
138 * \ElementsKit_Lite\Utils::kspan) so ATE sees an inline tag wrapping translatable text,
139 * not a placeholder token. Lone/unbalanced braces are left untouched.
140 *
141 * @param string $text
142 * @return string
143 */
144 public function braces_to_span( $text ) {
145 return preg_replace( '/\{+([^{}]*)\}+/', '<span>$1</span>', $text );
146 }
147
148 /**
149 * Restore to single-brace "{report}" form; kspan collapses braces to one span on render,
150 * so single braces reproduce the original focused-title output identically.
151 *
152 * @param string $text
153 * @return string
154 */
155 public function span_to_braces( $text ) {
156 return preg_replace( '/<span>(.*?)<\/span>/s', '{$1}', $text );
157 }
158
159 /**
160 * Get the ID in the current language or in another language you specify.
161 *
162 * @param int $element_id
163 * @return int|array Object id, or array of object ids.
164 * @since 2.6.1
165 */
166 public function wpml_template_translation( $element_id ) {
167 $element_type = get_post_type( $element_id );
168
169 if ( in_array( $element_type, [ 'elementskit_template', 'elementskit_content' ], true ) ) {
170 return apply_filters( 'wpml_object_id', $element_id, $element_type, true );
171 }
172
173 return $element_id;
174 }
175 }
176