[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * kses 0.2.2 - HTML/XHTML filter that only allows some elements and attributes
   4   * Copyright (C) 2002, 2003, 2005  Ulf Harnhammar
   5   *
   6   * This program is free software and open source software; you can redistribute
   7   * it and/or modify it under the terms of the GNU General Public License as
   8   * published by the Free Software Foundation; either version 2 of the License,
   9   * or (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful, but WITHOUT
  12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14   * more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19   * http://www.gnu.org/licenses/gpl.html
  20   *
  21   * [kses strips evil scripts!]
  22   *
  23   * Added wp_ prefix to avoid conflicts with existing kses users
  24   *
  25   * @version 0.2.2
  26   * @copyright (C) 2002, 2003, 2005
  27   * @author Ulf Harnhammar <http://advogato.org/person/metaur/>
  28   *
  29   * @package External
  30   * @subpackage KSES
  31   */
  32  
  33  /**
  34   * Specifies the default allowable HTML tags.
  35   *
  36   * Using `CUSTOM_TAGS` is not recommended and should be considered deprecated. The
  37   * {@see 'wp_kses_allowed_html'} filter is more powerful and supplies context.
  38   *
  39   * @see wp_kses_allowed_html()
  40   * @since 1.2.0
  41   *
  42   * @var array[]|false Array of default allowable HTML tags, or false to use the defaults.
  43   */
  44  if ( ! defined( 'CUSTOM_TAGS' ) ) {
  45      define( 'CUSTOM_TAGS', false );
  46  }
  47  
  48  // Ensure that these variables are added to the global namespace
  49  // (e.g. if using namespaces / autoload in the current PHP environment).
  50  global $allowedposttags, $allowedtags, $allowedentitynames, $allowedxmlentitynames;
  51  
  52  if ( ! CUSTOM_TAGS ) {
  53      /**
  54       * KSES global for default allowable HTML tags.
  55       *
  56       * Can be overridden with the `CUSTOM_TAGS` constant.
  57       *
  58       * @var array[] $allowedposttags Array of default allowable HTML tags.
  59       * @since 2.0.0
  60       */
  61      $allowedposttags = array(
  62          'address'    => array(),
  63          'a'          => array(
  64              'href'     => true,
  65              'rel'      => true,
  66              'rev'      => true,
  67              'name'     => true,
  68              'target'   => true,
  69              'download' => array(
  70                  'valueless' => 'y',
  71              ),
  72          ),
  73          'abbr'       => array(),
  74          'acronym'    => array(),
  75          'area'       => array(
  76              'alt'    => true,
  77              'coords' => true,
  78              'href'   => true,
  79              'nohref' => true,
  80              'shape'  => true,
  81              'target' => true,
  82          ),
  83          'article'    => array(
  84              'align' => true,
  85          ),
  86          'aside'      => array(
  87              'align' => true,
  88          ),
  89          'audio'      => array(
  90              'autoplay' => true,
  91              'controls' => true,
  92              'loop'     => true,
  93              'muted'    => true,
  94              'preload'  => true,
  95              'src'      => true,
  96          ),
  97          'b'          => array(),
  98          'bdo'        => array(),
  99          'big'        => array(),
 100          'blockquote' => array(
 101              'cite' => true,
 102          ),
 103          'br'         => array(),
 104          'button'     => array(
 105              'disabled' => true,
 106              'name'     => true,
 107              'type'     => true,
 108              'value'    => true,
 109          ),
 110          'caption'    => array(
 111              'align' => true,
 112          ),
 113          'cite'       => array(),
 114          'code'       => array(),
 115          'col'        => array(
 116              'align'   => true,
 117              'char'    => true,
 118              'charoff' => true,
 119              'span'    => true,
 120              'valign'  => true,
 121              'width'   => true,
 122          ),
 123          'colgroup'   => array(
 124              'align'   => true,
 125              'char'    => true,
 126              'charoff' => true,
 127              'span'    => true,
 128              'valign'  => true,
 129              'width'   => true,
 130          ),
 131          'del'        => array(
 132              'datetime' => true,
 133          ),
 134          'dd'         => array(),
 135          'dfn'        => array(),
 136          'details'    => array(
 137              'align' => true,
 138              'open'  => true,
 139          ),
 140          'div'        => array(
 141              'align' => true,
 142          ),
 143          'dl'         => array(),
 144          'dt'         => array(),
 145          'em'         => array(),
 146          'fieldset'   => array(),
 147          'figure'     => array(
 148              'align' => true,
 149          ),
 150          'figcaption' => array(
 151              'align' => true,
 152          ),
 153          'font'       => array(
 154              'color' => true,
 155              'face'  => true,
 156              'size'  => true,
 157          ),
 158          'footer'     => array(
 159              'align' => true,
 160          ),
 161          'h1'         => array(
 162              'align' => true,
 163          ),
 164          'h2'         => array(
 165              'align' => true,
 166          ),
 167          'h3'         => array(
 168              'align' => true,
 169          ),
 170          'h4'         => array(
 171              'align' => true,
 172          ),
 173          'h5'         => array(
 174              'align' => true,
 175          ),
 176          'h6'         => array(
 177              'align' => true,
 178          ),
 179          'header'     => array(
 180              'align' => true,
 181          ),
 182          'hgroup'     => array(
 183              'align' => true,
 184          ),
 185          'hr'         => array(
 186              'align'   => true,
 187              'noshade' => true,
 188              'size'    => true,
 189              'width'   => true,
 190          ),
 191          'i'          => array(),
 192          'img'        => array(
 193              'alt'      => true,
 194              'align'    => true,
 195              'border'   => true,
 196              'height'   => true,
 197              'hspace'   => true,
 198              'loading'  => true,
 199              'longdesc' => true,
 200              'vspace'   => true,
 201              'src'      => true,
 202              'usemap'   => true,
 203              'width'    => true,
 204          ),
 205          'ins'        => array(
 206              'datetime' => true,
 207              'cite'     => true,
 208          ),
 209          'kbd'        => array(),
 210          'label'      => array(
 211              'for' => true,
 212          ),
 213          'legend'     => array(
 214              'align' => true,
 215          ),
 216          'li'         => array(
 217              'align' => true,
 218              'value' => true,
 219          ),
 220          'main'       => array(
 221              'align' => true,
 222          ),
 223          'map'        => array(
 224              'name' => true,
 225          ),
 226          'mark'       => array(),
 227          'menu'       => array(
 228              'type' => true,
 229          ),
 230          'nav'        => array(
 231              'align' => true,
 232          ),
 233          'object'     => array(
 234              'data' => array(
 235                  'required'       => true,
 236                  'value_callback' => '_wp_kses_allow_pdf_objects',
 237              ),
 238              'type' => array(
 239                  'required' => true,
 240                  'values'   => array( 'application/pdf' ),
 241              ),
 242          ),
 243          'p'          => array(
 244              'align' => true,
 245          ),
 246          'pre'        => array(
 247              'width' => true,
 248          ),
 249          'q'          => array(
 250              'cite' => true,
 251          ),
 252          'rb'         => array(),
 253          'rp'         => array(),
 254          'rt'         => array(),
 255          'rtc'        => array(),
 256          'ruby'       => array(),
 257          's'          => array(),
 258          'samp'       => array(),
 259          'span'       => array(
 260              'align' => true,
 261          ),
 262          'section'    => array(
 263              'align' => true,
 264          ),
 265          'small'      => array(),
 266          'strike'     => array(),
 267          'strong'     => array(),
 268          'sub'        => array(),
 269          'summary'    => array(
 270              'align' => true,
 271          ),
 272          'sup'        => array(),
 273          'table'      => array(
 274              'align'       => true,
 275              'bgcolor'     => true,
 276              'border'      => true,
 277              'cellpadding' => true,
 278              'cellspacing' => true,
 279              'rules'       => true,
 280              'summary'     => true,
 281              'width'       => true,
 282          ),
 283          'tbody'      => array(
 284              'align'   => true,
 285              'char'    => true,
 286              'charoff' => true,
 287              'valign'  => true,
 288          ),
 289          'td'         => array(
 290              'abbr'    => true,
 291              'align'   => true,
 292              'axis'    => true,
 293              'bgcolor' => true,
 294              'char'    => true,
 295              'charoff' => true,
 296              'colspan' => true,
 297              'headers' => true,
 298              'height'  => true,
 299              'nowrap'  => true,
 300              'rowspan' => true,
 301              'scope'   => true,
 302              'valign'  => true,
 303              'width'   => true,
 304          ),
 305          'textarea'   => array(
 306              'cols'     => true,
 307              'rows'     => true,
 308              'disabled' => true,
 309              'name'     => true,
 310              'readonly' => true,
 311          ),
 312          'tfoot'      => array(
 313              'align'   => true,
 314              'char'    => true,
 315              'charoff' => true,
 316              'valign'  => true,
 317          ),
 318          'th'         => array(
 319              'abbr'    => true,
 320              'align'   => true,
 321              'axis'    => true,
 322              'bgcolor' => true,
 323              'char'    => true,
 324              'charoff' => true,
 325              'colspan' => true,
 326              'headers' => true,
 327              'height'  => true,
 328              'nowrap'  => true,
 329              'rowspan' => true,
 330              'scope'   => true,
 331              'valign'  => true,
 332              'width'   => true,
 333          ),
 334          'thead'      => array(
 335              'align'   => true,
 336              'char'    => true,
 337              'charoff' => true,
 338              'valign'  => true,
 339          ),
 340          'title'      => array(),
 341          'tr'         => array(
 342              'align'   => true,
 343              'bgcolor' => true,
 344              'char'    => true,
 345              'charoff' => true,
 346              'valign'  => true,
 347          ),
 348          'track'      => array(
 349              'default' => true,
 350              'kind'    => true,
 351              'label'   => true,
 352              'src'     => true,
 353              'srclang' => true,
 354          ),
 355          'tt'         => array(),
 356          'u'          => array(),
 357          'ul'         => array(
 358              'type' => true,
 359          ),
 360          'ol'         => array(
 361              'start'    => true,
 362              'type'     => true,
 363              'reversed' => true,
 364          ),
 365          'var'        => array(),
 366          'video'      => array(
 367              'autoplay'    => true,
 368              'controls'    => true,
 369              'height'      => true,
 370              'loop'        => true,
 371              'muted'       => true,
 372              'playsinline' => true,
 373              'poster'      => true,
 374              'preload'     => true,
 375              'src'         => true,
 376              'width'       => true,
 377          ),
 378      );
 379  
 380      /**
 381       * @var array[] $allowedtags Array of KSES allowed HTML elements.
 382       * @since 1.0.0
 383       */
 384      $allowedtags = array(
 385          'a'          => array(
 386              'href'  => true,
 387              'title' => true,
 388          ),
 389          'abbr'       => array(
 390              'title' => true,
 391          ),
 392          'acronym'    => array(
 393              'title' => true,
 394          ),
 395          'b'          => array(),
 396          'blockquote' => array(
 397              'cite' => true,
 398          ),
 399          'cite'       => array(),
 400          'code'       => array(),
 401          'del'        => array(
 402              'datetime' => true,
 403          ),
 404          'em'         => array(),
 405          'i'          => array(),
 406          'q'          => array(
 407              'cite' => true,
 408          ),
 409          's'          => array(),
 410          'strike'     => array(),
 411          'strong'     => array(),
 412      );
 413  
 414      /**
 415       * @var string[] $allowedentitynames Array of KSES allowed HTML entity names.
 416       * @since 1.0.0
 417       */
 418      $allowedentitynames = array(
 419          'nbsp',
 420          'iexcl',
 421          'cent',
 422          'pound',
 423          'curren',
 424          'yen',
 425          'brvbar',
 426          'sect',
 427          'uml',
 428          'copy',
 429          'ordf',
 430          'laquo',
 431          'not',
 432          'shy',
 433          'reg',
 434          'macr',
 435          'deg',
 436          'plusmn',
 437          'acute',
 438          'micro',
 439          'para',
 440          'middot',
 441          'cedil',
 442          'ordm',
 443          'raquo',
 444          'iquest',
 445          'Agrave',
 446          'Aacute',
 447          'Acirc',
 448          'Atilde',
 449          'Auml',
 450          'Aring',
 451          'AElig',
 452          'Ccedil',
 453          'Egrave',
 454          'Eacute',
 455          'Ecirc',
 456          'Euml',
 457          'Igrave',
 458          'Iacute',
 459          'Icirc',
 460          'Iuml',
 461          'ETH',
 462          'Ntilde',
 463          'Ograve',
 464          'Oacute',
 465          'Ocirc',
 466          'Otilde',
 467          'Ouml',
 468          'times',
 469          'Oslash',
 470          'Ugrave',
 471          'Uacute',
 472          'Ucirc',
 473          'Uuml',
 474          'Yacute',
 475          'THORN',
 476          'szlig',
 477          'agrave',
 478          'aacute',
 479          'acirc',
 480          'atilde',
 481          'auml',
 482          'aring',
 483          'aelig',
 484          'ccedil',
 485          'egrave',
 486          'eacute',
 487          'ecirc',
 488          'euml',
 489          'igrave',
 490          'iacute',
 491          'icirc',
 492          'iuml',
 493          'eth',
 494          'ntilde',
 495          'ograve',
 496          'oacute',
 497          'ocirc',
 498          'otilde',
 499          'ouml',
 500          'divide',
 501          'oslash',
 502          'ugrave',
 503          'uacute',
 504          'ucirc',
 505          'uuml',
 506          'yacute',
 507          'thorn',
 508          'yuml',
 509          'quot',
 510          'amp',
 511          'lt',
 512          'gt',
 513          'apos',
 514          'OElig',
 515          'oelig',
 516          'Scaron',
 517          'scaron',
 518          'Yuml',
 519          'circ',
 520          'tilde',
 521          'ensp',
 522          'emsp',
 523          'thinsp',
 524          'zwnj',
 525          'zwj',
 526          'lrm',
 527          'rlm',
 528          'ndash',
 529          'mdash',
 530          'lsquo',
 531          'rsquo',
 532          'sbquo',
 533          'ldquo',
 534          'rdquo',
 535          'bdquo',
 536          'dagger',
 537          'Dagger',
 538          'permil',
 539          'lsaquo',
 540          'rsaquo',
 541          'euro',
 542          'fnof',
 543          'Alpha',
 544          'Beta',
 545          'Gamma',
 546          'Delta',
 547          'Epsilon',
 548          'Zeta',
 549          'Eta',
 550          'Theta',
 551          'Iota',
 552          'Kappa',
 553          'Lambda',
 554          'Mu',
 555          'Nu',
 556          'Xi',
 557          'Omicron',
 558          'Pi',
 559          'Rho',
 560          'Sigma',
 561          'Tau',
 562          'Upsilon',
 563          'Phi',
 564          'Chi',
 565          'Psi',
 566          'Omega',
 567          'alpha',
 568          'beta',
 569          'gamma',
 570          'delta',
 571          'epsilon',
 572          'zeta',
 573          'eta',
 574          'theta',
 575          'iota',
 576          'kappa',
 577          'lambda',
 578          'mu',
 579          'nu',
 580          'xi',
 581          'omicron',
 582          'pi',
 583          'rho',
 584          'sigmaf',
 585          'sigma',
 586          'tau',
 587          'upsilon',
 588          'phi',
 589          'chi',
 590          'psi',
 591          'omega',
 592          'thetasym',
 593          'upsih',
 594          'piv',
 595          'bull',
 596          'hellip',
 597          'prime',
 598          'Prime',
 599          'oline',
 600          'frasl',
 601          'weierp',
 602          'image',
 603          'real',
 604          'trade',
 605          'alefsym',
 606          'larr',
 607          'uarr',
 608          'rarr',
 609          'darr',
 610          'harr',
 611          'crarr',
 612          'lArr',
 613          'uArr',
 614          'rArr',
 615          'dArr',
 616          'hArr',
 617          'forall',
 618          'part',
 619          'exist',
 620          'empty',
 621          'nabla',
 622          'isin',
 623          'notin',
 624          'ni',
 625          'prod',
 626          'sum',
 627          'minus',
 628          'lowast',
 629          'radic',
 630          'prop',
 631          'infin',
 632          'ang',
 633          'and',
 634          'or',
 635          'cap',
 636          'cup',
 637          'int',
 638          'sim',
 639          'cong',
 640          'asymp',
 641          'ne',
 642          'equiv',
 643          'le',
 644          'ge',
 645          'sub',
 646          'sup',
 647          'nsub',
 648          'sube',
 649          'supe',
 650          'oplus',
 651          'otimes',
 652          'perp',
 653          'sdot',
 654          'lceil',
 655          'rceil',
 656          'lfloor',
 657          'rfloor',
 658          'lang',
 659          'rang',
 660          'loz',
 661          'spades',
 662          'clubs',
 663          'hearts',
 664          'diams',
 665          'sup1',
 666          'sup2',
 667          'sup3',
 668          'frac14',
 669          'frac12',
 670          'frac34',
 671          'there4',
 672      );
 673  
 674      /**
 675       * @var string[] $allowedxmlentitynames Array of KSES allowed XML entity names.
 676       * @since 5.5.0
 677       */
 678      $allowedxmlentitynames = array(
 679          'amp',
 680          'lt',
 681          'gt',
 682          'apos',
 683          'quot',
 684      );
 685  
 686      $allowedposttags = array_map( '_wp_add_global_attributes', $allowedposttags );
 687  } else {
 688      $allowedtags     = wp_kses_array_lc( $allowedtags );
 689      $allowedposttags = wp_kses_array_lc( $allowedposttags );
 690  }
 691  
 692  /**
 693   * Filters text content and strips out disallowed HTML.
 694   *
 695   * This function makes sure that only the allowed HTML element names, attribute
 696   * names, attribute values, and HTML entities will occur in the given text string.
 697   *
 698   * This function expects unslashed data.
 699   *
 700   * @see wp_kses_post() for specifically filtering post content and fields.
 701   * @see wp_allowed_protocols() for the default allowed protocols in link URLs.
 702   *
 703   * @since 1.0.0
 704   *
 705   * @param string         $string            Text content to filter.
 706   * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
 707   *                                          or a context name such as 'post'. See wp_kses_allowed_html()
 708   *                                          for the list of accepted context names.
 709   * @param string[]       $allowed_protocols Array of allowed URL protocols.
 710   * @return string Filtered content containing only the allowed HTML.
 711   */
 712  function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
 713      if ( empty( $allowed_protocols ) ) {
 714          $allowed_protocols = wp_allowed_protocols();
 715      }
 716  
 717      $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
 718      $string = wp_kses_normalize_entities( $string );
 719      $string = wp_kses_hook( $string, $allowed_html, $allowed_protocols );
 720  
 721      return wp_kses_split( $string, $allowed_html, $allowed_protocols );
 722  }
 723  
 724  /**
 725   * Filters one HTML attribute and ensures its value is allowed.
 726   *
 727   * This function can escape data in some situations where `wp_kses()` must strip the whole attribute.
 728   *
 729   * @since 4.2.3
 730   *
 731   * @param string $string  The 'whole' attribute, including name and value.
 732   * @param string $element The HTML element name to which the attribute belongs.
 733   * @return string Filtered attribute.
 734   */
 735  function wp_kses_one_attr( $string, $element ) {
 736      $uris              = wp_kses_uri_attributes();
 737      $allowed_html      = wp_kses_allowed_html( 'post' );
 738      $allowed_protocols = wp_allowed_protocols();
 739      $string            = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
 740  
 741      // Preserve leading and trailing whitespace.
 742      $matches = array();
 743      preg_match( '/^\s*/', $string, $matches );
 744      $lead = $matches[0];
 745      preg_match( '/\s*$/', $string, $matches );
 746      $trail = $matches[0];
 747      if ( empty( $trail ) ) {
 748          $string = substr( $string, strlen( $lead ) );
 749      } else {
 750          $string = substr( $string, strlen( $lead ), -strlen( $trail ) );
 751      }
 752  
 753      // Parse attribute name and value from input.
 754      $split = preg_split( '/\s*=\s*/', $string, 2 );
 755      $name  = $split[0];
 756      if ( count( $split ) == 2 ) {
 757          $value = $split[1];
 758  
 759          // Remove quotes surrounding $value.
 760          // Also guarantee correct quoting in $string for this one attribute.
 761          if ( '' === $value ) {
 762              $quote = '';
 763          } else {
 764              $quote = $value[0];
 765          }
 766          if ( '"' === $quote || "'" === $quote ) {
 767              if ( substr( $value, -1 ) != $quote ) {
 768                  return '';
 769              }
 770              $value = substr( $value, 1, -1 );
 771          } else {
 772              $quote = '"';
 773          }
 774  
 775          // Sanitize quotes, angle braces, and entities.
 776          $value = esc_attr( $value );
 777  
 778          // Sanitize URI values.
 779          if ( in_array( strtolower( $name ), $uris, true ) ) {
 780              $value = wp_kses_bad_protocol( $value, $allowed_protocols );
 781          }
 782  
 783          $string = "$name=$quote$value$quote";
 784          $vless  = 'n';
 785      } else {
 786          $value = '';
 787          $vless = 'y';
 788      }
 789  
 790      // Sanitize attribute by name.
 791      wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
 792  
 793      // Restore whitespace.
 794      return $lead . $string . $trail;
 795  }
 796  
 797  /**
 798   * Returns an array of allowed HTML tags and attributes for a given context.
 799   *
 800   * @since 3.5.0
 801   * @since 5.0.1 `form` removed as allowable HTML tag.
 802   *
 803   * @global array $allowedposttags
 804   * @global array $allowedtags
 805   * @global array $allowedentitynames
 806   *
 807   * @param string|array $context The context for which to retrieve tags. Allowed values are 'post',
 808   *                              'strip', 'data', 'entities', or the name of a field filter such as
 809   *                              'pre_user_description', or an array of allowed HTML elements and attributes.
 810   * @return array Array of allowed HTML tags and their allowed attributes.
 811   */
 812  function wp_kses_allowed_html( $context = '' ) {
 813      global $allowedposttags, $allowedtags, $allowedentitynames;
 814  
 815      if ( is_array( $context ) ) {
 816          // When `$context` is an array it's actually an array of allowed HTML elements and attributes.
 817          $html    = $context;
 818          $context = 'explicit';
 819  
 820          /**
 821           * Filters the HTML tags that are allowed for a given context.
 822           *
 823           * HTML tags and attribute names are case-insensitive in HTML but must be
 824           * added to the KSES allow list in lowercase. An item added to the allow list
 825           * in upper or mixed case will not recognized as permitted by KSES.
 826           *
 827           * @since 3.5.0
 828           *
 829           * @param array[] $html    Allowed HTML tags.
 830           * @param string  $context Context name.
 831           */
 832          return apply_filters( 'wp_kses_allowed_html', $html, $context );
 833      }
 834  
 835      switch ( $context ) {
 836          case 'post':
 837              /** This filter is documented in wp-includes/kses.php */
 838              $tags = apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
 839  
 840              // 5.0.1 removed the `<form>` tag, allow it if a filter is allowing it's sub-elements `<input>` or `<select>`.
 841              if ( ! CUSTOM_TAGS && ! isset( $tags['form'] ) && ( isset( $tags['input'] ) || isset( $tags['select'] ) ) ) {
 842                  $tags = $allowedposttags;
 843  
 844                  $tags['form'] = array(
 845                      'action'         => true,
 846                      'accept'         => true,
 847                      'accept-charset' => true,
 848                      'enctype'        => true,
 849                      'method'         => true,
 850                      'name'           => true,
 851                      'target'         => true,
 852                  );
 853  
 854                  /** This filter is documented in wp-includes/kses.php */
 855                  $tags = apply_filters( 'wp_kses_allowed_html', $tags, $context );
 856              }
 857  
 858              return $tags;
 859  
 860          case 'user_description':
 861          case 'pre_user_description':
 862              $tags             = $allowedtags;
 863              $tags['a']['rel'] = true;
 864              /** This filter is documented in wp-includes/kses.php */
 865              return apply_filters( 'wp_kses_allowed_html', $tags, $context );
 866  
 867          case 'strip':
 868              /** This filter is documented in wp-includes/kses.php */
 869              return apply_filters( 'wp_kses_allowed_html', array(), $context );
 870  
 871          case 'entities':
 872              /** This filter is documented in wp-includes/kses.php */
 873              return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context );
 874  
 875          case 'data':
 876          default:
 877              /** This filter is documented in wp-includes/kses.php */
 878              return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
 879      }
 880  }
 881  
 882  /**
 883   * You add any KSES hooks here.
 884   *
 885   * There is currently only one KSES WordPress hook, {@see 'pre_kses'}, and it is called here.
 886   * All parameters are passed to the hooks and expected to receive a string.
 887   *
 888   * @since 1.0.0
 889   *
 890   * @param string         $string            Content to filter through KSES.
 891   * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
 892   *                                          or a context name such as 'post'. See wp_kses_allowed_html()
 893   *                                          for the list of accepted context names.
 894   * @param string[]       $allowed_protocols Array of allowed URL protocols.
 895   * @return string Filtered content through {@see 'pre_kses'} hook.
 896   */
 897  function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) {
 898      /**
 899       * Filters content to be run through KSES.
 900       *
 901       * @since 2.3.0
 902       *
 903       * @param string         $string            Content to filter through KSES.
 904       * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
 905       *                                          or a context name such as 'post'. See wp_kses_allowed_html()
 906       *                                          for the list of accepted context names.
 907       * @param string[]       $allowed_protocols Array of allowed URL protocols.
 908       */
 909      return apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols );
 910  }
 911  
 912  /**
 913   * Returns the version number of KSES.
 914   *
 915   * @since 1.0.0
 916   *
 917   * @return string KSES version number.
 918   */
 919  function wp_kses_version() {
 920      return '0.2.2';
 921  }
 922  
 923  /**
 924   * Searches for HTML tags, no matter how malformed.
 925   *
 926   * It also matches stray `>` characters.
 927   *
 928   * @since 1.0.0
 929   *
 930   * @global array[]|string $pass_allowed_html      An array of allowed HTML elements and attributes,
 931   *                                                or a context name such as 'post'.
 932   * @global string[]       $pass_allowed_protocols Array of allowed URL protocols.
 933   *
 934   * @param string         $string            Content to filter.
 935   * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
 936   *                                          or a context name such as 'post'. See wp_kses_allowed_html()
 937   *                                          for the list of accepted context names.
 938   * @param string[]       $allowed_protocols Array of allowed URL protocols.
 939   * @return string Content with fixed HTML tags
 940   */
 941  function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
 942      global $pass_allowed_html, $pass_allowed_protocols;
 943  
 944      $pass_allowed_html      = $allowed_html;
 945      $pass_allowed_protocols = $allowed_protocols;
 946  
 947      return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
 948  }
 949  
 950  /**
 951   * Returns an array of HTML attribute names whose value contains a URL.
 952   *
 953   * This function returns a list of all HTML attributes that must contain
 954   * a URL according to the HTML specification.
 955   *
 956   * This list includes URI attributes both allowed and disallowed by KSES.
 957   *
 958   * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
 959   *
 960   * @since 5.0.1
 961   *
 962   * @return string[] HTML attribute names whose value contains a URL.
 963   */
 964  function wp_kses_uri_attributes() {
 965      $uri_attributes = array(
 966          'action',
 967          'archive',
 968          'background',
 969          'cite',
 970          'classid',
 971          'codebase',
 972          'data',
 973          'formaction',
 974          'href',
 975          'icon',
 976          'longdesc',
 977          'manifest',
 978          'poster',
 979          'profile',
 980          'src',
 981          'usemap',
 982          'xmlns',
 983      );
 984  
 985      /**
 986       * Filters the list of attributes that are required to contain a URL.
 987       *
 988       * Use this filter to add any `data-` attributes that are required to be
 989       * validated as a URL.
 990       *
 991       * @since 5.0.1
 992       *
 993       * @param string[] $uri_attributes HTML attribute names whose value contains a URL.
 994       */
 995      $uri_attributes = apply_filters( 'wp_kses_uri_attributes', $uri_attributes );
 996  
 997      return $uri_attributes;
 998  }
 999  
