Make WordPress Core

Changeset 38713


Ignore:
Timestamp:
10/04/2016 12:38:45 AM (8 years ago)
Author:
pento
Message:

Shortcodes: Add a do_shortcode_tag filter.

The addition of the pre_do_shortcode_tag in [38506] allows plugins to short-circuit the shortcode execution process, which is particularly helpful for caching expensive shortcodes.

The do_shortcode_tag is the corresponding part of that system - when a shortcode hasn't been executed previously, there needs to be a clean method of populating the cache.

Props flixos90.
Fixes #32790.

Location:
trunk
Files:
2 edited

Legend:

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

    r38506 r38713  
    340340    }
    341341
    342     if ( isset( $m[5] ) ) {
    343         // enclosing tag - extra parameter
    344         return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
    345     } else {
    346         // self-closing tag
    347         return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null,  $tag ) . $m[6];
    348     }
     342    $content = isset( $m[5] ) ? $m[5] : null;
     343
     344    $output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];
     345
     346    /**
     347     * Filters the output created by a shortcode callback.
     348     *
     349     * @since 4.7.0
     350     *
     351     * @param string $output Shortcode output.
     352     * @param string $tag    Shortcode name.
     353     * @param array  $attr   Shortcode attributes array,
     354     * @param array  $m      Regular expression match array.
     355     */
     356    return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
    349357}
    350358
  • trunk/tests/phpunit/tests/shortcode.php

    r38506 r38713  
    720720        remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_p11' ), 11 );
    721721        remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_bar' ) );
    722         remove_shortcode( $str, array( $this, '_shortcode_pre_do_shortcode_tag' ) );
     722        remove_shortcode( $str );
    723723    }
    724724
     
    744744        return wp_json_encode( $arr );
    745745    }
     746
     747    /**
     748     * @ticket 32790
     749     */
     750    public function test_do_shortcode_tag_filter() {
     751        // does nothing if no filters are set up
     752        $str = 'do_shortcode_tag';
     753        add_shortcode( $str, array( $this, '_shortcode_do_shortcode_tag' ) );
     754        $result_nofilter = do_shortcode( "[{$str}]" );
     755        $this->assertSame( 'foo', $result_nofilter );
     756
     757        // modify output with filter
     758        add_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_replace' ) );
     759        $result_filter = do_shortcode( "[{$str}]" );
     760        $this->assertSame( 'fee', $result_filter );
     761
     762        // respect priority
     763        add_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_generate' ), 11 );
     764        $result_priority = do_shortcode( "[{$str}]" );
     765        $this->assertSame( 'foobar', $result_priority );
     766
     767        // pass arguments
     768        $arr = array(
     769            'return' => 'foobar',
     770            'key'    => $str,
     771            'atts'   => array( 'a' => 'b', 'c' => 'd' ),
     772            'm'      => array(
     773                "[{$str} a='b' c='d']",
     774                "",
     775                $str,
     776                " a='b' c='d'",
     777                "",
     778                "",
     779                "",
     780            ),
     781        );
     782        add_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_attr' ), 12, 4 );
     783        $result_atts = do_shortcode( "[{$str} a='b' c='d']" );
     784        $this->assertSame( wp_json_encode( $arr ), $result_atts );
     785
     786        remove_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_attr' ), 12 );
     787        remove_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_generate' ), 11 );
     788        remove_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_replace' ) );
     789        remove_shortcode( $str );
     790    }
     791
     792    public function _shortcode_do_shortcode_tag( $atts = array(), $content = '' ) {
     793        return 'foo';
     794    }
     795
     796    public function _filter_do_shortcode_tag_replace( $return ) {
     797        return str_replace( 'oo', 'ee', $return );
     798    }
     799
     800    public function _filter_do_shortcode_tag_generate( $return ) {
     801        return 'foobar';
     802    }
     803
     804    public function _filter_do_shortcode_tag_attr( $return, $key, $atts, $m ){
     805        $arr = array(
     806            'return' => $return,
     807            'key'    => $key,
     808            'atts'   => $atts,
     809            'm'      => $m,
     810        );
     811        return wp_json_encode( $arr );
     812    }
    746813}
Note: See TracChangeset for help on using the changeset viewer.