DoubleQuotes.php
1.3 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
<?php
namespace WPML\Compatibility\Divi;
/**
* Divi replaces double quotes with %22 when saving shortcode attributes.
* ATE needs valid HTML so we temporarily decode the double quotes.
* When we receive the translation we undo the change.
*
* @package WPML\Compatibility\Divi
*/
class DoubleQuotes implements \IWPML_Backend_Action, \IWPML_Frontend_Action {
public function add_hooks() {
add_filter( 'wpml_pb_shortcode_decode', [ $this, 'decode' ], -PHP_INT_MAX, 2 );
add_filter( 'wpml_pb_shortcode_encode', [ $this, 'encode' ], PHP_INT_MAX, 2 );
}
/**
* @param string $string
* @param string $encoding
*
* @return string
*/
public function decode( $string, $encoding ) {
if ( self::canHaveDoubleQuotes( $string, $encoding ) ) {
$string = str_replace( '%22', '"', $string );
}
return $string;
}
/**
* @param string $string
* @param string $encoding
*
* @return string
*/
public function encode( $string, $encoding ) {
if ( self::canHaveDoubleQuotes( $string, $encoding ) ) {
$string = str_replace( '"', '%22', $string );
}
return $string;
}
/**
* @param string $string
* @param string $encoding
*
* @return bool
*/
private static function canHaveDoubleQuotes( $string, $encoding ) {
return is_string( $string ) && 'allow_html_tags' === $encoding;
}
}