| 1 |
<?php |
| 2 |
/** |
| 3 |
* Encode/Decode helper functions. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Encode string. |
| 14 |
* |
| 15 |
* @param string|array $str - string to encode. |
| 16 |
* |
| 17 |
* @return string|array |
| 18 |
*/ |
| 19 |
function ghostkit_encode( $str ) { |
| 20 |
// Array. |
| 21 |
if ( is_array( $str ) ) { |
| 22 |
$result = array(); |
| 23 |
|
| 24 |
foreach ( $str as $k => $val ) { |
| 25 |
$result[ ghostkit_encode( $k ) ] = ghostkit_encode( $val ); |
| 26 |
} |
| 27 |
|
| 28 |
return $result; |
| 29 |
} |
| 30 |
|
| 31 |
// String. |
| 32 |
if ( is_string( $str ) ) { |
| 33 |
// Because of these replacements, some attributes can't be exported to XML without being broken. So, we need to replace it manually with something safe. |
| 34 |
// https://github.com/WordPress/gutenberg/blob/88645e4b268acf5746e914159e3ce790dcb1665a/packages/blocks/src/api/serializer.js#L246-L271 . |
| 35 |
$str = str_replace( '--', '_u002d__u002d_', $str ); |
| 36 |
|
| 37 |
// phpcs:ignore |
| 38 |
$str = rawurlencode( $str ); |
| 39 |
} |
| 40 |
|
| 41 |
return $str; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Decode string. |
| 46 |
* |
| 47 |
* @param string|array $str - string to decode. |
| 48 |
* |
| 49 |
* @return string|array |
| 50 |
*/ |
| 51 |
function ghostkit_decode( $str ) { |
| 52 |
// Array. |
| 53 |
if ( is_array( $str ) ) { |
| 54 |
$result = array(); |
| 55 |
|
| 56 |
foreach ( $str as $k => $val ) { |
| 57 |
$result[ ghostkit_decode( $k ) ] = ghostkit_decode( $val ); |
| 58 |
} |
| 59 |
|
| 60 |
return $result; |
| 61 |
} |
| 62 |
|
| 63 |
// String. |
| 64 |
if ( is_string( $str ) ) { |
| 65 |
// Previously we used urldecode() function, but it doesn't work properly with `+` character. |
| 66 |
// For example, there styles will be broken: width: calc( 100% + 20px );. |
| 67 |
$str = rawurldecode( $str ); |
| 68 |
|
| 69 |
// Because of these replacements, some attributes can't be exported to XML without being broken. So, we need to replace it manually with something safe. |
| 70 |
// https://github.com/WordPress/gutenberg/blob/88645e4b268acf5746e914159e3ce790dcb1665a/packages/blocks/src/api/serializer.js#L246-L271 . |
| 71 |
$str = str_replace( '_u002d__u002d_', '--', $str ); |
| 72 |
} |
| 73 |
|
| 74 |
return $str; |
| 75 |
} |
| 76 |
|