| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Interactivity API: WP_Interactivity_API_Directives_Processor class. 4 * 5 * @package WordPress 6 * @subpackage Interactivity API 7 * @since 6.5.0 8 */ 9 10 /** 11 * Class used to iterate over the tags of an HTML string and help process the 12 * directive attributes. 13 * 14 * @since 6.5.0 15 * 16 * @access private 17 */ 18 final class WP_Interactivity_API_Directives_Processor extends WP_HTML_Tag_Processor { 19 /** 20 * List of tags whose closer tag is not visited by the WP_HTML_Tag_Processor. 21 * 22 * @since 6.5.0 23 * @var string[] 24 */ 25 const TAGS_THAT_DONT_VISIT_CLOSER_TAG = array( 26 'SCRIPT', 27 'IFRAME', 28 'NOEMBED', 29 'NOFRAMES', 30 'STYLE', 31 'TEXTAREA', 32 'TITLE', 33 'XMP', 34 ); 35 36 /** 37 * Returns the content between two balanced template tags. 38 * 39 * It positions the cursor in the closer tag of the balanced template tag, 40 * if it exists. 41 * 42 * @since 6.5.0 43 * 44 * @access private 45 * 46 * @return string|null The content between the current opener template tag and its matching closer tag or null if it 47 * doesn't find the matching closing tag or the current tag is not a template opener tag. 48 */ 49 public function get_content_between_balanced_template_tags() { 50 if ( 'TEMPLATE' !== $this->get_tag() ) { 51 return null; 52 } 53 54 $positions = $this->get_after_opener_tag_and_before_closer_tag_positions(); 55 if ( ! $positions ) { 56 return null; 57 } 58 list( $after_opener_tag, $before_closer_tag ) = $positions; 59 60 return substr( $this->html, $after_opener_tag, $before_closer_tag - $after_opener_tag ); 61 } 62 63 /** 64 * Sets the content between two balanced tags. 65 * 66 * @since 6.5.0 67 * 68 * @access private 69 * 70 * @param string $new_content The string to replace the content between the matching tags. 71 * @return bool Whether the content was successfully replaced. 72 */ 73 public function set_content_between_balanced_tags( string $new_content ): bool { 74 return $this->set_raw_content_between_balanced_tags( esc_html( $new_content ) ); 75 } 76 77 /** 78 * Sets the content between two balanced tags, writing it as given. 79 * 80 * Unlike {@see WP_Interactivity_API_Directives_Processor::set_content_between_balanced_tags()}, 81 * it does not escape the content. Like it, it replaces the content between 82 * the current opener tag and its matching closer tag, and it rewinds to the 83 * opener tag. 84 * 85 * @since 7.2.0 86 * 87 * @access private 88 * 89 * @param string $new_content The markup to replace the content between the matching tags. It is written as is. 90 * @return bool Whether the content was successfully replaced. 91 */ 92 public function set_raw_content_between_balanced_tags( string $new_content ): bool { 93 $positions = $this->get_after_opener_tag_and_before_closer_tag_positions( true ); 94 if ( ! $positions ) { 95 return false; 96 } 97 list( $after_opener_tag, $before_closer_tag ) = $positions; 98 99 $this->lexical_updates[] = new WP_HTML_Text_Replacement( 100 $after_opener_tag, 101 $before_closer_tag - $after_opener_tag, 102 $new_content 103 ); 104 105 return true; 106 } 107 108 /** 109 * Appends content after the closing tag of a template tag. 110 * 111 * It positions the cursor in the closer tag of the balanced template tag, 112 * if it exists. 113 * 114 * @since 6.5.0 115 * 116 * @access private 117 * 118 * @param string $new_content The string to append after the closing template tag. 119 * @return bool Whether the content was successfully appended. 120 */ 121 public function append_content_after_template_tag_closer( string $new_content ): bool { 122 if ( empty( $new_content ) || 'TEMPLATE' !== $this->get_tag() || ! $this->is_tag_closer() ) { 123 return false; 124 } 125 126 // Flushes any changes. 127 $this->get_updated_html(); 128 129 $bookmark = 'append_content_after_template_tag_closer'; 130 $this->set_bookmark( $bookmark ); 131 $after_closing_tag = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length; 132 $this->release_bookmark( $bookmark ); 133 134 // Appends the new content. 135 $this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_closing_tag, 0, $new_content ); 136 137 return true; 138 } 139 140 /** 141 * Gets the positions right after the opener tag and right before the closer 142 * tag in a balanced tag. 143 * 144 * By default, it positions the cursor in the closer tag of the balanced tag. 145 * If $rewind is true, it seeks back to the opener tag. 146 * 147 * @since 6.5.0 148 * 149 * @access private 150 * 151 * @param bool $rewind Optional. Whether to seek back to the opener tag after finding the positions. Defaults to false. 152 * @return array|null Start and end byte position, or null when no balanced tag bookmarks. 153 */ 154 private function get_after_opener_tag_and_before_closer_tag_positions( bool $rewind = false ) { 155 // Flushes any changes. 156 $this->get_updated_html(); 157 158 $bookmarks = $this->get_balanced_tag_bookmarks(); 159 if ( ! $bookmarks ) { 160 return null; 161 } 162 list( $opener_tag, $closer_tag ) = $bookmarks; 163 164 $after_opener_tag = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length; 165 $before_closer_tag = $this->bookmarks[ $closer_tag ]->start; 166 167 if ( $rewind ) { 168 $this->seek( $opener_tag ); 169 } 170 171 $this->release_bookmark( $opener_tag ); 172 $this->release_bookmark( $closer_tag ); 173 174 return array( $after_opener_tag, $before_closer_tag ); 175 } 176 177 /** 178 * Returns a pair of bookmarks for the current opener tag and the matching 179 * closer tag. 180 * 181 * It positions the cursor in the closer tag of the balanced tag, if it 182 * exists. 183 * 184 * @since 6.5.0 185 * 186 * @return array|null A pair of bookmarks, or null if there's no matching closing tag. 187 */ 188 private function get_balanced_tag_bookmarks() { 189 static $i = 0; 190 $opener_tag = 'opener_tag_of_balanced_tag_' . ++$i; 191 192 $this->set_bookmark( $opener_tag ); 193 if ( ! $this->next_balanced_tag_closer_tag() ) { 194 $this->release_bookmark( $opener_tag ); 195 return null; 196 } 197 198 $closer_tag = 'closer_tag_of_balanced_tag_' . ++$i; 199 $this->set_bookmark( $closer_tag ); 200 201 return array( $opener_tag, $closer_tag ); 202 } 203 204 /** 205 * Skips processing the content between tags. 206 * 207 * It positions the cursor in the closer tag of the foreign element, if it 208 * exists. 209 * 210 * This function is intended to skip processing SVG and MathML inner content 211 * instead of bailing out the whole processing. 212 * 213 * @since 6.5.0 214 * 215 * @access private 216 * 217 * @return bool Whether the foreign content was successfully skipped. 218 */ 219 public function skip_to_tag_closer(): bool { 220 $depth = 1; 221 $tag_name = $this->get_tag(); 222 223 while ( $depth > 0 && $this->next_tag( array( 'tag_closers' => 'visit' ) ) ) { 224 if ( ! $this->is_tag_closer() && $this->get_attribute_names_with_prefix( 'data-wp-' ) ) { 225 /* translators: 1: SVG or MATH HTML tag. */ 226 $message = sprintf( __( 'Interactivity directives were detected inside an incompatible %1$s tag. These directives will be ignored in the server side render.' ), $tag_name ); 227 _doing_it_wrong( __METHOD__, $message, '6.6.0' ); 228 } 229 if ( $this->get_tag() === $tag_name ) { 230 if ( $this->has_self_closing_flag() ) { 231 continue; 232 } 233 $depth += $this->is_tag_closer() ? -1 : 1; 234 } 235 } 236 237 return 0 === $depth; 238 } 239 240 /** 241 * Finds the matching closing tag for an opening tag. 242 * 243 * When called while the processor is on an open tag, it traverses the HTML 244 * until it finds the matching closer tag, respecting any in-between content, 245 * including nested tags of the same name. Returns false when called on a 246 * closer tag, a tag that doesn't have a closer tag (void), a tag that 247 * doesn't visit the closer tag, or if no matching closing tag was found. 248 * 249 * @since 6.5.0 250 * 251 * @access private 252 * 253 * @return bool Whether a matching closing tag was found. 254 */ 255 public function next_balanced_tag_closer_tag(): bool { 256 $depth = 0; 257 $tag_name = $this->get_tag(); 258 259 if ( ! $this->has_and_visits_its_closer_tag() ) { 260 return false; 261 } 262 263 while ( $this->next_tag( 264 array( 265 'tag_name' => $tag_name, 266 'tag_closers' => 'visit', 267 ) 268 ) ) { 269 if ( ! $this->is_tag_closer() ) { 270 ++$depth; 271 continue; 272 } 273 274 if ( 0 === $depth ) { 275 return true; 276 } 277 278 --$depth; 279 } 280 281 return false; 282 } 283 284 /** 285 * Checks whether the current tag has and will visit its matching closer tag. 286 * 287 * @since 6.5.0 288 * 289 * @access private 290 * 291 * @return bool Whether the current tag has a closer tag. 292 */ 293 public function has_and_visits_its_closer_tag(): bool { 294 $tag_name = $this->get_tag(); 295 296 return null !== $tag_name && ( 297 ! WP_HTML_Processor::is_void( $tag_name ) && 298 ! in_array( $tag_name, self::TAGS_THAT_DONT_VISIT_CLOSER_TAG, true ) 299 ); 300 } 301 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Mon Oct 19 08:20:34 2026 | Cross-referenced by PHPXref |