Changeset 8899
- Timestamp:
- 05/30/2019 10:11:34 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/table-of-contents.php
r7994 r8899 89 89 $toc .= "<$contents_header>" . esc_html( $this->args->header_text ) . "</$contents_header><ul class=\"items\">"; 90 90 $last_item = false; 91 $used_ids = []; 92 91 93 foreach ( $items as $item ) { 92 94 if ( $last_item ) { … … 100 102 101 103 $last_item = $item[2]; 102 $toc .= '<li><a href="#' . sanitize_title_with_dashes($item[3]) . '">' . $item[3] . '</a>'; 104 105 $id = sanitize_title_with_dashes( $item[3] ); 106 // Append unique suffix if anchor ID isn't unique. 107 $count = 2; 108 $orig_id = $id; 109 while ( in_array( $id, $used_ids ) && $count < 50 ) { 110 $id = $orig_id . '-' . $count; 111 $count++; 112 } 113 $used_ids[] = $id; 114 115 $toc .= '<li><a href="#' . esc_attr( $id ) . '">' . $item[3] . '</a>'; 103 116 } 104 117 $toc .= "</ul>\n</div>\n"; … … 113 126 $matches = array(); 114 127 $replacements = array(); 128 $used_ids = array(); 115 129 116 130 foreach ( $items as $item ) { … … 119 133 $id = sanitize_title_with_dashes($item[2]); 120 134 135 // Append unique suffix if anchor ID isn't unique. 136 $count = 2; 137 $orig_id = $id; 138 while ( in_array( $id, $used_ids ) && $count < 50 ) { 139 $id = $orig_id . '-' . $count; 140 $count++; 141 } 142 $used_ids[] = $id; 143 121 144 if ( ! $first ) { 122 145 $replacement .= '<p class="toc-jump"><a href="#top">' . __( 'Top ↑', 'wporg' ) . '</a></p>'; … … 131 154 132 155 if ( $replacements ) { 133 $content = str_replace( $matches, $replacements, $content ); 156 if ( count( array_unique( $matches ) ) !== count( $matches ) ) { 157 foreach ( $matches as $i => $match ) { 158 $content = preg_replace( '/' . preg_quote( $match, '/' ) . '/', $replacements[ $i ], $content, 1 ); 159 } 160 } else { 161 $content = str_replace( $matches, $replacements, $content ); 162 } 134 163 } 135 164
Note: See TracChangeset
for help on using the changeset viewer.