|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * WP_HTML_Template class. |
| 5 | + * |
| 6 | + * @since 6.5.0 |
| 7 | + */ |
| 8 | +class WP_HTML_Template extends WP_HTML_Tag_Processor { |
| 9 | + /** |
| 10 | + * Renders an HTML template, replacing the placeholders with the provided values. |
| 11 | + * |
| 12 | + * @since 6.5.0 |
| 13 | + * |
| 14 | + * @param string $template The HTML template. |
| 15 | + * @param string $args Array of key/value pairs providing substitue values for the placeholders. |
| 16 | + * @return string The rendered HTML. |
| 17 | + */ |
| 18 | + public static function render( $template, $args = array() ) { |
| 19 | + $processor = new self( $template ); |
| 20 | + while ( $processor->next_token() ) { |
| 21 | + $type = $processor->get_token_type(); |
| 22 | + $text = $processor->get_modifiable_text(); |
| 23 | + |
| 24 | + if ( '#funky-comment' === $type && strlen( $text ) > 0 && '%' === $text[0] ) { |
| 25 | + $name = substr( $text, 1 ); |
| 26 | + $value = isset( $args[ $name ] ) && is_string( $args[ $name ] ) ? $args[ $name ] : null; |
| 27 | + $processor->set_bookmark( 'here' ); |
| 28 | + $processor->lexical_updates[] = new WP_HTML_Text_Replacement( |
| 29 | + $processor->bookmarks['here']->start, |
| 30 | + $processor->bookmarks['here']->length, |
| 31 | + null === $value ? '' : esc_html( $value ) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if ( '#tag' === $type ) { |
| 36 | + foreach ( $processor->get_attribute_names_with_prefix( '' ) ?? array() as $attribute_name ) { |
| 37 | + if ( str_starts_with( $attribute_name, '...' ) ) { |
| 38 | + $spread_name = substr( $attribute_name, 3 ); |
| 39 | + if ( isset( $args[ $spread_name ] ) && is_array( $args[ $spread_name ] ) ) { |
| 40 | + foreach ( $args[ $spread_name ] as $key => $value ) { |
| 41 | + if ( true === $value || null === $value || is_string( $value ) ) { |
| 42 | + $processor->set_attribute( $key, $value ); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + $processor->remove_attribute( $attribute_name ); |
| 47 | + } |
| 48 | + |
| 49 | + $value = $processor->get_attribute( $attribute_name ); |
| 50 | + |
| 51 | + if ( ! is_string( $value ) ) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + $full_match = null; |
| 56 | + if ( preg_match( '~^</%([^>]+)>$~', $value, $full_match ) ) { |
| 57 | + $name = $full_match[1]; |
| 58 | + |
| 59 | + if ( array_key_exists( $name, $args ) ) { |
| 60 | + $value = $args[ $name ]; |
| 61 | + if ( null === $value ) { |
| 62 | + $processor->remove_attribute( $attribute_name ); |
| 63 | + } elseif ( true === $value ) { |
| 64 | + $processor->set_attribute( $attribute_name, true ); |
| 65 | + } elseif ( is_string( $value ) ) { |
| 66 | + $processor->set_attribute( $attribute_name, esc_attr( $args[ $name ] ) ); |
| 67 | + } else { |
| 68 | + $processor->remove_attribute( $attribute_name ); |
| 69 | + } |
| 70 | + } else { |
| 71 | + $processor->remove_attribute( $attribute_name ); |
| 72 | + } |
| 73 | + |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $new_value = preg_replace_callback( |
| 78 | + '~</%([^>]+)>~', |
| 79 | + static function ( $matches ) use ( $args ) { |
| 80 | + return is_string( $args[ $matches[1] ] ) |
| 81 | + ? esc_attr( $args[ $matches[1] ] ) |
| 82 | + : ''; |
| 83 | + }, |
| 84 | + $value |
| 85 | + ); |
| 86 | + |
| 87 | + if ( $new_value !== $value ) { |
| 88 | + $processor->set_attribute( $attribute_name, $new_value ); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $processor->get_updated_html(); |
| 95 | + } |
| 96 | +} |
0 commit comments