class-html.php
7.42 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace WPML\PB\Gutenberg\StringsInBlock;
use WPML\FP\Str;
use WPML\FP\Obj;
use WPML\PB\Gutenberg\StringsInBlock\DOMHandler\DOMHandle;
use WPML\PB\Gutenberg\StringsInBlock\DOMHandler\HtmlBlock;
use WPML\PB\Gutenberg\StringsInBlock\DOMHandler\StandardBlock;
use WPML\PB\Gutenberg\StringsInBlock\DOMHandler\ListBlock;
use WPML\PB\Gutenberg\StringsInBlock\DOMHandler\ListItemBlock;
use WPML\PB\Gutenberg\XPath;
class HTML extends Base {
const LIST_BLOCK_NAME = 'core/list';
const LIST_ITEM_BLOCK_NAME = 'core/list-item';
const HTML_BLOCK_NAME = 'core/html';
/**
* @param \WP_Block_Parser_Block $block
*
* @return array
*/
public function find( \WP_Block_Parser_Block $block ) {
$strings = array();
$block_queries = $this->get_block_queries( $block );
if ( is_array( $block_queries ) && isset( $block->innerHTML ) ) {
$dom_handle = $this->get_dom_handler( $block );
$xpath = $dom_handle->getDomxpath( $block->innerHTML );
foreach ( $block_queries as $blockQuery ) {
list( $query, $definedType, $label ) = XPath::parse( $blockQuery );
$elements = $xpath->query( $query );
foreach ( $elements as $element ) {
list( $text, $type ) = $dom_handle->getPartialInnerHTML( $element );
if ( $text ) {
$string_id = $this->get_string_id( $block->blockName, $text );
$strings[] = $this->build_string(
$string_id,
$label ?: $this->get_block_label( $block ),
$text,
$definedType ? $definedType : $type
);
}
}
}
} else {
$string_id = $this->get_block_string_id( $block );
if ( $string_id ) {
$strings[] = $this->build_string(
$string_id,
$this->get_block_label( $block ),
$block->innerHTML,
'VISUAL'
);
}
}
return $strings;
}
/**
* @param \WP_Block_Parser_Block $block
* @param array $string_translations
* @param string $lang
*
* @return \WP_Block_Parser_Block
*/
public function update( \WP_Block_Parser_Block $block, array $string_translations, $lang ) {
$block_queries = $this->get_block_queries( $block );
if ( $block_queries && isset( $block->innerHTML ) ) {
$dom_handle = $this->get_dom_handler( $block );
$dom = $dom_handle->getDom( $block->innerHTML );
$xpath = new \DOMXPath( $dom );
foreach ( $block_queries as $query ) {
list( $query, ) = XPath::parse( $query );
$elements = $xpath->query( $query );
foreach ( $elements as $element ) {
list( $text, ) = $dom_handle->getPartialInnerHTML( $element );
$translation = $this->getTranslation( $text, $lang, $block, $string_translations );
$block = $this->updateTranslationInBlock(
$text,
$this->apply_placeholders_for_html_entities( $translation ),
$block,
$element,
$dom_handle
);
}
}
$content = $dom_handle->getFullInnerHTML( $dom->documentElement );
$block->innerHTML = $this->restore_placeholders_for_html_entities( reset( $content ) );
} elseif ( isset( $block->blockName, $block->innerHTML ) && '' !== trim( $block->innerHTML ) ) {
$translation = $this->getTranslation( $block->innerHTML, $lang, $block, $string_translations );
if ( $translation ) {
$block->innerHTML = $translation;
}
}
return $block;
}
/**
* @param \WP_Block_Parser_Block $block
*
* @return null|string
*/
private function get_block_string_id( \WP_Block_Parser_Block $block ) {
if ( isset( $block->blockName, $block->innerHTML ) && '' !== trim( $block->innerHTML ) ) {
return $this->get_string_id( $block->blockName, $block->innerHTML );
} else {
return null;
}
}
/**
* @param \WP_Block_Parser_Block $block
*
* @return array|null
*/
private function get_block_queries( \WP_Block_Parser_Block $block ) {
return $this->get_block_config( $block, 'xpath' );
}
/**
* @param \WP_Block_Parser_Block $block
*
* @return ListBlock|StandardBlock|HtmlBlock
*/
private function get_dom_handler( \WP_Block_Parser_Block $block ) {
$class = wpml_collect(
[
self::LIST_BLOCK_NAME => ListBlock::class,
self::HTML_BLOCK_NAME => HtmlBlock::class,
self::LIST_ITEM_BLOCK_NAME => ListItemBlock::class,
]
)->get( $block->blockName, StandardBlock::class );
return new $class();
}
/**
* @param string $text
* @param string $translation
* @param \WP_Block_Parser_Block $block
* @param \DOMNode $element
* @param DOMHandle $dom_handle
*
* @return \WP_Block_Parser_Block
*/
private function updateTranslationInBlock( $text, $translation, \WP_Block_Parser_Block $block, $element, $dom_handle ) {
if ( $translation ) {
$block = $dom_handle->applyStringTranslations( $block, $element, $translation, $text );
$dom_handle->setElementValue( $element, $translation );
}
return $block;
}
private function getTranslation( $text, $lang, \WP_Block_Parser_Block $block, array $string_translations ) {
$translationFromPageBuilder = apply_filters( 'wpml_pb_update_translations_in_content', $text, $lang );
if ( $translationFromPageBuilder === $text ) {
$string_id = $this->get_string_id( $block->blockName, $text );
if ( (int) Obj::path( [ $string_id, $lang, 'status' ], $string_translations ) === ICL_TM_COMPLETE ) {
return self::preserveNewLines( $text, $string_translations[ $string_id ][ $lang ]['value'] );
} else {
return null;
}
} else {
return $translationFromPageBuilder;
}
}
private static function preserveNewLines( $original, $translation ) {
$endsWith = function ( $find, $s ) {
return Str::sub( - Str::len( $find ), $s ) === $find; // @phpstan-ignore-line
};
if ( Str::startsWith( "\n", $original ) && ! Str::startsWith( "\n", $translation ) ) {
$translation = "\n" . $translation;
}
if ( $endsWith( "\n", $original ) && ! $endsWith( "\n", $translation ) ) {
$translation .= "\n";
}
return $translation;
}
/**
* HTML_ENTITY_PLACEHOLDERS
* Some translations are applied using \DomHandler, which converts any HTML entity
* back to it's character, i.e. ' becomes '.
* At some places (like shortcode attributes) it breaks the attribute value, because
* the delimter can use the same kind of quotes, i.e. [my attr='Some'value'] => broken.
* To avoid this problem the HTML entities are replaced before parsing the content with
* \DomDocument::loadHTML() and re-applied afterwards.
*/
const HTML_ENTITY_PLACEHOLDERS = [
''' => 'WPML_PLACEHOLDER_APOS',
'"' => 'WPML_PLACEHOLDER_QUOT',
];
/**
* Replaces HTML entities with WPML entity placeholders in given $content.
* See self::HTML_ENTITY_PLACEHOLDERS for affected entities.
*
* @param string $content
*
* @return string
*/
private function apply_placeholders_for_html_entities( $content ) {
if ( empty( $content ) ) {
return $content;
}
return str_replace(
array_keys( self::HTML_ENTITY_PLACEHOLDERS ),
array_values( self::HTML_ENTITY_PLACEHOLDERS ),
$content
);
}
/**
* Replaces WPML entity placeholders with HTML entities in given $content.
* See self::HTML_ENTITY_PLACEHOLDERS for affected entities.
*
* @param string $content
*
* @return string
*/
private function restore_placeholders_for_html_entities( $content ) {
if ( empty( $content ) ) {
return $content;
}
return str_replace(
array_values( self::HTML_ENTITY_PLACEHOLDERS ),
array_keys( self::HTML_ENTITY_PLACEHOLDERS ),
$content
);
}
}