Making WordPress.org

Changeset 751


Ignore:
Timestamp:
07/17/2014 07:14:08 PM (12 years ago)
Author:
coffee2code
Message:

Code Reference: convert doc block @link references to actual links. Fixes #562

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer
Files:
2 edited

Legend:

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

    r750 r751  
    5656
    5757        add_filter( 'the_excerpt', __NAMESPACE__ . '\\lowercase_P_dangit_just_once' );
     58        add_filter( 'the_content', __NAMESPACE__ . '\\make_doclink_clickable', 10, 5 );
    5859
    5960        // Temporarily disable comments
    … …  
    446447}
    447448
     449/**
     450 * Makes phpDoc @link references clickable.
     451 *
     452 * Handles these five different types of links:
     453 *
     454 * - {@link http://en.wikipedia.org/wiki/ISO_8601}
     455 * - {@link WP_Rewrite::$index}
     456 * - {@link WP_Query::query()}
     457 * - {@link esc_attr()}
     458 * - {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop}
     459 *
     460 * @param  string $content The content.
     461 * @return string
     462 */
     463function make_doclink_clickable( $content ) {
     464
     465        if ( false === strpos( $content, '{@link ' ) ) {
     466                return $content;
     467        }
     468
     469        return preg_replace_callback(
     470                '/\{@link ([^\}]+)\}/',
     471                function ( $matches ) {
     472
     473                        $link = $matches[1];
     474
     475                        // Fix URLs made clickable during initial parsing
     476                        if ( 0 === strpos( $link, '<a ' ) ) {
     477
     478                                if ( preg_match( '/^<a .*href=[\'\"]([^\'\"]+)[\'\"]>(.*)<\/a>$/', $link, $parts ) ) {
     479                                        $link = '<a href="' . $parts[1] . '">' . esc_html( trim( $parts[2] ) ) . '</a>';
     480                                }
     481
     482                        }
     483
     484                        // Link to an external resource.
     485                        elseif ( 0 === strpos( $link, 'http' ) ) {
     486
     487                                $parts = explode( ' ', $link, 2 );
     488
     489                                // Link without linked text: {@link http://en.wikipedia.org/wiki/ISO_8601}
     490                                if ( 1 === count( $parts ) ) {
     491                                        $link = '<a href="' . esc_url( $link ) . '">' . esc_html( $link ) . '</a>';
     492                                }
     493
     494                                // Link with linked text: {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop}
     495                                else {
     496                                        $link = '<a href="' . esc_url( $parts[0] ) . '">' . esc_html( $parts[1] ) . '</a>';
     497                                }
     498
     499                        }
     500
     501                        // Link to an internal resource.
     502                        else {
     503
     504                                // Link to class variable: {@link WP_Rewrite::$index}
     505                                if ( false !== strpos( $link, '::$' ) ) {
     506                                        // Nothing to link to currently.
     507                                }
     508
     509                                // Link to class method: {@link WP_Query::query()}
     510                                elseif ( false !== strpos( $link, '::' ) ) {
     511                                        $link = '<a href="' .
     512                                                get_post_type_archive_link( 'wp-parser-class' ) .
     513                                                str_replace( array( '::', '()' ), array( '/', '' ), $link ) .
     514                                                '">' . esc_html( $link ) . '</a>';
     515                                }
     516
     517                                // Link to function: {@link esc_attr()}
     518                                else {
     519                                        $link = '<a href="' .
     520                                                get_post_type_archive_link( 'wp-parser-function' ) .
     521                                                str_replace( '()', '', $link ) .
     522                                                '">' . esc_html( $link ) . '</a>';
     523                                }
     524
     525                        }
     526
     527                        return $link;
     528                },
     529                $content
     530        );
     531}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r741 r751  
    491491                                                $params[ $tag['variable'] ]['required'] = 'Required';
    492492                                        }
     493                                        $params[ $tag['variable'] ]['content'] = make_doclink_clickable( $params[ $tag['variable'] ]['content'] );
    493494                                }
    494495                        }
Note: See TracChangeset for help on using the changeset viewer.