class-attributes.php
6.05 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
<?php
namespace WPML\PB\Gutenberg\StringsInBlock;
use WPML\FP\Obj;
class Attributes extends Base {
/**
* @param \WP_Block_Parser_Block $block
*
* @return array
*/
public function find( \WP_Block_Parser_Block $block ) {
$strings = [];
$attrs = $this->getAttributes( $block );
if ( $attrs ) {
$keys = $this->getKeyConfig( $block );
$strings = $this->findStringsRecursively( $attrs, $keys, $block );
}
return $strings;
}
/**
* @param array $attrs
* @param array $config_keys
* @param \WP_Block_Parser_Block $block
*
* @return array
*/
private function findStringsRecursively( array $attrs, array $config_keys, \WP_Block_Parser_Block $block ) {
$strings = [];
foreach ( $attrs as $attr_key => $attr_value ) {
$matching_key = $this->getMatchingConfigKey( $attr_key, $config_keys );
if ( ! $matching_key ) {
continue;
}
if ( $this->hasJsonEncoding( $attr_key, $config_keys ) ) {
$attr_value = json_decode( urldecode( $attr_value ), true );
}
if ( is_array( $attr_value ) ) {
$children_config_keys = $this->getChildrenConfigKeys( $config_keys, $matching_key );
$strings = array_merge(
$strings,
$this->findStringsRecursively( $attr_value, $children_config_keys, $block )
);
} elseif ( ! is_numeric( $attr_value ) ) {
$type = self::get_string_type( $attr_value );
$string_id = $this->get_string_id( $block->blockName, $attr_value );
$label = isset( $config_keys[ $attr_key ]['label'] ) ? $config_keys[ $attr_key ]['label'] : $this->get_block_label( $block );
$strings[] = $this->build_string( $string_id, $label, $attr_value, $type );
}
}
return $strings;
}
/**
* @param string $attr_key
* @param array $config_keys
*
* @return string|null
*/
private function getMatchingConfigKey( $attr_key, array $config_keys ) {
if ( isset( $config_keys[ $attr_key ] ) ) {
return $attr_key;
}
/**
* If we don't find an exactly matching key,
* we'll try to find a key with a wildcard or a regex.
*/
foreach ( $config_keys as $config_key => $key_attrs ) {
if ( preg_match( $this->getRegex( $config_key, $key_attrs ), $attr_key ) ) {
return $config_key;
}
}
return null;
}
/**
* @param array $config_keys
* @param string $matching_key
*
* @return array
*/
private function getChildrenConfigKeys( array $config_keys, $matching_key ) {
return isset( $config_keys[ $matching_key ]['children'] )
? $config_keys[ $matching_key ]['children']
: $this->getMatchAllKey();
}
/**
* If the config key is not already a regex
* we will replace the wildcard (*) and make it a valid regex.
*
* @param string $config_key
* @param array $key_attrs
*
* @return string
*/
private function getRegex( $config_key, array $key_attrs ) {
if ( $this->isRegex( $key_attrs ) ) {
return $config_key;
}
return self::getWildcardRegex( $config_key );
}
/**
* @param string $config_key
*
* @return string
*/
public static function getWildcardRegex( $config_key ) {
return '/^' . str_replace( '*', 'S+', preg_quote( $config_key, '/' ) ) . '$/';;
}
/**
* @param array $key_attrs
*
* @return bool
*/
private function isRegex( array $key_attrs ) {
return isset( $key_attrs['search-method'] )
&& \WPML_Gutenberg_Config_Option::SEARCH_METHOD_REGEX === $key_attrs['search-method'];
}
/**
* @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 ) {
$attrs = $this->getAttributes( $block );
if ( $attrs ) {
$keys = $this->getKeyConfig( $block );
$block->attrs = $this->updateStringsRecursively( $attrs, $keys, $string_translations, $lang, $block->blockName );
}
return $block;
}
/**
* @param array $attrs
* @param array $config_keys
* @param array $translations
* @param string $lang
* @param string $block_name
*
* @return array
*/
public function updateStringsRecursively( array $attrs, array $config_keys, array $translations, $lang, $block_name ) {
foreach ( $attrs as $attr_key => $attr_value ) {
$matching_key = $this->getMatchingConfigKey( $attr_key, $config_keys );
if ( ! $matching_key ) {
continue;
}
if ( $this->hasJsonEncoding( $attr_key, $config_keys ) ) {
$attr_value = json_decode( urldecode( $attr_value ), true );
}
if ( is_array( $attr_value ) ) {
$children_config_keys = $this->getChildrenConfigKeys( $config_keys, $matching_key );
$attrs[ $attr_key ] = $this->updateStringsRecursively( $attr_value, $children_config_keys, $translations, $lang, $block_name );
} else {
$string_id = $this->get_string_id( $block_name, $attr_value );
if (
isset( $translations[ $string_id ][ $lang ] ) &&
ICL_TM_COMPLETE === (int) $translations[ $string_id ][ $lang ]['status']
) {
$attrs[ $attr_key ] = $translations[ $string_id ][ $lang ]['value'];
}
}
if ( $this->hasJsonEncoding( $attr_key, $config_keys ) ) {
$attrs[ $attr_key ] = rawurlencode( wp_json_encode( $attrs[ $attr_key ] ) );
}
}
return $attrs;
}
/**
* @param array $attr_key
* @param array $config_keys
*
* @retrun bool
*/
private function hasJsonEncoding( $attr_key, $config_keys ) {
return 'json' === Obj::path( [ $attr_key, 'encoding' ], $config_keys );
}
/**
* @param \WP_Block_Parser_Block $block
*
* @return array
*/
private function getAttributes( \WP_Block_Parser_Block $block ) {
return is_array( $block->attrs ) && $block->blockName ? $block->attrs : [];
}
/**
* @param \WP_Block_Parser_Block $block
*
* @return array
*/
private function getKeyConfig( \WP_Block_Parser_Block $block ) {
$config = $this->get_block_config( $block, 'key' );
return $config ? $config : [];
}
/**
* @return array
*/
private function getMatchAllKey() {
return [
'*' => [
'search-method' => \WPML_Gutenberg_Config_Option::SEARCH_METHOD_WILDCARD,
],
];
}
}