Make WordPress Core


Ignore:
Timestamp:
10/04/2016 12:38:45 AM (7 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.