Making WordPress.org


Ignore:
Timestamp:
04/19/2016 08:15:57 PM (10 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Prevent overzealous auto-linking of hash parameter text.

Currently, hash params are part of the description text of a single parameter and are not true sub-parameters, so auto-linking the entire hash notation text blob can affect non-description aspects of hash elements (namely when a class is the expected datatype for a hash element) and mess up hash formatting.

  • Move param_formatting_fixup() to inc/formatting.php.
  • Rename param_formatting_fixup() to fix_param_hash_formatting()`.
  • In autolink_references(), don't autolink hash notation text blobs.
  • Filter param description text with fix_param_hash_formatting() via filter rather than direct call in content-reference template.
  • Call autolink_references() within fix_param_hash_formatting() for hash param item descriptions.

See https://wordpress.slack.com/archives/meta/p1460967552000043
See https://developer.wordpress.org/reference/classes/wp_customize_control/__construct/

File:
1 edited

Legend:

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

    r2820 r2984  
    3232        add_filter( 'devhub-format-description', array( __CLASS__, 'autolink_references' ) );
    3333
     34        add_filter( 'devhub-format-description', array( __CLASS__, 'fix_param_hash_formatting' ), 9 );
    3435        add_action( 'the_content', array( __CLASS__, 'fix_unintended_markdown' ) );
    3536    }
     
    256257     */
    257258    public static function autolink_references( $text ) {
     259        // Temporary: Don't do anything if the text is a hash notation string.
     260        if ( '{' === $text[0] ) {
     261            return $text;
     262        }
     263
    258264        $r = '';
    259265        $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
     
    386392    }
    387393
     394    /**
     395     * Formats the output of params defined using hash notation.
     396     *
     397     * This is a temporary measure until the parser parses the hash notation
     398     * into component elements that the theme could then handle and style
     399     * properly.
     400     *
     401     * Also, as a stopgap this is going to begin as a barebones hack to simply
     402     * keep the text looking like one big jumble.
     403     *
     404     * @param  string $text The content for the param.
     405     * @return string
     406     */
     407    function fix_param_hash_formatting( $text ) {
     408        // Don't do anything if this isn't a hash notation string.
     409        if ( '{' != $text[0] ) {
     410            return $text;
     411        }
     412
     413        $new_text = '';
     414        $text     = trim( substr( $text, 1, -1 ) );
     415        $text     = str_replace( '@type', "\n@type", $text );
     416
     417        $in_list = false;
     418        $parts = explode( "\n", $text );
     419        foreach ( $parts as $part ) {
     420            $part = preg_replace( '/\s+/', ' ', $part );
     421            list( $wordtype, $type, $name, $description ) = explode( ' ', $part, 4 );
     422            $description = trim( $description );
     423            $description = self::autolink_references( $description );
     424
     425            $skip_closing_li = false;
     426
     427            // Handle nested hashes.
     428            if ( '{' === $description[0] || '{' === $name ) {
     429                $description = ltrim( $description, '{' ) . '<ul class="param-hash">';
     430                $skip_closing_li = true;
     431            } elseif ( '}' === substr( $description, -1 ) ) {
     432                $description = substr( $description, 0, -1 ) . "</li></ul>\n";
     433            }
     434
     435            if ( '@type' != $wordtype ) {
     436                if ( $in_list ) {
     437                    $in_list = false;
     438                    $new_text .= "</li></ul>\n";
     439                }
     440
     441                $new_text .= $part;
     442            } else {
     443                if ( $in_list ) {
     444                    $new_text .= '<li>';
     445                } else {
     446                    $new_text .= '<ul class="param-hash"><li>';
     447                    $in_list = true;
     448                }
     449
     450                // Normalize argument name.
     451                if ( $name === '{' ) {
     452                    // No name is specified, generally indicating an array of arrays.
     453                    $name = '';
     454                } else {
     455                    // The name is defined as a variable, so remove the leading '$'.
     456                    $name = ltrim( $name, '$' );
     457                }
     458                if ( $name ) {
     459                    $new_text .= "<b>'{$name}'</b><br />";
     460                }
     461                $new_text .= "<i><span class='type'>({$type})</span></i> {$description}";
     462                if ( ! $skip_closing_li ) {
     463                    $new_text .= '</li>';
     464                }
     465                $new_text .= "\n";
     466            }
     467        }
     468
     469        if ( $in_list ) {
     470            $new_text .= "</li></ul>\n";
     471        }
     472
     473        return $new_text;
     474    }
     475
    388476} // DevHub_Formatting
    389477
Note: See TracChangeset for help on using the changeset viewer.