Make WordPress Core

Ticket #47616: 47616.1.diff

File 47616.1.diff, 3.1 KB (added by audrasjb, 4 years ago)
  • src/wp-includes/shortcodes.php

    diff --git a/src/wp-includes/shortcodes.php b/src/wp-includes/shortcodes.php
    index 62d1d1e644..509957daf1 100644
    a b  
    4141 */
    4242$shortcode_tags = array();
    4343
     44/**
     45 * Container to keep track of currently running shortcodes.
     46 *
     47 * @since 5.7.0
     48 *
     49 * @var array
     50 * @global array $wp_current_shortcode
     51 */
     52global $wp_current_shortcode;
     53
     54if ( ! isset( $wp_current_shortcode ) ) {
     55        $wp_current_shortcode = array();
     56}
     57
    4458/**
    4559 * Adds a new shortcode.
    4660 *
    function has_shortcode( $content, $tag ) { 
    159173        return false;
    160174}
    161175
     176/**
     177 * Retrieve the name of the current shortcode being processed.
     178 *
     179 * @since 5.7.0
     180 *
     181 * @global array $wp_current_shortcode Stores the list of current shortcodes with the current one last.
     182 *
     183 * @return string Tag name of the current shortcode.
     184 */
     185function current_shortcode() {
     186        global $wp_current_shortcode;
     187        return end( $wp_current_shortcode );
     188}
     189
     190/**
     191 * Check if a shortcode is currently being processed.
     192 *
     193 * This function allows detection for any shortcode currently being executed
     194 * (despite not being the most recent shortcode to fire, in the case of
     195 * shortcodes called from shortcode callbacks) to be verified.
     196 *
     197 * @since 5.7.0
     198 *
     199 * @global array $wp_current_shortcode Current shortcode(s).
     200 *
     201 * @param null|string $tag Optional. Shortcode tag to check. Defaults to null, which
     202 *                         checks if any shortcode is currently being run.
     203 * @return bool Whether the shortcode is currently in the stack.
     204 */
     205function doing_shortcode( $tag = null ) {
     206        global $wp_current_shortcode;
     207
     208        if ( null === $tag ) {
     209                return ! empty( $wp_current_shortcode );
     210        }
     211
     212        return in_array( $tag, $wp_current_shortcode );
     213}
     214
    162215/**
    163216 * Search content for shortcodes and filter shortcodes through their hooks.
    164217 *
    function get_shortcode_regex( $tagnames = null ) { 
    298351 * @access private
    299352 *
    300353 * @global array $shortcode_tags
     354 * @global array $wp_current_shortcode
    301355 *
    302356 * @param array $m Regular expression match array.
    303357 * @return string|false Shortcode output on success, false on failure.
    304358 */
    305359function do_shortcode_tag( $m ) {
    306         global $shortcode_tags;
     360        global $shortcode_tags, $wp_current_shortcode;
    307361
    308362        // Allow [[foo]] syntax for escaping a tag.
    309363        if ( '[' === $m[1] && ']' === $m[6] ) {
    function do_shortcode_tag( $m ) { 
    320374                return $m[0];
    321375        }
    322376
     377        $wp_current_shortcode[] = $tag;
     378
    323379        /**
    324380         * Filters whether to call a shortcode callback.
    325381         *
    function do_shortcode_tag( $m ) { 
    335391         */
    336392        $return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
    337393        if ( false !== $return ) {
     394                array_pop( $wp_current_shortcode );
    338395                return $return;
    339396        }
    340397
    function do_shortcode_tag( $m ) { 
    352409         * @param array|string $attr   Shortcode attributes array or empty string.
    353410         * @param array        $m      Regular expression match array.
    354411         */
    355         return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
     412        $return = apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
     413
     414        array_pop( $wp_current_shortcode );
     415
     416        return $return;
    356417}
    357418
    358419/**