[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> shortcodes.php (source)

   1  <?php
   2  /**
   3   * WordPress API for creating bbcode-like tags or what WordPress calls
   4   * "shortcodes". The tag and attribute parsing or regular expression code is
   5   * based on the Textpattern tag parser.
   6   *
   7   * A few examples are below:
   8   *
   9   * [shortcode /]
  10   * [shortcode foo="bar" baz="bing" /]
  11   * [shortcode foo="bar"]content[/shortcode]
  12   *
  13   * Shortcode tags support attributes and enclosed content, but does not entirely
  14   * support inline shortcodes in other shortcodes. You will have to call the
  15   * shortcode parser in your function to account for that.
  16   *
  17   * {@internal
  18   * Please be aware that the above note was made during the beta of WordPress 2.6
  19   * and in the future may not be accurate. Please update the note when it is no
  20   * longer the case.}}
  21   *
  22   * To apply shortcode tags to content:
  23   *
  24   *     $out = do_shortcode( $content );
  25   *
  26   * @link https://developer.wordpress.org/plugins/shortcodes/
  27   *
  28   * @package WordPress
  29   * @subpackage Shortcodes
  30   * @since 2.5.0
  31   */
  32  
  33  /**
  34   * Container for storing shortcode tags and their hook to call for the shortcode
  35   *
  36   * @since 2.5.0
  37   *
  38   * @name $shortcode_tags
  39   * @var array
  40   * @global array $shortcode_tags
  41   */
  42  $shortcode_tags = array();
  43  
  44  /**
  45   * Adds a new shortcode.
  46   *
  47   * Care should be taken through prefixing or other means to ensure that the
  48   * shortcode tag being added is unique and will not conflict with other,
  49   * already-added shortcode tags. In the event of a duplicated tag, the tag
  50   * loaded last will take precedence.
  51   *
  52   * @since 2.5.0
  53   *
  54   * @global array $shortcode_tags
  55   *
  56   * @param string   $tag      Shortcode tag to be searched in post content.
  57   * @param callable $callback The callback function to run when the shortcode is found.
  58   *                           Every shortcode callback is passed three parameters by default,
  59   *                           including an array of attributes (`$atts`), the shortcode content
  60   *                           or null if not set (`$content`), and finally the shortcode tag
  61   *                           itself (`$shortcode_tag`), in that order.
  62   */
  63  function add_shortcode( $tag, $callback ) {
  64      global $shortcode_tags;
  65  
  66      if ( '' === trim( $tag ) ) {
  67          $message = __( 'Invalid shortcode name: Empty name given.' );
  68          _doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
  69          return;
  70      }
  71  
  72      if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
  73          /* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
  74          $message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' );
  75          _doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
  76          return;
  77      }
  78  
  79      $shortcode_tags[ $tag ] = $callback;
  80  }
  81  
  82  /**
  83   * Removes hook for shortcode.
  84   *
  85   * @since 2.5.0
  86   *
  87   * @global array $shortcode_tags
  88   *
  89   * @param string $tag Shortcode tag to remove hook for.
  90   */
  91  function remove_shortcode( $tag ) {
  92      global $shortcode_tags;
  93  
  94      unset( $shortcode_tags[ $tag ] );
  95  }
  96  
  97  /**
  98   * Clear all shortcodes.
  99   *
 100   * This function is simple, it clears all of the shortcode tags by replacing the
 101   * shortcodes global by a empty array. This is actually a very efficient method
 102   * for removing all shortcodes.
 103   *
 104   * @since 2.5.0
 105   *
 106   * @global array $shortcode_tags
 107   */
 108  function remove_all_shortcodes() {
 109      global $shortcode_tags;
 110  
 111      $shortcode_tags = array();
 112  }
 113  
 114  /**
 115   * Whether a registered shortcode exists named $tag
 116   *
 117   * @since 3.6.0
 118   *
 119   * @global array $shortcode_tags List of shortcode tags and their callback hooks.
 120   *
 121   * @param string $tag Shortcode tag to check.
 122   * @return bool Whether the given shortcode exists.
 123   */
 124  function shortcode_exists( $tag ) {
 125      global $shortcode_tags;
 126      return array_key_exists( $tag, $shortcode_tags );
 127  }
 128  
 129  /**
 130   * Whether the passed content contains the specified shortcode
 131   *
 132   * @since 3.6.0
 133   *
 134   * @global array $shortcode_tags
 135   *
 136   * @param string $content Content to search for shortcodes.
 137   * @param string $tag     Shortcode tag to check.
 138   * @return bool Whether the passed content contains the given shortcode.
 139   */
 140  function has_shortcode( $content, $tag ) {
 141      if ( false === strpos( $content, '[' ) ) {
 142          return false;
 143      }
 144  
 145      if ( shortcode_exists( $tag ) ) {
 146          preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
 147          if ( empty( $matches ) ) {
 148              return false;
 149          }
 150  
 151          foreach ( $matches as $shortcode ) {
 152              if ( $tag === $shortcode[2] ) {
 153                  return true;
 154              } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
 155                  return true;
 156              }
 157          }
 158      }
 159      return false;
 160  }
 161  
 162  /**
 163   * Search content for shortcodes and filter shortcodes through their hooks.
 164   *
 165   * This function is an alias for do_shortcode().
 166   *
 167   * @since 5.4.0
 168   *
 169   * @see do_shortcode()
 170   *
 171   * @param string $content     Content to search for shortcodes.
 172   * @param bool   $ignore_html When true, shortcodes inside HTML elements will be skipped.
 173   *                            Default false.
 174   * @return string Content with shortcodes filtered out.
 175   */
 176  function apply_shortcodes( $content, $ignore_html = false ) {
 177      return do_shortcode( $content, $ignore_html );
 178  }
 179  
 180  /**
 181   * Search content for shortcodes and filter shortcodes through their hooks.
 182   *
 183   * If there are no shortcode tags defined, then the content will be returned
 184   * without any filtering. This might cause issues when plugins are disabled but
 185   * the shortcode will still show up in the post or content.
 186   *
 187   * @since 2.5.0
 188   *
 189   * @global array $shortcode_tags List of shortcode tags and their callback hooks.
 190   *
 191   * @param string $content     Content to search for shortcodes.
 192   * @param bool   $ignore_html When true, shortcodes inside HTML elements will be skipped.
 193   *                            Default false.
 194   * @return string Content with shortcodes filtered out.
 195   */
 196  function do_shortcode( $content, $ignore_html = false ) {
 197      global $shortcode_tags;
 198  
 199      if ( false === strpos( $content, '[' ) ) {
 200          return $content;
 201      }
 202  
 203      if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
 204          return $content;
 205      }
 206  
 207      // Find all registered tag names in $content.
 208      preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
 209      $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
 210  
 211      if ( empty( $tagnames ) ) {
 212          return $content;
 213      }
 214  
 215      $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames );
 216  
 217      $pattern = get_shortcode_regex( $tagnames );
 218      $content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content );
 219  
 220      // Always restore square braces so we don't break things like <!--[if IE ]>.
 221      $content = unescape_invalid_shortcodes( $content );
 222  
 223      return $content;
 224  }
 225  
 226  /**
 227   * Retrieve the shortcode regular expression for searching.
 228   *
 229   * The regular expression combines the shortcode tags in the regular expression
 230   * in a regex class.
 231   *
 232   * The regular expression contains 6 different sub matches to help with parsing.
 233   *
 234   * 1 - An extra [ to allow for escaping shortcodes with double [[]]
 235   * 2 - The shortcode name
 236   * 3 - The shortcode argument list
 237   * 4 - The self closing /
 238   * 5 - The content of a shortcode when it wraps some content.
 239   * 6 - An extra ] to allow for escaping shortcodes with double [[]]
 240   *
 241   * @since 2.5.0
 242   * @since 4.4.0 Added the `$tagnames` parameter.
 243   *
 244   * @global array $shortcode_tags
 245   *
 246   * @param array $tagnames Optional. List of shortcodes to find. Defaults to all registered shortcodes.
 247   * @return string The shortcode search regular expression
 248   */
 249  function get_shortcode_regex( $tagnames = null ) {
 250      global $shortcode_tags;
 251  
 252      if ( empty( $tagnames ) ) {
 253          $tagnames = array_keys( $shortcode_tags );
 254      }
 255      $tagregexp = implode( '|', array_map( 'preg_quote', $tagnames ) );
 256  
 257      // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag().
 258      // Also, see shortcode_unautop() and shortcode.js.
 259  
 260      // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
 261      return '\\['                             // Opening bracket.
 262          . '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]].
 263          . "($tagregexp)"                     // 2: Shortcode name.
 264          . '(?![\\w-])'                       // Not followed by word character or hyphen.
 265          . '('                                // 3: Unroll the loop: Inside the opening shortcode tag.
 266          .     '[^\\]\\/]*'                   // Not a closing bracket or forward slash.
 267          .     '(?:'
 268          .         '\\/(?!\\])'               // A forward slash not followed by a closing bracket.
 269          .         '[^\\]\\/]*'               // Not a closing bracket or forward slash.
 270          .     ')*?'
 271          . ')'
 272          . '(?:'
 273          .     '(\\/)'                        // 4: Self closing tag...
 274          .     '\\]'                          // ...and closing bracket.
 275          . '|'
 276          .     '\\]'                          // Closing bracket.
 277          .     '(?:'
 278          .         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags.
 279          .             '[^\\[]*+'             // Not an opening bracket.
 280          .             '(?:'
 281          .                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag.
 282          .                 '[^\\[]*+'         // Not an opening bracket.
 283          .             ')*+'
 284          .         ')'
 285          .         '\\[\\/\\2\\]'             // Closing shortcode tag.
 286          .     ')?'
 287          . ')'
 288          . '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]].
 289      // phpcs:enable
 290  }
 291  
 292  /**
 293   * Regular Expression callable for do_shortcode() for calling shortcode hook.
 294   *
 295   * @see get_shortcode_regex() for details of the match array contents.
 296   *
 297   * @since 2.5.0
 298   * @access private
 299   *
 300   * @global array $shortcode_tags
 301   *
 302   * @param array $m Regular expression match array
 303   * @return string|false False on failure.
 304   */
 305  function do_shortcode_tag( $m ) {
 306      global $shortcode_tags;
 307  
 308      // Allow [[foo]] syntax for escaping a tag.
 309      if ( '[' === $m[1] && ']' === $m[6] ) {
 310          return substr( $m[0], 1, -1 );
 311      }
 312  
 313      $tag  = $m[2];
 314      $attr = shortcode_parse_atts( $m[3] );
 315  
 316      if ( ! is_callable( $shortcode_tags[ $tag ] ) ) {
 317          /* translators: %s: Shortcode tag. */
 318          $message = sprintf( __( 'Attempting to parse a shortcode without a valid callback: %s' ), $tag );
 319          _doing_it_wrong( __FUNCTION__, $message, '4.3.0' );
 320          return $m[0];
 321      }
 322  
 323      /**
 324       * Filters whether to call a shortcode callback.
 325       *
 326       * Returning a non-false value from filter will short-circuit the
 327       * shortcode generation process, returning that value instead.
 328       *
 329       * @since 4.7.0
 330       *
 331       * @param false|string $return      Short-circuit return value. Either false or the value to replace the shortcode with.
 332       * @param string       $tag         Shortcode name.
 333       * @param array|string $attr        Shortcode attributes array or empty string.
 334       * @param array        $m           Regular expression match array.
 335       */
 336      $return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
 337      if ( false !== $return ) {
 338          return $return;
 339      }
 340  
 341      $content = isset( $m[5] ) ? $m[5] : null;
 342  
 343      $output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];
 344  
 345      /**
 346       * Filters the output created by a shortcode callback.
 347       *
 348       * @since 4.7.0
 349       *
 350       * @param string       $output Shortcode output.
 351       * @param string       $tag    Shortcode name.
 352       * @param array|string $attr   Shortcode attributes array or empty string.
 353       * @param array        $m      Regular expression match array.
 354       */
 355      return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
 356  }
 357  
 358  /**
 359   * Search only inside HTML elements for shortcodes and process them.
 360   *
 361   * Any [ or ] characters remaining inside elements will be HTML encoded
 362   * to prevent interference with shortcodes that are outside the elements.
 363   * Assumes $content processed by KSES already.  Users with unfiltered_html
 364   * capability may get unexpected output if angle braces are nested in tags.
 365   *
 366   * @since 4.2.3
 367   *
 368   * @param string $content     Content to search for shortcodes.
 369   * @param bool   $ignore_html When true, all square braces inside elements will be encoded.
 370   * @param array  $tagnames    List of shortcodes to find.
 371   * @return string Content with shortcodes filtered out.
 372   */
 373  function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
 374      // Normalize entities in unfiltered HTML before adding placeholders.
 375      $trans   = array(
 376          '&#91;' => '&#091;',
 377          '&#93;' => '&#093;',
 378      );
 379      $content = strtr( $content, $trans );
 380      $trans   = array(
 381          '[' => '&#91;',
 382          ']' => '&#93;',
 383      );
 384  
 385      $pattern = get_shortcode_regex( $tagnames );
 386      $textarr = wp_html_split( $content );
 387  
 388      foreach ( $textarr as &$element ) {
 389          if ( '' === $element || '<' !== $element[0] ) {
 390              continue;
 391          }
 392  
 393          $noopen  = false === strpos( $element, '[' );
 394          $noclose = false === strpos( $element, ']' );
 395          if ( $noopen || $noclose ) {
 396              // This element does not contain shortcodes.
 397              if ( $noopen xor $noclose ) {
 398                  // Need to encode stray '[' or ']' chars.
 399                  $element = strtr( $element, $trans );
 400              }
 401              continue;
 402          }
 403  
 404          if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
 405              // Encode all '[' and ']' chars.
 406              $element = strtr( $element, $trans );
 407              continue;
 408          }
 409  
 410          $attributes = wp_kses_attr_parse( $element );
 411          if ( false === $attributes ) {
 412              // Some plugins are doing things like [name] <[email]>.
 413              if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) {
 414                  $element = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $element );
 415              }
 416  
 417              // Looks like we found some crazy unfiltered HTML. Skipping it for sanity.
 418              $element = strtr( $element, $trans );
 419              continue;
 420          }
 421  
 422          // Get element name.
 423          $front   = array_shift( $attributes );
 424          $back    = array_pop( $attributes );
 425          $matches = array();
 426          preg_match( '%[a-zA-Z0-9]+%', $front, $matches );
 427          $elname = $matches[0];
 428  
 429          // Look for shortcodes in each attribute separately.
 430          foreach ( $attributes as &$attr ) {
 431              $open  = strpos( $attr, '[' );
 432              $close = strpos( $attr, ']' );
 433              if ( false === $open || false === $close ) {
 434                  continue; // Go to next attribute. Square braces will be escaped at end of loop.
 435              }
 436              $double = strpos( $attr, '"' );
 437              $single = strpos( $attr, "'" );
 438              if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {
 439                  /*
 440                   * $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
 441                   * In this specific situation we assume KSES did not run because the input
 442                   * was written by an administrator, so we should avoid changing the output
 443                   * and we do not need to run KSES here.
 444                   */
 445                  $attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr );
 446              } else {
 447                  // $attr like 'name = "[shortcode]"' or "name = '[shortcode]'".
 448                  // We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
 449                  $count    = 0;
 450                  $new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count );
 451                  if ( $count > 0 ) {
 452                      // Sanitize the shortcode output using KSES.
 453                      $new_attr = wp_kses_one_attr( $new_attr, $elname );
 454                      if ( '' !== trim( $new_attr ) ) {
 455                          // The shortcode is safe to use now.
 456                          $attr = $new_attr;
 457                      }
 458                  }
 459              }
 460          }
 461          $element = $front . implode( '', $attributes ) . $back;
 462  
 463          // Now encode any remaining '[' or ']' chars.
 464          $element = strtr( $element, $trans );
 465      }
 466  
 467      $content = implode( '', $textarr );
 468  
 469      return $content;
 470  }
 471  
 472  /**
 473   * Remove placeholders added by do_shortcodes_in_html_tags().
 474   *
 475   * @since 4.2.3
 476   *
 477   * @param string $content Content to search for placeholders.
 478   * @return string Content with placeholders removed.
 479   */
 480  function unescape_invalid_shortcodes( $content ) {
 481      // Clean up entire string, avoids re-parsing HTML.
 482      $trans = array(
 483          '&#91;' => '[',
 484          '&#93;' => ']',
 485      );
 486  
 487      $content = strtr( $content, $trans );
 488  
 489      return $content;
 490  }
 491  
 492  /**
 493   * Retrieve the shortcode attributes regex.
 494   *
 495   * @since 4.4.0
 496   *
 497   * @return string The shortcode attribute regular expression
 498   */
 499  function get_shortcode_atts_regex() {
 500      return '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|\'([^\']*)\'(?:\s|$)|(\S+)(?:\s|$)/';
 501  }
 502  
 503  /**
 504   * Retrieve all attributes from the shortcodes tag.
 505   *
 506   * The attributes list has the attribute name as the key and the value of the
 507   * attribute as the value in the key/value pair. This allows for easier
 508   * retrieval of the attributes, since all attributes have to be known.
 509   *
 510   * @since 2.5.0
 511   *
 512   * @param string $text
 513   * @return array|string List of attribute values.
 514   *                      Returns empty array if '""' === trim( $text ).
 515   *                      Returns empty string if '' === trim( $text ).
 516   *                      All other matches are checked for not empty().
 517   */
 518  function shortcode_parse_atts( $text ) {
 519      $atts    = array();
 520      $pattern = get_shortcode_atts_regex();
 521      $text    = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text );
 522      if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) {
 523          foreach ( $match as $m ) {
 524              if ( ! empty( $m[1] ) ) {
 525                  $atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] );
 526              } elseif ( ! empty( $m[3] ) ) {
 527                  $atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] );
 528              } elseif ( ! empty( $m[5] ) ) {
 529                  $atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] );
 530              } elseif ( isset( $m[7] ) && strlen( $m[7] ) ) {
 531                  $atts[] = stripcslashes( $m[7] );
 532              } elseif ( isset( $m[8] ) && strlen( $m[8] ) ) {
 533                  $atts[] = stripcslashes( $m[8] );
 534              } elseif ( isset( $m[9] ) ) {
 535                  $atts[] = stripcslashes( $m[9] );
 536              }
 537          }
 538  
 539          // Reject any unclosed HTML elements.
 540          foreach ( $atts as &$value ) {
 541              if ( false !== strpos( $value, '<' ) ) {
 542                  if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
 543                      $value = '';
 544                  }
 545              }
 546          }
 547      } else {
 548          $atts = ltrim( $text );
 549      }
 550  
 551      return $atts;
 552  }
 553  
 554  /**
 555   * Combine user attributes with known attributes and fill in defaults when needed.
 556   *
 557   * The pairs should be considered to be all of the attributes which are
 558   * supported by the caller and given as a list. The returned attributes will
 559   * only contain the attributes in the $pairs list.
 560   *
 561   * If the $atts list has unsupported attributes, then they will be ignored and
 562   * removed from the final returned list.
 563   *
 564   * @since 2.5.0
 565   *
 566   * @param array  $pairs     Entire list of supported attributes and their defaults.
 567   * @param array  $atts      User defined attributes in shortcode tag.
 568   * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
 569   * @return array Combined and filtered attribute list.
 570   */
 571  function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
 572      $atts = (array) $atts;
 573      $out  = array();
 574      foreach ( $pairs as $name => $default ) {
 575          if ( array_key_exists( $name, $atts ) ) {
 576              $out[ $name ] = $atts[ $name ];
 577          } else {
 578              $out[ $name ] = $default;
 579          }
 580      }
 581  
 582      if ( $shortcode ) {
 583          /**
 584           * Filters shortcode attributes.
 585           *
 586           * If the third parameter of the shortcode_atts() function is present then this filter is available.
 587           * The third parameter, $shortcode, is the name of the shortcode.
 588           *
 589           * @since 3.6.0
 590           * @since 4.4.0 Added the `$shortcode` parameter.
 591           *
 592           * @param array  $out       The output array of shortcode attributes.
 593           * @param array  $pairs     The supported attributes and their defaults.
 594           * @param array  $atts      The user defined shortcode attributes.
 595           * @param string $shortcode The shortcode name.
 596           */
 597          $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
 598      }
 599  
 600      return $out;
 601  }
 602  
 603  /**
 604   * Remove all shortcode tags from the given content.
 605   *
 606   * @since 2.5.0
 607   *
 608   * @global array $shortcode_tags
 609   *
 610   * @param string $content Content to remove shortcode tags.
 611   * @return string Content without shortcode tags.
 612   */
 613  function strip_shortcodes( $content ) {
 614      global $shortcode_tags;
 615  
 616      if ( false === strpos( $content, '[' ) ) {
 617          return $content;
 618      }
 619  
 620      if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
 621          return $content;
 622      }
 623  
 624      // Find all registered tag names in $content.
 625      preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
 626  
 627      $tags_to_remove = array_keys( $shortcode_tags );
 628  
 629      /**
 630       * Filters the list of shortcode tags to remove from the content.
 631       *
 632       * @since 4.7.0
 633       *
 634       * @param array  $tags_to_remove Array of shortcode tags to remove.
 635       * @param string $content        Content shortcodes are being removed from.
 636       */
 637      $tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content );
 638  
 639      $tagnames = array_intersect( $tags_to_remove, $matches[1] );
 640  
 641      if ( empty( $tagnames ) ) {
 642          return $content;
 643      }
 644  
 645      $content = do_shortcodes_in_html_tags( $content, true, $tagnames );
 646  
 647      $pattern = get_shortcode_regex( $tagnames );
 648      $content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
 649  
 650      // Always restore square braces so we don't break things like <!--[if IE ]>.
 651      $content = unescape_invalid_shortcodes( $content );
 652  
 653      return $content;
 654  }
 655  
 656  /**
 657   * Strips a shortcode tag based on RegEx matches against post content.
 658   *
 659   * @since 3.3.0
 660   *
 661   * @param array $m RegEx matches against post content.
 662   * @return string|false The content stripped of the tag, otherwise false.
 663   */
 664  function strip_shortcode_tag( $m ) {
 665      // Allow [[foo]] syntax for escaping a tag.
 666      if ( '[' === $m[1] && ']' === $m[6] ) {
 667          return substr( $m[0], 1, -1 );
 668      }
 669  
 670      return $m[1] . $m[6];
 671  }


Generated: Wed Oct 28 01:00:03 2020 Cross-referenced by PHPXref 0.7.1