WordPress.org

Make WordPress Core

Ticket #47616: 47616.patch

File 47616.patch, 2.9 KB (added by keraweb, 5 months ago)

Add doing_shortcode() and current_shortcode() functions

  • wp-includes/shortcodes.php

     
    4242$shortcode_tags = array();
    4343
    4444/**
     45 * Container the keeps track of currently running shortcodes.
     46 *
     47 * @since 5.2.x
     48 * @var array
     49 * @global array $wp_current_shortcode
     50 */
     51global $wp_current_shortcode;
     52
     53if ( ! isset( $wp_current_shortcode ) ) {
     54        $wp_current_shortcode = array();
     55}
     56
     57/**
    4558 * Adds a new shortcode.
    4659 *
    4760 * Care should be taken through prefixing or other means to ensure that the
     
    160173}
    161174
    162175/**
     176 * Retrieve the name of the current shortcode being processed.
     177 *
     178 * @since 5.2.x
     179 *
     180 * @global array $wp_current_shortcode Stores the list of current shortcodes with the current one last.
     181 *
     182 * @return string Tag name of the current shortcode.
     183 */
     184function current_shortcode() {
     185        global $wp_current_shortcode;
     186        return end( $wp_current_shortcode );
     187}
     188
     189/**
     190 * Check if a shortcode is currently being processed.
     191 *
     192 * This function allows detection for any shortcode currently being
     193 * executed (despite not being the most recent shortcode to fire, in the case of
     194 * shortcodes called from shortcode callbacks) to be verified.
     195 *
     196 * @since 5.2.x
     197 *
     198 * @global array $wp_current_shortcode Current shortcode(s).
     199 *
     200 * @param null|string $tag Optional. Shortcode tag to check. Defaults to null, which
     201 *                         checks if any shortcode is currently being run.
     202 * @return bool Whether the shortcode is currently in the stack.
     203 */
     204function doing_shortcode( $tag = null ) {
     205        global $wp_current_shortcode;
     206
     207        if ( null === $tag ) {
     208                return ! empty( $wp_current_shortcode );
     209        }
     210
     211        return in_array( $tag, $wp_current_shortcode );
     212}
     213
     214/**
    163215 * Search content for shortcodes and filter shortcodes through their hooks.
    164216 *
    165217 * If there are no shortcode tags defined, then the content will be returned
     
    285337 * @return string|false False on failure.
    286338 */
    287339function do_shortcode_tag( $m ) {
    288         global $shortcode_tags;
     340        global $shortcode_tags, $wp_current_shortcode;
    289341
    290342        // allow [[foo]] syntax for escaping a tag
    291343        if ( $m[1] == '[' && $m[6] == ']' ) {
     
    302354                return $m[0];
    303355        }
    304356
     357        $wp_current_shortcode[] = $tag;
     358
    305359        /**
    306360         * Filters whether to call a shortcode callback.
    307361         *
     
    317371         */
    318372        $return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
    319373        if ( false !== $return ) {
     374                array_pop( $wp_current_shortcode );
    320375                return $return;
    321376        }
    322377
     
    334389         * @param array|string $attr   Shortcode attributes array or empty string.
    335390         * @param array        $m      Regular expression match array.
    336391         */
    337         return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
     392        $return = apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
     393
     394        array_pop( $wp_current_shortcode );
     395
     396        return $return;
    338397}
    339398
    340399/**