" can close the surrounding script element. These flags * hex-escape the characters that matter in HTML and JavaScript strings. * * @package ReactWoo\Atomic */ namespace ReactWoo\Atomic\Runtime; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Encodes data for wp_add_inline_script(). */ final class Script_Json { /** * Flags that stop a JSON value breaking out of a script element. */ const FLAGS = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT; /** * @param mixed $data Value. * @return string JSON, or an empty string when encoding fails. */ public static function encode( $data ) { $json = function_exists( 'wp_json_encode' ) ? wp_json_encode( $data, self::FLAGS ) : json_encode( $data, self::FLAGS ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- wp_json_encode() is used when WordPress is loaded. return is_string( $json ) ? $json : ''; } }