WordPress.org

Make WordPress Core

Changeset 38506


Ignore:
Timestamp:
09/02/2016 12:09:42 AM (3 years ago)
Author:
pento
Message:

Shortcodes: Add the pre_do_shortcode_tag filter.

This filter allows the shortcode generation process to be short-circuited, so expensive short codes can be cached and returned immediately.

Props ideag.
Fixes #37906.

Location:
trunk
Files:
2 edited

Legend:

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

    r37865 r38506  
    322322    }
    323323
     324    /**
     325     * Filters whether to call a shortcode callback.
     326     *
     327     * Passing a truthy value to the filter will effectively short-circuit the
     328     * shortcode generation process, returning that value instead.
     329     *
     330     * @since 4.7.0
     331     *
     332     * @param bool|string $return      Short-circuit return value. Either false or the value to replace the shortcode with.
     333     * @param string      $tag         Shortcode name.
     334     * @param array       $attr        Shortcode attributes array,
     335     * @param array       $m           Regular expression match array.
     336     */
     337    $return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
     338    if ( false !== $return ) {
     339        return $return;
     340    }
     341
    324342    if ( isset( $m[5] ) ) {
    325343        // enclosing tag - extra parameter
  • trunk/tests/phpunit/tests/shortcode.php

    r37298 r38506  
    677677        $this->assertEquals( $expected, $out );
    678678    }
     679
     680    /**
     681     * @ticket 37906
     682     */
     683    public function test_pre_do_shortcode_tag() {
     684        // does nothing if no filters are set up
     685        $str = 'pre_do_shortcode_tag';
     686        add_shortcode( $str, array( $this, '_shortcode_pre_do_shortcode_tag' ) );
     687        $result_nofilter = do_shortcode( "[{$str}]" );
     688        $this->assertSame( 'foo', $result_nofilter );
     689
     690        // short-circuit with filter
     691        add_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_bar' ) );
     692        $result_filter = do_shortcode( "[{$str}]" );
     693        $this->assertSame( 'bar', $result_filter );
     694
     695        // respect priority
     696        add_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_p11' ), 11 );
     697        $result_priority = do_shortcode( "[{$str}]" );
     698        $this->assertSame( 'p11', $result_priority );
     699
     700        // pass arguments
     701        $arr = array(
     702            'return'    => 'p11',
     703            'key'           => $str,
     704            'atts'      => array( 'a'=>'b', 'c'=>'d' ),
     705            'm'             => array(
     706                "[{$str} a='b' c='d']",
     707                "",
     708                $str,
     709                " a='b' c='d'",
     710                "",
     711                "",
     712                "",
     713            ),
     714        );
     715        add_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_attr' ), 12, 4 );
     716        $result_atts = do_shortcode( "[{$str} a='b' c='d']" );
     717        $this->assertSame( wp_json_encode( $arr ), $result_atts );
     718
     719        remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_attr' ), 12, 4 );
     720        remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_p11' ), 11 );
     721        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' ) );
     723    }
     724
     725    public function _shortcode_pre_do_shortcode_tag( $atts = array(), $content = '' ) {
     726        return 'foo';
     727    }
     728
     729    public function _filter_pre_do_shortcode_tag_bar() {
     730        return 'bar';
     731    }
     732
     733    public function _filter_pre_do_shortcode_tag_p11() {
     734        return 'p11';
     735    }
     736
     737    public function _filter_pre_do_shortcode_tag_attr( $return, $key, $atts, $m ){
     738        $arr = array(
     739            'return' => $return,
     740            'key'    => $key,
     741            'atts'   => $atts,
     742            'm'      => $m,
     743        );
     744        return wp_json_encode( $arr );
     745    }
    679746}
Note: See TracChangeset for help on using the changeset viewer.