[ 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 $positions = $this->get_after_opener_tag_and_before_closer_tag_positions( true ); 75 if ( ! $positions ) { 76 return false; 77 } 78 list( $after_opener_tag, $before_closer_tag ) = $positions; 79 80 $this->lexical_updates[] = new WP_HTML_Text_Replacement( 81 $after_opener_tag, 82 $before_closer_tag - $after_opener_tag, 83 esc_html( $new_content ) 84 ); 85 86 return true; 87 } 88 89 /** 90 * Appends content after the closing tag of a template tag. 91 * 92 * It positions the cursor in the closer tag of the balanced template tag, 93 * if it exists. 94 * 95 * @since 6.5.0 96 * 97 * @access private 98 * 99 * @param string $new_content The string to append after the closing template tag. 100 * @return bool Whether the content was successfully appended. 101 */ 102 public function append_content_after_template_tag_closer( string $new_content ): bool { 103 if ( empty( $new_content ) || 'TEMPLATE' !== $this->get_tag() || ! $this->is_tag_closer() ) { 104 return false; 105 } 106 107 // Flushes any changes. 108 $this->get_updated_html(); 109 110 $bookmark = 'append_content_after_template_tag_closer'; 111 $this->set_bookmark( $bookmark ); 112 $after_closing_tag = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length; 113 $this->release_bookmark( $bookmark ); 114 115 // Appends the new content. 116 $this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_closing_tag, 0, $new_content ); 117 118 return true; 119 } 120 121 /** 122 * Gets the positions right after the opener tag and right before the closer 123 * tag in a balanced tag. 124 * 125 * By default, it positions the cursor in the closer tag of the balanced tag. 126 * If $rewind is true, it seeks back to the opener tag. 127 * 128 * @since 6.5.0 129 * 130 * @access private 131 * 132 * @param bool $rewind Optional. Whether to seek back to the opener tag after finding the positions. Defaults to false. 133 * @return array|null Start and end byte position, or null when no balanced tag bookmarks. 134 */ 135 private function get_after_opener_tag_and_before_closer_tag_positions( bool $rewind = false ) { 136 // Flushes any changes. 137 $this->get_updated_html(); 138 139 $bookmarks = $this->get_balanced_tag_bookmarks(); 140 if ( ! $bookmarks ) { 141 return null; 142 } 143 list( $opener_tag, $closer_tag ) = $bookmarks; 144 145 $after_opener_tag = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length; 146 $before_closer_tag = $this->bookmarks[ $closer_tag ]->start; 147 148 if ( $rewind ) { 149 $this->seek( $opener_tag ); 150 } 151 152 $this->release_bookmark( $opener_tag ); 153 $this->release_bookmark( $closer_tag ); 154 155 return array( $after_opener_tag, $before_closer_tag ); 156 } 157 158 /** 159 * Returns a pair of bookmarks for the current opener tag and the matching 160 * closer tag. 161 * 162 * It positions the cursor in the closer tag of the balanced tag, if it 163 * exists. 164 * 165 * @since 6.5.0 166 * 167 * @return array|null A pair of bookmarks, or null if there's no matching closing tag. 168 */ 169 private function get_balanced_tag_bookmarks() { 170 static $i = 0; 171 $opener_tag = 'opener_tag_of_balanced_tag_' . ++$i; 172 173 $this->set_bookmark( $opener_tag ); 174 if ( ! $this->next_balanced_tag_closer_tag() ) { 175 $this->release_bookmark( $opener_tag ); 176 return null; 177 } 178 179 $closer_tag = 'closer_tag_of_balanced_tag_' . ++$i; 180 $this->set_bookmark( $closer_tag ); 181 182 return array( $opener_tag, $closer_tag ); 183 } 184 185 /** 186 * Skips processing the content between tags. 187 * 188 * It positions the cursor in the closer tag of the foreign element, if it 189 * exists. 190 * 191 * This function is intended to skip processing SVG and MathML inner content 192 * instead of bailing out the whole processing. 193 * 194 * @since 6.5.0 195 * 196 * @access private 197 * 198 * @return bool Whether the foreign content was successfully skipped. 199 */ 200 public function skip_to_tag_closer(): bool { 201 $depth = 1; 202 $tag_name = $this->get_tag(); 203 204 while ( $depth > 0 && $this->next_tag( array( 'tag_closers' => 'visit' ) ) ) { 205 if ( ! $this->is_tag_closer() && $this->get_attribute_names_with_prefix( 'data-wp-' ) ) { 206 /* translators: 1: SVG or MATH HTML tag. */ 207 $message = sprintf( __( 'Interactivity directives were detected inside an incompatible %1$s tag. These directives will be ignored in the server side render.' ), $tag_name ); 208 _doing_it_wrong( __METHOD__, $message, '6.6.0' ); 209 } 210 if ( $this->get_tag() === $tag_name ) { 211 if ( $this->has_self_closing_flag() ) { 212 continue; 213 } 214 $depth += $this->is_tag_closer() ? -1 : 1; 215 } 216 } 217 218 return 0 === $depth; 219 } 220 221 /** 222 * Finds the matching closing tag for an opening tag. 223 * 224 * When called while the processor is on an open tag, it traverses the HTML 225 * until it finds the matching closer tag, respecting any in-between content, 226 * including nested tags of the same name. Returns false when called on a 227 * closer tag, a tag that doesn't have a closer tag (void), a tag that 228 * doesn't visit the closer tag, or if no matching closing tag was found. 229 * 230 * @since 6.5.0 231 * 232 * @access private 233 * 234 * @return bool Whether a matching closing tag was found. 235 */ 236 public function next_balanced_tag_closer_tag(): bool { 237 $depth = 0; 238 $tag_name = $this->get_tag(); 239 240 if ( ! $this->has_and_visits_its_closer_tag() ) { 241 return false; 242 } 243 244 while ( $this->next_tag( 245 array( 246 'tag_name' => $tag_name, 247 'tag_closers' => 'visit', 248 ) 249 ) ) { 250 if ( ! $this->is_tag_closer() ) { 251 ++$depth; 252 continue; 253 } 254 255 if ( 0 === $depth ) { 256 return true; 257 } 258 259 --$depth; 260 } 261 262 return false; 263 } 264 265 /** 266 * Checks whether the current tag has and will visit its matching closer tag. 267 * 268 * @since 6.5.0 269 * 270 * @access private 271 * 272 * @return bool Whether the current tag has a closer tag. 273 */ 274 public function has_and_visits_its_closer_tag(): bool { 275 $tag_name = $this->get_tag(); 276 277 return null !== $tag_name && ( 278 ! WP_HTML_Processor::is_void( $tag_name ) && 279 ! in_array( $tag_name, self::TAGS_THAT_DONT_VISIT_CLOSER_TAG, true ) 280 ); 281 } 282 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sun Jul 13 08:20:01 2025 | Cross-referenced by PHPXref |