|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * HTML API: WP_HTML_Template helper class |
| 4 | + * |
| 5 | + * Provides the rendering code for the WP_HTML class. This needs to exist separately as |
| 6 | + * implemented so that it can subclass the WP_HTML_Tag_Processor class and gain access |
| 7 | + * to the bookmarks and lexical updates, which it uses to perform string operations. |
| 8 | + * |
| 9 | + * @package WordPress |
| 10 | + * @subpackage HTML-API |
| 11 | + * @since 6.5.0 |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * WP_HTML_Template class. |
| 16 | + * |
| 17 | + * To be used only by the WP_HTML class. |
| 18 | + * |
| 19 | + * @since 6.5.0 |
| 20 | + * |
| 21 | + * @access private |
| 22 | + */ |
| 23 | +class WP_HTML_Template extends WP_HTML_Tag_Processor { |
| 24 | + /** |
| 25 | + * Renders an HTML template, replacing the placeholders with the provided values. |
| 26 | + * |
| 27 | + * This function looks for placeholders in the template string and will replace |
| 28 | + * them with appropriately-escaped substitutions from the given arguments, if |
| 29 | + * provided and if those arguments are strings. |
| 30 | + * |
| 31 | + * Example: |
| 32 | + * |
| 33 | + * echo WP_HTML_Template::render( |
| 34 | + * '<a href="</%profile_url>"></%name></a>', |
| 35 | + * array( |
| 36 | + * 'profile_url' => 'https://profiles.example.com/username', |
| 37 | + * 'name' => $user->display_name |
| 38 | + * ) |
| 39 | + * ); |
| 40 | + * // Outputs: <a href="https://profiles.example.com/username">Bobby Tables</a> |
| 41 | + * |
| 42 | + * Do not escape the values supplied to the argument array! This function will escape each |
| 43 | + * parameter's value as needed and additional manual escaping may lead to incorrect output. |
| 44 | + * |
| 45 | + * ## Syntax. |
| 46 | + * |
| 47 | + * ### Substitution Placeholders. |
| 48 | + * |
| 49 | + * - `</%named_arg>` finds `named_arg` in the arguments array, escapes its value if possible, |
| 50 | + * and replaces the placeholder with the escaped value. These may exist inside double-quoted |
| 51 | + * HTML tag attributes or in HTML text content between tags. They cannot be used to output a tag |
| 52 | + * name or content inside a comment. |
| 53 | + * |
| 54 | + * ### Spread Attributes. |
| 55 | + * |
| 56 | + * - `...named_arg` when found within an HTML tag will lookup `named_arg` in the arguments array |
| 57 | + * and, if it's an array, will set the attribute on the tag for each key/value pair whose value |
| 58 | + * is a string. The |
| 59 | + * |
| 60 | + * ## Notes. |
| 61 | + * |
| 62 | + * - Attributes may only be supplied for a limited set of types: a string value assigns a double-quoted |
| 63 | + * attribute value; `true` sets the attribute as a boolean attribute; `null` removes the attribute. |
| 64 | + * If provided any other type of value the attribute will be ignored and its existing value persists. |
| 65 | + * |
| 66 | + * - If multiple HTML attributes are specified for a given tag they will be applied as if calling |
| 67 | + * `set_attribute()` in the order they are specified in the temlpate. This includes any attributes |
| 68 | + * assigned through the attribute spread syntax. |
| 69 | + * |
| 70 | + * - Substitutions in text nodes may only contain string values. If provided any other type of value |
| 71 | + * the placeholder will be removed with nothing in its place. |
| 72 | + * |
| 73 | + * - This function currently escapes all value provided in the arguments array. In the future |
| 74 | + * it may provide the ability to nest pre-rendered HTML into the template, but this functionality |
| 75 | + * is deferred for a future update. |
| 76 | + * |
| 77 | + * - This function will not replace content inside of TEXTAREA, TITLE, SCRIPT, or STYLE elements. |
| 78 | + * |
| 79 | + * @since 6.5.0 |
| 80 | + * |
| 81 | + * @access private |
| 82 | + * |
| 83 | + * @param string $template The HTML template. |
| 84 | + * @param string $args Array of key/value pairs providing substitue values for the placeholders. |
| 85 | + * @return string The rendered HTML. |
| 86 | + */ |
| 87 | + public static function render( $template, $args = array() ) { |
| 88 | + $processor = new self( $template ); |
| 89 | + while ( $processor->next_token() ) { |
| 90 | + $type = $processor->get_token_type(); |
| 91 | + $text = $processor->get_modifiable_text(); |
| 92 | + |
| 93 | + if ( '#funky-comment' === $type && strlen( $text ) > 0 && '%' === $text[0] ) { |
| 94 | + $name = substr( $text, 1 ); |
| 95 | + $value = isset( $args[ $name ] ) && is_string( $args[ $name ] ) ? $args[ $name ] : null; |
| 96 | + $processor->set_bookmark( 'here' ); |
| 97 | + $processor->lexical_updates[] = new WP_HTML_Text_Replacement( |
| 98 | + $processor->bookmarks['here']->start, |
| 99 | + $processor->bookmarks['here']->length, |
| 100 | + null === $value ? '' : esc_html( $value ) |
| 101 | + ); |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + if ( '#tag' === $type ) { |
| 106 | + foreach ( $processor->get_attribute_names_with_prefix( '' ) ?? array() as $attribute_name ) { |
| 107 | + if ( str_starts_with( $attribute_name, '...' ) ) { |
| 108 | + $spread_name = substr( $attribute_name, 3 ); |
| 109 | + if ( isset( $args[ $spread_name ] ) && is_array( $args[ $spread_name ] ) ) { |
| 110 | + foreach ( $args[ $spread_name ] as $key => $value ) { |
| 111 | + if ( true === $value || null === $value || is_string( $value ) ) { |
| 112 | + $processor->set_attribute( $key, $value ); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + $processor->remove_attribute( $attribute_name ); |
| 117 | + } |
| 118 | + |
| 119 | + $value = $processor->get_attribute( $attribute_name ); |
| 120 | + |
| 121 | + if ( ! is_string( $value ) ) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $full_match = null; |
| 126 | + if ( preg_match( '~^</%([^>]+)>$~', $value, $full_match ) ) { |
| 127 | + $name = $full_match[1]; |
| 128 | + |
| 129 | + if ( array_key_exists( $name, $args ) ) { |
| 130 | + $value = $args[ $name ]; |
| 131 | + if ( null === $value ) { |
| 132 | + $processor->remove_attribute( $attribute_name ); |
| 133 | + } elseif ( true === $value ) { |
| 134 | + $processor->set_attribute( $attribute_name, true ); |
| 135 | + } elseif ( is_string( $value ) ) { |
| 136 | + $processor->set_attribute( $attribute_name, esc_attr( $args[ $name ] ) ); |
| 137 | + } else { |
| 138 | + $processor->remove_attribute( $attribute_name ); |
| 139 | + } |
| 140 | + } else { |
| 141 | + $processor->remove_attribute( $attribute_name ); |
| 142 | + } |
| 143 | + |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + $new_value = preg_replace_callback( |
| 148 | + '~</%([^>]+)>~', |
| 149 | + static function ( $matches ) use ( $args ) { |
| 150 | + return is_string( $args[ $matches[1] ] ) |
| 151 | + ? esc_attr( $args[ $matches[1] ] ) |
| 152 | + : ''; |
| 153 | + }, |
| 154 | + $value |
| 155 | + ); |
| 156 | + |
| 157 | + if ( $new_value !== $value ) { |
| 158 | + $processor->set_attribute( $attribute_name, $new_value ); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return $processor->get_updated_html(); |
| 165 | + } |
| 166 | +} |
0 commit comments