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


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref