[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Functions related to registering and parsing blocks.
   4   *
   5   * @package WordPress
   6   * @subpackage Blocks
   7   * @since 5.0.0
   8   */
   9  
  10  /**
  11   * Removes the block asset's path prefix if provided.
  12   *
  13   * @since 5.5.0
  14   *
  15   * @param string $asset_handle_or_path Asset handle or prefixed path.
  16   * @return string Path without the prefix or the original value.
  17   */
  18  function remove_block_asset_path_prefix( $asset_handle_or_path ) {
  19      $path_prefix = 'file:';
  20      if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
  21          return $asset_handle_or_path;
  22      }
  23      $path = substr(
  24          $asset_handle_or_path,
  25          strlen( $path_prefix )
  26      );
  27      if ( str_starts_with( $path, './' ) ) {
  28          $path = substr( $path, 2 );
  29      }
  30      return $path;
  31  }
  32  
  33  /**
  34   * Generates the name for an asset based on the name of the block
  35   * and the field name provided.
  36   *
  37   * @since 5.5.0
  38   * @since 6.1.0 Added `$index` parameter.
  39   * @since 6.5.0 Added support for `viewScriptModule` field.
  40   *
  41   * @param string $block_name Name of the block.
  42   * @param string $field_name Name of the metadata field.
  43   * @param int    $index      Optional. Index of the asset when multiple items passed.
  44   *                           Default 0.
  45   * @return string Generated asset name for the block's field.
  46   *
  47   * @phpstan-param non-falsy-string $block_name
  48   * @phpstan-param 'editorScript'|'editorStyle'|'script'|'style'|'viewScript'|'viewScriptModule'|'viewStyle' $field_name
  49   * @phpstan-param int<0, max> $index
  50   * @phpstan-return non-falsy-string
  51   */
  52  function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
  53      if ( str_starts_with( $block_name, 'core/' ) ) {
  54          $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
  55          if ( str_starts_with( $field_name, 'editor' ) ) {
  56              $asset_handle .= '-editor';
  57          }
  58          if ( str_starts_with( $field_name, 'view' ) ) {
  59              $asset_handle .= '-view';
  60          }
  61          if ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) {
  62              $asset_handle .= '-script-module';
  63          }
  64          if ( $index > 0 ) {
  65              $asset_handle .= '-' . ( $index + 1 );
  66          }
  67          return $asset_handle;
  68      }
  69  
  70      $field_mappings = array(
  71          'editorScript'     => 'editor-script',
  72          'editorStyle'      => 'editor-style',
  73          'script'           => 'script',
  74          'style'            => 'style',
  75          'viewScript'       => 'view-script',
  76          'viewScriptModule' => 'view-script-module',
  77          'viewStyle'        => 'view-style',
  78      );
  79      $asset_handle   = str_replace( '/', '-', $block_name ) .
  80          '-' . $field_mappings[ $field_name ];
  81      if ( $index > 0 ) {
  82          $asset_handle .= '-' . ( $index + 1 );
  83      }
  84      return $asset_handle;
  85  }
  86  
  87  /**
  88   * Gets the URL to a block asset.
  89   *
  90   * @since 6.4.0
  91   *
  92   * @param string $path A normalized path to a block asset.
  93   * @return string|false The URL to the block asset or false on failure.
  94   *
  95   * @phpstan-return non-falsy-string|false
  96   */
  97  function get_block_asset_url( $path ) {
  98      if ( empty( $path ) ) {
  99          return false;
 100      }
 101  
 102      // Path needs to be normalized to work in Windows env.
 103      static $wpinc_path_norm = '';
 104      if ( ! $wpinc_path_norm ) {
 105          $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
 106      }
 107  
 108      if ( str_starts_with( $path, $wpinc_path_norm ) ) {
 109          return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
 110      }
 111  
 112      /** @var array<string, string> $template_paths_norm */
 113      static $template_paths_norm = array();
 114  
 115      $template = get_template();
 116      if ( ! isset( $template_paths_norm[ $template ] ) ) {
 117          $template_paths_norm[ $template ] = wp_normalize_path( realpath( get_template_directory() ) );
 118      }
 119  
 120      if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
 121          return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
 122      }
 123  
 124      if ( is_child_theme() ) {
 125          $stylesheet = get_stylesheet();
 126          if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) {
 127              $template_paths_norm[ $stylesheet ] = wp_normalize_path( realpath( get_stylesheet_directory() ) );
 128          }
 129  
 130          if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) {
 131              return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) );
 132          }
 133      }
 134  
 135      return plugins_url( basename( $path ), $path );
 136  }
 137  
 138  /**
 139   * Finds a script module ID for the selected block metadata field.
 140   *
 141   * Detects when a path to a file was provided and optionally finds a
 142   * corresponding asset file with details necessary to register the script
 143   * module with an automatically generated module ID. It returns the
 144   * unprocessed script module ID otherwise.
 145   *
 146   * @since 6.5.0
 147   *
 148   * @param array  $metadata   Block metadata.
 149   * @param string $field_name Field name to pick from metadata.
 150   * @param int    $index      Optional. Index of the script module ID to register when multiple
 151   *                           items passed. Default 0.
 152   * @return string|false Script module ID or false on failure.
 153   *
 154   * @phpstan-param array{
 155   *     name?: non-falsy-string,
 156   *     file: non-falsy-string|null,
 157   *     version?: string,
 158   *     supports?: array{
 159   *         interactivity?: bool|array{interactive?: bool, clientNavigation?: bool, ...},
 160   *         ...
 161   *     },
 162   *     viewScriptModule?: string|list<string>,
 163   *     ...
 164   * } $metadata
 165   * @phpstan-param 'viewScriptModule' $field_name
 166   * @phpstan-param int<0, max> $index
 167   * @phpstan-return non-falsy-string|false
 168   */
 169  function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
 170      if ( empty( $metadata[ $field_name ] ) ) {
 171          return false;
 172      }
 173  
 174      $module_id = $metadata[ $field_name ];
 175      if ( is_array( $module_id ) ) {
 176          if ( empty( $module_id[ $index ] ) ) {
 177              return false;
 178          }
 179          $module_id = $module_id[ $index ];
 180      }
 181  
 182      $module_path = remove_block_asset_path_prefix( $module_id );
 183      if ( $module_id === $module_path ) {
 184          return $module_id;
 185      }
 186  
 187      $path                  = dirname( $metadata['file'] );
 188      $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
 189      $module_id             = generate_block_asset_handle( $metadata['name'], $field_name, $index );
 190      $module_asset_path     = wp_normalize_path(
 191          realpath( $module_asset_raw_path )
 192      );
 193  
 194      $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
 195      $module_uri       = get_block_asset_url( $module_path_norm );
 196  
 197      /** @var array{ dependencies?: list<non-falsy-string|array{id: non-falsy-string, import?: 'static'|'dynamic'}>, version?: string|false|null, ... } $module_asset */
 198      $module_asset        = ! empty( $module_asset_path ) ? require $module_asset_path : array();
 199      $module_dependencies = $module_asset['dependencies'] ?? array();
 200      $block_version       = $metadata['version'] ?? false;
 201      $module_version      = $module_asset['version'] ?? $block_version;
 202  
 203      $supports_interactivity_true = isset( $metadata['supports']['interactivity'] ) && true === $metadata['supports']['interactivity'];
 204      $is_interactive              = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] );
 205      $supports_client_navigation  = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['clientNavigation'] ) && true === $metadata['supports']['interactivity']['clientNavigation'] );
 206  
 207      $args = array();
 208  
 209      // Blocks using the Interactivity API are server-side rendered, so they are
 210      // by design not in the critical rendering path and should be deprioritized.
 211      if ( $is_interactive ) {
 212          $args['fetchpriority'] = 'low';
 213          $args['in_footer']     = true;
 214      }
 215  
 216      // Blocks using the Interactivity API that support client-side navigation
 217      // must be marked as such in their script modules.
 218      if ( $is_interactive && $supports_client_navigation ) {
 219          wp_interactivity()->add_client_navigation_support_to_script_module( $module_id );
 220      }
 221  
 222      wp_register_script_module(
 223          $module_id,
 224          $module_uri,
 225          $module_dependencies,
 226          $module_version,
 227          $args
 228      );
 229  
 230      return $module_id;
 231  }
 232  
 233  /**
 234   * Finds a script handle for the selected block metadata field.
 235   *
 236   * Detects when a path to a file was provided and optionally finds a
 237   * corresponding asset file with details necessary to register the script. The
 238   * handle is taken from the asset file when it provides one, and is otherwise
 239   * generated automatically. It returns the unprocessed script handle when a
 240   * handle rather than a path was given.
 241   *
 242   * @since 5.5.0
 243   * @since 6.1.0 Added `$index` parameter.
 244   * @since 6.5.0 The asset file is optional. Added script handle support in the asset file.
 245   *
 246   * @param array  $metadata   Block metadata.
 247   * @param string $field_name Field name to pick from metadata.
 248   * @param int    $index      Optional. Index of the script to register when multiple items passed.
 249   *                           Default 0.
 250   * @return string|false Script handle provided directly or created through
 251   *                      script's registration, or false on failure.
 252   *
 253   * @phpstan-param array{
 254   *     name?: non-falsy-string,
 255   *     file: non-falsy-string|null,
 256   *     version?: string,
 257   *     textdomain?: string,
 258   *     editorScript?: string|list<string>,
 259   *     script?: string|list<string>,
 260   *     viewScript?: string|list<string>,
 261   *     ...
 262   * } $metadata
 263   * @phpstan-param 'editorScript'|'script'|'viewScript' $field_name
 264   * @phpstan-param int<0, max> $index
 265   * @phpstan-return non-falsy-string|false
 266   */
 267  function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
 268      if ( empty( $metadata[ $field_name ] ) ) {
 269          return false;
 270      }
 271  
 272      $script_handle_or_path = $metadata[ $field_name ];
 273      if ( is_array( $script_handle_or_path ) ) {
 274          if ( empty( $script_handle_or_path[ $index ] ) ) {
 275              return false;
 276          }
 277          $script_handle_or_path = $script_handle_or_path[ $index ];
 278      }
 279  
 280      $script_path = remove_block_asset_path_prefix( $script_handle_or_path );
 281      if ( $script_handle_or_path === $script_path ) {
 282          return $script_handle_or_path;
 283      }
 284  
 285      $path                  = dirname( $metadata['file'] );
 286      $script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
 287      $script_asset_path     = wp_normalize_path(
 288          realpath( $script_asset_raw_path )
 289      );
 290  
 291      // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
 292      /** @var array{ handle?: non-falsy-string, dependencies?: list<non-falsy-string>, version?: string|false|null, ... } $script_asset */
 293      $script_asset  = ! empty( $script_asset_path ) ? require $script_asset_path : array();
 294      $script_handle = $script_asset['handle'] ??
 295          generate_block_asset_handle( $metadata['name'], $field_name, $index );
 296      if ( wp_script_is( $script_handle, 'registered' ) ) {
 297          return $script_handle;
 298      }
 299  
 300      $script_path_norm    = wp_normalize_path( realpath( $path . '/' . $script_path ) );
 301      $script_uri          = get_block_asset_url( $script_path_norm );
 302      $script_dependencies = $script_asset['dependencies'] ?? array();
 303      $block_version       = $metadata['version'] ?? false;
 304      $script_version      = $script_asset['version'] ?? $block_version;
 305      $script_args         = array();
 306      if ( 'viewScript' === $field_name && $script_uri ) {
 307          $script_args['strategy'] = 'defer';
 308      }
 309  
 310      $result = wp_register_script(
 311          $script_handle,
 312          $script_uri,
 313          $script_dependencies,
 314          $script_version,
 315          $script_args
 316      );
 317      if ( ! $result ) {
 318          return false;
 319      }
 320  
 321      if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) {
 322          wp_set_script_translations( $script_handle, $metadata['textdomain'] );
 323      }
 324  
 325      return $script_handle;
 326  }
 327  
 328  /**
 329   * Finds a style handle for the block metadata field.
 330   *
 331   * Detects when a path to a file was provided and registers the style under an
 332   * automatically generated handle name. It returns the unprocessed style handle
 333   * otherwise, except for the first style of a core block, which is instead
 334   * registered from the block's own stylesheet when separate core block assets
 335   * are loaded. Core blocks accept only handles, not paths.
 336   *
 337   * @since 5.5.0
 338   * @since 6.1.0 Added `$index` parameter.
 339   *
 340   * @param array  $metadata   Block metadata.
 341   * @param string $field_name Field name to pick from metadata.
 342   * @param int    $index      Optional. Index of the style to register when multiple items passed.
 343   *                           Default 0.
 344   * @return string|false Style handle provided directly or created through
 345   *                      style's registration, or false on failure.
 346   *
 347   * @phpstan-param array{
 348   *     name?: non-falsy-string,
 349   *     file: non-falsy-string|null,
 350   *     version?: string,
 351   *     editorStyle?: string|list<string>,
 352   *     style?: string|list<string>,
 353   *     viewStyle?: string|list<string>,
 354   *     ...
 355   * } $metadata
 356   * @phpstan-param 'editorStyle'|'style'|'viewStyle' $field_name
 357   * @phpstan-param int<0, max> $index
 358   * @phpstan-return non-falsy-string|false
 359   */
 360  function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
 361      if ( empty( $metadata[ $field_name ] ) ) {
 362          return false;
 363      }
 364  
 365      $style_handle = $metadata[ $field_name ];
 366      if ( is_array( $style_handle ) ) {
 367          if ( empty( $style_handle[ $index ] ) ) {
 368              return false;
 369          }
 370          $style_handle = $style_handle[ $index ];
 371      }
 372  
 373      $style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
 374      // If the style handle is already registered, skip re-registering.
 375      if ( wp_style_is( $style_handle_name, 'registered' ) ) {
 376          return $style_handle_name;
 377      }
 378  
 379      static $wpinc_path_norm = '';
 380      if ( ! $wpinc_path_norm ) {
 381          $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
 382      }
 383  
 384      $is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
 385      // Skip registering individual styles for each core block when a bundled version provided.
 386      if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
 387          return false;
 388      }
 389  
 390      $style_path      = remove_block_asset_path_prefix( $style_handle );
 391      $is_style_handle = $style_handle === $style_path;
 392      // Allow only passing style handles for core blocks.
 393      if ( $is_core_block && ! $is_style_handle ) {
 394          return false;
 395      }
 396      // Return the style handle unless it's the first item for every core block that requires special treatment.
 397      if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
 398          return $style_handle;
 399      }
 400  
 401      // Check whether styles should have a ".min" suffix or not.
 402      $suffix = SCRIPT_DEBUG ? '' : '.min';
 403      if ( $is_core_block ) {
 404          $style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css";
 405      }
 406  
 407      $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
 408      $style_uri       = get_block_asset_url( $style_path_norm );
 409  
 410      $block_version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
 411      $version       = $style_path_norm && defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( $style_path_norm ) : $block_version;
 412      $result        = wp_register_style(
 413          $style_handle_name,
 414          $style_uri,
 415          array(),
 416          $version
 417      );
 418      if ( ! $result ) {
 419          return false;
 420      }
 421  
 422      if ( $style_uri ) {
 423          wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
 424  
 425          if ( $is_core_block ) {
 426              $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
 427          } else {
 428              $rtl_file = str_replace( '.css', '-rtl.css', $style_path_norm );
 429          }
 430  
 431          if ( is_rtl() && file_exists( $rtl_file ) ) {
 432              wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
 433              wp_style_add_data( $style_handle_name, 'suffix', $suffix );
 434              wp_style_add_data( $style_handle_name, 'path', $rtl_file );
 435          }
 436      }
 437  
 438      return $style_handle_name;
 439  }
 440  
 441  /**
 442   * Gets i18n schema for block's metadata read from `block.json` file.
 443   *
 444   * @since 5.9.0
 445   *
 446   * @return object The schema for block's metadata.
 447   */
 448  function get_block_metadata_i18n_schema() {
 449      static $i18n_block_schema;
 450  
 451      if ( ! isset( $i18n_block_schema ) ) {
 452          $i18n_block_schema = wp_json_file_decode( __DIR__ . '/block-i18n.json' );
 453      }
 454  
 455      return $i18n_block_schema;
 456  }
 457  
 458  /**
 459   * Registers all block types from a block metadata collection.
 460   *
 461   * This can either reference a previously registered metadata collection or, if the `$manifest` parameter is provided,
 462   * register the metadata collection directly within the same function call.
 463   *
 464   * @since 6.8.0
 465   * @see wp_register_block_metadata_collection()
 466   * @see register_block_type_from_metadata()
 467   *
 468   * @param string $path     The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
 469   * @param string $manifest Optional. The absolute path to the manifest file containing the metadata collection, in
 470   *                         order to register the collection. If this parameter is not provided, the `$path` parameter
 471   *                         must reference a previously registered block metadata collection.
 472   */
 473  function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) {
 474      if ( $manifest ) {
 475          wp_register_block_metadata_collection( $path, $manifest );
 476      }
 477  
 478      $block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path );
 479      foreach ( $block_metadata_files as $block_metadata_file ) {
 480          register_block_type_from_metadata( $block_metadata_file );
 481      }
 482  }
 483  
 484  /**
 485   * Registers a block metadata collection.
 486   *
 487   * This function allows core and third-party plugins to register their block metadata
 488   * collections in a centralized location. Registering collections can improve performance
 489   * by avoiding multiple reads from the filesystem and parsing JSON.
 490   *
 491   * @since 6.7.0
 492   *
 493   * @param string $path     The base path in which block files for the collection reside.
 494   * @param string $manifest The path to the manifest file for the collection.
 495   */
 496  function wp_register_block_metadata_collection( $path, $manifest ) {
 497      WP_Block_Metadata_Registry::register_collection( $path, $manifest );
 498  }
 499  
 500  /**
 501   * Registers a block type from the metadata stored in the `block.json` file.
 502   *
 503   * @since 5.5.0
 504   * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
 505   * @since 5.9.0 Added support for `variations` and `viewScript` fields.
 506   * @since 6.1.0 Added support for `render` field.
 507   * @since 6.3.0 Added `selectors` field.
 508   * @since 6.4.0 Added support for `blockHooks` field.
 509   * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields.
 510   * @since 6.7.0 Allow PHP filename as `variations` argument.
 511   *
 512   * @param string $file_or_folder Path to the JSON file with metadata definition for
 513   *                               the block or path to the folder where the `block.json` file is located.
 514   *                               If providing the path to a JSON file, the filename must end with `block.json`.
 515   * @param array  $args           Optional. Array of block type arguments. Accepts any public property
 516   *                               of `WP_Block_Type`. See WP_Block_Type::__construct() for information
 517   *                               on accepted arguments. Default empty array.
 518   * @return WP_Block_Type|false The registered block type on success, or false on failure.
 519   */
 520  function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
 521      /*
 522       * Get an array of metadata from a PHP file.
 523       * This improves performance for core blocks as it's only necessary to read a single PHP file
 524       * instead of reading a JSON file per-block, and then decoding from JSON to PHP.
 525       * Using a static variable ensures that the metadata is only read once per request.
 526       */
 527  
 528      $file_or_folder = wp_normalize_path( $file_or_folder );
 529  
 530      $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
 531          trailingslashit( $file_or_folder ) . 'block.json' :
 532          $file_or_folder;
 533  
 534      $is_core_block        = str_starts_with( $file_or_folder, wp_normalize_path( ABSPATH . WPINC ) );
 535      $metadata_file_exists = $is_core_block || file_exists( $metadata_file );
 536      $registry_metadata    = WP_Block_Metadata_Registry::get_metadata( $file_or_folder );
 537  
 538      if ( $registry_metadata ) {
 539          $metadata = $registry_metadata;
 540      } elseif ( $metadata_file_exists ) {
 541          $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
 542      } else {
 543          $metadata = array();
 544      }
 545  
 546      if ( ! is_array( $metadata ) || ( empty( $metadata['name'] ) && empty( $args['name'] ) ) ) {
 547          return false;
 548      }
 549  
 550      $metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null;
 551  
 552      /**
 553       * Filters the metadata provided for registering a block type.
 554       *
 555       * @since 5.7.0
 556       *
 557       * @param array $metadata Metadata for registering a block type.
 558       */
 559      $metadata = apply_filters( 'block_type_metadata', $metadata );
 560  
 561      // Add `style` and `editor_style` for core blocks if missing.
 562      if ( ! empty( $metadata['name'] ) && str_starts_with( $metadata['name'], 'core/' ) ) {
 563          $block_name = str_replace( 'core/', '', $metadata['name'] );
 564  
 565          if ( ! isset( $metadata['style'] ) ) {
 566              $metadata['style'] = "wp-block-$block_name";
 567          }
 568          if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) {
 569              $metadata['style']   = (array) $metadata['style'];
 570              $metadata['style'][] = "wp-block-{$block_name}-theme";
 571          }
 572          if ( ! isset( $metadata['editorStyle'] ) ) {
 573              $metadata['editorStyle'] = "wp-block-{$block_name}-editor";
 574          }
 575      }
 576  
 577      $settings          = array();
 578      $property_mappings = array(
 579          'apiVersion'      => 'api_version',
 580          'name'            => 'name',
 581          'title'           => 'title',
 582          'category'        => 'category',
 583          'parent'          => 'parent',
 584          'ancestor'        => 'ancestor',
 585          'icon'            => 'icon',
 586          'description'     => 'description',
 587          'keywords'        => 'keywords',
 588          'attributes'      => 'attributes',
 589          'providesContext' => 'provides_context',
 590          'usesContext'     => 'uses_context',
 591          'selectors'       => 'selectors',
 592          'supports'        => 'supports',
 593          'styles'          => 'styles',
 594          'variations'      => 'variations',
 595          'example'         => 'example',
 596          'allowedBlocks'   => 'allowed_blocks',
 597      );
 598      $textdomain        = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null;
 599      $i18n_schema       = get_block_metadata_i18n_schema();
 600  
 601      foreach ( $property_mappings as $key => $mapped_key ) {
 602          if ( isset( $metadata[ $key ] ) ) {
 603              $settings[ $mapped_key ] = $metadata[ $key ];
 604              if ( $metadata_file_exists && $textdomain && isset( $i18n_schema->$key ) ) {
 605                  $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain );
 606              }
 607          }
 608      }
 609  
 610      if ( ! empty( $metadata['render'] ) ) {
 611          $template_path = wp_normalize_path(
 612              realpath(
 613                  dirname( $metadata['file'] ) . '/' .
 614                  remove_block_asset_path_prefix( $metadata['render'] )
 615              )
 616          );
 617          if ( $template_path ) {
 618              /**
 619               * Renders the block on the server.
 620               *
 621               * @since 6.1.0
 622               *
 623               * @param array    $attributes Block attributes.
 624               * @param string   $content    Block default content.
 625               * @param WP_Block $block      Block instance.
 626               *
 627               * @return string Returns the block content.
 628               */
 629              $settings['render_callback'] = static function ( $attributes, $content, $block ) use ( $template_path ) {
 630                  ob_start();
 631                  require $template_path;
 632                  return ob_get_clean();
 633              };
 634          }
 635      }
 636  
 637      // If `variations` is a string, it's the name of a PHP file that
 638      // generates the variations.
 639      if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) {
 640          $variations_path = wp_normalize_path(
 641              realpath(
 642                  dirname( $metadata['file'] ) . '/' .
 643                  remove_block_asset_path_prefix( $metadata['variations'] )
 644              )
 645          );
 646          if ( $variations_path ) {
 647              /**
 648               * Generates the list of block variations.
 649               *
 650               * @since 6.7.0
 651               *
 652               * @return string Returns the list of block variations.
 653               */
 654              $settings['variation_callback'] = static function () use ( $variations_path ) {
 655                  $variations = require $variations_path;
 656                  return $variations;
 657              };
 658              // The block instance's `variations` field is only allowed to be an array
 659              // (of known block variations). We unset it so that the block instance will
 660              // provide a getter that returns the result of the `variation_callback` instead.
 661              unset( $settings['variations'] );
 662          }
 663      }
 664  
 665      $settings = array_merge( $settings, $args );
 666  
 667      $script_fields = array(
 668          'editorScript' => 'editor_script_handles',
 669          'script'       => 'script_handles',
 670          'viewScript'   => 'view_script_handles',
 671      );
 672      foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
 673          if ( ! empty( $settings[ $metadata_field_name ] ) ) {
 674              $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
 675          }
 676          if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
 677              $scripts           = $metadata[ $metadata_field_name ];
 678              $processed_scripts = array();
 679              if ( is_array( $scripts ) ) {
 680                  for ( $index = 0; $index < count( $scripts ); $index++ ) {
 681                      $result = register_block_script_handle(
 682                          $metadata,
 683                          $metadata_field_name,
 684                          $index
 685                      );
 686                      if ( $result ) {
 687                          $processed_scripts[] = $result;
 688                      }
 689                  }
 690              } else {
 691                  $result = register_block_script_handle(
 692                      $metadata,
 693                      $metadata_field_name
 694                  );
 695                  if ( $result ) {
 696                      $processed_scripts[] = $result;
 697                  }
 698              }
 699              $settings[ $settings_field_name ] = $processed_scripts;
 700          }
 701      }
 702  
 703      $module_fields = array(
 704          'viewScriptModule' => 'view_script_module_ids',
 705      );
 706      foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
 707          if ( ! empty( $settings[ $metadata_field_name ] ) ) {
 708              $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
 709          }
 710          if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
 711              $modules           = $metadata[ $metadata_field_name ];
 712              $processed_modules = array();
 713              if ( is_array( $modules ) ) {
 714                  for ( $index = 0; $index < count( $modules ); $index++ ) {
 715                      $result = register_block_script_module_id(
 716                          $metadata,
 717                          $metadata_field_name,
 718                          $index
 719                      );
 720                      if ( $result ) {
 721                          $processed_modules[] = $result;
 722                      }
 723                  }
 724              } else {
 725                  $result = register_block_script_module_id(
 726                      $metadata,
 727                      $metadata_field_name
 728                  );
 729                  if ( $result ) {
 730                      $processed_modules[] = $result;
 731                  }
 732              }
 733              $settings[ $settings_field_name ] = $processed_modules;
 734          }
 735      }
 736  
 737      $style_fields = array(
 738          'editorStyle' => 'editor_style_handles',
 739          'style'       => 'style_handles',
 740          'viewStyle'   => 'view_style_handles',
 741      );
 742      foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
 743          if ( ! empty( $settings[ $metadata_field_name ] ) ) {
 744              $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
 745          }
 746          if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
 747              $styles           = $metadata[ $metadata_field_name ];
 748              $processed_styles = array();
 749              if ( is_array( $styles ) ) {
 750                  for ( $index = 0; $index < count( $styles ); $index++ ) {
 751                      $result = register_block_style_handle(
 752                          $metadata,
 753                          $metadata_field_name,
 754                          $index
 755                      );
 756                      if ( $result ) {
 757                          $processed_styles[] = $result;
 758                      }
 759                  }
 760              } else {
 761                  $result = register_block_style_handle(
 762                      $metadata,
 763                      $metadata_field_name
 764                  );
 765                  if ( $result ) {
 766                      $processed_styles[] = $result;
 767                  }
 768              }
 769              $settings[ $settings_field_name ] = $processed_styles;
 770          }
 771      }
 772  
 773      if ( ! empty( $metadata['blockHooks'] ) ) {
 774          /**
 775           * Map camelCased position string (from block.json) to snake_cased block type position.
 776           *
 777           * @var array
 778           */
 779          $position_mappings = array(
 780              'before'     => 'before',
 781              'after'      => 'after',
 782              'firstChild' => 'first_child',
 783              'lastChild'  => 'last_child',
 784          );
 785  
 786          $settings['block_hooks'] = array();
 787          foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) {
 788              // Avoid infinite recursion (hooking to itself).
 789              if ( $metadata['name'] === $anchor_block_name ) {
 790                  _doing_it_wrong(
 791                      __METHOD__,
 792                      __( 'Cannot hook block to itself.' ),
 793                      '6.4.0'
 794                  );
 795                  continue;
 796              }
 797  
 798              if ( ! isset( $position_mappings[ $position ] ) ) {
 799                  continue;
 800              }
 801  
 802              $settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ];
 803          }
 804      }
 805  
 806      /**
 807       * Filters the settings determined from the block type metadata.
 808       *
 809       * @since 5.7.0
 810       *
 811       * @param array $settings Array of determined settings for registering a block type.
 812       * @param array $metadata Metadata provided for registering a block type.
 813       */
 814      $settings = apply_filters( 'block_type_metadata_settings', $settings, $metadata );
 815  
 816      $metadata['name'] = ! empty( $settings['name'] ) ? $settings['name'] : $metadata['name'];
 817  
 818      return WP_Block_Type_Registry::get_instance()->register(
 819          $metadata['name'],
 820          $settings
 821      );
 822  }
 823  
 824  /**
 825   * Registers a block type. The recommended way is to register a block type using
 826   * the metadata stored in the `block.json` file.
 827   *
 828   * @since 5.0.0
 829   * @since 5.8.0 First parameter now accepts a path to the `block.json` file.
 830   *
 831   * @param string|WP_Block_Type $block_type Block type name including namespace, or alternatively
 832   *                                         a path to the JSON file with metadata definition for the block,
 833   *                                         or a path to the folder where the `block.json` file is located,
 834   *                                         or a complete WP_Block_Type instance.
 835   *                                         In case a WP_Block_Type is provided, the $args parameter will be ignored.
 836   * @param array                $args       Optional. Array of block type arguments. Accepts any public property
 837   *                                         of `WP_Block_Type`. See WP_Block_Type::__construct() for information
 838   *                                         on accepted arguments. Default empty array.
 839   *
 840   * @return WP_Block_Type|false The registered block type on success, or false on failure.
 841   */
 842  function register_block_type( $block_type, $args = array() ) {
 843      if ( is_string( $block_type ) && file_exists( $block_type ) ) {
 844          return register_block_type_from_metadata( $block_type, $args );
 845      }
 846  
 847      return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
 848  }
 849  
 850  /**
 851   * Unregisters a block type.
 852   *
 853   * @since 5.0.0
 854   *
 855   * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
 856   *                                   a complete WP_Block_Type instance.
 857   * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
 858   */
 859  function unregister_block_type( $name ) {
 860      return WP_Block_Type_Registry::get_instance()->unregister( $name );
 861  }
 862  
 863  /**
 864   * Determines whether a post or content string has blocks.
 865   *
 866   * This test optimizes for performance rather than strict accuracy, detecting
 867   * the pattern of a block but not validating its structure. For strict accuracy,
 868   * you should use the block parser on post content.
 869   *
 870   * @since 5.0.0
 871   *
 872   * @see parse_blocks()
 873   *
 874   * @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object.
 875   *                                      Defaults to global $post.
 876   * @return bool Whether the post has blocks.
 877   */
 878  function has_blocks( $post = null ) {
 879      if ( ! is_string( $post ) ) {
 880          $wp_post = get_post( $post );
 881  
 882          if ( ! $wp_post instanceof WP_Post ) {
 883              return false;
 884          }
 885  
 886          $post = $wp_post->post_content;
 887      }
 888  
 889      return str_contains( (string) $post, '<!-- wp:' );
 890  }
 891  
 892  /**
 893   * Determines whether a $post or a string contains a specific block type.
 894   *
 895   * This test optimizes for performance rather than strict accuracy, detecting
 896   * whether the block type exists but not validating its structure and not checking
 897   * synced patterns (formerly called reusable blocks). For strict accuracy,
 898   * you should use the block parser on post content.
 899   *
 900   * @since 5.0.0
 901   *
 902   * @see parse_blocks()
 903   *
 904   * @param string                  $block_name Full block type to look for.
 905   * @param int|string|WP_Post|null $post       Optional. Post content, post ID, or post object.
 906   *                                            Defaults to global $post.
 907   * @return bool Whether the post content contains the specified block.
 908   */
 909  function has_block( $block_name, $post = null ) {
 910      if ( ! has_blocks( $post ) ) {
 911          return false;
 912      }
 913  
 914      if ( ! is_string( $post ) ) {
 915          $wp_post = get_post( $post );
 916          if ( $wp_post instanceof WP_Post ) {
 917              $post = $wp_post->post_content;
 918          }
 919      }
 920  
 921      /*
 922       * Normalize block name to include namespace, if provided as non-namespaced.
 923       * This matches behavior for WordPress 5.0.0 - 5.3.0 in matching blocks by
 924       * their serialized names.
 925       */
 926      if ( ! str_contains( $block_name, '/' ) ) {
 927          $block_name = 'core/' . $block_name;
 928      }
 929  
 930      // Test for existence of block by its fully qualified name.
 931      $has_block = str_contains( $post, '<!-- wp:' . $block_name . ' ' );
 932  
 933      if ( ! $has_block ) {
 934          /*
 935           * If the given block name would serialize to a different name, test for
 936           * existence by the serialized form.
 937           */
 938          $serialized_block_name = strip_core_block_namespace( $block_name );
 939          if ( $serialized_block_name !== $block_name ) {
 940              $has_block = str_contains( $post, '<!-- wp:' . $serialized_block_name . ' ' );
 941          }
 942      }
 943  
 944      return $has_block;
 945  }
 946  
 947  /**
 948   * Returns an array of the names of all registered dynamic block types.
 949   *
 950   * @since 5.0.0
 951   *
 952   * @return string[] Array of dynamic block names.
 953   */
 954  function get_dynamic_block_names() {
 955      $dynamic_block_names = array();
 956  
 957      $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
 958      foreach ( $block_types as $block_type ) {
 959          if ( $block_type->is_dynamic() ) {
 960              $dynamic_block_names[] = $block_type->name;
 961          }
 962      }
 963  
 964      return $dynamic_block_names;
 965  }
 966  
 967  /**
 968   * Retrieves block types hooked into the given block, grouped by anchor block type and the relative position.
 969   *
 970   * @since 6.4.0
 971   *
 972   * @return array[] Array of block types grouped by anchor block type and the relative position.
 973   */
 974  function get_hooked_blocks() {
 975      $block_types   = WP_Block_Type_Registry::get_instance()->get_all_registered();
 976      $hooked_blocks = array();
 977      foreach ( $block_types as $block_type ) {
 978          if ( ! ( $block_type instanceof WP_Block_Type ) || ! is_array( $block_type->block_hooks ) ) {
 979              continue;
 980          }
 981          foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
 982              if ( ! isset( $hooked_blocks[ $anchor_block_type ] ) ) {
 983                  $hooked_blocks[ $anchor_block_type ] = array();
 984              }
 985              if ( ! isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
 986                  $hooked_blocks[ $anchor_block_type ][ $relative_position ] = array();
 987              }
 988              $hooked_blocks[ $anchor_block_type ][ $relative_position ][] = $block_type->name;
 989          }
 990      }
 991  
 992      return $hooked_blocks;
 993  }
 994  
 995  /**
 996   * Returns the markup for blocks hooked to the given anchor block in a specific relative position.
 997   *
 998   * @since 6.5.0
 999   * @access private
1000   *
1001   * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
1002   * @param string                          $relative_position   The relative position of the hooked blocks.
1003   *                                                             Can be one of 'before', 'after', 'first_child', or 'last_child'.
1004   * @param array                           $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
1005   * @param WP_Block_Template|WP_Post|array $context             The block template, template part, or pattern that the anchor block belongs to.
1006   * @return string
1007   */
1008  function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
1009      $anchor_block_type  = $parsed_anchor_block['blockName'];
1010      $hooked_block_types = isset( $anchor_block_type, $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
1011          ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
1012          : array();
1013  
1014      /**
1015       * Filters the list of hooked block types for a given anchor block type and relative position.
1016       *
1017       * @since 6.4.0
1018       *
1019       * @param string[]                        $hooked_block_types The list of hooked block types.
1020       * @param string                          $relative_position  The relative position of the hooked blocks.
1021       *                                                            Can be one of 'before', 'after', 'first_child', or 'last_child'.
1022       * @param string                          $anchor_block_type  The anchor block type.
1023       * @param WP_Block_Template|WP_Post|array $context            The block template, template part, post object,
1024       *                                                            or pattern that the anchor block belongs to.
1025       */
1026      $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
1027  
1028      $markup = '';
1029      foreach ( $hooked_block_types as $hooked_block_type ) {
1030          $parsed_hooked_block = array(
1031              'blockName'    => $hooked_block_type,
1032              'attrs'        => array(),
1033              'innerBlocks'  => array(),
1034              'innerHTML'    => '',
1035              'innerContent' => array(),
1036          );
1037  
1038          /**
1039           * Filters the parsed block array for a given hooked block.
1040           *
1041           * @since 6.5.0
1042           *
1043           * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
1044           * @param string                          $hooked_block_type   The hooked block type name.
1045           * @param string                          $relative_position   The relative position of the hooked block.
1046           * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
1047           * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,
1048           *                                                             or pattern that the anchor block belongs to.
1049           */
1050          $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
1051  
1052          /**
1053           * Filters the parsed block array for a given hooked block.
1054           *
1055           * The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.
1056           *
1057           * @since 6.5.0
1058           *
1059           * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
1060           * @param string                          $hooked_block_type   The hooked block type name.
1061           * @param string                          $relative_position   The relative position of the hooked block.
1062           * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
1063           * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,
1064           *                                                             or pattern that the anchor block belongs to.
1065           */
1066          $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
1067  
1068          if ( null === $parsed_hooked_block ) {
1069              continue;
1070          }
1071  
1072          // It's possible that the filter returned a block of a different type, so we explicitly
1073          // look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
1074          if (
1075              ! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
1076              ! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
1077          ) {
1078              $markup .= serialize_block( $parsed_hooked_block );
1079          }
1080      }
1081  
1082      return $markup;
1083  }
1084  
1085  /**
1086   * Adds a list of hooked block types to an anchor block's ignored hooked block types.
1087   *
1088   * This function is meant for internal use only.
1089   *
1090   * @since 6.5.0
1091   * @access private
1092   *
1093   * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
1094   * @param string                          $relative_position   The relative position of the hooked blocks.
1095   *                                                             Can be one of 'before', 'after', 'first_child', or 'last_child'.
1096   * @param array                           $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
1097   * @param WP_Block_Template|WP_Post|array $context             The block template, template part, or pattern that the anchor block belongs to.
1098   * @return string Empty string.
1099   */
1100  function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
1101      $anchor_block_type  = $parsed_anchor_block['blockName'];
1102      $hooked_block_types = isset( $anchor_block_type, $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
1103          ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
1104          : array();
1105  
1106      /** This filter is documented in wp-includes/blocks.php */
1107      $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
1108      if ( empty( $hooked_block_types ) ) {
1109          return '';
1110      }
1111  
1112      foreach ( $hooked_block_types as $index => $hooked_block_type ) {
1113          $parsed_hooked_block = array(
1114              'blockName'    => $hooked_block_type,
1115              'attrs'        => array(),
1116              'innerBlocks'  => array(),
1117              'innerContent' => array(),
1118          );
1119  
1120          /** This filter is documented in wp-includes/blocks.php */
1121          $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
1122  
1123          /** This filter is documented in wp-includes/blocks.php */
1124          $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
1125  
1126          if ( null === $parsed_hooked_block ) {
1127              unset( $hooked_block_types[ $index ] );
1128          }
1129      }
1130  
1131      $previously_ignored_hooked_blocks = $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array();
1132  
1133      $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
1134          array_merge(
1135              $previously_ignored_hooked_blocks,
1136              $hooked_block_types
1137          )
1138      );
1139  
1140      // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
1141      return '';
1142  }
1143  
1144  /**
1145   * Runs the hooked blocks algorithm on the given content.
1146   *
1147   * @since 6.6.0
1148   * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered.
1149   * @since 6.8.0 Have the `$context` parameter default to `null`, in which case `get_post()` will be called to use the current post as context.
1150   * @access private
1151   *
1152   * @param string                               $content  Serialized content.
1153   * @param WP_Block_Template|WP_Post|array|null $context  A block template, template part, post object, or pattern
1154   *                                                       that the blocks belong to. If set to `null`, `get_post()`
1155   *                                                       will be called to use the current post as context.
1156   *                                                       Default: `null`.
1157   * @param callable                             $callback A function that will be called for each block to generate
1158   *                                                       the markup for a given list of blocks that are hooked to it.
1159   *                                                       Default: 'insert_hooked_blocks'.
1160   * @return string The serialized markup.
1161   */
1162  function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) {
1163      // Default to the current post if no context is provided.
1164      if ( null === $context ) {
1165          $context = get_post();
1166      }
1167  
1168      $hooked_blocks = get_hooked_blocks();
1169  
1170      $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
1171      $after_block_visitor  = null;
1172      if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
1173          $before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
1174          $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $context, $callback );
1175      }
1176  
1177      $block_allows_multiple_instances = array();
1178      /*
1179       * Remove hooked blocks from `$hooked_block_types` if they have `multiple` set to false and
1180       * are already present in `$content`.
1181       */
1182      foreach ( $hooked_blocks as $anchor_block_type => $relative_positions ) {
1183          foreach ( $relative_positions as $relative_position => $hooked_block_types ) {
1184              foreach ( $hooked_block_types as $index => $hooked_block_type ) {
1185                  $hooked_block_type_definition =
1186                      WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );
1187  
1188                  $block_allows_multiple_instances[ $hooked_block_type ] =
1189                      block_has_support( $hooked_block_type_definition, 'multiple', true );
1190  
1191                  if (
1192                      ! $block_allows_multiple_instances[ $hooked_block_type ] &&
1193                      has_block( $hooked_block_type, $content )
1194                  ) {
1195                      unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ][ $index ] );
1196                  }
1197              }
1198              if ( empty( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
1199                  unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] );
1200              }
1201          }
1202          if ( empty( $hooked_blocks[ $anchor_block_type ] ) ) {
1203              unset( $hooked_blocks[ $anchor_block_type ] );
1204          }
1205      }
1206  
1207      /*
1208       * We also need to cover the case where the hooked block is not present in
1209       * `$content` at first and we're allowed to insert it once -- but not again.
1210       */
1211      $suppress_single_instance_blocks = static function ( $hooked_block_types ) use ( &$block_allows_multiple_instances, $content ) {
1212          static $single_instance_blocks_present_in_content = array();
1213          foreach ( $hooked_block_types as $index => $hooked_block_type ) {
1214              if ( ! isset( $block_allows_multiple_instances[ $hooked_block_type ] ) ) {
1215                  $hooked_block_type_definition =
1216                      WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );
1217  
1218                  $block_allows_multiple_instances[ $hooked_block_type ] =
1219                      block_has_support( $hooked_block_type_definition, 'multiple', true );
1220              }
1221  
1222              if ( $block_allows_multiple_instances[ $hooked_block_type ] ) {
1223                  continue;
1224              }
1225  
1226              // The block doesn't allow multiple instances, so we need to check if it's already present.
1227              if (
1228                  in_array( $hooked_block_type, $single_instance_blocks_present_in_content, true ) ||
1229                  has_block( $hooked_block_type, $content )
1230              ) {
1231                  unset( $hooked_block_types[ $index ] );
1232              } else {
1233                  // We can insert the block once, but need to remember not to insert it again.
1234                  $single_instance_blocks_present_in_content[] = $hooked_block_type;
1235              }
1236          }
1237          return $hooked_block_types;
1238      };
1239      add_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );
1240      $content = traverse_and_serialize_blocks(
1241          parse_blocks( $content ),
1242          $before_block_visitor,
1243          $after_block_visitor
1244      );
1245      remove_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );
1246  
1247      return $content;
1248  }
1249  
1250  /**
1251   * Run the Block Hooks algorithm on a post object's content.
1252   *
1253   * This function is different from `apply_block_hooks_to_content` in that
1254   * it takes ignored hooked block information from the post's metadata into
1255   * account. This ensures that any blocks hooked as first or last child
1256   * of the block that corresponds to the post type are handled correctly.
1257   *
1258   * @since 6.8.0
1259   * @since 7.0.0 Added the `$ignored_hooked_blocks_at_root` parameter.
1260   * @access private
1261   *
1262   * @param string       $content  Serialized content.
1263   * @param WP_Post|null $post     A post object that the content belongs to. If set to `null`,
1264   *                               `get_post()` will be called to use the current post as context.
1265   *                               Default: `null`.
1266   * @param callable     $callback A function that will be called for each block to generate
1267   *                               the markup for a given list of blocks that are hooked to it.
1268   *                               Default: 'insert_hooked_blocks'.
1269   * @param array|null   $ignored_hooked_blocks_at_root A reference to an array that will be populated
1270   *                               with the ignored hooked blocks at the root level.
1271   *                               Default: `null`.
1272   * @return string The serialized markup.
1273   */
1274  function apply_block_hooks_to_content_from_post_object(
1275      $content,
1276      $post = null,
1277      $callback = 'insert_hooked_blocks',
1278      &$ignored_hooked_blocks_at_root = null
1279  ) {
1280      // Default to the current post if no context is provided.
1281      if ( null === $post ) {
1282          $post = get_post();
1283      }
1284  
1285      if ( ! $post instanceof WP_Post ) {
1286          return apply_block_hooks_to_content( $content, $post, $callback );
1287      }
1288  
1289      /*
1290       * If the content was created using the classic editor or using a single Classic block
1291       * (`core/freeform`), it might not contain any block markup at all.
1292       * However, we still might need to inject hooked blocks in the first child or last child
1293       * positions of the parent block. To be able to apply the Block Hooks algorithm, we wrap
1294       * the content in a `core/freeform` wrapper block.
1295       */
1296      if ( ! has_blocks( $content ) ) {
1297          $original_content = $content;
1298  
1299          $content_wrapped_in_classic_block = get_comment_delimited_block_content(
1300              'core/freeform',
1301              array(),
1302              $content
1303          );
1304  
1305          $content = $content_wrapped_in_classic_block;
1306      }
1307  
1308      $attributes = array();
1309  
1310      // If context is a post object, `ignoredHookedBlocks` information is stored in its post meta.
1311      $ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
1312      if ( ! empty( $ignored_hooked_blocks ) ) {
1313          $ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
1314          $attributes['metadata'] = array(
1315              'ignoredHookedBlocks' => $ignored_hooked_blocks,
1316          );
1317      }
1318  
1319      /*
1320       * We need to wrap the content in a temporary wrapper block with that metadata
1321       * so the Block Hooks algorithm can insert blocks that are hooked as first or last child
1322       * of the wrapper block.
1323       * To that end, we need to determine the wrapper block type based on the post type.
1324       */
1325      if ( 'wp_navigation' === $post->post_type ) {
1326          $wrapper_block_type = 'core/navigation';
1327      } elseif ( 'wp_block' === $post->post_type ) {
1328          $wrapper_block_type = 'core/block';
1329      } else {
1330          $wrapper_block_type = 'core/post-content';
1331      }
1332  
1333      $content = get_comment_delimited_block_content(
1334          $wrapper_block_type,
1335          $attributes,
1336          $content
1337      );
1338  
1339      /*
1340       * We need to avoid inserting any blocks hooked into the `before` and `after` positions
1341       * of the temporary wrapper block that we create to wrap the content.
1342       * See https://core.trac.wordpress.org/ticket/63287 for more details.
1343       */
1344      $suppress_blocks_from_insertion_before_and_after_wrapper_block = static function ( $hooked_block_types, $relative_position, $anchor_block_type ) use ( $wrapper_block_type ) {
1345          if (
1346              $wrapper_block_type === $anchor_block_type &&
1347              in_array( $relative_position, array( 'before', 'after' ), true )
1348          ) {
1349              return array();
1350          }
1351          return $hooked_block_types;
1352      };
1353  
1354      // Apply Block Hooks.
1355      add_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX, 3 );
1356      $content = apply_block_hooks_to_content( $content, $post, $callback );
1357      remove_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX );
1358  
1359      if ( null !== $ignored_hooked_blocks_at_root ) {
1360          // Check wrapper block's metadata for ignored hooked blocks at the root level, and populate the reference parameter if needed.
1361          $wrapper_block_markup = extract_serialized_parent_block( $content );
1362          $wrapper_block        = parse_blocks( $wrapper_block_markup )[0];
1363  
1364          if ( ! empty( $wrapper_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) {
1365              $ignored_hooked_blocks_at_root = $wrapper_block['attrs']['metadata']['ignoredHookedBlocks'];
1366          }
1367      }
1368  
1369      // Finally, we need to remove the temporary wrapper block.
1370      $content = remove_serialized_parent_block( $content );
1371  
1372      // If we wrapped the content in a `core/freeform` block, we also need to remove that.
1373      if ( ! empty( $content_wrapped_in_classic_block ) ) {
1374          /*
1375           * We cannot simply use remove_serialized_parent_block() here,
1376           * as that function assumes that the block wrapper is at the top level.
1377           * However, there might now be a hooked block inserted next to it
1378           * (as first or last child of the parent).
1379           */
1380          $content = str_replace( $content_wrapped_in_classic_block, $original_content, $content );
1381      }
1382  
1383      return $content;
1384  }
1385  
1386  /**
1387   * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks.
1388   *
1389   * @since 6.6.0
1390   * @access private
1391   *
1392   * @param string $serialized_block The serialized markup of a block and its inner blocks.
1393   * @return string The serialized markup of the inner blocks.
1394   */
1395  function remove_serialized_parent_block( $serialized_block ) {
1396      $start = strpos( $serialized_block, '-->' ) + strlen( '-->' );
1397      $end   = strrpos( $serialized_block, '<!--' );
1398      return substr( $serialized_block, $start, $end - $start );
1399  }
1400  
1401  /**
1402   * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the wrapper block.
1403   *
1404   * @since 6.7.0
1405   * @access private
1406   *
1407   * @see remove_serialized_parent_block()
1408   *
1409   * @param string $serialized_block The serialized markup of a block and its inner blocks.
1410   * @return string The serialized markup of the wrapper block.
1411   */
1412  function extract_serialized_parent_block( $serialized_block ) {
1413      $start = strpos( $serialized_block, '-->' ) + strlen( '-->' );
1414      $end   = strrpos( $serialized_block, '<!--' );
1415      return substr( $serialized_block, 0, $start ) . substr( $serialized_block, $end );
1416  }
1417  
1418  /**
1419   * Updates the wp_postmeta with the list of ignored hooked blocks
1420   * where the inner blocks are stored as post content.
1421   *
1422   * @since 6.6.0
1423   * @since 6.8.0 Support non-`wp_navigation` post types.
1424   * @access private
1425   *
1426   * @param stdClass $post Post object.
1427   * @return stdClass The updated post object.
1428   */
1429  function update_ignored_hooked_blocks_postmeta( $post ) {
1430      /*
1431       * In this scenario the user has likely tried to create a new post object via the REST API.
1432       * In which case we won't have a post ID to work with and store meta against.
1433       */
1434      if ( empty( $post->ID ) ) {
1435          return $post;
1436      }
1437  
1438      /*
1439       * Skip meta generation when consumers intentionally update specific fields
1440       * and omit the content update.
1441       */
1442      if ( ! isset( $post->post_content ) ) {
1443          return $post;
1444      }
1445  
1446      /*
1447       * Skip meta generation if post type is not set.
1448       */
1449      if ( ! isset( $post->post_type ) ) {
1450          return $post;
1451      }
1452  
1453      $attributes = array();
1454  
1455      $ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
1456      if ( ! empty( $ignored_hooked_blocks ) ) {
1457          $ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
1458          $attributes['metadata'] = array(
1459              'ignoredHookedBlocks' => $ignored_hooked_blocks,
1460          );
1461      }
1462  
1463      if ( 'wp_navigation' === $post->post_type ) {
1464          $wrapper_block_type = 'core/navigation';
1465      } elseif ( 'wp_block' === $post->post_type ) {
1466          $wrapper_block_type = 'core/block';
1467      } else {
1468          $wrapper_block_type = 'core/post-content';
1469      }
1470  
1471      $markup = get_comment_delimited_block_content(
1472          $wrapper_block_type,
1473          $attributes,
1474          $post->post_content
1475      );
1476  
1477      $existing_post = get_post( $post->ID );
1478      // Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
1479      $context          = (object) array_merge( (array) $existing_post, (array) $post );
1480      $context          = new WP_Post( $context ); // Convert to WP_Post object.
1481      $serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
1482      $root_block       = parse_blocks( $serialized_block )[0];
1483  
1484      $ignored_hooked_blocks = $root_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array();
1485  
1486      if ( ! empty( $ignored_hooked_blocks ) ) {
1487          $existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
1488          if ( ! empty( $existing_ignored_hooked_blocks ) ) {
1489              $existing_ignored_hooked_blocks = json_decode( $existing_ignored_hooked_blocks, true );
1490              $ignored_hooked_blocks          = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) );
1491          }
1492  
1493          if ( ! isset( $post->meta_input ) ) {
1494              $post->meta_input = array();
1495          }
1496          $post->meta_input['_wp_ignored_hooked_blocks'] = json_encode( $ignored_hooked_blocks );
1497      }
1498  
1499      $post->post_content = remove_serialized_parent_block( $serialized_block );
1500      return $post;
1501  }
1502  
1503  /**
1504   * Returns the markup for blocks hooked to the given anchor block in a specific relative position and then
1505   * adds a list of hooked block types to an anchor block's ignored hooked block types.
1506   *
1507   * This function is meant for internal use only.
1508   *
1509   * @since 6.6.0
1510   * @access private
1511   *
1512   * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
1513   * @param string                          $relative_position   The relative position of the hooked blocks.
1514   *                                                             Can be one of 'before', 'after', 'first_child', or 'last_child'.
1515   * @param array                           $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
1516   * @param WP_Block_Template|WP_Post|array $context             The block template, template part, or pattern that the anchor block belongs to.
1517   * @return string
1518   */
1519  function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
1520      $markup  = insert_hooked_blocks( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
1521      $markup .= set_ignored_hooked_blocks_metadata( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
1522  
1523      return $markup;
1524  }
1525  
1526  /**
1527   * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
1528   *
1529   * @since 6.6.0
1530   * @since 6.8.0 Support non-`wp_navigation` post types.
1531   * @since 7.0.0 Set `_wp_ignored_hooked_blocks` meta in the response for blocks hooked at the root level.
1532   *
1533   * @param WP_REST_Response $response The response object.
1534   * @param WP_Post          $post     Post object.
1535   * @return WP_REST_Response The response object.
1536   */
1537  function insert_hooked_blocks_into_rest_response( $response, $post ) {
1538      if ( empty( $response->data['content']['raw'] ) ) {
1539          return $response;
1540      }
1541  
1542      $ignored_hooked_blocks_at_root    = array();
1543      $response->data['content']['raw'] = apply_block_hooks_to_content_from_post_object(
1544          $response->data['content']['raw'],
1545          $post,
1546          'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata',
1547          $ignored_hooked_blocks_at_root
1548      );
1549  
1550      if ( ! empty( $ignored_hooked_blocks_at_root ) ) {
1551          $response->data['meta']['_wp_ignored_hooked_blocks'] = wp_json_encode( $ignored_hooked_blocks_at_root );
1552      }
1553  
1554      // If the rendered content was previously empty, we leave it like that.
1555      if ( empty( $response->data['content']['rendered'] ) ) {
1556          return $response;
1557      }
1558  
1559      // `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter.
1560      $priority = has_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object' );
1561      if ( false !== $priority ) {
1562          remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
1563      }
1564  
1565      /** This filter is documented in wp-includes/post-template.php */
1566      $response->data['content']['rendered'] = apply_filters(
1567          'the_content',
1568          $response->data['content']['raw']
1569      );
1570  
1571      // Restore the filter if it was set initially.
1572      if ( false !== $priority ) {
1573          add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
1574      }
1575  
1576      return $response;
1577  }
1578  
1579  /**
1580   * Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
1581   *
1582   * The returned function can be used as `$pre_callback` argument to `traverse_and_serialize_block(s)`,
1583   * where it will inject the `theme` attribute into all Template Part blocks, and prepend the markup for
1584   * any blocks hooked `before` the given block and as its parent's `first_child`, respectively.
1585   *
1586   * This function is meant for internal use only.
1587   *
1588   * @since 6.4.0
1589   * @since 6.5.0 Added $callback argument.
1590   * @access private
1591   *
1592   * @param array                           $hooked_blocks An array of blocks hooked to another given block.
1593   * @param WP_Block_Template|WP_Post|array $context       A block template, template part, post object,
1594   *                                                       or pattern that the blocks belong to.
1595   * @param callable                        $callback      A function that will be called for each block to generate
1596   *                                                       the markup for a given list of blocks that are hooked to it.
1597   *                                                       Default: 'insert_hooked_blocks'.
1598   * @return callable A function that returns the serialized markup for the given block,
1599   *                  including the markup for any hooked blocks before it.
1600   */
1601  function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
1602      /**
1603       * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
1604       *
1605       * If the current block is a Template Part block, inject the `theme` attribute.
1606       * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
1607       * `first_child`, respectively, to the serialized markup for the given block.
1608       *
1609       * @param array $block        The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
1610       * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
1611       * @param array $prev         The previous sibling block of the given block. Default null.
1612       * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
1613       */
1614      return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
1615          _inject_theme_attribute_in_template_part_block( $block );
1616  
1617          $markup = '';
1618  
1619          if ( $parent_block && ! $prev ) {
1620              // Candidate for first-child insertion.
1621              $markup .= call_user_func_array(
1622                  $callback,
1623                  array( &$parent_block, 'first_child', $hooked_blocks, $context )
1624              );
1625          }
1626  
1627          $markup .= call_user_func_array(
1628              $callback,
1629              array( &$block, 'before', $hooked_blocks, $context )
1630          );
1631  
1632          return $markup;
1633      };
1634  }
1635  
1636  /**
1637   * Returns a function that injects the hooked blocks after a given block.
1638   *
1639   * The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`,
1640   * where it will append the markup for any blocks hooked `after` the given block and as its parent's
1641   * `last_child`, respectively.
1642   *
1643   * This function is meant for internal use only.
1644   *
1645   * @since 6.4.0
1646   * @since 6.5.0 Added $callback argument.
1647   * @access private
1648   *
1649   * @param array                           $hooked_blocks An array of blocks hooked to another block.
1650   * @param WP_Block_Template|WP_Post|array $context       A block template, template part, post object,
1651   *                                                       or pattern that the blocks belong to.
1652   * @param callable                        $callback      A function that will be called for each block to generate
1653   *                                                       the markup for a given list of blocks that are hooked to it.
1654   *                                                       Default: 'insert_hooked_blocks'.
1655   * @return callable A function that returns the serialized markup for the given block,
1656   *                  including the markup for any hooked blocks after it.
1657   */
1658  function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
1659      /**
1660       * Injects hooked blocks after the given block, and returns the serialized markup.
1661       *
1662       * Append the markup for any blocks hooked `after` the given block and as its parent's
1663       * `last_child`, respectively, to the serialized markup for the given block.
1664       *
1665       * @param array $block        The block to inject the hooked blocks after. Passed by reference.
1666       * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
1667       * @param array $next         The next sibling block of the given block. Default null.
1668       * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
1669       */
1670      return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
1671          $markup = call_user_func_array(
1672              $callback,
1673              array( &$block, 'after', $hooked_blocks, $context )
1674          );
1675  
1676          if ( $parent_block && ! $next ) {
1677              // Candidate for last-child insertion.
1678              $markup .= call_user_func_array(
1679                  $callback,
1680                  array( &$parent_block, 'last_child', $hooked_blocks, $context )
1681              );
1682          }
1683  
1684          return $markup;
1685      };
1686  }
1687  
1688  /**
1689   * Given an array of attributes, returns a string in the serialized attributes
1690   * format prepared for post content.
1691   *
1692   * The serialized result is a JSON-encoded string, with unicode escape sequence
1693   * substitution for characters which might otherwise interfere with embedding
1694   * the result in an HTML comment.
1695   *
1696   * This function must produce output that remains in sync with the output of
1697   * the serializeAttributes JavaScript function in the block editor in order
1698   * to ensure consistent operation between PHP and JavaScript.
1699   *
1700   * @since 5.3.1
1701   *
1702   * @param array $block_attributes Attributes object.
1703   * @return string Serialized attributes.
1704   */
1705  function serialize_block_attributes( $block_attributes ) {
1706      $encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
1707  
1708      return strtr(
1709          $encoded_attributes,
1710          array(
1711              '\\\\' => '\\u005c',
1712              '--'   => '\\u002d\\u002d',
1713              '<'    => '\\u003c',
1714              '>'    => '\\u003e',
1715              '&'    => '\\u0026',
1716              '\\"'  => '\\u0022',
1717          )
1718      );
1719  }
1720  
1721  /**
1722   * Returns the block name to use for serialization. This will remove the default
1723   * "core/" namespace from a block name.
1724   *
1725   * @since 5.3.1
1726   *
1727   * @param string|null $block_name Optional. Original block name. Null if the block name is unknown,
1728   *                                e.g. Classic blocks have their name set to null. Default null.
1729   * @return string Block name to use for serialization.
1730   */
1731  function strip_core_block_namespace( $block_name = null ) {
1732      if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
1733          return substr( $block_name, 5 );
1734      }
1735  
1736      return $block_name;
1737  }
1738  
1739  /**
1740   * Returns the content of a block, including comment delimiters.
1741   *
1742   * @since 5.3.1
1743   *
1744   * @param string|null $block_name       Block name. Null if the block name is unknown,
1745   *                                      e.g. Classic blocks have their name set to null.
1746   * @param array       $block_attributes Block attributes.
1747   * @param string      $block_content    Block save content.
1748   * @return string Comment-delimited block content.
1749   */
1750  function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) {
1751      if ( is_null( $block_name ) ) {
1752          return $block_content;
1753      }
1754  
1755      $serialized_block_name = strip_core_block_namespace( $block_name );
1756      $serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' ';
1757  
1758      if ( empty( $block_content ) ) {
1759          return sprintf( '<!-- wp:%s %s/-->', $serialized_block_name, $serialized_attributes );
1760      }
1761  
1762      return sprintf(
1763          '<!-- wp:%s %s-->%s<!-- /wp:%s -->',
1764          $serialized_block_name,
1765          $serialized_attributes,
1766          $block_content,
1767          $serialized_block_name
1768      );
1769  }
1770  
1771  /**
1772   * Returns the content of a block, including comment delimiters, serializing all
1773   * attributes from the given parsed block.
1774   *
1775   * This should be used when preparing a block to be saved to post content.
1776   * Prefer `render_block` when preparing a block for display. Unlike
1777   * `render_block`, this does not evaluate a block's `render_callback`, and will
1778   * instead preserve the markup as parsed.
1779   *
1780   * @since 5.3.1
1781   *
1782   * @param array $block {
1783   *     An associative array of a single parsed block object. See WP_Block_Parser_Block.
1784   *
1785   *     @type string|null $blockName    Name of block.
1786   *     @type array       $attrs        Attributes from block comment delimiters.
1787   *     @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
1788   *                                     have the same structure as this one.
1789   *     @type string      $innerHTML    HTML from inside block comment delimiters.
1790   *     @type array       $innerContent List of string fragments and null markers where
1791   *                                     inner blocks were found.
1792   * }
1793   * @return string String of rendered HTML.
1794   */
1795  function serialize_block( $block ) {
1796      $block_content = '';
1797  
1798      $index = 0;
1799      foreach ( $block['innerContent'] as $chunk ) {
1800          $block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
1801      }
1802  
1803      if ( ! is_array( $block['attrs'] ) ) {
1804          $block['attrs'] = array();
1805      }
1806  
1807      return get_comment_delimited_block_content(
1808          $block['blockName'],
1809          $block['attrs'],
1810          $block_content
1811      );
1812  }
1813  
1814  /**
1815   * Returns a joined string of the aggregate serialization of the given
1816   * parsed blocks.
1817   *
1818   * @since 5.3.1
1819   *
1820   * @param array[] $blocks {
1821   *     Array of block structures.
1822   *
1823   *     @type array ...$0 {
1824   *         An associative array of a single parsed block object. See WP_Block_Parser_Block.
1825   *
1826   *         @type string|null $blockName    Name of block.
1827   *         @type array       $attrs        Attributes from block comment delimiters.
1828   *         @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
1829   *                                         have the same structure as this one.
1830   *         @type string      $innerHTML    HTML from inside block comment delimiters.
1831   *         @type array       $innerContent List of string fragments and null markers where
1832   *                                         inner blocks were found.
1833   *     }
1834   * }
1835   * @return string String of rendered HTML.
1836   */
1837  function serialize_blocks( $blocks ) {
1838      return implode( '', array_map( 'serialize_block', $blocks ) );
1839  }
1840  
1841  /**
1842   * Traverses a parsed block tree and applies callbacks before and after serializing it.
1843   *
1844   * Recursively traverses the block and its inner blocks and applies the two callbacks provided as
1845   * arguments, the first one before serializing the block, and the second one after serializing it.
1846   * If either callback returns a string value, it will be prepended and appended to the serialized
1847   * block markup, respectively.
1848   *
1849   * The callbacks will receive a reference to the current block as their first argument, so that they
1850   * can also modify it, and the current block's parent block as second argument. Finally, the
1851   * `$pre_callback` receives the previous block, whereas the `$post_callback` receives
1852   * the next block as third argument.
1853   *
1854   * Serialized blocks are returned including comment delimiters, and with all attributes serialized.
1855   *
1856   * This function should be used when there is a need to modify the saved block, or to inject markup
1857   * into the return value. Prefer `serialize_block` when preparing a block to be saved to post content.
1858   *
1859   * This function is meant for internal use only.
1860   *
1861   * @since 6.4.0
1862   * @access private
1863   *
1864   * @see serialize_block()
1865   *
1866   * @param array    $block         An associative array of a single parsed block object. See WP_Block_Parser_Block.
1867   * @param callable $pre_callback  Callback to run on each block in the tree before it is traversed and serialized.
1868   *                                It is called with the following arguments: &$block, $parent_block, $previous_block.
1869   *                                Its string return value will be prepended to the serialized block markup.
1870   * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
1871   *                                It is called with the following arguments: &$block, $parent_block, $next_block.
1872   *                                Its string return value will be appended to the serialized block markup.
1873   * @return string Serialized block markup.
1874   */
1875  function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) {
1876      $block_content = '';
1877      $block_index   = 0;
1878  
1879      foreach ( $block['innerContent'] as $chunk ) {
1880          if ( is_string( $chunk ) ) {
1881              $block_content .= $chunk;
1882          } else {
1883              $inner_block = $block['innerBlocks'][ $block_index ];
1884  
1885              if ( is_callable( $pre_callback ) ) {
1886                  $prev = 0 === $block_index
1887                      ? null
1888                      : $block['innerBlocks'][ $block_index - 1 ];
1889  
1890                  $block_content .= call_user_func_array(
1891                      $pre_callback,
1892                      array( &$inner_block, &$block, $prev )
1893                  );
1894              }
1895  
1896              if ( is_callable( $post_callback ) ) {
1897                  $next = count( $block['innerBlocks'] ) - 1 === $block_index
1898                      ? null
1899                      : $block['innerBlocks'][ $block_index + 1 ];
1900  
1901                  $post_markup = call_user_func_array(
1902                      $post_callback,
1903                      array( &$inner_block, &$block, $next )
1904                  );
1905              }
1906  
1907              $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
1908              $block_content .= $post_markup ?? '';
1909  
1910              ++$block_index;
1911          }
1912      }
1913  
1914      if ( ! is_array( $block['attrs'] ) ) {
1915          $block['attrs'] = array();
1916      }
1917  
1918      return get_comment_delimited_block_content(
1919          $block['blockName'],
1920          $block['attrs'],
1921          $block_content
1922      );
1923  }
1924  
1925  /**
1926   * Replaces patterns in a block tree with their content.
1927   *
1928   * @since 6.6.0
1929   * @since 7.0.0 Adds metadata to attributes of single-pattern container blocks.
1930   *
1931   * @param array $blocks An array blocks.
1932   *
1933   * @return array An array of blocks with patterns replaced by their content.
1934   */
1935  function resolve_pattern_blocks( $blocks ) {
1936      static $inner_content;
1937      // Keep track of seen references to avoid infinite loops.
1938      static $seen_refs = array();
1939      $i                = 0;
1940      while ( $i < count( $blocks ) ) {
1941          if ( 'core/pattern' === $blocks[ $i ]['blockName'] ) {
1942              $attrs = $blocks[ $i ]['attrs'];
1943  
1944              if ( empty( $attrs['slug'] ) ) {
1945                  ++$i;
1946                  continue;
1947              }
1948  
1949              $slug = $attrs['slug'];
1950  
1951              if ( isset( $seen_refs[ $slug ] ) ) {
1952                  // Skip recursive patterns.
1953                  array_splice( $blocks, $i, 1 );
1954                  continue;
1955              }
1956  
1957              $registry = WP_Block_Patterns_Registry::get_instance();
1958              $pattern  = $registry->get_registered( $slug );
1959  
1960              // Skip unknown patterns.
1961              if ( ! $pattern ) {
1962                  ++$i;
1963                  continue;
1964              }
1965  
1966              $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
1967  
1968              /*
1969               * For single-root patterns, add the pattern name to make this a pattern instance in the editor.
1970               * If the pattern has metadata, merge it with the existing metadata.
1971               */
1972              if ( count( $blocks_to_insert ) === 1 ) {
1973                  $block_metadata                = $blocks_to_insert[0]['attrs']['metadata'] ?? array();
1974                  $block_metadata['patternName'] = $slug;
1975  
1976                  /*
1977                   * Merge pattern metadata with existing block metadata.
1978                   * Pattern metadata takes precedence, but existing block metadata
1979                   * is preserved as a fallback when the pattern doesn't define that field.
1980                   * Only the defined fields (name, description, categories) are updated;
1981                   * other metadata keys are preserved.
1982                   */
1983                  foreach ( array(
1984                      'name'        => 'title', // 'title' is the field in the pattern object 'name' is the field in the block metadata.
1985                      'description' => 'description',
1986                      'categories'  => 'categories',
1987                  ) as $key => $pattern_key ) {
1988                      $value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null;
1989                      if ( $value ) {
1990                          $block_metadata[ $key ] = is_array( $value )
1991                              ? array_map( 'sanitize_text_field', $value )
1992                              : sanitize_text_field( $value );
1993                      }
1994                  }
1995  
1996                  $blocks_to_insert[0]['attrs']['metadata'] = $block_metadata;
1997              }
1998  
1999              $seen_refs[ $slug ] = true;
2000              $prev_inner_content = $inner_content;
2001              $inner_content      = null;
2002              $blocks_to_insert   = resolve_pattern_blocks( $blocks_to_insert );
2003              $inner_content      = $prev_inner_content;
2004              unset( $seen_refs[ $slug ] );
2005              array_splice( $blocks, $i, 1, $blocks_to_insert );
2006  
2007              // If we have inner content, we need to insert nulls in the
2008              // inner content array, otherwise serialize_blocks will skip
2009              // blocks.
2010              if ( $inner_content ) {
2011                  $null_indices  = array_keys( $inner_content, null, true );
2012                  $content_index = $null_indices[ $i ];
2013                  $nulls         = array_fill( 0, count( $blocks_to_insert ), null );
2014                  array_splice( $inner_content, $content_index, 1, $nulls );
2015              }
2016  
2017              // Skip inserted blocks.
2018              $i += count( $blocks_to_insert );
2019          } else {
2020              if ( ! empty( $blocks[ $i ]['innerBlocks'] ) ) {
2021                  $prev_inner_content           = $inner_content;
2022                  $inner_content                = $blocks[ $i ]['innerContent'];
2023                  $blocks[ $i ]['innerBlocks']  = resolve_pattern_blocks(
2024                      $blocks[ $i ]['innerBlocks']
2025                  );
2026                  $blocks[ $i ]['innerContent'] = $inner_content;
2027                  $inner_content                = $prev_inner_content;
2028              }
2029              ++$i;
2030          }
2031      }
2032      return $blocks;
2033  }
2034  
2035  /**
2036   * Given an array of parsed block trees, applies callbacks before and after serializing them and
2037   * returns their concatenated output.
2038   *
2039   * Recursively traverses the blocks and their inner blocks and applies the two callbacks provided as
2040   * arguments, the first one before serializing a block, and the second one after serializing.
2041   * If either callback returns a string value, it will be prepended and appended to the serialized
2042   * block markup, respectively.
2043   *
2044   * The callbacks will receive a reference to the current block as their first argument, so that they
2045   * can also modify it, and the current block's parent block as second argument. Finally, the
2046   * `$pre_callback` receives the previous block, whereas the `$post_callback` receives
2047   * the next block as third argument.
2048   *
2049   * Serialized blocks are returned including comment delimiters, and with all attributes serialized.
2050   *
2051   * This function should be used when there is a need to modify the saved blocks, or to inject markup
2052   * into the return value. Prefer `serialize_blocks` when preparing blocks to be saved to post content.
2053   *
2054   * This function is meant for internal use only.
2055   *
2056   * @since 6.4.0
2057   * @access private
2058   *
2059   * @see serialize_blocks()
2060   *
2061   * @param array[]  $blocks        An array of parsed blocks. See WP_Block_Parser_Block.
2062   * @param callable $pre_callback  Callback to run on each block in the tree before it is traversed and serialized.
2063   *                                It is called with the following arguments: &$block, $parent_block, $previous_block.
2064   *                                Its string return value will be prepended to the serialized block markup.
2065   * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
2066   *                                It is called with the following arguments: &$block, $parent_block, $next_block.
2067   *                                Its string return value will be appended to the serialized block markup.
2068   * @return string Serialized block markup.
2069   */
2070  function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
2071      $result       = '';
2072      $parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference.
2073  
2074      $pre_callback_is_callable  = is_callable( $pre_callback );
2075      $post_callback_is_callable = is_callable( $post_callback );
2076  
2077      foreach ( $blocks as $index => $block ) {
2078          if ( $pre_callback_is_callable ) {
2079              $prev = 0 === $index
2080                  ? null
2081                  : $blocks[ $index - 1 ];
2082  
2083              $result .= call_user_func_array(
2084                  $pre_callback,
2085                  array( &$block, &$parent_block, $prev )
2086              );
2087          }
2088  
2089          if ( $post_callback_is_callable ) {
2090              $next = count( $blocks ) - 1 === $index
2091                  ? null
2092                  : $blocks[ $index + 1 ];
2093  
2094              $post_markup = call_user_func_array(
2095                  $post_callback,
2096                  array( &$block, &$parent_block, $next )
2097              );
2098          }
2099  
2100          $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
2101          $result .= $post_markup ?? '';
2102      }
2103  
2104      return $result;
2105  }
2106  
2107  /**
2108   * Filters and sanitizes block content to remove non-allowable HTML
2109   * from parsed block attribute values.
2110   *
2111   * @since 5.3.1
2112   *
2113   * @param string         $text              Text that may contain block content.
2114   * @param array[]|string $allowed_html      Optional. An array of allowed HTML elements and attributes,
2115   *                                          or a context name such as 'post'. See wp_kses_allowed_html()
2116   *                                          for the list of accepted context names. Default 'post'.
2117   * @param string[]       $allowed_protocols Optional. Array of allowed URL protocols.
2118   *                                          Defaults to the result of wp_allowed_protocols().
2119   * @return string The filtered and sanitized content result.
2120   */
2121  function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) {
2122      $result = '';
2123  
2124      if ( str_contains( $text, '<!--' ) && str_contains( $text, '--->' ) ) {
2125          $text = preg_replace_callback( '%<!--(.*?)--->%', '_filter_block_content_callback', $text );
2126      }
2127  
2128      $blocks = parse_blocks( $text );
2129      foreach ( $blocks as $block ) {
2130          $block   = filter_block_kses( $block, $allowed_html, $allowed_protocols );
2131          $result .= serialize_block( $block );
2132      }
2133  
2134      return $result;
2135  }
2136  
2137  /**
2138   * Callback used for regular expression replacement in filter_block_content().
2139   *
2140   * @since 6.2.1
2141   * @access private
2142   *
2143   * @param array $matches Array of preg_replace_callback matches.
2144   * @return string Replacement string.
2145   */
2146  function _filter_block_content_callback( $matches ) {
2147      return '<!--' . rtrim( $matches[1], '-' ) . '-->';
2148  }
2149  
2150  /**
2151   * Filters and sanitizes a parsed block to remove non-allowable HTML
2152   * from block attribute values.
2153   *
2154   * @since 5.3.1
2155   *
2156   * @param WP_Block_Parser_Block $block             The parsed block object.
2157   * @param array[]|string        $allowed_html      An array of allowed HTML elements and attributes,
2158   *                                                 or a context name such as 'post'. See wp_kses_allowed_html()
2159   *                                                 for the list of accepted context names.
2160   * @param string[]              $allowed_protocols Optional. Array of allowed URL protocols.
2161   *                                                 Defaults to the result of wp_allowed_protocols().
2162   * @return array The filtered and sanitized block object result.
2163   */
2164  function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) {
2165      $block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols, $block );
2166  
2167      if ( is_array( $block['innerBlocks'] ) ) {
2168          foreach ( $block['innerBlocks'] as $i => $inner_block ) {
2169              $block['innerBlocks'][ $i ] = filter_block_kses( $inner_block, $allowed_html, $allowed_protocols );
2170          }
2171      }
2172  
2173      return $block;
2174  }
2175  
2176  /**
2177   * Filters and sanitizes a parsed block attribute value to remove
2178   * non-allowable HTML.
2179   *
2180   * @since 5.3.1
2181   * @since 6.5.5 Added the `$block_context` parameter.
2182   *
2183   * @param string[]|string $value             The attribute value to filter.
2184   * @param array[]|string  $allowed_html      An array of allowed HTML elements and attributes,
2185   *                                           or a context name such as 'post'. See wp_kses_allowed_html()
2186   *                                           for the list of accepted context names.
2187   * @param string[]        $allowed_protocols Optional. Array of allowed URL protocols.
2188   *                                           Defaults to the result of wp_allowed_protocols().
2189   * @param array           $block_context     Optional. The block the attribute belongs to, in parsed block array format.
2190   * @return string[]|string The filtered and sanitized result.
2191   */
2192  function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array(), $block_context = null ) {
2193      if ( is_array( $value ) ) {
2194          foreach ( $value as $key => $inner_value ) {
2195              $filtered_key   = filter_block_kses_value( $key, $allowed_html, $allowed_protocols, $block_context );
2196              $filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols, $block_context );
2197  
2198              if ( isset( $block_context['blockName'] ) && 'core/template-part' === $block_context['blockName'] ) {
2199                  $filtered_value = filter_block_core_template_part_attributes( $filtered_value, $filtered_key, $allowed_html );
2200              }
2201              if ( $filtered_key !== $key ) {
2202                  unset( $value[ $key ] );
2203              }
2204  
2205              $value[ $filtered_key ] = $filtered_value;
2206          }
2207      } elseif ( is_string( $value ) ) {
2208          return wp_kses( $value, $allowed_html, $allowed_protocols );
2209      }
2210  
2211      return $value;
2212  }
2213  
2214  /**
2215   * Sanitizes the value of the Template Part block's `tagName` attribute.
2216   *
2217   * @since 6.5.5
2218   *
2219   * @param string         $attribute_value The attribute value to filter.
2220   * @param string         $attribute_name  The attribute name.
2221   * @param array[]|string $allowed_html    An array of allowed HTML elements and attributes,
2222   *                                        or a context name such as 'post'. See wp_kses_allowed_html()
2223   *                                        for the list of accepted context names.
2224   * @return string The sanitized attribute value.
2225   */
2226  function filter_block_core_template_part_attributes( $attribute_value, $attribute_name, $allowed_html ) {
2227      if ( empty( $attribute_value ) || 'tagName' !== $attribute_name ) {
2228          return $attribute_value;
2229      }
2230      if ( ! is_array( $allowed_html ) ) {
2231          $allowed_html = wp_kses_allowed_html( $allowed_html );
2232      }
2233      return isset( $allowed_html[ $attribute_value ] ) ? $attribute_value : '';
2234  }
2235  
2236  /**
2237   * Parses blocks out of a content string, and renders those appropriate for the excerpt.
2238   *
2239   * As the excerpt should be a small string of text relevant to the full post content,
2240   * this function renders the blocks that are most likely to contain such text.
2241   *
2242   * @since 5.0.0
2243   *
2244   * @param string $content The content to parse.
2245   * @return string The parsed and filtered content.
2246   */
2247  function excerpt_remove_blocks( $content ) {
2248      if ( ! has_blocks( $content ) ) {
2249          return $content;
2250      }
2251  
2252      $allowed_inner_blocks = array(
2253          // Classic blocks have their blockName set to null.
2254          null,
2255          'core/freeform',
2256          'core/heading',
2257          'core/html',
2258          'core/list',
2259          'core/media-text',
2260          'core/paragraph',
2261          'core/preformatted',
2262          'core/pullquote',
2263          'core/quote',
2264          'core/table',
2265          'core/verse',
2266      );
2267  
2268      $allowed_wrapper_blocks = array(
2269          'core/columns',
2270          'core/column',
2271          'core/group',
2272      );
2273  
2274      /**
2275       * Filters the list of blocks that can be used as wrapper blocks, allowing
2276       * excerpts to be generated from the `innerBlocks` of these wrappers.
2277       *
2278       * @since 5.8.0
2279       *
2280       * @param string[] $allowed_wrapper_blocks The list of names of allowed wrapper blocks.
2281       */
2282      $allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );
2283  
2284      $allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );
2285  
2286      /**
2287       * Filters the list of blocks that can contribute to the excerpt.
2288       *
2289       * If a dynamic block is added to this list, it must not generate another
2290       * excerpt, as this will cause an infinite loop to occur.
2291       *
2292       * @since 5.0.0
2293       *
2294       * @param string[] $allowed_blocks The list of names of allowed blocks.
2295       */
2296      $allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
2297      $blocks         = parse_blocks( $content );
2298      $output         = '';
2299  
2300      foreach ( $blocks as $block ) {
2301          // Hide the block whenever the value is boolean false, regardless of the
2302          // block's current visibility support. This prevents blocks that previously
2303          // supported visibility from unintentionally appearing on the front end
2304          // after their support was disabled.
2305          if ( false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
2306              continue;
2307          }
2308  
2309          if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
2310              if ( ! empty( $block['innerBlocks'] ) ) {
2311                  if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
2312                      $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
2313                      continue;
2314                  }
2315  
2316                  // Skip the block if it has disallowed or nested inner blocks.
2317                  foreach ( $block['innerBlocks'] as $inner_block ) {
2318                      if (
2319                          ! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
2320                          ! empty( $inner_block['innerBlocks'] )
2321                      ) {
2322                          continue 2;
2323                      }
2324                  }
2325              }
2326  
2327              $output .= render_block( $block );
2328          }
2329      }
2330  
2331      return $output;
2332  }
2333  
2334  /**
2335   * Parses footnotes markup out of a content string,
2336   * and renders those appropriate for the excerpt.
2337   *
2338   * @since 6.3.0
2339   *
2340   * @param string $content The content to parse.
2341   * @return string The parsed and filtered content.
2342   */
2343  function excerpt_remove_footnotes( $content ) {
2344      if ( ! str_contains( $content, 'data-fn=' ) ) {
2345          return $content;
2346      }
2347  
2348      return preg_replace(
2349          '_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_',
2350          '',
2351          $content
2352      );
2353  }
2354  
2355  /**
2356   * Renders inner blocks from the allowed wrapper blocks
2357   * for generating an excerpt.
2358   *
2359   * @since 5.8.0
2360   * @access private
2361   *
2362   * @param array $parsed_block   The parsed block.
2363   * @param array $allowed_blocks The list of allowed inner blocks.
2364   * @return string The rendered inner blocks.
2365   */
2366  function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
2367      $output = '';
2368  
2369      foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
2370          // Hide the block whenever the value is boolean false, regardless of the
2371          // block's current visibility support. This prevents blocks that previously
2372          // supported visibility from unintentionally appearing on the front end
2373          // after their support was disabled.
2374          if ( false === ( $inner_block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
2375              continue;
2376          }
2377  
2378          if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
2379              continue;
2380          }
2381  
2382          if ( empty( $inner_block['innerBlocks'] ) ) {
2383              $output .= render_block( $inner_block );
2384          } else {
2385              $output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
2386          }
2387      }
2388  
2389      return $output;
2390  }
2391  
2392  /**
2393   * Renders a single block into a HTML string.
2394   *
2395   * @since 5.0.0
2396   *
2397   * @global WP_Post $post The post to edit.
2398   *
2399   * @param array $parsed_block {
2400   *     An associative array of the block being rendered. See WP_Block_Parser_Block.
2401   *
2402   *     @type string|null $blockName    Name of block.
2403   *     @type array       $attrs        Attributes from block comment delimiters.
2404   *     @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
2405   *                                     have the same structure as this one.
2406   *     @type string      $innerHTML    HTML from inside block comment delimiters.
2407   *     @type array       $innerContent List of string fragments and null markers where
2408   *                                     inner blocks were found.
2409   * }
2410   * @return string String of rendered HTML.
2411   */
2412  function render_block( $parsed_block ) {
2413      global $post;
2414      $parent_block = null;
2415  
2416      /**
2417       * Allows render_block() to be short-circuited, by returning a non-null value.
2418       *
2419       * @since 5.1.0
2420       * @since 5.9.0 The `$parent_block` parameter was added.
2421       *
2422       * @param string|null   $pre_render   The pre-rendered content. Default null.
2423       * @param array         $parsed_block {
2424       *     An associative array of the block being rendered. See WP_Block_Parser_Block.
2425       *
2426       *     @type string|null $blockName    Name of block.
2427       *     @type array       $attrs        Attributes from block comment delimiters.
2428       *     @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
2429       *                                     have the same structure as this one.
2430       *     @type string      $innerHTML    HTML from inside block comment delimiters.
2431       *     @type array       $innerContent List of string fragments and null markers where
2432       *                                     inner blocks were found.
2433       * }
2434       * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
2435       */
2436      $pre_render = apply_filters( 'pre_render_block', null, $parsed_block, $parent_block );
2437      if ( ! is_null( $pre_render ) ) {
2438          return $pre_render;
2439      }
2440  
2441      $source_block = $parsed_block;
2442  
2443      /**
2444       * Filters the block being rendered in render_block(), before it's processed.
2445       *
2446       * @since 5.1.0
2447       * @since 5.9.0 The `$parent_block` parameter was added.
2448       *
2449       * @param array         $parsed_block {
2450       *     An associative array of the block being rendered. See WP_Block_Parser_Block.
2451       *
2452       *     @type string|null $blockName    Name of block.
2453       *     @type array       $attrs        Attributes from block comment delimiters.
2454       *     @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
2455       *                                     have the same structure as this one.
2456       *     @type string      $innerHTML    HTML from inside block comment delimiters.
2457       *     @type array       $innerContent List of string fragments and null markers where
2458       *                                     inner blocks were found.
2459       * }
2460       * @param array         $source_block {
2461       *     An un-modified copy of `$parsed_block`, as it appeared in the source content.
2462       *     See WP_Block_Parser_Block.
2463       *
2464       *     @type string|null $blockName    Name of block.
2465       *     @type array       $attrs        Attributes from block comment delimiters.
2466       *     @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
2467       *                                     have the same structure as this one.
2468       *     @type string      $innerHTML    HTML from inside block comment delimiters.
2469       *     @type array       $innerContent List of string fragments and null markers where
2470       *                                     inner blocks were found.
2471       * }
2472       * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
2473       */
2474      $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block, $parent_block );
2475  
2476      $context = array();
2477  
2478      if ( $post instanceof WP_Post ) {
2479          $context['postId'] = $post->ID;
2480  
2481          /*
2482           * The `postType` context is largely unnecessary server-side, since the ID
2483           * is usually sufficient on its own. That being said, since a block's
2484           * manifest is expected to be shared between the server and the client,
2485           * it should be included to consistently fulfill the expectation.
2486           */
2487          $context['postType'] = $post->post_type;
2488      }
2489  
2490      /**
2491       * Filters the default context provided to a rendered block.
2492       *
2493       * @since 5.5.0
2494       * @since 5.9.0 The `$parent_block` parameter was added.
2495       *
2496       * @param array         $context      Default context.
2497       * @param array         $parsed_block {
2498       *     An associative array of the block being rendered. See WP_Block_Parser_Block.
2499       *
2500       *     @type string|null $blockName    Name of block.
2501       *     @type array       $attrs        Attributes from block comment delimiters.
2502       *     @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
2503       *                                     have the same structure as this one.
2504       *     @type string      $innerHTML    HTML from inside block comment delimiters.
2505       *     @type array       $innerContent List of string fragments and null markers where
2506       *                                     inner blocks were found.
2507       * }
2508       * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
2509       */
2510      $context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block );
2511  
2512      $block = new WP_Block( $parsed_block, $context );
2513  
2514      return $block->render();
2515  }
2516  
2517  /**
2518   * Parses blocks out of a content string.
2519   *
2520   * Given an HTML document, this function fully-parses block content, producing
2521   * a tree of blocks and their contents, as well as top-level non-block content,
2522   * which will appear as a block with no `blockName`.
2523   *
2524   * This function can be memory heavy for certain documents, particularly those
2525   * with deeply-nested blocks or blocks with extensive attribute values. Further,
2526   * this function must parse an entire document in one atomic operation.
2527   *
2528   * If the entire parsed document is not necessary, consider using {@see WP_Block_Processor}
2529   * instead, as it provides a streaming and low-overhead interface for finding blocks.
2530   *
2531   * @since 5.0.0
2532   *
2533   * @param string $content Post content.
2534   * @return array[] {
2535   *     Array of block structures.
2536   *
2537   *     @type array ...$0 {
2538   *         An associative array of a single parsed block object. See WP_Block_Parser_Block.
2539   *
2540   *         @type string|null $blockName    Name of block.
2541   *         @type array       $attrs        Attributes from block comment delimiters.
2542   *         @type array[]     $innerBlocks  List of inner blocks. An array of arrays that
2543   *                                         have the same structure as this one.
2544   *         @type string      $innerHTML    HTML from inside block comment delimiters.
2545   *         @type array       $innerContent List of string fragments and null markers where
2546   *                                         inner blocks were found.
2547   *     }
2548   * }
2549   */
2550  function parse_blocks( $content ) {
2551      /**
2552       * Filter to allow plugins to replace the server-side block parser.
2553       *
2554       * @since 5.0.0
2555       *
2556       * @param string $parser_class Name of block parser class.
2557       */
2558      $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
2559  
2560      $parser = new $parser_class();
2561      return $parser->parse( $content );
2562  }
2563  
2564  /**
2565   * Parses dynamic blocks out of `post_content` and re-renders them.
2566   *
2567   * @since 5.0.0
2568   *
2569   * @param string $content Post content.
2570   * @return string Updated post content.
2571   */
2572  function do_blocks( $content ) {
2573      $blocks                = parse_blocks( $content );
2574      $top_level_block_count = count( $blocks );
2575      $output                = '';
2576  
2577      /**
2578       * Parsed blocks consist of a list of top-level blocks. Those top-level
2579       * blocks may themselves contain nested inner blocks. However, every
2580       * top-level block is rendered independently, meaning there are no data
2581       * dependencies between them.
2582       *
2583       * Ideally, therefore, the parser would only need to parse one complete
2584       * top-level block at a time, render it, and move on. Unfortunately, this
2585       * is not possible with {@see \parse_blocks()} because it must parse the
2586       * entire given document at once.
2587       *
2588       * While the current implementation prevents this optimization, it’s still
2589       * possible to reduce the peak memory use when calls to `render_block()`
2590       * on those top-level blocks are memory-heavy (which many of them are).
2591       * By setting each parsed block to `NULL` after rendering it, any memory
2592       * allocated during the render will be freed and reused for the next block.
2593       * Before making this change, that memory was retained and would lead to
2594       * out-of-memory crashes for certain posts that now run with this change.
2595       */
2596      for ( $i = 0; $i < $top_level_block_count; $i++ ) {
2597          $output      .= render_block( $blocks[ $i ] );
2598          $blocks[ $i ] = null;
2599      }
2600  
2601      // If there are blocks in this content, we shouldn't run wpautop() on it later.
2602      $priority = has_filter( 'the_content', 'wpautop' );
2603      if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
2604          remove_filter( 'the_content', 'wpautop', $priority );
2605          add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
2606      }
2607  
2608      return $output;
2609  }
2610  
2611  /**
2612   * If do_blocks() needs to remove wpautop() from the `the_content` filter, this re-adds it afterwards,
2613   * for subsequent `the_content` usage.
2614   *
2615   * @since 5.0.0
2616   * @access private
2617   *
2618   * @param string $content The post content running through this filter.
2619   * @return string The unmodified content.
2620   */
2621  function _restore_wpautop_hook( $content ) {
2622      $current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );
2623  
2624      add_filter( 'the_content', 'wpautop', $current_priority - 1 );
2625      remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
2626  
2627      return $content;
2628  }
2629  
2630  /**
2631   * Applies standard content filters similar to the 'the_content' filter.
2632   *
2633   * This function runs the typical content processing filters that WordPress
2634   * applies to post content, useful for blocks that render nested content.
2635   *
2636   * @since 7.1.0
2637   * @access private
2638   *
2639   * @global WP_Embed $wp_embed WordPress Embed object.
2640   *
2641   * @param string      $content  The content to process.
2642   * @param string      $context  Optional. Context identifier for wp_filter_content_tags().
2643   *                              Default empty string.
2644   * @param array|null  $seen_ids Optional. Reference to an array tracking seen IDs for
2645   *                              recursion prevention. Default null.
2646   * @param string|null $id       Optional. Unique identifier for this content, used with
2647   *                              $seen_ids. Default null.
2648   * @return string The processed content.
2649   */
2650  function _wp_apply_block_content_filters( $content, $context = '', &$seen_ids = null, $id = null ) {
2651      $content = shortcode_unautop( $content );
2652      $content = do_shortcode( $content );
2653  
2654      if ( null !== $seen_ids && null !== $id ) {
2655          $seen_ids[ $id ] = true;
2656      }
2657  
2658      try {
2659          $content = do_blocks( $content );
2660      } finally {
2661          if ( null !== $seen_ids && null !== $id ) {
2662              unset( $seen_ids[ $id ] );
2663          }
2664      }
2665  
2666      $content = wptexturize( $content );
2667      $content = convert_smilies( $content );
2668      $content = wp_filter_content_tags( $content, $context );
2669  
2670      global $wp_embed;
2671      $content = $wp_embed->autoembed( $content );
2672  
2673      return $content;
2674  }
2675  
2676  /**
2677   * Returns the current version of the block format that the content string is using.
2678   *
2679   * If the string doesn't contain blocks, it returns 0.
2680   *
2681   * @since 5.0.0
2682   *
2683   * @param string $content Content to test.
2684   * @return int The block format version is 1 if the content contains one or more blocks, 0 otherwise.
2685   */
2686  function block_version( $content ) {
2687      return has_blocks( $content ) ? 1 : 0;
2688  }
2689  
2690  /**
2691   * Registers a new block style.
2692   *
2693   * @since 5.3.0
2694   * @since 6.6.0 Added support for registering styles for multiple block types.
2695   *
2696   * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/
2697   *
2698   * @param string|string[] $block_name       Block type name including namespace or array of namespaced block type names.
2699   * @param array           $style_properties Array containing the properties of the style name, label,
2700   *                                          style_handle (name of the stylesheet to be enqueued),
2701   *                                          inline_style (string containing the CSS to be added),
2702   *                                          style_data (theme.json-like array to generate CSS from).
2703   *                                          See WP_Block_Styles_Registry::register().
2704   * @return bool True if the block style was registered with success and false otherwise.
2705   */
2706  function register_block_style( $block_name, $style_properties ) {
2707      return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
2708  }
2709  
2710  /**
2711   * Unregisters a block style.
2712   *
2713   * @since 5.3.0
2714   *
2715   * @param string $block_name       Block type name including namespace.
2716   * @param string $block_style_name Block style name.
2717   * @return bool True if the block style was unregistered with success and false otherwise.
2718   */
2719  function unregister_block_style( $block_name, $block_style_name ) {
2720      return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
2721  }
2722  
2723  /**
2724   * Checks whether the current block type supports the feature requested.
2725   *
2726   * @since 5.8.0
2727   * @since 6.4.0 The `$feature` parameter now supports a string.
2728   *
2729   * @param WP_Block_Type|null $block_type    Block type to check for support.
2730   * @param string|array       $feature       Feature slug, or path to a specific feature to check support for.
2731   * @param mixed              $default_value Optional. Fallback value for feature support. Default false.
2732   * @return bool Whether the feature is supported.
2733   */
2734  function block_has_support( $block_type, $feature, $default_value = false ) {
2735      $block_support = $default_value;
2736      if ( $block_type instanceof WP_Block_Type ) {
2737          if ( is_array( $feature ) && count( $feature ) === 1 ) {
2738              $feature = $feature[0];
2739          }
2740  
2741          if ( is_array( $feature ) ) {
2742              $block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
2743          } elseif ( isset( $block_type->supports[ $feature ] ) ) {
2744              $block_support = $block_type->supports[ $feature ];
2745          }
2746      }
2747  
2748      return true === $block_support || is_array( $block_support );
2749  }
2750  
2751  /**
2752   * Converts typography keys declared under `supports.*` to `supports.typography.*`.
2753   *
2754   * Displays a `_doing_it_wrong()` notice when a block using the older format is detected.
2755   *
2756   * @since 5.8.0
2757   *
2758   * @param array $metadata Metadata for registering a block type.
2759   * @return array Filtered metadata for registering a block type.
2760   */
2761  function wp_migrate_old_typography_shape( $metadata ) {
2762      if ( ! isset( $metadata['supports'] ) ) {
2763          return $metadata;
2764      }
2765  
2766      $typography_keys = array(
2767          '__experimentalFontFamily',
2768          '__experimentalFontStyle',
2769          '__experimentalFontWeight',
2770          '__experimentalLetterSpacing',
2771          '__experimentalTextDecoration',
2772          '__experimentalTextTransform',
2773          'fontSize',
2774          'lineHeight',
2775      );
2776  
2777      foreach ( $typography_keys as $typography_key ) {
2778          $support_for_key = $metadata['supports'][ $typography_key ] ?? null;
2779  
2780          if ( null !== $support_for_key ) {
2781              _doing_it_wrong(
2782                  'register_block_type_from_metadata()',
2783                  sprintf(
2784                      /* translators: 1: Block type, 2: Typography supports key, e.g: fontSize, lineHeight, etc. 3: block.json, 4: Old metadata key, 5: New metadata key. */
2785                      __( 'Block "%1$s" is declaring %2$s support in %3$s file under %4$s. %2$s support is now declared under %5$s.' ),
2786                      $metadata['name'],
2787                      "<code>$typography_key</code>",
2788                      '<code>block.json</code>',
2789                      "<code>supports.$typography_key</code>",
2790                      "<code>supports.typography.$typography_key</code>"
2791                  ),
2792                  '5.8.0'
2793              );
2794  
2795              _wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key );
2796              unset( $metadata['supports'][ $typography_key ] );
2797          }
2798      }
2799  
2800      return $metadata;
2801  }
2802  
2803  /**
2804   * Helper function that constructs a WP_Query args array from
2805   * a `Query` block properties.
2806   *
2807   * It's used in Query Loop, Query Pagination Numbers and Query Pagination Next blocks.
2808   *
2809   * @since 5.8.0
2810   * @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query.
2811   * @since 6.7.0 Added support for the `format` property in query.
2812   * @since 7.0.0 Updated `taxQuery` structure.
2813   * @since 7.1.0 Added support for the `excludeCurrent` property in query.
2814   *
2815   * @param WP_Block $block Block instance.
2816   * @param int      $page  Current query's page.
2817   *
2818   * @return array Returns the constructed WP_Query arguments.
2819   */
2820  function build_query_vars_from_query_block( $block, $page ) {
2821      $query = array(
2822          'post_type'    => 'post',
2823          'order'        => 'DESC',
2824          'orderby'      => 'date',
2825          'post__not_in' => array(),
2826          'tax_query'    => array(),
2827      );
2828  
2829      if ( isset( $block->context['query'] ) ) {
2830          if ( ! empty( $block->context['query']['postType'] ) ) {
2831              $post_type_param = $block->context['query']['postType'];
2832              if ( is_post_type_viewable( $post_type_param ) ) {
2833                  $query['post_type'] = $post_type_param;
2834              }
2835          }
2836          if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) {
2837              $sticky = get_option( 'sticky_posts' );
2838              if ( 'only' === $block->context['query']['sticky'] ) {
2839                  /*
2840                   * Passing an empty array to post__in will return have_posts() as true (and all posts will be returned).
2841                   * Logic should be used beforehand to determine if WP_Query should be used in the event that the array
2842                   * being passed to post__in is empty.
2843                   *
2844                   * @see https://core.trac.wordpress.org/ticket/28099
2845                   */
2846                  $query['post__in']            = ! empty( $sticky ) ? $sticky : array( 0 );
2847                  $query['ignore_sticky_posts'] = 1;
2848              } elseif ( 'exclude' === $block->context['query']['sticky'] ) {
2849                  $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
2850              } elseif ( 'ignore' === $block->context['query']['sticky'] ) {
2851                  $query['ignore_sticky_posts'] = 1;
2852              }
2853          }
2854          if ( ! empty( $block->context['query']['exclude'] ) ) {
2855              $excluded_post_ids     = array_map( 'intval', $block->context['query']['exclude'] );
2856              $excluded_post_ids     = array_filter( $excluded_post_ids );
2857              $query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids );
2858          }
2859          if ( ! empty( $block->context['query']['excludeCurrent'] ) ) {
2860              $current_post_id = get_the_ID();
2861              if ( $current_post_id ) {
2862                  $query['post__not_in'][] = $current_post_id;
2863              }
2864          }
2865          if (
2866              isset( $block->context['query']['perPage'] ) &&
2867              is_numeric( $block->context['query']['perPage'] )
2868          ) {
2869              $per_page = absint( $block->context['query']['perPage'] );
2870              $offset   = 0;
2871  
2872              if (
2873                  isset( $block->context['query']['offset'] ) &&
2874                  is_numeric( $block->context['query']['offset'] )
2875              ) {
2876                  $offset = absint( $block->context['query']['offset'] );
2877              }
2878  
2879              $query['offset']         = ( $per_page * ( $page - 1 ) ) + $offset;
2880              $query['posts_per_page'] = $per_page;
2881          }
2882          // Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
2883          if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
2884              $tax_query_back_compat = array();
2885              if ( ! empty( $block->context['query']['categoryIds'] ) ) {
2886                  $tax_query_back_compat[] = array(
2887                      'taxonomy'         => 'category',
2888                      'terms'            => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ),
2889                      'include_children' => false,
2890                  );
2891              }
2892              if ( ! empty( $block->context['query']['tagIds'] ) ) {
2893                  $tax_query_back_compat[] = array(
2894                      'taxonomy'         => 'post_tag',
2895                      'terms'            => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ),
2896                      'include_children' => false,
2897                  );
2898              }
2899              $query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );
2900          }
2901  
2902          if ( ! empty( $block->context['query']['taxQuery'] ) && is_array( $block->context['query']['taxQuery'] ) ) {
2903              $tax_query_input = $block->context['query']['taxQuery'];
2904              $tax_query       = array();
2905              // If there are keys other than include/exclude, it's the old
2906              // format e.g. "taxQuery":{"category":[4]}
2907              if ( ! empty( array_diff( array_keys( $tax_query_input ), array( 'include', 'exclude' ) ) ) ) {
2908                  foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
2909                      if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
2910                          $tax_query[] = array(
2911                              'taxonomy'         => $taxonomy,
2912                              'terms'            => array_filter( array_map( 'intval', $terms ) ),
2913                              'include_children' => false,
2914                          );
2915                      }
2916                  }
2917              } else {
2918                  // This is the new format e.g. "taxQuery":{"include":{"category":[4]},"exclude":{"post_tag":[5]}}
2919  
2920                  // Helper function to build tax_query conditions from taxonomy terms.
2921                  $build_conditions = static function ( $terms, string $operator = 'IN' ): array {
2922                      $terms      = (array) $terms;
2923                      $conditions = array();
2924                      foreach ( $terms as $taxonomy => $tax_terms ) {
2925                          if ( ! empty( $tax_terms ) && is_taxonomy_viewable( $taxonomy ) ) {
2926                              $conditions[] = array(
2927                                  'taxonomy'         => $taxonomy,
2928                                  'terms'            => array_filter( array_map( 'intval', $tax_terms ) ),
2929                                  'operator'         => $operator,
2930                                  'include_children' => false,
2931                              );
2932                          }
2933                      }
2934                      return $conditions;
2935                  };
2936  
2937                  // Separate exclude from include terms.
2938                  $exclude_terms = isset( $tax_query_input['exclude'] ) && is_array( $tax_query_input['exclude'] )
2939                      ? $tax_query_input['exclude']
2940                      : array();
2941                  $include_terms = isset( $tax_query_input['include'] ) && is_array( $tax_query_input['include'] )
2942                      ? $tax_query_input['include']
2943                      : array();
2944  
2945                  $tax_query = array_merge(
2946                      $build_conditions( $include_terms ),
2947                      $build_conditions( $exclude_terms, 'NOT IN' )
2948                  );
2949              }
2950  
2951              if ( ! empty( $tax_query ) ) {
2952                  // Merge with any existing `tax_query` conditions.
2953                  $query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
2954              }
2955          }
2956          if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) {
2957              $formats = $block->context['query']['format'];
2958              /*
2959               * Validate that the format is either `standard` or a supported post format.
2960               * - First, add `standard` to the array of valid formats.
2961               * - Then, remove any invalid formats.
2962               */
2963              $valid_formats = array_merge( array( 'standard' ), get_post_format_slugs() );
2964              $formats       = array_intersect( $formats, $valid_formats );
2965  
2966              /*
2967               * The relation needs to be set to `OR` since the request can contain
2968               * two separate conditions. The user may be querying for items that have
2969               * either the `standard` format or a specific format.
2970               */
2971              $formats_query = array( 'relation' => 'OR' );
2972  
2973              /*
2974               * The default post format, `standard`, is not stored in the database.
2975               * If `standard` is part of the request, the query needs to exclude all post items that
2976               * have a format assigned.
2977               */
2978              if ( in_array( 'standard', $formats, true ) ) {
2979                  $formats_query[] = array(
2980                      'taxonomy' => 'post_format',
2981                      'field'    => 'slug',
2982                      'operator' => 'NOT EXISTS',
2983                  );
2984                  // Remove the `standard` format, since it cannot be queried.
2985                  unset( $formats[ array_search( 'standard', $formats, true ) ] );
2986              }
2987              // Add any remaining formats to the formats query.
2988              if ( ! empty( $formats ) ) {
2989                  // Add the `post-format-` prefix.
2990                  $terms           = array_map(
2991                      static function ( $format ) {
2992                          return "post-format-$format";
2993                      },
2994                      $formats
2995                  );
2996                  $formats_query[] = array(
2997                      'taxonomy' => 'post_format',
2998                      'field'    => 'slug',
2999                      'terms'    => $terms,
3000                      'operator' => 'IN',
3001                  );
3002              }
3003  
3004              /*
3005               * Add `$formats_query` to `$query`, as long as it contains more than one key:
3006               * If `$formats_query` only contains the initial `relation` key, there are no valid formats to query,
3007               * and the query should not be modified.
3008               */
3009              if ( count( $formats_query ) > 1 ) {
3010                  // Enable filtering by both post formats and other taxonomies by combining them with `AND`.
3011                  if ( empty( $query['tax_query'] ) ) {
3012                      $query['tax_query'] = $formats_query;
3013                  } else {
3014                      $query['tax_query'] = array(
3015                          'relation' => 'AND',
3016                          $query['tax_query'],
3017                          $formats_query,
3018                      );
3019                  }
3020              }
3021          }
3022  
3023          if (
3024              isset( $block->context['query']['order'] ) &&
3025                  in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true )
3026          ) {
3027              $query['order'] = strtoupper( $block->context['query']['order'] );
3028          }
3029          if ( isset( $block->context['query']['orderBy'] ) ) {
3030              $query['orderby'] = $block->context['query']['orderBy'];
3031          }
3032          if (
3033              isset( $block->context['query']['author'] )
3034          ) {
3035              if ( is_array( $block->context['query']['author'] ) ) {
3036                  $query['author__in'] = array_filter( array_map( 'intval', $block->context['query']['author'] ) );
3037              } elseif ( is_string( $block->context['query']['author'] ) ) {
3038                  $query['author__in'] = array_filter( array_map( 'intval', explode( ',', $block->context['query']['author'] ) ) );
3039              } elseif ( is_int( $block->context['query']['author'] ) && $block->context['query']['author'] > 0 ) {
3040                  $query['author'] = $block->context['query']['author'];
3041              }
3042          }
3043          if ( ! empty( $block->context['query']['search'] ) ) {
3044              $query['s'] = $block->context['query']['search'];
3045          }
3046          if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
3047              $query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) );
3048          }
3049      }
3050  
3051      /**
3052       * Filters the arguments which will be passed to `WP_Query` for the Query Loop Block.
3053       *
3054       * Anything to this filter should be compatible with the `WP_Query` API to form
3055       * the query context which will be passed down to the Query Loop Block's children.
3056       * This can help, for example, to include additional settings or meta queries not
3057       * directly supported by the core Query Loop Block, and extend its capabilities.
3058       *
3059       * Please note that this will only influence the query that will be rendered on the
3060       * front-end. The editor preview is not affected by this filter. Also, worth noting
3061       * that the editor preview uses the REST API, so, ideally, one should aim to provide
3062       * attributes which are also compatible with the REST API, in order to be able to
3063       * implement identical queries on both sides.
3064       *
3065       * @since 6.1.0
3066       *
3067       * @param array    $query Array containing parameters for `WP_Query` as parsed by the block context.
3068       * @param WP_Block $block Block instance.
3069       * @param int      $page  Current query's page.
3070       */
3071      return apply_filters( 'query_loop_block_query_vars', $query, $block, $page );
3072  }
3073  
3074  /**
3075   * Helper function that returns the proper pagination arrow HTML for
3076   * `QueryPaginationNext` and `QueryPaginationPrevious` blocks based
3077   * on the provided `paginationArrow` from `QueryPagination` context.
3078   *
3079   * It's used in QueryPaginationNext and QueryPaginationPrevious blocks.
3080   *
3081   * @since 5.9.0
3082   *
3083   * @param WP_Block $block   Block instance.
3084   * @param bool     $is_next Flag for handling `next/previous` blocks.
3085   * @return string|null The pagination arrow HTML or null if there is none.
3086   */
3087  function get_query_pagination_arrow( $block, $is_next ) {
3088      $arrow_map = array(
3089          'none'    => '',
3090          'arrow'   => array(
3091              'next'     => '→',
3092              'previous' => '←',
3093          ),
3094          'chevron' => array(
3095              'next'     => '»',
3096              'previous' => '«',
3097          ),
3098      );
3099      if ( ! empty( $block->context['paginationArrow'] ) && array_key_exists( $block->context['paginationArrow'], $arrow_map ) && ! empty( $arrow_map[ $block->context['paginationArrow'] ] ) ) {
3100          $pagination_type = $is_next ? 'next' : 'previous';
3101          $arrow_attribute = $block->context['paginationArrow'];
3102          $arrow           = $arrow_map[ $block->context['paginationArrow'] ][ $pagination_type ];
3103          $arrow_classes   = "wp-block-query-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
3104          return "<span class='$arrow_classes' aria-hidden='true'>$arrow</span>";
3105      }
3106      return null;
3107  }
3108  
3109  /**
3110   * Helper function that constructs a comment query vars array from the passed
3111   * block properties.
3112   *
3113   * It's used with the Comment Query Loop inner blocks.
3114   *
3115   * @since 6.0.0
3116   *
3117   * @param WP_Block $block Block instance.
3118   * @return array Returns the comment query parameters to use with the
3119   *               WP_Comment_Query constructor.
3120   */
3121  function build_comment_query_vars_from_block( $block ) {
3122  
3123      $comment_args = array(
3124          'orderby'       => 'comment_date_gmt',
3125          'order'         => 'ASC',
3126          'status'        => 'approve',
3127          'no_found_rows' => false,
3128      );
3129  
3130      if ( is_user_logged_in() ) {
3131          $comment_args['include_unapproved'] = array( get_current_user_id() );
3132      } else {
3133          $unapproved_email = wp_get_unapproved_comment_author_email();
3134  
3135          if ( $unapproved_email ) {
3136              $comment_args['include_unapproved'] = array( $unapproved_email );
3137          }
3138      }
3139  
3140      if ( ! empty( $block->context['postId'] ) ) {
3141          $comment_args['post_id'] = (int) $block->context['postId'];
3142      }
3143  
3144      if ( get_option( 'thread_comments' ) ) {
3145          $comment_args['hierarchical'] = 'threaded';
3146      } else {
3147          $comment_args['hierarchical'] = false;
3148      }
3149  
3150      if ( get_option( 'page_comments' ) === '1' || get_option( 'page_comments' ) === true ) {
3151          $per_page     = get_option( 'comments_per_page' );
3152          $default_page = get_option( 'default_comments_page' );
3153          if ( $per_page > 0 ) {
3154              $comment_args['number'] = $per_page;
3155  
3156              $page = (int) get_query_var( 'cpage' );
3157              if ( $page ) {
3158                  $comment_args['paged'] = $page;
3159              } elseif ( 'oldest' === $default_page ) {
3160                  $comment_args['paged'] = 1;
3161              } elseif ( 'newest' === $default_page ) {
3162                  $max_num_pages = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
3163                  if ( 0 !== $max_num_pages ) {
3164                      $comment_args['paged'] = $max_num_pages;
3165                  }
3166              }
3167          }
3168      }
3169  
3170      return $comment_args;
3171  }
3172  
3173  /**
3174   * Helper function that returns the proper pagination arrow HTML for
3175   * `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based on the
3176   * provided `paginationArrow` from `CommentsPagination` context.
3177   *
3178   * It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks.
3179   *
3180   * @since 6.0.0
3181   *
3182   * @param WP_Block $block           Block instance.
3183   * @param string   $pagination_type Optional. Type of the arrow we will be rendering.
3184   *                                  Accepts 'next' or 'previous'. Default 'next'.
3185   * @return string|null The pagination arrow HTML or null if there is none.
3186   */
3187  function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
3188      $arrow_map = array(
3189          'none'    => '',
3190          'arrow'   => array(
3191              'next'     => '→',
3192              'previous' => '←',
3193          ),
3194          'chevron' => array(
3195              'next'     => '»',
3196              'previous' => '«',
3197          ),
3198      );
3199      if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) {
3200          $arrow_attribute = $block->context['comments/paginationArrow'];
3201          $arrow           = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ];
3202          $arrow_classes   = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
3203          return "<span class='$arrow_classes' aria-hidden='true'>$arrow</span>";
3204      }
3205      return null;
3206  }
3207  
3208  /**
3209   * Strips all HTML from the content of footnotes, and sanitizes the ID.
3210   *
3211   * This function expects slashed data on the footnotes content.
3212   *
3213   * @access private
3214   * @since 6.3.2
3215   *
3216   * @param string $footnotes JSON-encoded string of an array containing the content and ID of each footnote.
3217   * @return string Filtered content without any HTML on the footnote content and with the sanitized ID.
3218   */
3219  function _wp_filter_post_meta_footnotes( $footnotes ) {
3220      $footnotes_decoded = json_decode( $footnotes, true );
3221      if ( ! is_array( $footnotes_decoded ) ) {
3222          return '';
3223      }
3224      $footnotes_sanitized = array();
3225      foreach ( $footnotes_decoded as $footnote ) {
3226          if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
3227              $footnotes_sanitized[] = array(
3228                  'id'      => sanitize_key( $footnote['id'] ),
3229                  'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
3230              );
3231          }
3232      }
3233      return wp_json_encode( $footnotes_sanitized );
3234  }
3235  
3236  /**
3237   * Adds the filters for footnotes meta field.
3238   *
3239   * @access private
3240   * @since 6.3.2
3241   */
3242  function _wp_footnotes_kses_init_filters() {
3243      add_filter( 'sanitize_post_meta_footnotes', '_wp_filter_post_meta_footnotes' );
3244  }
3245  
3246  /**
3247   * Removes the filters for footnotes meta field.
3248   *
3249   * @access private
3250   * @since 6.3.2
3251   */
3252  function _wp_footnotes_remove_filters() {
3253      remove_filter( 'sanitize_post_meta_footnotes', '_wp_filter_post_meta_footnotes' );
3254  }
3255  
3256  /**
3257   * Registers the filter of footnotes meta field if the user does not have `unfiltered_html` capability.
3258   *
3259   * @access private
3260   * @since 6.3.2
3261   */
3262  function _wp_footnotes_kses_init() {
3263      _wp_footnotes_remove_filters();
3264      if ( ! current_user_can( 'unfiltered_html' ) ) {
3265          _wp_footnotes_kses_init_filters();
3266      }
3267  }
3268  
3269  /**
3270   * Initializes the filters for footnotes meta field when imported data should be filtered.
3271   *
3272   * This filter is the last one being executed on {@see 'force_filtered_html_on_import'}.
3273   * If the input of the filter is true, it means we are in an import situation and should
3274   * enable kses, independently of the user capabilities. So in that case we call
3275   * _wp_footnotes_kses_init_filters().
3276   *
3277   * @access private
3278   * @since 6.3.2
3279   *
3280   * @param string $arg Input argument of the filter.
3281   * @return string Input argument of the filter.
3282   */
3283  function _wp_footnotes_force_filtered_html_on_import_filter( $arg ) {
3284      // If `force_filtered_html_on_import` is true, we need to init the global styles kses filters.
3285      if ( $arg ) {
3286          _wp_footnotes_kses_init_filters();
3287      }
3288      return $arg;
3289  }
3290  
3291  /**
3292   * Exposes blocks with autoRegister flag for ServerSideRender in the editor.
3293   *
3294   * Detects blocks that have the autoRegister flag set in their supports
3295   * and passes them to JavaScript for auto-registration with ServerSideRender.
3296   *
3297   * @access private
3298   * @since 7.0.0
3299   */
3300  function _wp_enqueue_auto_register_blocks() {
3301      $auto_register_blocks = array();
3302      $registered_blocks    = WP_Block_Type_Registry::get_instance()->get_all_registered();
3303  
3304      foreach ( $registered_blocks as $block_name => $block_type ) {
3305          if ( ! empty( $block_type->supports['autoRegister'] ) && ! empty( $block_type->render_callback ) ) {
3306              $auto_register_blocks[] = $block_name;
3307          }
3308      }
3309  
3310      if ( ! empty( $auto_register_blocks ) ) {
3311          wp_add_inline_script(
3312              'wp-block-library',
3313              sprintf( 'window.__unstableAutoRegisterBlocks = %s;', wp_json_encode( $auto_register_blocks ) ),
3314              'before'
3315          );
3316      }
3317  }


Generated : Tue Aug 4 08:20:20 2026 Cross-referenced by PHPXref