Making WordPress.org

Changeset 2984


Ignore:
Timestamp:
04/19/2016 08:15:57 PM (9 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/

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

Legend:

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

    r2779 r2984  
    6565                    <?php endif; ?>
    6666                    <?php if ( ! empty( $param['content'] ) ) : ?>
    67                     <span class="description"><?php echo param_formatting_fixup( wp_kses_post( $param['content'] ) ); ?></span>
     67                    <span class="description"><?php echo wp_kses_post( $param['content'] ); ?></span>
    6868                    <?php endif; ?>
    6969                </p>
  • 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
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r2966 r2984  
    11911191
    11921192    /**
    1193      * Formats the output of params defined using hash notation.
    1194      *
    1195      * This is a temporary measure until the parser parses the hash notation
    1196      * into component elements that the theme could then handle and style
    1197      * properly.
    1198      *
    1199      * Also, as a stopgap this is going to begin as a barebones hack to simply
    1200      * keep the text looking like one big jumble.
    1201      *
    1202      * @param  string $text The content for the param.
    1203      * @return string
    1204      */
    1205     function param_formatting_fixup( $text ) {
    1206         // Don't do anything if this isn't a hash notation string.
    1207         if ( '{' != $text[0] ) {
    1208             return $text;
    1209         }
    1210 
    1211         $new_text = '';
    1212         $text     = trim( substr( $text, 1, -1 ) );
    1213         $text     = str_replace( '@type', "\n@type", $text );
    1214 
    1215         $in_list = false;
    1216         $parts = explode( "\n", $text );
    1217         foreach ( $parts as $part ) {
    1218             $part = preg_replace( '/\s+/', ' ', $part );
    1219             list( $wordtype, $type, $name, $description ) = explode( ' ', $part, 4 );
    1220             $description = trim( $description );
    1221 
    1222             $skip_closing_li = false;
    1223 
    1224             // Handle nested hashes.
    1225             if ( '{' === $description[0] || '{' === $name ) {
    1226                 $description = ltrim( $description, '{' ) . '<ul class="param-hash">';
    1227                 $skip_closing_li = true;
    1228             } elseif ( '}' === substr( $description, -1 ) ) {
    1229                 $description = substr( $description, 0, -1 ) . "</li></ul>\n";
    1230             }
    1231 
    1232             if ( '@type' != $wordtype ) {
    1233                 if ( $in_list ) {
    1234                     $in_list = false;
    1235                     $new_text .= "</li></ul>\n";
    1236                 }
    1237 
    1238                 $new_text .= $part;
    1239             } else {
    1240                 if ( $in_list ) {
    1241                     $new_text .= '<li>';
    1242                 } else {
    1243                     $new_text .= '<ul class="param-hash"><li>';
    1244                     $in_list = true;
    1245                 }
    1246 
    1247                 // Normalize argument name.
    1248                 if ( $name === '{' ) {
    1249                     // No name is specified, generally indicating an array of arrays.
    1250                     $name = '';
    1251                 } else {
    1252                     // The name is defined as a variable, so remove the leading '$'.
    1253                     $name = ltrim( $name, '$' );
    1254                 }
    1255                 if ( $name ) {
    1256                     $new_text .= "<b>'{$name}'</b><br />";
    1257                 }
    1258                 $new_text .= "<i><span class='type'>({$type})</span></i> {$description}";
    1259                 if ( ! $skip_closing_li ) {
    1260                     $new_text .= '</li>';
    1261                 }
    1262                 $new_text .= "\n";
    1263             }
    1264         }
    1265 
    1266         if ( $in_list ) {
    1267             $new_text .= "</li></ul>\n";
    1268         }
    1269 
    1270         return $new_text;
    1271     }
    1272 
    1273     /**
    12741193     * Should the search bar be shown?
    12751194     *
Note: See TracChangeset for help on using the changeset viewer.