Changeset 5763 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/cli.php
- Timestamp:
- 08/07/2017 07:44:24 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/cli.php
r5761 r5763 266 266 // Transform emdash back to triple-dashes 267 267 $content = str_replace( '-–', '---', $content ); 268 269 // Include the excerpt in the main content well 270 $excerpt = get_the_excerpt(); 271 if ( $excerpt ) { 272 $content = wpautop( $excerpt ) . PHP_EOL . $content; 273 } 274 275 // Add 'Quick Links' across the top 276 $items = self::get_tags( 'h([1-4])', $content ); 277 if ( count( $items ) > 1 ) { 278 for ( $i = 1; $i <= 4; $i++ ) { 279 $content = self::add_ids_and_jumpto_links( "h$i", $content ); 280 } 281 $quick_links = '<p class="quick-links"><small>Quick Links: '; 282 foreach( $items as $item ) { 283 $quick_links .= '<a href="#' . sanitize_title_with_dashes( $item[3] ) . '">' . ucwords( strtolower( $item[3] ) ) . '</a> | '; 284 } 285 $quick_links = rtrim( $quick_links, ' |' ) . '</small></p>'; 286 $content = $quick_links . PHP_EOL . PHP_EOL . $content; 287 } 288 268 289 return $content; 269 290 } 270 291 292 protected static function add_ids_and_jumpto_links( $tag, $content ) { 293 $items = self::get_tags( $tag, $content ); 294 $first = true; 295 $matches = array(); 296 $replacements = array(); 297 298 foreach ( $items as $item ) { 299 $replacement = ''; 300 $matches[] = $item[0]; 301 $id = sanitize_title_with_dashes($item[2]); 302 303 if ( ! $first ) { 304 $replacement .= '<p class="toc-jump"><a href="#top">' . __( 'Top ↑', 'wporg' ) . '</a></p>'; 305 } else { 306 $first = false; 307 } 308 $a11y_text = sprintf( '<span class="screen-reader-text">%s</span>', $item[2] ); 309 $anchor = sprintf( '<a href="#%1$s" class="anchor"><span aria-hidden="true">#</span>%2$s</a>', $id, $a11y_text ); 310 $replacement .= sprintf( '<%1$s class="toc-heading" id="%2$s" tabindex="-1">%3$s %4$s</%1$s>', $tag, $id, $item[2], $anchor ); 311 $replacements[] = $replacement; 312 } 313 314 if ( $replacements ) { 315 $content = str_replace( $matches, $replacements, $content ); 316 } 317 318 return $content; 319 } 320 321 private static function get_tags( $tag, $content ) { 322 preg_match_all( "/(<{$tag}>)(.*)(<\/{$tag}>)/", $content, $matches, PREG_SET_ORDER ); 323 return $matches; 324 } 325 271 326 } 272 327
Note: See TracChangeset
for help on using the changeset viewer.