1000  /**
1001   * Callback for `wp_kses_split()`.
1002   *
1003   * @since 3.1.0
1004   * @access private
1005   * @ignore
1006   *
1007   * @global array[]|string $pass_allowed_html      An array of allowed HTML elements and attributes,
1008   *                                                or a context name such as 'post'.
1009   * @global string[]       $pass_allowed_protocols Array of allowed URL protocols.
1010   *
1011   * @param array $match preg_replace regexp matches
1012   * @return string
1013   */
1014  function _wp_kses_split_callback( $match ) {
1015      global $pass_allowed_html, $pass_allowed_protocols;
1016  
1017      return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
1018  }
1019  
1020  /**
1021   * Callback for `wp_kses_split()` for fixing malformed HTML tags.
1022   *
1023   * This function does a lot of work. It rejects some very malformed things like
1024   * `<:::>`. It returns an empty string, if the element isn't allowed (look ma, no
1025   * `strip_tags()`!). Otherwise it splits the tag into an element and an attribute
1026   * list.
1027   *
1028   * After the tag is split into an element and an attribute list, it is run
1029   * through another filter which will remove illegal attributes and once that is
1030   * completed, will be returned.
1031   *
1032   * @access private
1033   * @ignore
1034   * @since 1.0.0
1035   *
1036   * @param string         $string            Content to filter.
1037   * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
1038   *                                          or a context name such as 'post'. See wp_kses_allowed_html()
1039   *                                          for the list of accepted context names.
1040   * @param string[]       $allowed_protocols Array of allowed URL protocols.
1041   * @return string Fixed HTML element
1042   */
1043  function wp_kses_split2( $string, $allowed_html, $allowed_protocols ) {
1044      $string = wp_kses_stripslashes( $string );
1045  
1046      // It matched a ">" character.
1047      if ( '<' !== substr( $string, 0, 1 ) ) {
1048          return '&gt;';
1049      }
1050  
1051      // Allow HTML comments.
1052      if ( '<!--' === substr( $string, 0, 4 ) ) {
1053          $string = str_replace( array( '<!--', '-->' ), '', $string );
1054          while ( ( $newstring = wp_kses( $string, $allowed_html, $allowed_protocols ) ) != $string ) {
1055              $string = $newstring;
1056          }
1057          if ( '' === $string ) {
1058              return '';
1059          }
1060          // Prevent multiple dashes in comments.
1061          $string = preg_replace( '/--+/', '-', $string );
1062          // Prevent three dashes closing a comment.
1063          $string = preg_replace( '/-$/', '', $string );
1064          return "<!--{$string}-->";
1065      }
1066  
1067      // It's seriously malformed.
1068      if ( ! preg_match( '%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $string, $matches ) ) {
1069          return '';
1070      }
1071  
1072      $slash    = trim( $matches[1] );
1073      $elem     = $matches[2];
1074      $attrlist = $matches[3];
1075  
1076      if ( ! is_array( $allowed_html ) ) {
1077          $allowed_html = wp_kses_allowed_html( $allowed_html );
1078      }
1079  
1080      // They are using a not allowed HTML element.
1081      if ( ! isset( $allowed_html[ strtolower( $elem ) ] ) ) {
1082          return '';
1083      }
1084  
1085      // No attributes are allowed for closing elements.
1086      if ( '' !== $slash ) {
1087          return "</$elem>";
1088      }
1089  
1090      return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
1091  }
1092  
1093  /**
1094   * Removes all attributes, if none are allowed for this element.
1095   *
1096   * If some are allowed it calls `wp_kses_hair()` to split them further, and then
1097   * it builds up new HTML code from the data that `wp_kses_hair()` returns. It also
1098   * removes `<` and `>` characters, if there are any left. One more thing it does
1099   * is to check if the tag has a closing XHTML slash, and if it does, it puts one
1100   * in the returned code as well.
1101   *
1102   * An array of allowed values can be defined for attributes. If the attribute value
1103   * doesn't fall into the list, the attribute will be removed from the tag.
1104   *
1105   * Attributes can be marked as required. If a required attribute is not present,
1106   * KSES will remove all attributes from the tag. As KSES doesn't match opening and
1107   * closing tags, it's not possible to safely remove the tag itself, the safest
1108   * fallback is to strip all attributes from the tag, instead.
1109   *
1110   * @since 1.0.0
1111   * @since 5.9.0 Added support for an array of allowed values for attributes.
1112   *              Added support for required attributes.
1113   *
1114   * @param string         $element           HTML element/tag.
1115   * @param string         $attr              HTML attributes from HTML element to closing HTML element tag.
1116   * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
1117   *                                          or a context name such as 'post'. See wp_kses_allowed_html()
1118   *                                          for the list of accepted context names.
1119   * @param string[]       $allowed_protocols Array of allowed URL protocols.
1120   * @return string Sanitized HTML element.
1121   */
1122  function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) {
1123      if ( ! is_array( $allowed_html ) ) {
1124          $allowed_html = wp_kses_allowed_html( $allowed_html );
1125      }
1126  
1127      // Is there a closing XHTML slash at the end of the attributes?
1128      $xhtml_slash = '';
1129      if ( preg_match( '%\s*/\s*$%', $attr ) ) {
1130          $xhtml_slash = ' /';
1131      }
1132  
1133      // Are any attributes allowed at all for this element?
1134      $element_low = strtolower( $element );
1135      if ( empty( $allowed_html[ $element_low ] ) || true === $allowed_html[ $element_low ] ) {
1136          return "<$element$xhtml_slash>";
1137      }
1138  
1139      // Split it.
1140      $attrarr = wp_kses_hair( $attr, $allowed_protocols );
1141  
1142      // Check if there are attributes that are required.
1143      $required_attrs = array_filter(
1144          $allowed_html[ $element_low ],
1145          function( $required_attr_limits ) {
1146              return isset( $required_attr_limits['required'] ) && true === $required_attr_limits['required'];
1147          }
1148      );
1149  
1150      /*
1151       * If a required attribute check fails, we can return nothing for a self-closing tag,
1152       * but for a non-self-closing tag the best option is to return the element with attributes,
1153       * as KSES doesn't handle matching the relevant closing tag.
1154       */
1155      $stripped_tag = '';
1156      if ( empty( $xhtml_slash ) ) {
1157          $stripped_tag = "<$element>";
1158      }
1159  
1160      // Go through $attrarr, and save the allowed attributes for this element in $attr2.
1161      $attr2 = '';
1162      foreach ( $attrarr as $arreach ) {
1163          // Check if this attribute is required.
1164          $required = isset( $required_attrs[ strtolower( $arreach['name'] ) ] );
1165  
1166          if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
1167              $attr2 .= ' ' . $arreach['whole'];
1168  
1169              // If this was a required attribute, we can mark it as found.
1170              if ( $required ) {
1171                  unset( $required_attrs[ strtolower( $arreach['name'] ) ] );
1172              }
1173          } elseif ( $required ) {
1174              // This attribute was required, but didn't pass the check. The entire tag is not allowed.
1175              return $stripped_tag;
1176          }
1177      }
1178  
1179      // If some required attributes weren't set, the entire tag is not allowed.
1180      if ( ! empty( $required_attrs ) ) {
1181          return $stripped_tag;
1182      }
1183  
1184      // Remove any "<" or ">" characters.
1185      $attr2 = preg_replace( '/[<>]/', '', $attr2 );
1186  
1187      return "<$element$attr2$xhtml_slash>";
1188  }
1189  
1190  /**
1191   * Determines whether an attribute is allowed.
1192   *
1193   * @since 4.2.3
1194   * @since 5.0.0 Added support for `data-*` wildcard attributes.
1195   *
1196   * @param string $name         The attribute name. Passed by reference. Returns empty string when not allowed.
1197   * @param string $value        The attribute value. Passed by reference. Returns a filtered value.
1198   * @param string $whole        The `name=value` input. Passed by reference. Returns filtered input.
1199   * @param string $vless        Whether the attribute is valueless. Use 'y' or 'n'.
1200   * @param string $element      The name of the element to which this attribute belongs.
1201   * @param array  $allowed_html The full list of allowed elements and attributes.
1202   * @return bool Whether or not the attribute is allowed.
1203   */
1204  function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
1205      $name_low    = strtolower( $name );
1206      $element_low = strtolower( $element );
1207  
1208      if ( ! isset( $allowed_html[ $element_low ] ) ) {
1209          $name  = '';
1210          $value = '';
1211          $whole = '';
1212          return false;
1213      }
1214  
1215      $allowed_attr = $allowed_html[ $element_low ];
1216  
1217      if ( ! isset( $allowed_attr[ $name_low ] ) || '' === $allowed_attr[ $name_low ] ) {
1218          /*
1219           * Allow `data-*` attributes.
1220           *
1221           * When specifying `$allowed_html`, the attribute name should be set as
1222           * `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see
1223           * https://www.w3.org/TR/html40/struct/objects.html#adef-data).
1224           *
1225           * Note: the attribute name should only contain `A-Za-z0-9_-` chars,
1226           * double hyphens `--` are not accepted by WordPress.
1227           */
1228          if ( strpos( $name_low, 'data-' ) === 0 && ! empty( $allowed_attr['data-*'] )
1229              && preg_match( '/^data(?:-[a-z0-9_]+)+$/', $name_low, $match )
1230          ) {
1231              /*
1232               * Add the whole attribute name to the allowed attributes and set any restrictions
1233               * for the `data-*` attribute values for the current element.
1234               */
1235              $allowed_attr[ $match[0] ] = $allowed_attr['data-*'];
1236          } else {
1237              $name  = '';
1238              $value = '';
1239              $whole = '';
1240              return false;
1241          }
1242      }
1243  
1244      if ( 'style' === $name_low ) {
1245          $new_value = safecss_filter_attr( $value );
1246  
1247          if ( empty( $new_value ) ) {
1248              $name  = '';
1249              $value = '';
1250              $whole = '';
1251              return false;
1252          }
1253  
1254          $whole = str_replace( $value, $new_value, $whole );
1255          $value = $new_value;
1256      }
1257  
1258      if ( is_array( $allowed_attr[ $name_low ] ) ) {
1259          // There are some checks.
1260          foreach ( $allowed_attr[ $name_low ] as $currkey => $currval ) {
1261              if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
1262                  $name  = '';
1263                  $value = '';
1264                  $whole = '';
1265                  return false;
1266              }
1267          }
1268      }
1269  
1270      return true;
1271  }
1272  
1273  /**
1274   * Builds an attribute list from string containing attributes.
1275   *
1276   * This function does a lot of work. It parses an attribute list into an array
1277   * with attribute data, and tries to do the right thing even if it gets weird
1278   * input. It will add quotes around attribute values that don't have any quotes
1279   * or apostrophes around them, to make it easier to produce HTML code that will
1280   * conform to W3C's HTML specification. It will also remove bad URL protocols
1281   * from attribute values. It also reduces duplicate attributes by using the
1282   * attribute defined first (`foo='bar' foo='baz'` will result in `foo='bar'`).
1283   *
1284   * @since 1.0.0
1285   *
1286   * @param string   $attr              Attribute list from HTML element to closing HTML element tag.
1287   * @param string[] $allowed_protocols Array of allowed URL protocols.
1288   * @return array[] Array of attribute information after parsing.
1289   */
1290  function wp_kses_hair( $attr, $allowed_protocols ) {
1291      $attrarr  = array();
1292      $mode     = 0;
1293      $attrname = '';
1294      $uris     = wp_kses_uri_attributes();
1295  
1296      // Loop through the whole attribute list.
1297  
1298      while ( strlen( $attr ) != 0 ) {
1299          $working = 0; // Was the last operation successful?
1300  
1301          switch ( $mode ) {
1302              case 0:
1303                  if ( preg_match( '/^([_a-zA-Z][-_a-zA-Z0-9:.]*)/', $attr, $match ) ) {
1304                      $attrname = $match[1];
1305                      $working  = 1;
1306                      $mode     = 1;
1307                      $attr     = preg_replace( '/^[_a-zA-Z][-_a-zA-Z0-9:.]*/', '', $attr );
1308                  }
1309  
1310                  break;
1311  
1312              case 1:
1313                  if ( preg_match( '/^\s*=\s*/', $attr ) ) { // Equals sign.
1314                      $working = 1;
1315                      $mode    = 2;
1316                      $attr    = preg_replace( '/^\s*=\s*/', '', $attr );
1317                      break;
1318                  }
1319  
1320                  if ( preg_match( '/^\s+/', $attr ) ) { // Valueless.
1321                      $working = 1;
1322                      $mode    = 0;
1323                      if ( false === array_key_exists( $attrname, $attrarr ) ) {
1324                          $attrarr[ $attrname ] = array(
1325                              'name'  => $attrname,
1326                              'value' => '',
1327                              'whole' => $attrname,
1328                              'vless' => 'y',
1329                          );
1330                      }
1331                      $attr = preg_replace( '/^\s+/', '', $attr );
1332                  }
1333  
1334                  break;
1335  
1336              case 2:
1337                  if ( preg_match( '%^"([^"]*)"(\s+|/?$)%', $attr, $match ) ) {
1338                      // "value"
1339                      $thisval = $match[1];
1340                      if ( in_array( strtolower( $attrname ), $uris, true ) ) {
1341                          $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
1342                      }
1343  
1344                      if ( false === array_key_exists( $attrname, $attrarr ) ) {
1345                          $attrarr[ $attrname ] = array(
1346                              'name'  => $attrname,
1347                              'value' => $thisval,
1348                              'whole' => "$attrname=\"$thisval\"",
1349                              'vless' => 'n',
1350                          );
1351                      }
1352                      $working = 1;
1353                      $mode    = 0;
1354                      $attr    = preg_replace( '/^"[^"]*"(\s+|$)/', '', $attr );
1355                      break;
1356                  }
1357  
1358                  if ( preg_match( "%^'([^']*)'(\s+|/?$)%", $attr, $match ) ) {
1359                      // 'value'
1360                      $thisval = $match[1];
1361                      if ( in_array( strtolower( $attrname ), $uris, true ) ) {
1362                          $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
1363                      }
1364  
1365                      if ( false === array_key_exists( $attrname, $attrarr ) ) {
1366                          $attrarr[ $attrname ] = array(
1367                              'name'  => $attrname,
1368                              'value' => $thisval,
1369                              'whole' => "$attrname='$thisval'",
1370                              'vless' => 'n',
1371                          );
1372                      }
1373                      $working = 1;
1374                      $mode    = 0;
1375                      $attr    = preg_replace( "/^'[^']*'(\s+|$)/", '', $attr );
1376                      break;
1377                  }
1378  
1379                  if ( preg_match( "%^([^\s\"']+)(\s+|/?$)%", $attr, $match ) ) {
1380                      // value
1381                      $thisval = $match[1];
1382                      if ( in_array( strtolower( $attrname ), $uris, true ) ) {
1383                          $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
1384                      }
1385  
1386                      if ( false === array_key_exists( $attrname, $attrarr ) ) {
1387                          $attrarr[ $attrname ] = array(
1388                              'name'  => $attrname,
1389                              'value' => $thisval,
1390                              'whole' => "$attrname=\"$thisval\"",
1391                              'vless' => 'n',
1392                          );
1393                      }
1394                      // We add quotes to conform to W3C's HTML spec.
1395                      $working = 1;
1396                      $mode    = 0;
1397                      $attr    = preg_replace( "%^[^\s\"']+(\s+|$)%", '', $attr );
1398                  }
1399  
1400                  break;
1401          } // End switch.
1402  
1403          if ( 0 == $working ) { // Not well-formed, remove and try again.
1404              $attr = wp_kses_html_error( $attr );
1405              $mode = 0;
1406          }
1407      } // End while.
1408  
1409      if ( 1 == $mode && false === array_key_exists( $attrname, $attrarr ) ) {
1410          // Special case, for when the attribute list ends with a valueless
1411          // attribute like "selected".
1412          $attrarr[ $attrname ] = array(
1413              'name'  => $attrname,
1414              'value' => '',
1415              'whole' => $attrname,
1416              'vless' => 'y',
1417          );
1418      }
1419  
1420      return $attrarr;
1421  }
1422  
1423  /**
1424   * Finds all attributes of an HTML element.
1425   *
1426   * Does not modify input.  May return "evil" output.
1427   *
1428   * Based on `wp_kses_split2()` and `wp_kses_attr()`.
1429   *
1430   * @since 4.2.3
1431   *
1432   * @param string $element HTML element.
1433   * @return array|false List of attributes found in the element. Returns false on failure.
1434   */
1435  function wp_kses_attr_parse( $element ) {
1436      $valid = preg_match( '%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches );
1437      if ( 1 !== $valid ) {
1438          return false;
1439      }
1440  
1441      $begin  = $matches[1];
1442      $slash  = $matches[2];
1443      $elname = $matches[3];
1444      $attr   = $matches[4];
1445      $end    = $matches[5];
1446  
1447      if ( '' !== $slash ) {
1448          // Closing elements do not get parsed.
1449          return false;
1450      }
1451  
1452      // Is there a closing XHTML slash at the end of the attributes?
1453      if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
1454          $xhtml_slash = $matches[0];
1455          $attr        = substr( $attr, 0, -strlen( $xhtml_slash ) );
1456      } else {
1457          $xhtml_slash = '';
1458      }
1459  
1460      // Split it.
1461      $attrarr = wp_kses_hair_parse( $attr );
1462      if ( false === $attrarr ) {
1463          return false;
1464      }
1465  
1466      // Make sure all input is returned by adding front and back matter.
1467      array_unshift( $attrarr, $begin . $slash . $elname );
1468      array_push( $attrarr, $xhtml_slash . $end );
1469  
1470      return $attrarr;
1471  }
1472  
1473  /**
1474   * Builds an attribute list from string containing attributes.
1475   *
1476   * Does not modify input.  May return "evil" output.
1477   * In case of unexpected input, returns false instead of stripping things.
1478   *
1479   * Based on `wp_kses_hair()` but does not return a multi-dimensional array.
1480   *
1481   * @since 4.2.3
1482   *
1483   * @param string $attr Attribute list from HTML element to closing HTML element tag.
1484   * @return array|false List of attributes found in $attr. Returns false on failure.
1485   */
1486  function wp_kses_hair_parse( $attr ) {
1487      if ( '' === $attr ) {
1488          return array();
1489      }
1490  
1491      // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
1492      $regex =
1493          '(?:'
1494          .     '[_a-zA-Z][-_a-zA-Z0-9:.]*' // Attribute name.
1495          . '|'
1496          .     '\[\[?[^\[\]]+\]\]?'        // Shortcode in the name position implies unfiltered_html.
1497          . ')'
1498          . '(?:'               // Attribute value.
1499          .     '\s*=\s*'       // All values begin with '='.
1500          .     '(?:'
1501          .         '"[^"]*"'   // Double-quoted.
1502          .     '|'
1503          .         "'[^']*'"   // Single-quoted.
1504          .     '|'
1505          .         '[^\s"\']+' // Non-quoted.
1506          .         '(?:\s|$)'  // Must have a space.
1507          .     ')'
1508          . '|'
1509          .     '(?:\s|$)'      // If attribute has no value, space is required.
1510          . ')'
1511          . '\s*';              // Trailing space is optional except as mentioned above.
1512      // phpcs:enable
1513  
1514      // Although it is possible to reduce this procedure to a single regexp,
1515      // we must run that regexp twice to get exactly the expected result.
1516  
1517      $validation = "%^($regex)+$%";
1518      $extraction = "%$regex%";
1519  
1520      if ( 1 === preg_match( $validation, $attr ) ) {
1521          preg_match_all( $extraction, $attr, $attrarr );
1522          return $attrarr[0];
1523      } else {
1524          return false;
1525      }
1526  }
1527  
1528  /**
1529   * Performs different checks for attribute values.
1530   *
1531   * The currently implemented checks are "maxlen", "minlen", "maxval", "minval",
1532   * and "valueless".
1533   *
1534   * @since 1.0.0
1535   *
1536   * @param string $value      Attribute value.
1537   * @param string $vless      Whether the attribute is valueless. Use 'y' or 'n'.
1538   * @param string $checkname  What $checkvalue is checking for.
1539   * @param mixed  $checkvalue What constraint the value should pass.
1540   * @return bool Whether check passes.
1541   */
1542  function wp_kses_check_attr_val( $value, $vless, $checkname, $checkvalue ) {
1543      $ok = true;
1544  
1545      switch ( strtolower( $checkname ) ) {
1546          case 'maxlen':
1547              /*
1548               * The maxlen check makes sure that the attribute value has a length not
1549               * greater than the given value. This can be used to avoid Buffer Overflows
1550               * in WWW clients and various Internet servers.
1551               */
1552  
1553              if ( strlen( $value ) > $checkvalue ) {
1554                  $ok = false;
1555              }
1556              break;
1557  
1558          case 'minlen':
1559              /*
1560               * The minlen check makes sure that the attribute value has a length not
1561               * smaller than the given value.
1562               */
1563  
1564              if ( strlen( $value ) < $checkvalue ) {
1565                  $ok = false;
1566              }
1567              break;
1568  
1569          case 'maxval':
1570              /*
1571               * The maxval check does two things: it checks that the attribute value is
1572               * an integer from 0 and up, without an excessive amount of zeroes or
1573               * whitespace (to avoid Buffer Overflows). It also checks that the attribute
1574               * value is not greater than the given value.
1575               * This check can be used to avoid Denial of Service attacks.
1576               */
1577  
1578              if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) {
1579                  $ok = false;
1580              }
1581              if ( $value > $checkvalue ) {
1582                  $ok = false;
1583              }
1584              break;
1585  
1586          case 'minval':
1587              /*
1588               * The minval check makes sure that the attribute value is a positive integer,
1589               * and that it is not smaller than the given value.
1590               */
1591  
1592              if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) {
1593                  $ok = false;
1594              }
1595              if ( $value < $checkvalue ) {
1596                  $ok = false;
1597              }
1598              break;
1599  
1600          case 'valueless':
1601              /*
1602               * The valueless check makes sure if the attribute has a value
1603               * (like `<a href="blah">`) or not (`<option selected>`). If the given value
1604               * is a "y" or a "Y", the attribute must not have a value.
1605               * If the given value is an "n" or an "N", the attribute must have a value.
1606               */
1607  
1608              if ( strtolower( $checkvalue ) != $vless ) {
1609                  $ok = false;
1610              }
1611              break;
1612  
1613          case 'values':
1614              /*
1615               * The values check is used when you want to make sure that the attribute
1616               * has one of the given values.
1617               */
1618  
1619              if ( false === array_search( strtolower( $value ), $checkvalue, true ) ) {
1620                  $ok = false;
1621              }
1622              break;
1623  
1624          case 'value_callback':
1625              /*
1626               * The value_callback check is used when you want to make sure that the attribute
1627               * value is accepted by the callback function.
1628               */
1629  
1630              if ( ! call_user_func( $checkvalue, $value ) ) {
1631                  $ok = false;
1632              }
1633              break;
1634      } // End switch.
1635  
1636      return $ok;
1637  }
1638  
1639  /**
1640   * Sanitizes a string and removed disallowed URL protocols.
1641   *
1642   * This function removes all non-allowed protocols from the beginning of the
1643   * string. It ignores whitespace and the case of the letters, and it does
1644   * understand HTML entities. It does its work recursively, so it won't be
1645   * fooled by a string like `javascript:javascript:alert(57)`.
1646   *
1647   * @since 1.0.0
1648   *
1649   * @param string   $string            Content to filter bad protocols from.
1650   * @param string[] $allowed_protocols Array of allowed URL protocols.
1651   * @return string Filtered content.
1652   */
1653  function wp_kses_bad_protocol( $string, $allowed_protocols ) {
1654      $string     = wp_kses_no_null( $string );
1655      $iterations = 0;
1656  
1657      do {
1658          $original_string = $string;
1659          $string          = wp_kses_bad_protocol_once( $string, $allowed_protocols );
1660      } while ( $original_string != $string && ++$iterations < 6 );
1661  
1662      if ( $original_string != $string ) {
1663          return '';
1664      }
1665  
1666      return $string;
1667  }
1668  
1669  /**
1670   * Removes any invalid control characters in a text string.
1671   *
1672   * Also removes any instance of the `\0` string.
1673   *
1674   * @since 1.0.0
1675   *
1676   * @param string $string  Content to filter null characters from.
1677   * @param array  $options Set 'slash_zero' => 'keep' when '\0' is allowed. Default is 'remove'.
1678   * @return string Filtered content.
1679   */
1680  function wp_kses_no_null( $string, $options = null ) {
1681      if ( ! isset( $options['slash_zero'] ) ) {
1682          $options = array( 'slash_zero' => 'remove' );
1683      }
1684  
1685      $string = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string );
1686      if ( 'remove' === $options['slash_zero'] ) {
1687          $string = preg_replace( '/\\\\+0+/', '', $string );
1688      }
1689  
1690      return $string;
1691  }
1692  
1693  /**
1694   * Strips slashes from in front of quotes.
1695   *
1696   * This function changes the character sequence `\"` to just `"`. It leaves all other
1697   * slashes alone. The quoting from `preg_replace(//e)` requires this.
1698   *
1699   * @since 1.0.0
1700   *
1701   * @param string $string String to strip slashes from.
1702   * @return string Fixed string with quoted slashes.
1703   */
1704  function wp_kses_stripslashes( $string ) {
1705      return preg_replace( '%\\\\"%', '"', $string );
1706  }
1707  
1708  /**
1709   * Converts the keys of an array to lowercase.
1710   *
1711   * @since 1.0.0
1712   *
1713   * @param array $inarray Unfiltered array.
1714   * @return array Fixed array with all lowercase keys.
1715   */
1716  function wp_kses_array_lc( $inarray ) {
1717      $outarray = array();
1718  
1719      foreach ( (array) $inarray as $inkey => $inval ) {
1720          $outkey              = strtolower( $inkey );
1721          $outarray[ $outkey ] = array();
1722  
1723          foreach ( (array) $inval as $inkey2 => $inval2 ) {
1724              $outkey2                         = strtolower( $inkey2 );
1725              $outarray[ $outkey ][ $outkey2 ] = $inval2;
1726          }
1727      }
1728  
1729      return $outarray;
1730  }
1731  
1732  /**
1733   * Handles parsing errors in `wp_kses_hair()`.
1734   *
1735   * The general plan is to remove everything to and including some whitespace,
1736   * but it deals with quotes and apostrophes as well.
1737   *
1738   * @since 1.0.0
1739   *
1740   * @param string $string
1741   * @return string
1742   */
1743  function wp_kses_html_error( $string ) {
1744      return preg_replace( '/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string );
1745  }
1746  
1747  /**
1748   * Sanitizes content from bad protocols and other characters.
1749   *
1750   * This function searches for URL protocols at the beginning of the string, while
1751   * handling whitespace and HTML entities.
1752   *
1753   * @since 1.0.0
1754   *
1755   * @param string   $string            Content to check for bad protocols.
1756   * @param string[] $allowed_protocols Array of allowed URL protocols.
1757   * @param int      $count             Depth of call recursion to this function.
1758   * @return string Sanitized content.
1759   */
1760  function wp_kses_bad_protocol_once( $string, $allowed_protocols, $count = 1 ) {
1761      $string  = preg_replace( '/(&#0*58(?![;0-9])|&#x0*3a(?![;a-f0-9]))/i', '$1;', $string );
1762      $string2 = preg_split( '/:|&#0*58;|&#x0*3a;|&colon;/i', $string, 2 );
1763      if ( isset( $string2[1] ) && ! preg_match( '%/\?%', $string2[0] ) ) {
1764          $string   = trim( $string2[1] );
1765          $protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
1766          if ( 'feed:' === $protocol ) {
1767              if ( $count > 2 ) {
1768                  return '';
1769              }
1770              $string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
1771              if ( empty( $string ) ) {
1772                  return $string;
1773              }
1774          }
1775          $string = $protocol . $string;
1776      }
1777  
1778      return $string;
1779  }
1780  
1781  /**
1782   * Callback for `wp_kses_bad_protocol_once()` regular expression.
1783   *
1784   * This function processes URL protocols, checks to see if they're in the
1785   * list of allowed protocols or not, and returns different data depending
1786   * on the answer.
1787   *
1788   * @access private
1789   * @ignore
1790   * @since 1.0.0
1791   *
1792   * @param string   $string            URI scheme to check against the list of allowed protocols.
1793   * @param string[] $allowed_protocols Array of allowed URL protocols.
1794   * @return string Sanitized content.
1795   */
1796  function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
1797      $string2 = wp_kses_decode_entities( $string );
1798      $string2 = preg_replace( '/\s/', '', $string2 );
1799      $string2 = wp_kses_no_null( $string2 );
1800      $string2 = strtolower( $string2 );
1801  
1802      $allowed = false;
1803      foreach ( (array) $allowed_protocols as $one_protocol ) {
1804          if ( strtolower( $one_protocol ) == $string2 ) {
1805              $allowed = true;
1806              break;
1807          }
1808      }
1809  
1810      if ( $allowed ) {
1811          return "$string2:";
1812      } else {
1813          return '';
1814      }
1815  }
1816  
1817  /**
1818   * Converts and fixes HTML entities.
1819   *
1820   * This function normalizes HTML entities. It will convert `AT&T` to the correct
1821   * `AT&amp;T`, `&#00058;` to `&#058;`, `&#XYZZY;` to `&amp;#XYZZY;` and so on.
1822   *
1823   * When `$context` is set to 'xml', HTML entities are converted to their code points.  For
1824   * example, `AT&T&hellip;&#XYZZY;` is converted to `AT&amp;T…&amp;#XYZZY;`.
1825   *
1826   * @since 1.0.0
1827   * @since 5.5.0 Added `$context` parameter.
1828   *
1829   * @param string $string  Content to normalize entities.
1830   * @param string $context Context for normalization. Can be either 'html' or 'xml'.
1831   *                        Default 'html'.
1832   * @return string Content with normalized entities.
1833   */
1834  function wp_kses_normalize_entities( $string, $context = 'html' ) {
1835      // Disarm all entities by converting & to &amp;
1836      $string = str_replace( '&', '&amp;', $string );
1837  
1838      // Change back the allowed entities in our list of allowed entities.
1839      if ( 'xml' === $context ) {
1840          $string = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_xml_named_entities', $string );
1841      } else {
1842          $string = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string );
1843      }
1844      $string = preg_replace_callback( '/&amp;#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string );
1845      $string = preg_replace_callback( '/&amp;#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string );
1846  
1847      return $string;
1848  }
1849  
1850  /**
1851   * Callback for `wp_kses_normalize_entities()` regular expression.
1852   *
1853   * This function only accepts valid named entity references, which are finite,
1854   * case-sensitive, and highly scrutinized by HTML and XML validators.
1855   *
1856   * @since 3.0.0
1857   *
1858   * @global array $allowedentitynames
1859   *
1860   * @param array $matches preg_replace_callback() matches array.
1861   * @return string Correctly encoded entity.
1862   */
1863  function wp_kses_named_entities( $matches ) {
1864      global $allowedentitynames;
1865  
1866      if ( empty( $matches[1] ) ) {
1867          return '';
1868      }
1869  
1870      $i = $matches[1];
1871      return ( ! in_array( $i, $allowedentitynames, true ) ) ? "&amp;$i;" : "&$i;";
1872  }
1873  
1874  /**
1875   * Callback for `wp_kses_normalize_entities()` regular expression.
1876   *
1877   * This function only accepts valid named entity references, which are finite,
1878   * case-sensitive, and highly scrutinized by XML validators.  HTML named entity
1879   * references are converted to their code points.
1880   *
1881   * @since 5.5.0
1882   *
1883   * @global array $allowedentitynames
1884   * @global array $allowedxmlentitynames
1885   *
1886   * @param array $matches preg_replace_callback() matches array.
1887   * @return string Correctly encoded entity.
1888   */
1889  function wp_kses_xml_named_entities( $matches ) {
1890      global $allowedentitynames, $allowedxmlentitynames;
1891  
1892      if ( empty( $matches[1] ) ) {
1893          return '';
1894      }
1895  
1896      $i = $matches[1];
1897  
1898      if ( in_array( $i, $allowedxmlentitynames, true ) ) {
1899          return "&$i;";
1900      } elseif ( in_array( $i, $allowedentitynames, true ) ) {
1901          return html_entity_decode( "&$i;", ENT_HTML5 );
1902      }
1903  
1904      return "&amp;$i;";
1905  }
1906  
1907  /**
1908   * Callback for `wp_kses_normalize_entities()` regular expression.
1909   *
1910   * This function helps `wp_kses_normalize_entities()` to only accept 16-bit
1911   * values and nothing more for `&#number;` entities.
1912   *
1913   * @access private
1914   * @ignore
1915   * @since 1.0.0
1916   *
1917   * @param array $matches `preg_replace_callback()` matches array.
1918   * @return string Correctly encoded entity.
1919   */
1920  function wp_kses_normalize_entities2( $matches ) {
1921      if ( empty( $matches[1] ) ) {
1922          return '';
1923      }
1924  
1925      $i = $matches[1];
1926      if ( valid_unicode( $i ) ) {
1927          $i = str_pad( ltrim( $i, '0' ), 3, '0', STR_PAD_LEFT );
1928          $i = "&#$i;";
1929      } else {
1930          $i = "&amp;#$i;";
1931      }
1932  
1933      return $i;
1934  }
1935  
1936  /**
1937   * Callback for `wp_kses_normalize_entities()` for regular expression.
1938   *
1939   * This function helps `wp_kses_normalize_entities()` to only accept valid Unicode
1940   * numeric entities in hex form.
1941   *
1942   * @since 2.7.0
1943   * @access private
1944   * @ignore
1945   *
1946   * @param array $matches `preg_replace_callback()` matches array.
1947   * @return string Correctly encoded entity.
1948   */
1949  function wp_kses_normalize_entities3( $matches ) {
1950      if ( empty( $matches[1] ) ) {
1951          return '';
1952      }
1953  
1954      $hexchars = $matches[1];
1955      return ( ! valid_unicode( hexdec( $hexchars ) ) ) ? "&amp;#x$hexchars;" : '&#x' . ltrim( $hexchars, '0' ) . ';';
1956  }
1957  
1958  /**
1959   * Determines if a Unicode codepoint is valid.
1960   *
1961   * @since 2.7.0
1962   *
1963   * @param int $i Unicode codepoint.
1964   * @return bool Whether or not the codepoint is a valid Unicode codepoint.
1965   */
1966  function valid_unicode( $i ) {
1967      return ( 0x9 == $i || 0xa == $i || 0xd == $i ||
1968              ( 0x20 <= $i && $i <= 0xd7ff ) ||
1969              ( 0xe000 <= $i && $i <= 0xfffd ) ||
1970              ( 0x10000 <= $i && $i <= 0x10ffff ) );
1971  }
1972  
1973  /**
1974   * Converts all numeric HTML entities to their named counterparts.
1975   *
1976   * This function decodes numeric HTML entities (`&#65;` and `&#x41;`).
1977   * It doesn't do anything with named entities like `&auml;`, but we don't
1978   * need them in the allowed URL protocols system anyway.
1979   *
1980   * @since 1.0.0
1981   *
1982   * @param string $string Content to change entities.
1983   * @return string Content after decoded entities.
1984   */
1985  function wp_kses_decode_entities( $string ) {
1986      $string = preg_replace_callback( '/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string );
1987      $string = preg_replace_callback( '/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string );
1988  
1989      return $string;
1990  }
1991  
1992  /**
1993   * Regex callback for `wp_kses_decode_entities()`.
1994   *
1995   * @since 2.9.0
1996   * @access private
1997   * @ignore
1998   *
1999   * @param array $match preg match
2000   * @return string
2001   */
2002  function _wp_kses_decode_entities_chr( $match ) {
2003      return chr( $match[1] );
2004  }
2005  
2006  /**
2007   * Regex callback for `wp_kses_decode_entities()`.
2008   *
2009   * @since 2.9.0
2010   * @access private
2011   * @ignore
2012   *
2013   * @param array $match preg match
2014   * @return string
2015   */
2016  function _wp_kses_decode_entities_chr_hexdec( $match ) {
2017      return chr( hexdec( $match[1] ) );
2018  }
2019  
2020  /**
2021   * Sanitize content with allowed HTML KSES rules.
2022   *
2023   * This function expects slashed data.
2024   *
2025   * @since 1.0.0
2026   *
2027   * @param string $data Content to filter, expected to be escaped with slashes.
2028   * @return string Filtered content.
2029   */
2030  function wp_filter_kses( $data ) {
2031      return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
2032  }
2033  
2034  /**
2035   * Sanitize content with allowed HTML KSES rules.
2036   *
2037   * This function expects unslashed data.
2038   *
2039   * @since 2.9.0
2040   *
2041   * @param string $data Content to filter, expected to not be escaped.
2042   * @return string Filtered content.
2043   */
2044  function wp_kses_data( $data ) {
2045      return wp_kses( $data, current_filter() );
2046  }
2047  
2048  /**
2049   * Sanitizes content for allowed HTML tags for post content.
2050   *
2051   * Post content refers to the page contents of the 'post' type and not `$_POST`
2052   * data from forms.
2053   *
2054   * This function expects slashed data.
2055   *
2056   * @since 2.0.0
2057   *
2058   * @param string $data Post content to filter, expected to be escaped with slashes.
2059   * @return string Filtered post content with allowed HTML tags and attributes intact.
2060   */
2061  function wp_filter_post_kses( $data ) {
2062      return addslashes( wp_kses( stripslashes( $data ), 'post' ) );
2063  }
2064  
2065  /**
2066   * Sanitizes global styles user content removing unsafe rules.
2067   *
2068   * @since 5.9.0
2069   *
2070   * @param string $data Post content to filter.
2071   * @return string Filtered post content with unsafe rules removed.
2072   */
2073  function wp_filter_global_styles_post( $data ) {
2074      $decoded_data        = json_decode( wp_unslash( $data ), true );
2075      $json_decoding_error = json_last_error();
2076      if (
2077          JSON_ERROR_NONE === $json_decoding_error &&
2078          is_array( $decoded_data ) &&
2079          isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
2080          $decoded_data['isGlobalStylesUserThemeJSON']
2081      ) {
2082          unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
2083  
2084          $data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data );
2085  
2086          $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
2087          return wp_slash( wp_json_encode( $data_to_encode ) );
2088      }
2089      return $data;
2090  }
2091  
2092  /**
2093   * Sanitizes content for allowed HTML tags for post content.
2094   *
2095   * Post content refers to the page contents of the 'post' type and not `$_POST`
2096   * data from forms.
2097   *
2098   * This function expects unslashed data.
2099   *
2100   * @since 2.9.0
2101   *
2102   * @param string $data Post content to filter.
2103   * @return string Filtered post content with allowed HTML tags and attributes intact.
2104   */
2105  function wp_kses_post( $data ) {
2106      return wp_kses( $data, 'post' );
2107  }
2108  
2109  /**
2110   * Navigates through an array, object, or scalar, and sanitizes content for
2111   * allowed HTML tags for post content.
2112   *
2113   * @since 4.4.2
2114   *
2115   * @see map_deep()
2116   *
2117   * @param mixed $data The array, object, or scalar value to inspect.
2118   * @return mixed The filtered content.
2119   */
2120  function wp_kses_post_deep( $data ) {
2121      return map_deep( $data, 'wp_kses_post' );
2122  }
2123  
2124  /**
2125   * Strips all HTML from a text string.
2126   *
2127   * This function expects slashed data.
2128   *
2129   * @since 2.1.0
2130   *
2131   * @param string $data Content to strip all HTML from.
2132   * @return string Filtered content without any HTML.
2133   */
2134  function wp_filter_nohtml_kses( $data ) {
2135      return addslashes( wp_kses( stripslashes( $data ), 'strip' ) );
2136  }
2137  
2138  /**
2139   * Adds all KSES input form content filters.
2140   *
2141   * All hooks have default priority. The `wp_filter_kses()` function is added to
2142   * the 'pre_comment_content' and 'title_save_pre' hooks.
2143   *
2144   * The `wp_filter_post_kses()` function is added to the 'content_save_pre',
2145   * 'excerpt_save_pre', and 'content_filtered_save_pre' hooks.
2146   *
2147   * @since 2.0.0
2148   */
2149  function kses_init_filters() {
2150      // Normal filtering.
2151      add_filter( 'title_save_pre', 'wp_filter_kses' );
2152  
2153      // Comment filtering.
2154      if ( current_user_can( 'unfiltered_html' ) ) {
2155          add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
2156      } else {
2157          add_filter( 'pre_comment_content', 'wp_filter_kses' );
2158      }
2159  
2160      // Global Styles filtering: Global Styles filters should be executed before normal post_kses HTML filters.
2161      add_filter( 'content_save_pre', 'wp_filter_global_styles_post', 9 );
2162      add_filter( 'content_filtered_save_pre', 'wp_filter_global_styles_post', 9 );
2163  
2164      // Post filtering.
2165      add_filter( 'content_save_pre', 'wp_filter_post_kses' );
2166      add_filter( 'excerpt_save_pre', 'wp_filter_post_kses' );
2167      add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
2168  }
2169  
2170  /**
2171   * Removes all KSES input form content filters.
2172   *
2173   * A quick procedural method to removing all of the filters that KSES uses for
2174   * content in WordPress Loop.
2175   *
2176   * Does not remove the `kses_init()` function from {@see 'init'} hook (priority is
2177   * default). Also does not remove `kses_init()` function from {@see 'set_current_user'}
2178   * hook (priority is also default).
2179   *
2180   * @since 2.0.6
2181   */
2182  function kses_remove_filters() {
2183      // Normal filtering.
2184      remove_filter( 'title_save_pre', 'wp_filter_kses' );
2185  
2186      // Comment filtering.
2187      remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
2188      remove_filter( 'pre_comment_content', 'wp_filter_kses' );
2189  
2190      // Global Styles filtering.
2191      remove_filter( 'content_save_pre', 'wp_filter_global_styles_post', 9 );
2192      remove_filter( 'content_filtered_save_pre', 'wp_filter_global_styles_post', 9 );
2193  
2194      // Post filtering.
2195      remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
2196      remove_filter( 'excerpt_save_pre', 'wp_filter_post_kses' );
2197      remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
2198  }
2199  
2200  /**
2201   * Sets up most of the KSES filters for input form content.
2202   *
2203   * First removes all of the KSES filters in case the current user does not need
2204   * to have KSES filter the content. If the user does not have `unfiltered_html`
2205   * capability, then KSES filters are added.
2206   *
2207   * @since 2.0.0
2208   */
2209  function kses_init() {
2210      kses_remove_filters();
2211  
2212      if ( ! current_user_can( 'unfiltered_html' ) ) {
2213          kses_init_filters();
2214      }
2215  }
2216  
2217  /**
2218   * Filters an inline style attribute and removes disallowed rules.
2219   *
2220   * @since 2.8.1
2221   * @since 4.4.0 Added support for `min-height`, `max-height`, `min-width`, and `max-width`.
2222   * @since 4.6.0 Added support for `list-style-type`.
2223   * @since 5.0.0 Added support for `background-image`.
2224   * @since 5.1.0 Added support for `text-transform`.
2225   * @since 5.2.0 Added support for `background-position` and `grid-template-columns`.
2226   * @since 5.3.0 Added support for `grid`, `flex` and `column` layout properties.
2227   *              Extend `background-*` support of individual properties.
2228   * @since 5.3.1 Added support for gradient backgrounds.
2229   * @since 5.7.1 Added support for `object-position`.
2230   * @since 5.8.0 Added support for `calc()` and `var()` values.
2231   *
2232   * @param string $css        A string of CSS rules.
2233   * @param string $deprecated Not used.
2234   * @return string Filtered string of CSS rules.
2235   */
2236  function safecss_filter_attr( $css, $deprecated = '' ) {
2237      if ( ! empty( $deprecated ) ) {
2238          _deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented.
2239      }
2240  
2241      $css = wp_kses_no_null( $css );
2242      $css = str_replace( array( "\n", "\r", "\t" ), '', $css );
2243  
2244      $allowed_protocols = wp_allowed_protocols();
2245  
2246      $css_array = explode( ';', trim( $css ) );
2247  
2248      /**
2249       * Filters the list of allowed CSS attributes.
2250       *
2251       * @since 2.8.1
2252       *
2253       * @param string[] $attr Array of allowed CSS attributes.
2254       */
2255      $allowed_attr = apply_filters(
2256          'safe_style_css',
2257          array(
2258              'background',
2259              'background-color',
2260              'background-image',
2261              'background-position',
2262              'background-size',
2263              'background-attachment',
2264              'background-blend-mode',
2265  
2266              'border',
2267              'border-radius',
2268              'border-width',
2269              'border-color',
2270              'border-style',
2271              'border-right',
2272              'border-right-color',
2273              'border-right-style',
2274              'border-right-width',
2275              'border-bottom',
2276              'border-bottom-color',
2277              'border-bottom-left-radius',
2278              'border-bottom-right-radius',
2279              'border-bottom-style',
2280              'border-bottom-width',
2281              'border-bottom-right-radius',
2282              'border-bottom-left-radius',
2283              'border-left',
2284              'border-left-color',
2285              'border-left-style',
2286              'border-left-width',
2287              'border-top',
2288              'border-top-color',
2289              'border-top-left-radius',
2290              'border-top-right-radius',
2291              'border-top-style',
2292              'border-top-width',
2293              'border-top-left-radius',
2294              'border-top-right-radius',
2295  
2296              'border-spacing',
2297              'border-collapse',
2298              'caption-side',
2299  
2300              'columns',
2301              'column-count',
2302              'column-fill',
2303              'column-gap',
2304              'column-rule',
2305              'column-span',
2306              'column-width',
2307  
2308              'color',
2309              'filter',
2310              'font',
2311              'font-family',
2312              'font-size',
2313              'font-style',
2314              'font-variant',
2315              'font-weight',
2316              'letter-spacing',
2317              'line-height',
2318              'text-align',
2319              'text-decoration',
2320              'text-indent',
2321              'text-transform',
2322  
2323              'height',
2324              'min-height',
2325              'max-height',
2326  
2327              'width',
2328              'min-width',
2329              'max-width',
2330  
2331              'margin',
2332              'margin-right',
2333              'margin-bottom',
2334              'margin-left',
2335              'margin-top',
2336  
2337              'padding',
2338              'padding-right',
2339              'padding-bottom',
2340              'padding-left',
2341              'padding-top',
2342  
2343              'flex',
2344              'flex-basis',
2345              'flex-direction',
2346              'flex-flow',
2347              'flex-grow',
2348              'flex-shrink',
2349  
2350              'grid-template-columns',
2351              'grid-auto-columns',
2352              'grid-column-start',
2353              'grid-column-end',
2354              'grid-column-gap',
2355              'grid-template-rows',
2356              'grid-auto-rows',
2357              'grid-row-start',
2358              'grid-row-end',
2359              'grid-row-gap',
2360              'grid-gap',
2361  
2362              'justify-content',
2363              'justify-items',
2364              'justify-self',
2365              'align-content',
2366              'align-items',
2367              'align-self',
2368  
2369              'clear',
2370              'cursor',
2371              'direction',
2372              'float',
2373              'list-style-type',
2374              'object-position',
2375              'overflow',
2376              'vertical-align',
2377          )
2378      );
2379  
2380      /*
2381       * CSS attributes that accept URL data types.
2382       *
2383       * This is in accordance to the CSS spec and unrelated to
2384       * the sub-set of supported attributes above.
2385       *
2386       * See: https://developer.mozilla.org/en-US/docs/Web/CSS/url
2387       */
2388      $css_url_data_types = array(
2389          'background',
2390          'background-image',
2391  
2392          'cursor',
2393  
2394          'list-style',
2395          'list-style-image',
2396      );
2397  
2398      /*
2399       * CSS attributes that accept gradient data types.
2400       *
2401       */
2402      $css_gradient_data_types = array(
2403          'background',
2404          'background-image',
2405      );
2406  
2407      if ( empty( $allowed_attr ) ) {
2408          return $css;
2409      }
2410  
2411      $css = '';
2412      foreach ( $css_array as $css_item ) {
2413          if ( '' === $css_item ) {
2414              continue;
2415          }
2416  
2417          $css_item        = trim( $css_item );
2418          $css_test_string = $css_item;
2419          $found           = false;
2420          $url_attr        = false;
2421          $gradient_attr   = false;
2422  
2423          if ( strpos( $css_item, ':' ) === false ) {
2424              $found = true;
2425          } else {
2426              $parts        = explode( ':', $css_item, 2 );
2427              $css_selector = trim( $parts[0] );
2428  
2429              if ( in_array( $css_selector, $allowed_attr, true ) ) {
2430                  $found         = true;
2431                  $url_attr      = in_array( $css_selector, $css_url_data_types, true );
2432                  $gradient_attr = in_array( $css_selector, $css_gradient_data_types, true );
2433              }
2434          }
2435  
2436          if ( $found && $url_attr ) {
2437              // Simplified: matches the sequence `url(*)`.
2438              preg_match_all( '/url\([^)]+\)/', $parts[1], $url_matches );
2439  
2440              foreach ( $url_matches[0] as $url_match ) {
2441                  // Clean up the URL from each of the matches above.
2442                  preg_match( '/^url\(\s*([\'\"]?)(.*)(\g1)\s*\)$/', $url_match, $url_pieces );
2443  
2444                  if ( empty( $url_pieces[2] ) ) {
2445                      $found = false;
2446                      break;
2447                  }
2448  
2449                  $url = trim( $url_pieces[2] );
2450  
2451                  if ( empty( $url ) || wp_kses_bad_protocol( $url, $allowed_protocols ) !== $url ) {
2452                      $found = false;
2453                      break;
2454                  } else {
2455                      // Remove the whole `url(*)` bit that was matched above from the CSS.
2456                      $css_test_string = str_replace( $url_match, '', $css_test_string );
2457                  }
2458              }
2459          }
2460  
2461          if ( $found && $gradient_attr ) {
2462              $css_value = trim( $parts[1] );
2463              if ( preg_match( '/^(repeating-)?(linear|radial|conic)-gradient\(([^()]|rgb[a]?\([^()]*\))*\)$/', $css_value ) ) {
2464                  // Remove the whole `gradient` bit that was matched above from the CSS.
2465                  $css_test_string = str_replace( $css_value, '', $css_test_string );
2466              }
2467          }
2468  
2469          if ( $found ) {
2470              // Allow CSS calc().
2471              $css_test_string = preg_replace( '/calc\(((?:\([^()]*\)?|[^()])*)\)/', '', $css_test_string );
2472              // Allow CSS var().
2473              $css_test_string = preg_replace( '/\(?var\(--[a-zA-Z0-9_-]*\)/', '', $css_test_string );
2474  
2475              // Check for any CSS containing \ ( & } = or comments,
2476              // except for url(), calc(), or var() usage checked above.
2477              $allow_css = ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string );
2478  
2479              /**
2480               * Filters the check for unsafe CSS in `safecss_filter_attr`.
2481               *
2482               * Enables developers to determine whether a section of CSS should be allowed or discarded.
2483               * By default, the value will be false if the part contains \ ( & } = or comments.
2484               * Return true to allow the CSS part to be included in the output.
2485               *
2486               * @since 5.5.0
2487               *
2488               * @param bool   $allow_css       Whether the CSS in the test string is considered safe.
2489               * @param string $css_test_string The CSS string to test.
2490               */
2491              $allow_css = apply_filters( 'safecss_filter_attr_allow_css', $allow_css, $css_test_string );
2492  
2493              // Only add the CSS part if it passes the regex check.
2494              if ( $allow_css ) {
2495                  if ( '' !== $css ) {
2496                      $css .= ';';
2497                  }
2498  
2499                  $css .= $css_item;
2500              }
2501          }
2502      }
2503  
2504      return $css;
2505  }
2506  
2507  /**
2508   * Helper function to add global attributes to a tag in the allowed HTML list.
2509   *
2510   * @since 3.5.0
2511   * @since 5.0.0 Added support for `data-*` wildcard attributes.
2512   * @since 6.0.0 Added `dir`, `lang`, and `xml:lang` to global attributes.
2513   *
2514   * @access private
2515   * @ignore
2516   *
2517   * @param array $value An array of attributes.
2518   * @return array The array of attributes with global attributes added.
2519   */
2520  function _wp_add_global_attributes( $value ) {
2521      $global_attributes = array(
2522          'aria-describedby' => true,
2523          'aria-details'     => true,
2524          'aria-label'       => true,
2525          'aria-labelledby'  => true,
2526          'aria-hidden'      => true,
2527          'class'            => true,
2528          'data-*'           => true,
2529          'dir'              => true,
2530          'id'               => true,
2531          'lang'             => true,
2532          'style'            => true,
2533          'title'            => true,
2534          'role'             => true,
2535          'xml:lang'         => true,
2536      );
2537  
2538      if ( true === $value ) {
2539          $value = array();
2540      }
2541  
2542      if ( is_array( $value ) ) {
2543          return array_merge( $value, $global_attributes );
2544      }
2545  
2546      return $value;
2547  }
2548  
2549  /**
2550   * Helper function to check if this is a safe PDF URL.
2551   *
2552   * @since 5.9.0
2553   * @access private
2554   * @ignore
2555   *
2556   * @param string $url The URL to check.
2557   * @return bool True if the URL is safe, false otherwise.
2558   */
2559  function _wp_kses_allow_pdf_objects( $url ) {
2560      // We're not interested in URLs that contain query strings or fragments.
2561      if ( str_contains( $url, '?' ) || str_contains( $url, '#' ) ) {
2562          return false;
2563      }
2564  
2565      // If it doesn't have a PDF extension, it's not safe.
2566      if ( ! str_ends_with( $url, '.pdf' ) ) {
2567          return false;
2568      }
2569  
2570      // If the URL host matches the current site's media URL, it's safe.
2571      $upload_info = wp_upload_dir( null, false );
2572      $parsed_url  = wp_parse_url( $upload_info['url'] );
2573      $upload_host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : '';
2574      $upload_port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '';
2575  
2576      if ( str_starts_with( $url, "http://$upload_host$upload_port/" )
2577          || str_starts_with( $url, "https://$upload_host$upload_port/" )
2578      ) {
2579          return true;
2580      }
2581  
2582      return false;
2583  }


Generated: Sat Nov 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1