get( $matches[1], '' ); return self::escape( $raw, $format ); }, $template ); } /** * Escape a value for the target format. * * @param string $value Raw value. * @param EscapeFormat $format Format. * * @since 0.1.0 */ public static function escape( string $value, EscapeFormat $format ): string { return match ( $format ) { EscapeFormat::Html => self::escape_html( $value ), EscapeFormat::Text => $value, EscapeFormat::Url => rawurlencode( $value ), }; } /** * HTML-escape without requiring WordPress bootstrap. * * @param string $value Raw. */ private static function escape_html( string $value ): string { if ( function_exists( 'esc_html' ) ) { return esc_html( $value ); } return htmlspecialchars( $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ); } }