From 187dc5f8340cfc38e61bf3dfa4b301fa25fb9d16 Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Wed, 30 May 2018 14:07:25 -0600
Subject: [PATCH] propose new filter for the attributes on an external link's
anchor element. @link https://meta.trac.wordpress.org/ticket/3649
---
inc/formatting.php | 68 +++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 49 insertions(+), 19 deletions(-)
diff --git a/inc/formatting.php b/inc/formatting.php
index 25921e2..5a5b13a 100644
|
a
|
b
|
class DevHub_Formatting { |
| 133 | 133 | |
| 134 | 134 | // Link without linked text: {@link http://en.wikipedia.org/wiki/ISO_8601} |
| 135 | 135 | if ( 1 === count( $parts ) ) { |
| 136 | | $link = '<a href="' . esc_url( $link ) . '">' . esc_html( $link ) . '</a>'; |
| | 136 | $url = $text = $link; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | // Link with linked text: {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop} |
| 140 | 140 | else { |
| 141 | | $link = '<a href="' . esc_url( $parts[0] ) . '">' . esc_html( $parts[1] ) . '</a>'; |
| | 141 | $url = $parts[0]; |
| | 142 | $text = $parts[1]; |
| 142 | 143 | } |
| 143 | 144 | |
| | 145 | $link = self::generate_link( $url, $text ); |
| 144 | 146 | } |
| 145 | 147 | |
| 146 | 148 | // Link to an internal resource. |
| … |
… |
class DevHub_Formatting { |
| 164 | 166 | * @return string HTML link markup if a valid element was found. |
| 165 | 167 | */ |
| 166 | 168 | public static function link_internal_element( $link ) { |
| | 169 | $url = ''; |
| | 170 | |
| 167 | 171 | // Link to class variable: {@see WP_Rewrite::$index} |
| 168 | 172 | if ( false !== strpos( $link, '::$' ) ) { |
| 169 | 173 | // Nothing to link to currently. |
| … |
… |
class DevHub_Formatting { |
| 171 | 175 | |
| 172 | 176 | // Link to class method: {@see WP_Query::query()} |
| 173 | 177 | elseif ( false !== strpos( $link, '::' ) ) { |
| 174 | | $link = '<a href="' . |
| 175 | | get_post_type_archive_link( 'wp-parser-class' ) . |
| 176 | | str_replace( array( '::', '()' ), array( '/', '' ), $link ) . |
| 177 | | '">' . esc_html( $link ) . '</a>'; |
| | 178 | $url = get_post_type_archive_link( 'wp-parser-class' ) . |
| | 179 | str_replace( array( '::', '()' ), array( '/', '' ), $link ); |
| 178 | 180 | } |
| 179 | 181 | |
| 180 | 182 | // Link to hook: {@see 'pre_get_search_form'} |
| 181 | 183 | elseif ( 1 === preg_match( '/^(?:\'|(?:‘))([\$\w-&;]+)(?:\'|(?:’))$/', $link, $hook ) ) { |
| 182 | 184 | if ( ! empty( $hook[1] ) ) { |
| 183 | | $link = '<a href="' . |
| 184 | | get_post_type_archive_link( 'wp-parser-hook' ) . |
| 185 | | sanitize_title_with_dashes( html_entity_decode( $hook[1] ) ) . '/' . |
| 186 | | '">' . esc_html( $link ) . '</a>'; |
| | 185 | $url = get_post_type_archive_link( 'wp-parser-hook' ) . |
| | 186 | sanitize_title_with_dashes( html_entity_decode( $hook[1] ) ) . '/'; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| … |
… |
class DevHub_Formatting { |
| 196 | 196 | || |
| 197 | 197 | ( 1 === preg_match ( '/^_?[A-Z][a-zA-Z]+_\w+/', $link ) ) // Otherwise, class names start with (optional underscore, then) uppercase and have underscore |
| 198 | 198 | ) { |
| 199 | | $link = sprintf( |
| 200 | | '<a href="%s">%s</a>', |
| 201 | | esc_url( get_post_type_archive_link( 'wp-parser-class' ) . sanitize_key( $link ) ), |
| 202 | | esc_html( $link ) |
| 203 | | ); |
| | 199 | $url = get_post_type_archive_link( 'wp-parser-class' ) . sanitize_key( $link ); |
| 204 | 200 | } |
| 205 | 201 | |
| 206 | 202 | // Link to function: {@see esc_attr()} |
| 207 | 203 | else { |
| 208 | | $link = '<a href="' . |
| 209 | | get_post_type_archive_link( 'wp-parser-function' ) . |
| 210 | | sanitize_title_with_dashes( html_entity_decode( $link ) ) . |
| 211 | | '">' . esc_html( $link ) . '</a>'; |
| | 204 | $url = get_post_type_archive_link( 'wp-parser-function' ) . |
| | 205 | sanitize_title_with_dashes( html_entity_decode( $link ) ); |
| | 206 | } |
| | 207 | |
| | 208 | if ( $url ) { |
| | 209 | $link = self::generate_link( $url, $link ); |
| 212 | 210 | } |
| 213 | 211 | return $link; |
| 214 | 212 | } |
| 215 | 213 | |
| 216 | 214 | /** |
| | 215 | * Generates a link given a URL and text. |
| | 216 | * |
| | 217 | * @since x.y.z |
| | 218 | * |
| | 219 | * @param string $url The URL, for the link's href attribute. |
| | 220 | * @param string $text The text content of the link. |
| | 221 | * @return string The HTML for the link. |
| | 222 | */ |
| | 223 | public static function generate_link( $url, $text ) { |
| | 224 | /** |
| | 225 | * Filters the HTML attributes applied to a link's anchor element. |
| | 226 | * |
| | 227 | * @since x.y.z |
| | 228 | * |
| | 229 | * @param array $attrs The HTML attributes applied to the link's anchor element. |
| | 230 | * @param string $url The URL for the link. |
| | 231 | */ |
| | 232 | $attrs = apply_filters( 'devhub-format-link-attributes', array( 'href' => $url ), $url ); |
| | 233 | // make sure the filter didn't completely remove the href attribute |
| | 234 | if ( empty( $attrs['href'] ) ) { |
| | 235 | $attrs['href'] = $url; |
| | 236 | } |
| | 237 | $attributes = ''; |
| | 238 | foreach ( $attrs as $name => $value ) { |
| | 239 | $value = 'href' === $name ? esc_url( $value ) : esc_attr( $value ); |
| | 240 | $attributes .= " $name='$value'"; |
| | 241 | } |
| | 242 | |
| | 243 | return sprintf( '<a%s>%s</a>', $attributes, esc_html( $text ) ); |
| | 244 | } |
| | 245 | |
| | 246 | /** |
| 217 | 247 | * Fixes unintended markup generated by Markdown during parsing. |
| 218 | 248 | * |
| 219 | 249 | * The parser interprets underscores surrounding text as Markdown indicating |