DynamicContent.php
2.62 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
<?php
namespace WPML\Compatibility\FusionBuilder;
use WPML\Compatibility\BaseDynamicContent;
class DynamicContent extends BaseDynamicContent {
/** @var array */
protected $positions = [ 'before', 'after', 'fallback' ];
/**
* Sets $positions dynamic content to be translatable.
*
* @param string|array $string The decoded string so far.
* @param string $encoding The encoding used.
*
* @return string|array
*/
public function decode_dynamic_content( $string, $encoding ) {
if ( ! $string || is_array( $string ) ) {
return $string;
}
if ( $this->is_dynamic_content( $string ) ) {
$field = $this->decode_field( $string );
$decodedContent = [
'element-content' => [
'value' => $string,
'translate' => false,
],
];
foreach ( $this->positions as $position ) {
if ( ! empty( $field[ $position ] ) ) {
$decodedContent[ $position ] = [
'value' => $field[ $position ],
'translate' => true,
];
}
}
return $decodedContent;
}
return $string;
}
/**
* Rebuilds dynamic content with translated strings.
*
* @param string|array $string The field array or string.
* @param string $encoding The encoding used.
*
* @return string
*/
public function encode_dynamic_content( $string, $encoding ) {
if ( is_array( $string ) && isset( $string['element-content'] ) ) {
$field = $this->decode_field( $string['element-content'] );
foreach ( $this->positions as $position ) {
if ( isset( $string[ $position ] ) ) {
$field[ $position ] = $string[ $position ];
}
}
return $this->encode_field( $field );
}
return $string;
}
/**
* Check if a certain field contains dynamic content.
*
* @param string $string The string to check.
*
* @return bool
*/
protected function is_dynamic_content( $string ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
return isset( json_decode( base64_decode( $string ), true )['element_content'] );
}
/**
* Decode a dynamic-content field.
*
* @param string $string The string to decode.
*
* @return array
*/
protected function decode_field( $string ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
return json_decode( base64_decode( $string ), true )['element_content'];
}
/**
* Encode a dynamic-content field.
*
* @param array $field The field to encode.
*
* @return string
*/
protected function encode_field( $field ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
return base64_encode( wp_json_encode( [ 'element_content' => $field ] ) );
}
}