Skip to content

Commit 1ff56c5

Browse files
committed
Add placeholder compilation skeleton to WP_HTML_Template
Add $compiled property and get_placeholders() accessor. The compile() method is stubbed and will be implemented to extract placeholder positions, lengths, and contexts from the template.
1 parent 6f30f61 commit 1ff56c5

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/wp-includes/html-api/class-wp-html-template.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,44 @@ class WP_HTML_Template {
1919

2020
private array $replacements = array();
2121

22+
/**
23+
* Compiled placeholder metadata.
24+
*
25+
* Array of placeholder_name => array with keys:
26+
* - 'offsets': array of [start, length] pairs for each occurrence
27+
* - 'context': 'text' or 'attribute' (attribute takes precedence)
28+
*
29+
* @since 7.0.0
30+
* @var array|null
31+
*/
32+
private ?array $compiled = null;
33+
34+
/**
35+
* Returns the compiled placeholder metadata.
36+
*
37+
* Triggers compilation if not already done.
38+
*
39+
* @since 7.0.0
40+
*
41+
* @return array Associative array of placeholder_name => metadata.
42+
*/
43+
public function get_placeholders(): array {
44+
$this->compile();
45+
return $this->compiled;
46+
}
47+
48+
/**
49+
* Compiles the template to extract placeholder metadata.
50+
*
51+
* @since 7.0.0
52+
*/
53+
private function compile(): void {
54+
if ( null !== $this->compiled ) {
55+
return;
56+
}
57+
$this->compiled = array();
58+
}
59+
2260
private function __construct( string $template_string, array $replacements ) {
2361
$this->template_string = $template_string;
2462
$this->replacements = $replacements;

tests/phpunit/tests/html-api/wpHtmlTemplate.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,4 +754,22 @@ public static function data_pre_element_leading_newline() {
754754
),
755755
);
756756
}
757+
758+
/**
759+
* Verifies that get_placeholders returns placeholder metadata.
760+
*
761+
* @ticket 60229
762+
*
763+
* @covers ::get_placeholders
764+
*/
765+
public function test_get_placeholders_returns_metadata() {
766+
$template = T::from( '<p class="</%class>"></%content></p>' );
767+
768+
$placeholders = $template->get_placeholders();
769+
770+
$this->assertArrayHasKey( 'class', $placeholders );
771+
$this->assertArrayHasKey( 'content', $placeholders );
772+
$this->assertSame( 'attribute', $placeholders['class']['context'] );
773+
$this->assertSame( 'text', $placeholders['content']['context'] );
774+
}
757775
}

0 commit comments

Comments
 (0)