Making WordPress.org

Changeset 7769


Ignore:
Timestamp:
10/24/2018 09:41:00 PM (8 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Centralize creation of doclink link markup into new generate_link().

Also adds 'devhub-format-link-attributes' filter for adding/editing attributes used for a link.

Props pbiron.
Fixes #3649.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/formatting.php

    r6448 r7769  
    134134                                        // Link without linked text: {@link http://en.wikipedia.org/wiki/ISO_8601}
    135135                                        if ( 1 === count( $parts ) ) {
    136                                                 $link = '<a href="' . esc_url( $link ) . '">' . esc_html( $link ) . '</a>';
     136                                                $url = $text = $link;
    137137                                        }
    138138
    139139                                        // Link with linked text: {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop}
    140140                                        else {
    141                                                 $link = '<a href="' . esc_url( $parts[0] ) . '">' . esc_html( $parts[1] ) . '</a>';
     141                                                $url = $parts[0];
     142                                                $text = $parts[1];
    142143                                        }
    143144
     145                                        $link = self::generate_link( $url, $text );
    144146                                }
    145147
     
    165167         */
    166168        public static function link_internal_element( $link ) {
     169                $url = '';
     170
    167171                // Link to class variable: {@see WP_Rewrite::$index}
    168172                if ( false !== strpos( $link, '::$' ) ) {
     
    172176                // Link to class method: {@see WP_Query::query()}
    173177                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 );
    178180                }
    179181
     
    181183                elseif ( 1 === preg_match( '/^(?:\'|(?:&#8216;))([\$\w-&;]+)(?:\'|(?:&#8217;))$/', $link, $hook ) ) {
    182184                        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] ) ) . '/';
    187187                        }
    188188                }
     
    197197                        ( 1 === preg_match ( '/^_?[A-Z][a-zA-Z]+_\w+/', $link ) ) // Otherwise, class names start with (optional underscore, then) uppercase and have underscore
    198198                ) {
    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 );
    204200                }
    205201
    206202                // Link to function: {@see esc_attr()}
    207203                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 );
    212210                }
    213211                return $link;
    214212        }
    215213
     214        /**
     215         * Generates a link given a URL and text.
     216         *
     217         * @param string $url  The URL, for the link's href attribute.
     218         * @param string $text The text content of the link.
     219         * @return string The HTML for the link.
     220         */
     221        public static function generate_link( $url, $text ) {
     222                /**
     223                 * Filters the HTML attributes applied to a link's anchor element.
     224                 *
     225                 * @param array  $attrs The HTML attributes applied to the link's anchor element.
     226                 * @param string $url   The URL for the link.
     227                 */
     228                $attrs = (array) apply_filters( 'devhub-format-link-attributes', array( 'href' => $url ), $url );
     229
     230                // Make sure the filter didn't completely remove the href attribute.
     231                if ( empty( $attrs['href'] ) ) {
     232                        $attrs['href'] = $url;
     233                }
     234
     235                $attributes = '';
     236                foreach ( $attrs as $name => $value ) {
     237                        $value = 'href' === $name ? esc_url( $value ) : esc_attr( $value );
     238                        $attributes .= sprintf( ' %s="%s"', esc_attr( $name ), $value );
     239                }
     240
     241                return sprintf( '<a%s>%s</a>', $attributes, esc_html( $text ) );
     242        }
     243       
    216244        /**
    217245         * Fixes unintended markup generated by Markdown during parsing.
Note: See TracChangeset for help on using the changeset viewer.