Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/shortcodes.php

    r42249 r42343  
    8989 * @param string $tag Shortcode tag to remove hook for.
    9090 */
    91 function remove_shortcode($tag) {
    92         global $shortcode_tags;
    93 
    94         unset($shortcode_tags[$tag]);
     91function remove_shortcode( $tag ) {
     92        global $shortcode_tags;
     93
     94        unset( $shortcode_tags[ $tag ] );
    9595}
    9696
    … …  
    145145        if ( shortcode_exists( $tag ) ) {
    146146                preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
    147                 if ( empty( $matches ) )
     147                if ( empty( $matches ) ) {
    148148                        return false;
     149                }
    149150
    150151                foreach ( $matches as $shortcode ) {
    … …  
    181182        }
    182183
    183         if (empty($shortcode_tags) || !is_array($shortcode_tags))
     184        if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
    184185                return $content;
     186        }
    185187
    186188        // Find all registered tag names in $content.
    … …  
    232234                $tagnames = array_keys( $shortcode_tags );
    233235        }
    234         $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
     236        $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
    235237
    236238        // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
    … …  
    272274/**
    273275 * Regular Expression callable for do_shortcode() for calling shortcode hook.
     276 *
    274277 * @see get_shortcode_regex for details of the match array contents.
    275278 *
    … …  
    287290        // allow [[foo]] syntax for escaping a tag
    288291        if ( $m[1] == '[' && $m[6] == ']' ) {
    289                 return substr($m[0], 1, -1);
    290         }
    291 
    292         $tag = $m[2];
     292                return substr( $m[0], 1, -1 );
     293        }
     294
     295        $tag  = $m[2];
    293296        $attr = shortcode_parse_atts( $m[3] );
    294297
    … …  
    352355function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
    353356        // Normalize entities in unfiltered HTML before adding placeholders.
    354         $trans = array( '[' => '[', ']' => ']' );
     357        $trans   = array(
     358                '[' => '[',
     359                ']' => ']',
     360        );
    355361        $content = strtr( $content, $trans );
    356         $trans = array( '[' => '[', ']' => ']' );
     362        $trans   = array(
     363                '[' => '[',
     364                ']' => ']',
     365        );
    357366
    358367        $pattern = get_shortcode_regex( $tagnames );
    … …  
    364373                }
    365374
    366                 $noopen = false === strpos( $element, '[' );
     375                $noopen  = false === strpos( $element, '[' );
    367376                $noclose = false === strpos( $element, ']' );
    368377                if ( $noopen || $noclose ) {
    … …  
    394403
    395404                // Get element name
    396                 $front = array_shift( $attributes );
    397                 $back = array_pop( $attributes );
     405                $front   = array_shift( $attributes );
     406                $back    = array_pop( $attributes );
    398407                $matches = array();
    399                 preg_match('%[a-zA-Z0-9]+%', $front, $matches);
     408                preg_match( '%[a-zA-Z0-9]+%', $front, $matches );
    400409                $elname = $matches[0];
    401410
    402411                // Look for shortcodes in each attribute separately.
    403412                foreach ( $attributes as &$attr ) {
    404                         $open = strpos( $attr, '[' );
     413                        $open  = strpos( $attr, '[' );
    405414                        $close = strpos( $attr, ']' );
    406415                        if ( false === $open || false === $close ) {
    … …  
    418427                                // $attr like 'name = "[shortcode]"' or "name = '[shortcode]'"
    419428                                // We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
    420                                 $count = 0;
     429                                $count    = 0;
    421430                                $new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count );
    422431                                if ( $count > 0 ) {
    … …  
    450459 */
    451460function unescape_invalid_shortcodes( $content ) {
    452         // Clean up entire string, avoids re-parsing HTML.
    453         $trans = array( '[' => '[', ']' => ']' );
    454         $content = strtr( $content, $trans );
    455 
    456         return $content;
     461                // Clean up entire string, avoids re-parsing HTML.
     462                $trans   = array(
     463                        '[' => '[',
     464                        ']' => ']',
     465                );
     466                $content = strtr( $content, $trans );
     467
     468                return $content;
    457469}
    458470
    … …  
    483495 *                      All other matches are checked for not empty().
    484496 */
    485 function shortcode_parse_atts($text) {
    486         $atts = array();
     497function shortcode_parse_atts( $text ) {
     498        $atts    = array();
    487499        $pattern = get_shortcode_atts_regex();
    488         $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
    489         if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
    490                 foreach ($match as $m) {
    491                         if (!empty($m[1]))
    492                                 $atts[strtolower($m[1])] = stripcslashes($m[2]);
    493                         elseif (!empty($m[3]))
    494                                 $atts[strtolower($m[3])] = stripcslashes($m[4]);
    495                         elseif (!empty($m[5]))
    496                                 $atts[strtolower($m[5])] = stripcslashes($m[6]);
    497                         elseif (isset($m[7]) && strlen($m[7]))
    498                                 $atts[] = stripcslashes($m[7]);
    499                         elseif (isset($m[8]) && strlen($m[8]))
    500                                 $atts[] = stripcslashes($m[8]);
    501                         elseif (isset($m[9]))
    502                                 $atts[] = stripcslashes($m[9]);
     500        $text    = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text );
     501        if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) {
     502                foreach ( $match as $m ) {
     503                        if ( ! empty( $m[1] ) ) {
     504                                $atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] );
     505                        } elseif ( ! empty( $m[3] ) ) {
     506                                $atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] );
     507                        } elseif ( ! empty( $m[5] ) ) {
     508                                $atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] );
     509                        } elseif ( isset( $m[7] ) && strlen( $m[7] ) ) {
     510                                $atts[] = stripcslashes( $m[7] );
     511                        } elseif ( isset( $m[8] ) && strlen( $m[8] ) ) {
     512                                $atts[] = stripcslashes( $m[8] );
     513                        } elseif ( isset( $m[9] ) ) {
     514                                $atts[] = stripcslashes( $m[9] );
     515                        }
    503516                }
    504517
    505518                // Reject any unclosed HTML elements
    506                 foreach( $atts as &$value ) {
     519                foreach ( $atts as &$value ) {
    507520                        if ( false !== strpos( $value, '<' ) ) {
    508521                                if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
    … …  
    512525                }
    513526        } else {
    514                 $atts = ltrim($text);
     527                $atts = ltrim( $text );
    515528        }
    516529        return $atts;
    … …  
    535548 */
    536549function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
    537         $atts = (array)$atts;
    538         $out = array();
    539         foreach ($pairs as $name => $default) {
    540                 if ( array_key_exists($name, $atts) )
    541                         $out[$name] = $atts[$name];
    542                 else
    543                         $out[$name] = $default;
     550        $atts = (array) $atts;
     551        $out  = array();
     552        foreach ( $pairs as $name => $default ) {
     553                if ( array_key_exists( $name, $atts ) ) {
     554                        $out[ $name ] = $atts[ $name ];
     555                } else {
     556                        $out[ $name ] = $default;
     557                }
    544558        }
    545559        /**
    … …  
    581595        }
    582596
    583         if (empty($shortcode_tags) || !is_array($shortcode_tags))
     597        if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
    584598                return $content;
     599        }
    585600
    586601        // Find all registered tag names in $content.
    … …  
    627642        // allow [[foo]] syntax for escaping a tag
    628643        if ( $m[1] == '[' && $m[6] == ']' ) {
    629                 return substr($m[0], 1, -1);
     644                return substr( $m[0], 1, -1 );
    630645        }
    631646
Note: See TracChangeset for help on using the changeset viewer.