wp/wp-includes/blocks.php
author ymh <ymh.work@gmail.com>
Mon, 08 Sep 2025 19:44:41 +0200
changeset 23 417f20492bf7
parent 22 8c2e4d02f4ef
permissions -rw-r--r--
Update Docker configuration and plugin versions - Upgrade MariaDB from 10.6 to 11 with auto-upgrade support - Add WordPress debug environment variable to FPM container - Update PHP-FPM Dockerfile base image - Update Include Mastodon Feed plugin with bug fixes and improvements - Update Portfolio plugin (v2.58) with latest translations and demo data enhancements - Remove old README.md from Mastodon Feed plugin 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Functions related to registering and parsing blocks.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Blocks
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    11
 * Removes the block asset's path prefix if provided.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    12
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    13
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    14
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    15
 * @param string $asset_handle_or_path Asset handle or prefixed path.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    16
 * @return string Path without the prefix or the original value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    17
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    18
function remove_block_asset_path_prefix( $asset_handle_or_path ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    19
	$path_prefix = 'file:';
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    20
	if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    21
		return $asset_handle_or_path;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    22
	}
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    23
	$path = substr(
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    24
		$asset_handle_or_path,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    25
		strlen( $path_prefix )
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    26
	);
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    27
	if ( str_starts_with( $path, './' ) ) {
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    28
		$path = substr( $path, 2 );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    29
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    30
	return $path;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    31
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    32
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    33
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    34
 * Generates the name for an asset based on the name of the block
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    35
 * and the field name provided.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    36
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    37
 * @since 5.5.0
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    38
 * @since 6.1.0 Added `$index` parameter.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    39
 * @since 6.5.0 Added support for `viewScriptModule` field.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    40
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    41
 * @param string $block_name Name of the block.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    42
 * @param string $field_name Name of the metadata field.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    43
 * @param int    $index      Optional. Index of the asset when multiple items passed.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    44
 *                           Default 0.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    45
 * @return string Generated asset name for the block's field.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    46
 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    47
function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    48
	if ( str_starts_with( $block_name, 'core/' ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    49
		$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    50
		if ( str_starts_with( $field_name, 'editor' ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    51
			$asset_handle .= '-editor';
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    52
		}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    53
		if ( str_starts_with( $field_name, 'view' ) ) {
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    54
			$asset_handle .= '-view';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    55
		}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    56
		if ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    57
			$asset_handle .= '-script-module';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    58
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    59
		if ( $index > 0 ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    60
			$asset_handle .= '-' . ( $index + 1 );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    61
		}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    62
		return $asset_handle;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    63
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    64
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    65
	$field_mappings = array(
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    66
		'editorScript'     => 'editor-script',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    67
		'editorStyle'      => 'editor-style',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    68
		'script'           => 'script',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    69
		'style'            => 'style',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    70
		'viewScript'       => 'view-script',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    71
		'viewScriptModule' => 'view-script-module',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    72
		'viewStyle'        => 'view-style',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    73
	);
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    74
	$asset_handle   = str_replace( '/', '-', $block_name ) .
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    75
		'-' . $field_mappings[ $field_name ];
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    76
	if ( $index > 0 ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    77
		$asset_handle .= '-' . ( $index + 1 );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    78
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    79
	return $asset_handle;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    80
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    81
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    82
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    83
 * Gets the URL to a block asset.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    84
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    85
 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    86
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    87
 * @param string $path A normalized path to a block asset.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    88
 * @return string|false The URL to the block asset or false on failure.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    89
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    90
function get_block_asset_url( $path ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    91
	if ( empty( $path ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    92
		return false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    93
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    94
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    95
	// Path needs to be normalized to work in Windows env.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    96
	static $wpinc_path_norm = '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    97
	if ( ! $wpinc_path_norm ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    98
		$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    99
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   100
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   101
	if ( str_starts_with( $path, $wpinc_path_norm ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   102
		return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   103
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   104
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   105
	static $template_paths_norm = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   106
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   107
	$template = get_template();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   108
	if ( ! isset( $template_paths_norm[ $template ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   109
		$template_paths_norm[ $template ] = wp_normalize_path( realpath( get_template_directory() ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   110
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   111
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   112
	if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   113
		return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   114
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   115
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   116
	if ( is_child_theme() ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   117
		$stylesheet = get_stylesheet();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   118
		if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   119
			$template_paths_norm[ $stylesheet ] = wp_normalize_path( realpath( get_stylesheet_directory() ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   120
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   121
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   122
		if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   123
			return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   124
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   125
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   126
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   127
	return plugins_url( basename( $path ), $path );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   128
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   129
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   130
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   131
 * Finds a script module ID for the selected block metadata field. It detects
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   132
 * when a path to file was provided and optionally finds a corresponding asset
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   133
 * file with details necessary to register the script module under with an
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   134
 * automatically generated module ID. It returns unprocessed script module
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   135
 * ID otherwise.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   136
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   137
 * @since 6.5.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   138
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   139
 * @param array  $metadata   Block metadata.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   140
 * @param string $field_name Field name to pick from metadata.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   141
 * @param int    $index      Optional. Index of the script module ID to register when multiple
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   142
 *                           items passed. Default 0.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   143
 * @return string|false Script module ID or false on failure.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   144
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   145
function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   146
	if ( empty( $metadata[ $field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   147
		return false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   148
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   149
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   150
	$module_id = $metadata[ $field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   151
	if ( is_array( $module_id ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   152
		if ( empty( $module_id[ $index ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   153
			return false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   154
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   155
		$module_id = $module_id[ $index ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   156
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   157
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   158
	$module_path = remove_block_asset_path_prefix( $module_id );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   159
	if ( $module_id === $module_path ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   160
		return $module_id;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   161
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   162
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   163
	$path                  = dirname( $metadata['file'] );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   164
	$module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   165
	$module_id             = generate_block_asset_handle( $metadata['name'], $field_name, $index );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   166
	$module_asset_path     = wp_normalize_path(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   167
		realpath( $module_asset_raw_path )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   168
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   169
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   170
	$module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   171
	$module_uri       = get_block_asset_url( $module_path_norm );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   172
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   173
	$module_asset        = ! empty( $module_asset_path ) ? require $module_asset_path : array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   174
	$module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   175
	$block_version       = isset( $metadata['version'] ) ? $metadata['version'] : false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   176
	$module_version      = isset( $module_asset['version'] ) ? $module_asset['version'] : $block_version;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   177
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   178
	wp_register_script_module(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   179
		$module_id,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   180
		$module_uri,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   181
		$module_dependencies,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   182
		$module_version
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   183
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   184
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   185
	return $module_id;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   186
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   187
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   188
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   189
 * Finds a script handle for the selected block metadata field. It detects
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   190
 * when a path to file was provided and optionally finds a corresponding asset
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   191
 * file with details necessary to register the script under automatically
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   192
 * generated handle name. It returns unprocessed script handle otherwise.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   193
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   194
 * @since 5.5.0
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   195
 * @since 6.1.0 Added `$index` parameter.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   196
 * @since 6.5.0 The asset file is optional. Added script handle support in the asset file.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   197
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   198
 * @param array  $metadata   Block metadata.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   199
 * @param string $field_name Field name to pick from metadata.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   200
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   201
 *                           Default 0.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   202
 * @return string|false Script handle provided directly or created through
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   203
 *                      script's registration, or false on failure.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   204
 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   205
function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   206
	if ( empty( $metadata[ $field_name ] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   207
		return false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   208
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   209
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   210
	$script_handle_or_path = $metadata[ $field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   211
	if ( is_array( $script_handle_or_path ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   212
		if ( empty( $script_handle_or_path[ $index ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   213
			return false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   214
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   215
		$script_handle_or_path = $script_handle_or_path[ $index ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   216
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   217
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   218
	$script_path = remove_block_asset_path_prefix( $script_handle_or_path );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   219
	if ( $script_handle_or_path === $script_path ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   220
		return $script_handle_or_path;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   221
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   222
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   223
	$path                  = dirname( $metadata['file'] );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   224
	$script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   225
	$script_asset_path     = wp_normalize_path(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   226
		realpath( $script_asset_raw_path )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   227
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   228
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   229
	// Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   230
	$script_asset  = ! empty( $script_asset_path ) ? require $script_asset_path : array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   231
	$script_handle = isset( $script_asset['handle'] ) ?
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   232
		$script_asset['handle'] :
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   233
		generate_block_asset_handle( $metadata['name'], $field_name, $index );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   234
	if ( wp_script_is( $script_handle, 'registered' ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   235
		return $script_handle;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   236
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   237
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   238
	$script_path_norm    = wp_normalize_path( realpath( $path . '/' . $script_path ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   239
	$script_uri          = get_block_asset_url( $script_path_norm );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   240
	$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   241
	$block_version       = isset( $metadata['version'] ) ? $metadata['version'] : false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   242
	$script_version      = isset( $script_asset['version'] ) ? $script_asset['version'] : $block_version;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   243
	$script_args         = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   244
	if ( 'viewScript' === $field_name && $script_uri ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   245
		$script_args['strategy'] = 'defer';
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   246
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   247
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   248
	$result = wp_register_script(
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   249
		$script_handle,
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   250
		$script_uri,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   251
		$script_dependencies,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   252
		$script_version,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   253
		$script_args
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   254
	);
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   255
	if ( ! $result ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   256
		return false;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   257
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   258
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   259
	if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   260
		wp_set_script_translations( $script_handle, $metadata['textdomain'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   261
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   262
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   263
	return $script_handle;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   264
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   265
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   266
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   267
 * Finds a style handle for the block metadata field. It detects when a path
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   268
 * to file was provided and registers the style under automatically
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   269
 * generated handle name. It returns unprocessed style handle otherwise.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   270
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   271
 * @since 5.5.0
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   272
 * @since 6.1.0 Added `$index` parameter.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   273
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   274
 * @param array  $metadata   Block metadata.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   275
 * @param string $field_name Field name to pick from metadata.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   276
 * @param int    $index      Optional. Index of the style to register when multiple items passed.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   277
 *                           Default 0.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   278
 * @return string|false Style handle provided directly or created through
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   279
 *                      style's registration, or false on failure.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   280
 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   281
function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   282
	if ( empty( $metadata[ $field_name ] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   283
		return false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   284
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   285
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   286
	$style_handle = $metadata[ $field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   287
	if ( is_array( $style_handle ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   288
		if ( empty( $style_handle[ $index ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   289
			return false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   290
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   291
		$style_handle = $style_handle[ $index ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   292
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   293
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   294
	$style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   295
	// If the style handle is already registered, skip re-registering.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   296
	if ( wp_style_is( $style_handle_name, 'registered' ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   297
		return $style_handle_name;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   298
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   299
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   300
	static $wpinc_path_norm = '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   301
	if ( ! $wpinc_path_norm ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   302
		$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   303
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   304
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   305
	$is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   306
	// Skip registering individual styles for each core block when a bundled version provided.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   307
	if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   308
		return false;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   309
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   310
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   311
	$style_path      = remove_block_asset_path_prefix( $style_handle );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   312
	$is_style_handle = $style_handle === $style_path;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   313
	// Allow only passing style handles for core blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   314
	if ( $is_core_block && ! $is_style_handle ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   315
		return false;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   316
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   317
	// Return the style handle unless it's the first item for every core block that requires special treatment.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   318
	if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   319
		return $style_handle;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   320
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   321
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   322
	// Check whether styles should have a ".min" suffix or not.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   323
	$suffix = SCRIPT_DEBUG ? '' : '.min';
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   324
	if ( $is_core_block ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   325
		$style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css";
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   326
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   327
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   328
	$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   329
	$style_uri       = get_block_asset_url( $style_path_norm );
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   330
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   331
	$block_version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   332
	$version       = $style_path_norm && defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( $style_path_norm ) : $block_version;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   333
	$result        = wp_register_style(
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   334
		$style_handle_name,
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   335
		$style_uri,
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   336
		array(),
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   337
		$version
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   338
	);
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   339
	if ( ! $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   340
		return false;
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   341
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   342
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   343
	if ( $style_uri ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   344
		wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   345
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   346
		if ( $is_core_block ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   347
			$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   348
		} else {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   349
			$rtl_file = str_replace( '.css', '-rtl.css', $style_path_norm );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   350
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   351
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   352
		if ( is_rtl() && file_exists( $rtl_file ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   353
			wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   354
			wp_style_add_data( $style_handle_name, 'suffix', $suffix );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   355
			wp_style_add_data( $style_handle_name, 'path', $rtl_file );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   356
		}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   357
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   358
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   359
	return $style_handle_name;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   360
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   361
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   362
/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   363
 * Gets i18n schema for block's metadata read from `block.json` file.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   364
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   365
 * @since 5.9.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   366
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   367
 * @return object The schema for block's metadata.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   368
 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   369
function get_block_metadata_i18n_schema() {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   370
	static $i18n_block_schema;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   371
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   372
	if ( ! isset( $i18n_block_schema ) ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   373
		$i18n_block_schema = wp_json_file_decode( __DIR__ . '/block-i18n.json' );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   374
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   375
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   376
	return $i18n_block_schema;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   377
}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   378
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   379
/**
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   380
 * Registers all block types from a block metadata collection.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   381
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   382
 * This can either reference a previously registered metadata collection or, if the `$manifest` parameter is provided,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   383
 * register the metadata collection directly within the same function call.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   384
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   385
 * @since 6.8.0
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   386
 * @see wp_register_block_metadata_collection()
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   387
 * @see register_block_type_from_metadata()
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   388
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   389
 * @param string $path     The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   390
 * @param string $manifest Optional. The absolute path to the manifest file containing the metadata collection, in
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   391
 *                         order to register the collection. If this parameter is not provided, the `$path` parameter
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   392
 *                         must reference a previously registered block metadata collection.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   393
 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   394
function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   395
	if ( $manifest ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   396
		wp_register_block_metadata_collection( $path, $manifest );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   397
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   398
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   399
	$block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   400
	foreach ( $block_metadata_files as $block_metadata_file ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   401
		register_block_type_from_metadata( $block_metadata_file );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   402
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   403
}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   404
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   405
/**
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   406
 * Registers a block metadata collection.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   407
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   408
 * This function allows core and third-party plugins to register their block metadata
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   409
 * collections in a centralized location. Registering collections can improve performance
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   410
 * by avoiding multiple reads from the filesystem and parsing JSON.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   411
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   412
 * @since 6.7.0
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   413
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   414
 * @param string $path     The base path in which block files for the collection reside.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   415
 * @param string $manifest The path to the manifest file for the collection.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   416
 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   417
function wp_register_block_metadata_collection( $path, $manifest ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   418
	WP_Block_Metadata_Registry::register_collection( $path, $manifest );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   419
}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   420
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   421
/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   422
 * Registers a block type from the metadata stored in the `block.json` file.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   423
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   424
 * @since 5.5.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   425
 * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   426
 * @since 5.9.0 Added support for `variations` and `viewScript` fields.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   427
 * @since 6.1.0 Added support for `render` field.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   428
 * @since 6.3.0 Added `selectors` field.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   429
 * @since 6.4.0 Added support for `blockHooks` field.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   430
 * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   431
 * @since 6.7.0 Allow PHP filename as `variations` argument.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   432
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   433
 * @param string $file_or_folder Path to the JSON file with metadata definition for
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   434
 *                               the block or path to the folder where the `block.json` file is located.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   435
 *                               If providing the path to a JSON file, the filename must end with `block.json`.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   436
 * @param array  $args           Optional. Array of block type arguments. Accepts any public property
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   437
 *                               of `WP_Block_Type`. See WP_Block_Type::__construct() for information
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   438
 *                               on accepted arguments. Default empty array.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   439
 * @return WP_Block_Type|false The registered block type on success, or false on failure.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   440
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   441
function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   442
	/*
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   443
	 * Get an array of metadata from a PHP file.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   444
	 * This improves performance for core blocks as it's only necessary to read a single PHP file
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   445
	 * instead of reading a JSON file per-block, and then decoding from JSON to PHP.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   446
	 * Using a static variable ensures that the metadata is only read once per request.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   447
	 */
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   448
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   449
	$file_or_folder = wp_normalize_path( $file_or_folder );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   450
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   451
	$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   452
		trailingslashit( $file_or_folder ) . 'block.json' :
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   453
		$file_or_folder;
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   454
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   455
	$is_core_block        = str_starts_with( $file_or_folder, wp_normalize_path( ABSPATH . WPINC ) );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   456
	$metadata_file_exists = $is_core_block || file_exists( $metadata_file );
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   457
	$registry_metadata    = WP_Block_Metadata_Registry::get_metadata( $file_or_folder );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   458
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   459
	if ( $registry_metadata ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   460
		$metadata = $registry_metadata;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   461
	} elseif ( $metadata_file_exists ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   462
		$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   463
	} else {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   464
		$metadata = array();
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   465
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   466
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   467
	if ( ! is_array( $metadata ) || ( empty( $metadata['name'] ) && empty( $args['name'] ) ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   468
		return false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   469
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   470
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   471
	$metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   472
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   473
	/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   474
	 * Filters the metadata provided for registering a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   475
	 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   476
	 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   477
	 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   478
	 * @param array $metadata Metadata for registering a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   479
	 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   480
	$metadata = apply_filters( 'block_type_metadata', $metadata );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   481
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   482
	// Add `style` and `editor_style` for core blocks if missing.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   483
	if ( ! empty( $metadata['name'] ) && str_starts_with( $metadata['name'], 'core/' ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   484
		$block_name = str_replace( 'core/', '', $metadata['name'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   485
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   486
		if ( ! isset( $metadata['style'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   487
			$metadata['style'] = "wp-block-$block_name";
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   488
		}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   489
		if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   490
			$metadata['style']   = (array) $metadata['style'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   491
			$metadata['style'][] = "wp-block-{$block_name}-theme";
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   492
		}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   493
		if ( ! isset( $metadata['editorStyle'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   494
			$metadata['editorStyle'] = "wp-block-{$block_name}-editor";
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   495
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   496
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   497
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   498
	$settings          = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   499
	$property_mappings = array(
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   500
		'apiVersion'      => 'api_version',
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   501
		'name'            => 'name',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   502
		'title'           => 'title',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   503
		'category'        => 'category',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   504
		'parent'          => 'parent',
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   505
		'ancestor'        => 'ancestor',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   506
		'icon'            => 'icon',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   507
		'description'     => 'description',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   508
		'keywords'        => 'keywords',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   509
		'attributes'      => 'attributes',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   510
		'providesContext' => 'provides_context',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   511
		'usesContext'     => 'uses_context',
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   512
		'selectors'       => 'selectors',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   513
		'supports'        => 'supports',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   514
		'styles'          => 'styles',
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   515
		'variations'      => 'variations',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   516
		'example'         => 'example',
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   517
		'allowedBlocks'   => 'allowed_blocks',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   518
	);
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   519
	$textdomain        = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   520
	$i18n_schema       = get_block_metadata_i18n_schema();
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   521
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   522
	foreach ( $property_mappings as $key => $mapped_key ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   523
		if ( isset( $metadata[ $key ] ) ) {
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   524
			$settings[ $mapped_key ] = $metadata[ $key ];
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   525
			if ( $metadata_file_exists && $textdomain && isset( $i18n_schema->$key ) ) {
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   526
				$settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   527
			}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   528
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   529
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   530
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   531
	if ( ! empty( $metadata['render'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   532
		$template_path = wp_normalize_path(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   533
			realpath(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   534
				dirname( $metadata['file'] ) . '/' .
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   535
				remove_block_asset_path_prefix( $metadata['render'] )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   536
			)
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   537
		);
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   538
		if ( $template_path ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   539
			/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   540
			 * Renders the block on the server.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   541
			 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   542
			 * @since 6.1.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   543
			 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   544
			 * @param array    $attributes Block attributes.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   545
			 * @param string   $content    Block default content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   546
			 * @param WP_Block $block      Block instance.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   547
			 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   548
			 * @return string Returns the block content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   549
			 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   550
			$settings['render_callback'] = static function ( $attributes, $content, $block ) use ( $template_path ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   551
				ob_start();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   552
				require $template_path;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   553
				return ob_get_clean();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   554
			};
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   555
		}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   556
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   557
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   558
	// If `variations` is a string, it's the name of a PHP file that
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   559
	// generates the variations.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   560
	if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   561
		$variations_path = wp_normalize_path(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   562
			realpath(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   563
				dirname( $metadata['file'] ) . '/' .
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   564
				remove_block_asset_path_prefix( $metadata['variations'] )
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   565
			)
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   566
		);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   567
		if ( $variations_path ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   568
			/**
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   569
			 * Generates the list of block variations.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   570
			 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   571
			 * @since 6.7.0
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   572
			 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   573
			 * @return string Returns the list of block variations.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   574
			 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   575
			$settings['variation_callback'] = static function () use ( $variations_path ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   576
				$variations = require $variations_path;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   577
				return $variations;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   578
			};
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   579
			// The block instance's `variations` field is only allowed to be an array
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   580
			// (of known block variations). We unset it so that the block instance will
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   581
			// provide a getter that returns the result of the `variation_callback` instead.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   582
			unset( $settings['variations'] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   583
		}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   584
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   585
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   586
	$settings = array_merge( $settings, $args );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   587
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   588
	$script_fields = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   589
		'editorScript' => 'editor_script_handles',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   590
		'script'       => 'script_handles',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   591
		'viewScript'   => 'view_script_handles',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   592
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   593
	foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   594
		if ( ! empty( $settings[ $metadata_field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   595
			$metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   596
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   597
		if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   598
			$scripts           = $metadata[ $metadata_field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   599
			$processed_scripts = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   600
			if ( is_array( $scripts ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   601
				for ( $index = 0; $index < count( $scripts ); $index++ ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   602
					$result = register_block_script_handle(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   603
						$metadata,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   604
						$metadata_field_name,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   605
						$index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   606
					);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   607
					if ( $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   608
						$processed_scripts[] = $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   609
					}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   610
				}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   611
			} else {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   612
				$result = register_block_script_handle(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   613
					$metadata,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   614
					$metadata_field_name
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   615
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   616
				if ( $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   617
					$processed_scripts[] = $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   618
				}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   619
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   620
			$settings[ $settings_field_name ] = $processed_scripts;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   621
		}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   622
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   623
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   624
	$module_fields = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   625
		'viewScriptModule' => 'view_script_module_ids',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   626
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   627
	foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   628
		if ( ! empty( $settings[ $metadata_field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   629
			$metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   630
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   631
		if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   632
			$modules           = $metadata[ $metadata_field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   633
			$processed_modules = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   634
			if ( is_array( $modules ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   635
				for ( $index = 0; $index < count( $modules ); $index++ ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   636
					$result = register_block_script_module_id(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   637
						$metadata,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   638
						$metadata_field_name,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   639
						$index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   640
					);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   641
					if ( $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   642
						$processed_modules[] = $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   643
					}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   644
				}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   645
			} else {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   646
				$result = register_block_script_module_id(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   647
					$metadata,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   648
					$metadata_field_name
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   649
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   650
				if ( $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   651
					$processed_modules[] = $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   652
				}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   653
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   654
			$settings[ $settings_field_name ] = $processed_modules;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   655
		}
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   656
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   657
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   658
	$style_fields = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   659
		'editorStyle' => 'editor_style_handles',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   660
		'style'       => 'style_handles',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   661
		'viewStyle'   => 'view_style_handles',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   662
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   663
	foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   664
		if ( ! empty( $settings[ $metadata_field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   665
			$metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   666
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   667
		if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   668
			$styles           = $metadata[ $metadata_field_name ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   669
			$processed_styles = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   670
			if ( is_array( $styles ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   671
				for ( $index = 0; $index < count( $styles ); $index++ ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   672
					$result = register_block_style_handle(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   673
						$metadata,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   674
						$metadata_field_name,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   675
						$index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   676
					);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   677
					if ( $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   678
						$processed_styles[] = $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   679
					}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   680
				}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   681
			} else {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   682
				$result = register_block_style_handle(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   683
					$metadata,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   684
					$metadata_field_name
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   685
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   686
				if ( $result ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   687
					$processed_styles[] = $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   688
				}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   689
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   690
			$settings[ $settings_field_name ] = $processed_styles;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   691
		}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   692
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   693
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   694
	if ( ! empty( $metadata['blockHooks'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   695
		/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   696
		 * Map camelCased position string (from block.json) to snake_cased block type position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   697
		 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   698
		 * @var array
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   699
		 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   700
		$position_mappings = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   701
			'before'     => 'before',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   702
			'after'      => 'after',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   703
			'firstChild' => 'first_child',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   704
			'lastChild'  => 'last_child',
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   705
		);
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   706
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   707
		$settings['block_hooks'] = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   708
		foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   709
			// Avoid infinite recursion (hooking to itself).
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   710
			if ( $metadata['name'] === $anchor_block_name ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   711
				_doing_it_wrong(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   712
					__METHOD__,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   713
					__( 'Cannot hook block to itself.' ),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   714
					'6.4.0'
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   715
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   716
				continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   717
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   718
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   719
			if ( ! isset( $position_mappings[ $position ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   720
				continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   721
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   722
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   723
			$settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   724
		}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   725
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   726
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   727
	/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   728
	 * Filters the settings determined from the block type metadata.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   729
	 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   730
	 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   731
	 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   732
	 * @param array $settings Array of determined settings for registering a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   733
	 * @param array $metadata Metadata provided for registering a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   734
	 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   735
	$settings = apply_filters( 'block_type_metadata_settings', $settings, $metadata );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   736
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   737
	$metadata['name'] = ! empty( $settings['name'] ) ? $settings['name'] : $metadata['name'];
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   738
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   739
	return WP_Block_Type_Registry::get_instance()->register(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   740
		$metadata['name'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   741
		$settings
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   742
	);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   743
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   744
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   745
/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   746
 * Registers a block type. The recommended way is to register a block type using
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   747
 * the metadata stored in the `block.json` file.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   748
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   749
 * @since 5.0.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   750
 * @since 5.8.0 First parameter now accepts a path to the `block.json` file.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   751
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   752
 * @param string|WP_Block_Type $block_type Block type name including namespace, or alternatively
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   753
 *                                         a path to the JSON file with metadata definition for the block,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   754
 *                                         or a path to the folder where the `block.json` file is located,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   755
 *                                         or a complete WP_Block_Type instance.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   756
 *                                         In case a WP_Block_Type is provided, the $args parameter will be ignored.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   757
 * @param array                $args       Optional. Array of block type arguments. Accepts any public property
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   758
 *                                         of `WP_Block_Type`. See WP_Block_Type::__construct() for information
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   759
 *                                         on accepted arguments. Default empty array.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   760
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   761
 * @return WP_Block_Type|false The registered block type on success, or false on failure.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   762
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   763
function register_block_type( $block_type, $args = array() ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   764
	if ( is_string( $block_type ) && file_exists( $block_type ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   765
		return register_block_type_from_metadata( $block_type, $args );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   766
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   767
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   768
	return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   769
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   770
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   771
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   772
 * Unregisters a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   773
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   774
 * @since 5.0.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   775
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   776
 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   777
 *                                   a complete WP_Block_Type instance.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   778
 * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   779
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   780
function unregister_block_type( $name ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   781
	return WP_Block_Type_Registry::get_instance()->unregister( $name );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   782
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   783
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   784
/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   785
 * Determines whether a post or content string has blocks.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
 * This test optimizes for performance rather than strict accuracy, detecting
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
 * the pattern of a block but not validating its structure. For strict accuracy,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
 * you should use the block parser on post content.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
 * @since 5.0.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   792
 *
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
 * @see parse_blocks()
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   795
 * @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   796
 *                                      Defaults to global $post.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
 * @return bool Whether the post has blocks.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
function has_blocks( $post = null ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
	if ( ! is_string( $post ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		$wp_post = get_post( $post );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   802
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   803
		if ( ! $wp_post instanceof WP_Post ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   804
			return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
		}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   806
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   807
		$post = $wp_post->post_content;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   810
	return str_contains( (string) $post, '<!-- wp:' );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   814
 * Determines whether a $post or a string contains a specific block type.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
 * This test optimizes for performance rather than strict accuracy, detecting
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   817
 * whether the block type exists but not validating its structure and not checking
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   818
 * synced patterns (formerly called reusable blocks). For strict accuracy,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   819
 * you should use the block parser on post content.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
 * @since 5.0.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   822
 *
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
 * @see parse_blocks()
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   825
 * @param string                  $block_name Full block type to look for.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   826
 * @param int|string|WP_Post|null $post       Optional. Post content, post ID, or post object.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   827
 *                                            Defaults to global $post.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
 * @return bool Whether the post content contains the specified block.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   830
function has_block( $block_name, $post = null ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
	if ( ! has_blocks( $post ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
		return false;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
	if ( ! is_string( $post ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
		$wp_post = get_post( $post );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		if ( $wp_post instanceof WP_Post ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
			$post = $wp_post->post_content;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   842
	/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   843
	 * Normalize block name to include namespace, if provided as non-namespaced.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   844
	 * This matches behavior for WordPress 5.0.0 - 5.3.0 in matching blocks by
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   845
	 * their serialized names.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   846
	 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   847
	if ( ! str_contains( $block_name, '/' ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   848
		$block_name = 'core/' . $block_name;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   849
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   850
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   851
	// Test for existence of block by its fully qualified name.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   852
	$has_block = str_contains( $post, '<!-- wp:' . $block_name . ' ' );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   853
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   854
	if ( ! $has_block ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   855
		/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   856
		 * If the given block name would serialize to a different name, test for
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   857
		 * existence by the serialized form.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   858
		 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   859
		$serialized_block_name = strip_core_block_namespace( $block_name );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   860
		if ( $serialized_block_name !== $block_name ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   861
			$has_block = str_contains( $post, '<!-- wp:' . $serialized_block_name . ' ' );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   862
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   863
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   864
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   865
	return $has_block;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
 * Returns an array of the names of all registered dynamic block types.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   873
 * @return string[] Array of dynamic block names.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
function get_dynamic_block_names() {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
	$dynamic_block_names = array();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
	$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	foreach ( $block_types as $block_type ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
		if ( $block_type->is_dynamic() ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
			$dynamic_block_names[] = $block_type->name;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	return $dynamic_block_names;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   889
 * Retrieves block types hooked into the given block, grouped by anchor block type and the relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   890
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   891
 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   892
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   893
 * @return array[] Array of block types grouped by anchor block type and the relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   894
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   895
function get_hooked_blocks() {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   896
	$block_types   = WP_Block_Type_Registry::get_instance()->get_all_registered();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   897
	$hooked_blocks = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   898
	foreach ( $block_types as $block_type ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   899
		if ( ! ( $block_type instanceof WP_Block_Type ) || ! is_array( $block_type->block_hooks ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   900
			continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   901
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   902
		foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   903
			if ( ! isset( $hooked_blocks[ $anchor_block_type ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   904
				$hooked_blocks[ $anchor_block_type ] = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   905
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   906
			if ( ! isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   907
				$hooked_blocks[ $anchor_block_type ][ $relative_position ] = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   908
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   909
			$hooked_blocks[ $anchor_block_type ][ $relative_position ][] = $block_type->name;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   910
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   911
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   912
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   913
	return $hooked_blocks;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   914
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   915
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   916
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   917
 * Returns the markup for blocks hooked to the given anchor block in a specific relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   918
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   919
 * @since 6.5.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   920
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   921
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   922
 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   923
 * @param string                          $relative_position   The relative position of the hooked blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   924
 *                                                             Can be one of 'before', 'after', 'first_child', or 'last_child'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   925
 * @param array                           $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   926
 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, or pattern that the anchor block belongs to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   927
 * @return string
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   928
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   929
function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   930
	$anchor_block_type  = $parsed_anchor_block['blockName'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   931
	$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   932
		? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   933
		: array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   934
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   935
	/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   936
	 * Filters the list of hooked block types for a given anchor block type and relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   937
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   938
	 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   939
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   940
	 * @param string[]                        $hooked_block_types The list of hooked block types.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   941
	 * @param string                          $relative_position  The relative position of the hooked blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   942
	 *                                                            Can be one of 'before', 'after', 'first_child', or 'last_child'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   943
	 * @param string                          $anchor_block_type  The anchor block type.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   944
	 * @param WP_Block_Template|WP_Post|array $context            The block template, template part, post object,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   945
	 *                                                            or pattern that the anchor block belongs to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   946
	 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   947
	$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   948
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   949
	$markup = '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   950
	foreach ( $hooked_block_types as $hooked_block_type ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   951
		$parsed_hooked_block = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   952
			'blockName'    => $hooked_block_type,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   953
			'attrs'        => array(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   954
			'innerBlocks'  => array(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   955
			'innerContent' => array(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   956
		);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   957
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   958
		/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   959
		 * Filters the parsed block array for a given hooked block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   960
		 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   961
		 * @since 6.5.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   962
		 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   963
		 * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   964
		 * @param string                          $hooked_block_type   The hooked block type name.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   965
		 * @param string                          $relative_position   The relative position of the hooked block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   966
		 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   967
		 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   968
		 *                                                             or pattern that the anchor block belongs to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   969
		 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   970
		$parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   971
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   972
		/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   973
		 * Filters the parsed block array for a given hooked block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   974
		 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   975
		 * The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   976
		 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   977
		 * @since 6.5.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   978
		 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   979
		 * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   980
		 * @param string                          $hooked_block_type   The hooked block type name.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   981
		 * @param string                          $relative_position   The relative position of the hooked block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   982
		 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
   983
		 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   984
		 *                                                             or pattern that the anchor block belongs to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   985
		 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   986
		$parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   987
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   988
		if ( null === $parsed_hooked_block ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   989
			continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   990
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   991
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   992
		// It's possible that the filter returned a block of a different type, so we explicitly
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   993
		// look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   994
		if (
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   995
			! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   996
			! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   997
		) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   998
			$markup .= serialize_block( $parsed_hooked_block );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   999
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1000
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1001
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1002
	return $markup;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1003
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1004
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1005
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1006
 * Adds a list of hooked block types to an anchor block's ignored hooked block types.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1007
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1008
 * This function is meant for internal use only.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1009
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1010
 * @since 6.5.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1011
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1012
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1013
 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1014
 * @param string                          $relative_position   The relative position of the hooked blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1015
 *                                                             Can be one of 'before', 'after', 'first_child', or 'last_child'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1016
 * @param array                           $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1017
 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, or pattern that the anchor block belongs to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1018
 * @return string Empty string.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1019
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1020
function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1021
	$anchor_block_type  = $parsed_anchor_block['blockName'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1022
	$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1023
		? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1024
		: array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1025
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1026
	/** This filter is documented in wp-includes/blocks.php */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1027
	$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1028
	if ( empty( $hooked_block_types ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1029
		return '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1030
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1031
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1032
	foreach ( $hooked_block_types as $index => $hooked_block_type ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1033
		$parsed_hooked_block = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1034
			'blockName'    => $hooked_block_type,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1035
			'attrs'        => array(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1036
			'innerBlocks'  => array(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1037
			'innerContent' => array(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1038
		);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1039
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1040
		/** This filter is documented in wp-includes/blocks.php */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1041
		$parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1042
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1043
		/** This filter is documented in wp-includes/blocks.php */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1044
		$parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1045
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1046
		if ( null === $parsed_hooked_block ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1047
			unset( $hooked_block_types[ $index ] );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1048
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1049
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1050
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1051
	$previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1052
		? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks']
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1053
		: array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1054
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1055
	$parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1056
		array_merge(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1057
			$previously_ignored_hooked_blocks,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1058
			$hooked_block_types
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1059
		)
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1060
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1061
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1062
	// Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1063
	return '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1064
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1065
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1066
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1067
 * Runs the hooked blocks algorithm on the given content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1068
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1069
 * @since 6.6.0
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1070
 * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1071
 * @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.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1072
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1073
 *
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1074
 * @param string                               $content  Serialized content.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1075
 * @param WP_Block_Template|WP_Post|array|null $context  A block template, template part, post object, or pattern
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1076
 *                                                       that the blocks belong to. If set to `null`, `get_post()`
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1077
 *                                                       will be called to use the current post as context.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1078
 *                                                       Default: `null`.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1079
 * @param callable                             $callback A function that will be called for each block to generate
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1080
 *                                                       the markup for a given list of blocks that are hooked to it.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1081
 *                                                       Default: 'insert_hooked_blocks'.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1082
 * @return string The serialized markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1083
 */
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1084
function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1085
	// Default to the current post if no context is provided.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1086
	if ( null === $context ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1087
		$context = get_post();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1088
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1089
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1090
	$hooked_blocks = get_hooked_blocks();
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1091
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1092
	$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1093
	$after_block_visitor  = null;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1094
	if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1095
		$before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1096
		$after_block_visitor  = make_after_block_visitor( $hooked_blocks, $context, $callback );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1097
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1098
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1099
	$block_allows_multiple_instances = array();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1100
	/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1101
	 * Remove hooked blocks from `$hooked_block_types` if they have `multiple` set to false and
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1102
	 * are already present in `$content`.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1103
	 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1104
	foreach ( $hooked_blocks as $anchor_block_type => $relative_positions ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1105
		foreach ( $relative_positions as $relative_position => $hooked_block_types ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1106
			foreach ( $hooked_block_types as $index => $hooked_block_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1107
				$hooked_block_type_definition =
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1108
					WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1109
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1110
				$block_allows_multiple_instances[ $hooked_block_type ] =
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1111
					block_has_support( $hooked_block_type_definition, 'multiple', true );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1112
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1113
				if (
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1114
					! $block_allows_multiple_instances[ $hooked_block_type ] &&
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1115
					has_block( $hooked_block_type, $content )
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1116
				) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1117
					unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ][ $index ] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1118
				}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1119
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1120
			if ( empty( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1121
				unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1122
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1123
		}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1124
		if ( empty( $hooked_blocks[ $anchor_block_type ] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1125
			unset( $hooked_blocks[ $anchor_block_type ] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1126
		}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1127
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1128
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1129
	/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1130
	 * We also need to cover the case where the hooked block is not present in
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1131
	 * `$content` at first and we're allowed to insert it once -- but not again.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1132
	 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1133
	$suppress_single_instance_blocks = static function ( $hooked_block_types ) use ( &$block_allows_multiple_instances, $content ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1134
		static $single_instance_blocks_present_in_content = array();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1135
		foreach ( $hooked_block_types as $index => $hooked_block_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1136
			if ( ! isset( $block_allows_multiple_instances[ $hooked_block_type ] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1137
				$hooked_block_type_definition =
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1138
					WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1139
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1140
				$block_allows_multiple_instances[ $hooked_block_type ] =
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1141
					block_has_support( $hooked_block_type_definition, 'multiple', true );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1142
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1143
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1144
			if ( $block_allows_multiple_instances[ $hooked_block_type ] ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1145
				continue;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1146
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1147
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1148
			// The block doesn't allow multiple instances, so we need to check if it's already present.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1149
			if (
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1150
				in_array( $hooked_block_type, $single_instance_blocks_present_in_content, true ) ||
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1151
				has_block( $hooked_block_type, $content )
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1152
			) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1153
				unset( $hooked_block_types[ $index ] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1154
			} else {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1155
				// We can insert the block once, but need to remember not to insert it again.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1156
				$single_instance_blocks_present_in_content[] = $hooked_block_type;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1157
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1158
		}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1159
		return $hooked_block_types;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1160
	};
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1161
	add_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1162
	$content = traverse_and_serialize_blocks(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1163
		parse_blocks( $content ),
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1164
		$before_block_visitor,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1165
		$after_block_visitor
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1166
	);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1167
	remove_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1168
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1169
	return $content;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1170
}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1171
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1172
/**
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1173
 * Run the Block Hooks algorithm on a post object's content.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1174
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1175
 * This function is different from `apply_block_hooks_to_content` in that
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1176
 * it takes ignored hooked block information from the post's metadata into
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1177
 * account. This ensures that any blocks hooked as first or last child
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1178
 * of the block that corresponds to the post type are handled correctly.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1179
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1180
 * @since 6.8.0
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1181
 * @access private
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1182
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1183
 * @param string       $content  Serialized content.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1184
 * @param WP_Post|null $post     A post object that the content belongs to. If set to `null`,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1185
 *                               `get_post()` will be called to use the current post as context.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1186
 *                               Default: `null`.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1187
 * @param callable     $callback A function that will be called for each block to generate
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1188
 *                               the markup for a given list of blocks that are hooked to it.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1189
 *                               Default: 'insert_hooked_blocks'.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1190
 * @return string The serialized markup.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1191
 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1192
function apply_block_hooks_to_content_from_post_object( $content, $post = null, $callback = 'insert_hooked_blocks' ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1193
	// Default to the current post if no context is provided.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1194
	if ( null === $post ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1195
		$post = get_post();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1196
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1197
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1198
	if ( ! $post instanceof WP_Post ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1199
		return apply_block_hooks_to_content( $content, $post, $callback );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1200
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1201
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1202
	/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1203
	 * If the content was created using the classic editor or using a single Classic block
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1204
	 * (`core/freeform`), it might not contain any block markup at all.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1205
	 * However, we still might need to inject hooked blocks in the first child or last child
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1206
	 * positions of the parent block. To be able to apply the Block Hooks algorithm, we wrap
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1207
	 * the content in a `core/freeform` wrapper block.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1208
	 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1209
	if ( ! has_blocks( $content ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1210
		$original_content = $content;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1211
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1212
		$content_wrapped_in_classic_block = get_comment_delimited_block_content(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1213
			'core/freeform',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1214
			array(),
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1215
			$content
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1216
		);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1217
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1218
		$content = $content_wrapped_in_classic_block;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1219
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1220
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1221
	$attributes = array();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1222
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1223
	// If context is a post object, `ignoredHookedBlocks` information is stored in its post meta.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1224
	$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1225
	if ( ! empty( $ignored_hooked_blocks ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1226
		$ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1227
		$attributes['metadata'] = array(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1228
			'ignoredHookedBlocks' => $ignored_hooked_blocks,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1229
		);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1230
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1231
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1232
	/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1233
	 * We need to wrap the content in a temporary wrapper block with that metadata
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1234
	 * so the Block Hooks algorithm can insert blocks that are hooked as first or last child
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1235
	 * of the wrapper block.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1236
	 * To that end, we need to determine the wrapper block type based on the post type.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1237
	 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1238
	if ( 'wp_navigation' === $post->post_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1239
		$wrapper_block_type = 'core/navigation';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1240
	} elseif ( 'wp_block' === $post->post_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1241
		$wrapper_block_type = 'core/block';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1242
	} else {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1243
		$wrapper_block_type = 'core/post-content';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1244
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1245
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1246
	$content = get_comment_delimited_block_content(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1247
		$wrapper_block_type,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1248
		$attributes,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1249
		$content
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1250
	);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1251
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1252
	/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1253
	 * We need to avoid inserting any blocks hooked into the `before` and `after` positions
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1254
	 * of the temporary wrapper block that we create to wrap the content.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1255
	 * See https://core.trac.wordpress.org/ticket/63287 for more details.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1256
	 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1257
	$suppress_blocks_from_insertion_before_and_after_wrapper_block = static function ( $hooked_block_types, $relative_position, $anchor_block_type ) use ( $wrapper_block_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1258
		if (
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1259
			$wrapper_block_type === $anchor_block_type &&
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1260
			in_array( $relative_position, array( 'before', 'after' ), true )
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1261
		) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1262
			return array();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1263
		}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1264
		return $hooked_block_types;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1265
	};
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1266
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1267
	// Apply Block Hooks.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1268
	add_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX, 3 );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1269
	$content = apply_block_hooks_to_content( $content, $post, $callback );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1270
	remove_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1271
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1272
	// Finally, we need to remove the temporary wrapper block.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1273
	$content = remove_serialized_parent_block( $content );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1274
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1275
	// If we wrapped the content in a `core/freeform` block, we also need to remove that.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1276
	if ( ! empty( $content_wrapped_in_classic_block ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1277
		/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1278
		 * We cannot simply use remove_serialized_parent_block() here,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1279
		 * as that function assumes that the block wrapper is at the top level.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1280
		 * However, there might now be a hooked block inserted next to it
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1281
		 * (as first or last child of the parent).
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1282
		 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1283
		$content = str_replace( $content_wrapped_in_classic_block, $original_content, $content );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1284
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1285
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1286
	return $content;
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1287
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1288
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1289
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1290
 * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1291
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1292
 * @since 6.6.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1293
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1294
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1295
 * @param string $serialized_block The serialized markup of a block and its inner blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1296
 * @return string The serialized markup of the inner blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1297
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1298
function remove_serialized_parent_block( $serialized_block ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1299
	$start = strpos( $serialized_block, '-->' ) + strlen( '-->' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1300
	$end   = strrpos( $serialized_block, '<!--' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1301
	return substr( $serialized_block, $start, $end - $start );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1302
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1303
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1304
/**
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1305
 * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the wrapper block.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1306
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1307
 * @since 6.7.0
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1308
 * @access private
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1309
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1310
 * @see remove_serialized_parent_block()
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1311
 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1312
 * @param string $serialized_block The serialized markup of a block and its inner blocks.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1313
 * @return string The serialized markup of the wrapper block.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1314
 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1315
function extract_serialized_parent_block( $serialized_block ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1316
	$start = strpos( $serialized_block, '-->' ) + strlen( '-->' );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1317
	$end   = strrpos( $serialized_block, '<!--' );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1318
	return substr( $serialized_block, 0, $start ) . substr( $serialized_block, $end );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1319
}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1320
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1321
/**
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1322
 * Updates the wp_postmeta with the list of ignored hooked blocks
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1323
 * where the inner blocks are stored as post content.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1324
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1325
 * @since 6.6.0
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1326
 * @since 6.8.0 Support non-`wp_navigation` post types.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1327
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1328
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1329
 * @param stdClass $post Post object.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1330
 * @return stdClass The updated post object.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1331
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1332
function update_ignored_hooked_blocks_postmeta( $post ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1333
	/*
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1334
	 * In this scenario the user has likely tried to create a new post object via the REST API.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1335
	 * In which case we won't have a post ID to work with and store meta against.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1336
	 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1337
	if ( empty( $post->ID ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1338
		return $post;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1339
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1340
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1341
	/*
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1342
	 * Skip meta generation when consumers intentionally update specific fields
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1343
	 * and omit the content update.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1344
	 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1345
	if ( ! isset( $post->post_content ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1346
		return $post;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1347
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1348
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1349
	/*
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1350
	 * Skip meta generation if post type is not set.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1351
	 */
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1352
	if ( ! isset( $post->post_type ) ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1353
		return $post;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1354
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1355
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1356
	$attributes = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1357
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1358
	$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1359
	if ( ! empty( $ignored_hooked_blocks ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1360
		$ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1361
		$attributes['metadata'] = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1362
			'ignoredHookedBlocks' => $ignored_hooked_blocks,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1363
		);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1364
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1365
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1366
	if ( 'wp_navigation' === $post->post_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1367
		$wrapper_block_type = 'core/navigation';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1368
	} elseif ( 'wp_block' === $post->post_type ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1369
		$wrapper_block_type = 'core/block';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1370
	} else {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1371
		$wrapper_block_type = 'core/post-content';
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1372
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1373
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1374
	$markup = get_comment_delimited_block_content(
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1375
		$wrapper_block_type,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1376
		$attributes,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1377
		$post->post_content
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1378
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1379
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1380
	$existing_post = get_post( $post->ID );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1381
	// Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1382
	$context          = (object) array_merge( (array) $existing_post, (array) $post );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1383
	$context          = new WP_Post( $context ); // Convert to WP_Post object.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1384
	$serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1385
	$root_block       = parse_blocks( $serialized_block )[0];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1386
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1387
	$ignored_hooked_blocks = isset( $root_block['attrs']['metadata']['ignoredHookedBlocks'] )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1388
		? $root_block['attrs']['metadata']['ignoredHookedBlocks']
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1389
		: array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1390
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1391
	if ( ! empty( $ignored_hooked_blocks ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1392
		$existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1393
		if ( ! empty( $existing_ignored_hooked_blocks ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1394
			$existing_ignored_hooked_blocks = json_decode( $existing_ignored_hooked_blocks, true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1395
			$ignored_hooked_blocks          = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1396
		}
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1397
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1398
		if ( ! isset( $post->meta_input ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1399
			$post->meta_input = array();
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1400
		}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1401
		$post->meta_input['_wp_ignored_hooked_blocks'] = json_encode( $ignored_hooked_blocks );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1402
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1403
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1404
	$post->post_content = remove_serialized_parent_block( $serialized_block );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1405
	return $post;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1406
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1407
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1408
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1409
 * Returns the markup for blocks hooked to the given anchor block in a specific relative position and then
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1410
 * adds a list of hooked block types to an anchor block's ignored hooked block types.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1411
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1412
 * This function is meant for internal use only.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1413
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1414
 * @since 6.6.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1415
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1416
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1417
 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1418
 * @param string                          $relative_position   The relative position of the hooked blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1419
 *                                                             Can be one of 'before', 'after', 'first_child', or 'last_child'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1420
 * @param array                           $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1421
 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, or pattern that the anchor block belongs to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1422
 * @return string
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1423
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1424
function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1425
	$markup  = insert_hooked_blocks( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1426
	$markup .= set_ignored_hooked_blocks_metadata( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1427
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1428
	return $markup;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1429
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1430
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1431
/**
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1432
 * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1433
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1434
 * @since 6.6.0
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1435
 * @since 6.8.0 Support non-`wp_navigation` post types.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1436
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1437
 * @param WP_REST_Response $response The response object.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1438
 * @param WP_Post          $post     Post object.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1439
 * @return WP_REST_Response The response object.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1440
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1441
function insert_hooked_blocks_into_rest_response( $response, $post ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1442
	if ( empty( $response->data['content']['raw'] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1443
		return $response;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1444
	}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1445
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1446
	$response->data['content']['raw'] = apply_block_hooks_to_content_from_post_object(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1447
		$response->data['content']['raw'],
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1448
		$post,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1449
		'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1450
	);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1451
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1452
	// If the rendered content was previously empty, we leave it like that.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1453
	if ( empty( $response->data['content']['rendered'] ) ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1454
		return $response;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1455
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1456
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1457
	// `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1458
	$priority = has_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object' );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1459
	if ( false !== $priority ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1460
		remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1461
	}
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1462
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1463
	/** This filter is documented in wp-includes/post-template.php */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1464
	$response->data['content']['rendered'] = apply_filters(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1465
		'the_content',
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1466
		$response->data['content']['raw']
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1467
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1468
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1469
	// Restore the filter if it was set initially.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1470
	if ( false !== $priority ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1471
		add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1472
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1473
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1474
	return $response;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1475
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1476
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1477
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1478
 * Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1479
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1480
 * The returned function can be used as `$pre_callback` argument to `traverse_and_serialize_block(s)`,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1481
 * where it will inject the `theme` attribute into all Template Part blocks, and prepend the markup for
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1482
 * any blocks hooked `before` the given block and as its parent's `first_child`, respectively.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1483
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1484
 * This function is meant for internal use only.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1485
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1486
 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1487
 * @since 6.5.0 Added $callback argument.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1488
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1489
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1490
 * @param array                           $hooked_blocks An array of blocks hooked to another given block.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1491
 * @param WP_Block_Template|WP_Post|array $context       A block template, template part, post object,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1492
 *                                                       or pattern that the blocks belong to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1493
 * @param callable                        $callback      A function that will be called for each block to generate
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1494
 *                                                       the markup for a given list of blocks that are hooked to it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1495
 *                                                       Default: 'insert_hooked_blocks'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1496
 * @return callable A function that returns the serialized markup for the given block,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1497
 *                  including the markup for any hooked blocks before it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1498
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1499
function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1500
	/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1501
	 * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1502
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1503
	 * If the current block is a Template Part block, inject the `theme` attribute.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1504
	 * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1505
	 * `first_child`, respectively, to the serialized markup for the given block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1506
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1507
	 * @param array $block        The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1508
	 * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1509
	 * @param array $prev         The previous sibling block of the given block. Default null.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1510
	 * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1511
	 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1512
	return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1513
		_inject_theme_attribute_in_template_part_block( $block );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1514
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1515
		$markup = '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1516
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1517
		if ( $parent_block && ! $prev ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1518
			// Candidate for first-child insertion.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1519
			$markup .= call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1520
				$callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1521
				array( &$parent_block, 'first_child', $hooked_blocks, $context )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1522
			);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1523
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1524
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1525
		$markup .= call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1526
			$callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1527
			array( &$block, 'before', $hooked_blocks, $context )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1528
		);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1529
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1530
		return $markup;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1531
	};
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1532
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1533
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1534
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1535
 * Returns a function that injects the hooked blocks after a given block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1536
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1537
 * The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1538
 * where it will append the markup for any blocks hooked `after` the given block and as its parent's
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1539
 * `last_child`, respectively.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1540
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1541
 * This function is meant for internal use only.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1542
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1543
 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1544
 * @since 6.5.0 Added $callback argument.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1545
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1546
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1547
 * @param array                           $hooked_blocks An array of blocks hooked to another block.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1548
 * @param WP_Block_Template|WP_Post|array $context       A block template, template part, post object,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1549
 *                                                       or pattern that the blocks belong to.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1550
 * @param callable                        $callback      A function that will be called for each block to generate
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1551
 *                                                       the markup for a given list of blocks that are hooked to it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1552
 *                                                       Default: 'insert_hooked_blocks'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1553
 * @return callable A function that returns the serialized markup for the given block,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1554
 *                  including the markup for any hooked blocks after it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1555
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1556
function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1557
	/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1558
	 * Injects hooked blocks after the given block, and returns the serialized markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1559
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1560
	 * Append the markup for any blocks hooked `after` the given block and as its parent's
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1561
	 * `last_child`, respectively, to the serialized markup for the given block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1562
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1563
	 * @param array $block        The block to inject the hooked blocks after. Passed by reference.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1564
	 * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1565
	 * @param array $next         The next sibling block of the given block. Default null.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1566
	 * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1567
	 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1568
	return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1569
		$markup = call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1570
			$callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1571
			array( &$block, 'after', $hooked_blocks, $context )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1572
		);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1573
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1574
		if ( $parent_block && ! $next ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1575
			// Candidate for last-child insertion.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1576
			$markup .= call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1577
				$callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1578
				array( &$parent_block, 'last_child', $hooked_blocks, $context )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1579
			);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1580
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1581
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1582
		return $markup;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1583
	};
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1584
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1585
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1586
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1587
 * Given an array of attributes, returns a string in the serialized attributes
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1588
 * format prepared for post content.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1589
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1590
 * The serialized result is a JSON-encoded string, with unicode escape sequence
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1591
 * substitution for characters which might otherwise interfere with embedding
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1592
 * the result in an HTML comment.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1593
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1594
 * This function must produce output that remains in sync with the output of
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1595
 * the serializeAttributes JavaScript function in the block editor in order
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1596
 * to ensure consistent operation between PHP and JavaScript.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1597
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1598
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1599
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1600
 * @param array $block_attributes Attributes object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1601
 * @return string Serialized attributes.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1602
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1603
function serialize_block_attributes( $block_attributes ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1604
	$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1605
	$encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1606
	$encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1607
	$encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1608
	$encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1609
	// Regex: /\\"/
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1610
	$encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1611
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1612
	return $encoded_attributes;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1613
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1614
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1615
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1616
 * Returns the block name to use for serialization. This will remove the default
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1617
 * "core/" namespace from a block name.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1618
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1619
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1620
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1621
 * @param string|null $block_name Optional. Original block name. Null if the block name is unknown,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1622
 *                                e.g. Classic blocks have their name set to null. Default null.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1623
 * @return string Block name to use for serialization.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1624
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1625
function strip_core_block_namespace( $block_name = null ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1626
	if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1627
		return substr( $block_name, 5 );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1628
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1629
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1630
	return $block_name;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1631
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1632
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1633
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1634
 * Returns the content of a block, including comment delimiters.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1635
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1636
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1637
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1638
 * @param string|null $block_name       Block name. Null if the block name is unknown,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1639
 *                                      e.g. Classic blocks have their name set to null.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1640
 * @param array       $block_attributes Block attributes.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1641
 * @param string      $block_content    Block save content.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1642
 * @return string Comment-delimited block content.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1643
 */
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1644
function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1645
	if ( is_null( $block_name ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1646
		return $block_content;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1647
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1648
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1649
	$serialized_block_name = strip_core_block_namespace( $block_name );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1650
	$serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' ';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1651
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1652
	if ( empty( $block_content ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1653
		return sprintf( '<!-- wp:%s %s/-->', $serialized_block_name, $serialized_attributes );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1654
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1655
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1656
	return sprintf(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1657
		'<!-- wp:%s %s-->%s<!-- /wp:%s -->',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1658
		$serialized_block_name,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1659
		$serialized_attributes,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1660
		$block_content,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1661
		$serialized_block_name
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1662
	);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1663
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1664
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1665
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1666
 * Returns the content of a block, including comment delimiters, serializing all
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1667
 * attributes from the given parsed block.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1668
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1669
 * This should be used when preparing a block to be saved to post content.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1670
 * Prefer `render_block` when preparing a block for display. Unlike
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1671
 * `render_block`, this does not evaluate a block's `render_callback`, and will
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1672
 * instead preserve the markup as parsed.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1673
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1674
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1675
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1676
 * @param array $block {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1677
 *     An associative array of a single parsed block object. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1678
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1679
 *     @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1680
 *     @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1681
 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1682
 *                                  have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1683
 *     @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1684
 *     @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1685
 *                                  inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1686
 * }
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1687
 * @return string String of rendered HTML.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1688
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1689
function serialize_block( $block ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1690
	$block_content = '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1691
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1692
	$index = 0;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1693
	foreach ( $block['innerContent'] as $chunk ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1694
		$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1695
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1696
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1697
	if ( ! is_array( $block['attrs'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1698
		$block['attrs'] = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1699
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1700
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1701
	return get_comment_delimited_block_content(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1702
		$block['blockName'],
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1703
		$block['attrs'],
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1704
		$block_content
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1705
	);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1706
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1707
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1708
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1709
 * Returns a joined string of the aggregate serialization of the given
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1710
 * parsed blocks.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1711
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1712
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1713
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1714
 * @param array[] $blocks {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1715
 *     Array of block structures.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1716
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1717
 *     @type array ...$0 {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1718
 *         An associative array of a single parsed block object. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1719
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1720
 *         @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1721
 *         @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1722
 *         @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1723
 *                                      have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1724
 *         @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1725
 *         @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1726
 *                                      inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1727
 *     }
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1728
 * }
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1729
 * @return string String of rendered HTML.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1730
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1731
function serialize_blocks( $blocks ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1732
	return implode( '', array_map( 'serialize_block', $blocks ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1733
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1734
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1735
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1736
 * Traverses a parsed block tree and applies callbacks before and after serializing it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1737
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1738
 * Recursively traverses the block and its inner blocks and applies the two callbacks provided as
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1739
 * arguments, the first one before serializing the block, and the second one after serializing it.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1740
 * If either callback returns a string value, it will be prepended and appended to the serialized
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1741
 * block markup, respectively.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1742
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1743
 * The callbacks will receive a reference to the current block as their first argument, so that they
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1744
 * can also modify it, and the current block's parent block as second argument. Finally, the
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1745
 * `$pre_callback` receives the previous block, whereas the `$post_callback` receives
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1746
 * the next block as third argument.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1747
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1748
 * Serialized blocks are returned including comment delimiters, and with all attributes serialized.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1749
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1750
 * This function should be used when there is a need to modify the saved block, or to inject markup
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1751
 * into the return value. Prefer `serialize_block` when preparing a block to be saved to post content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1752
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1753
 * This function is meant for internal use only.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1754
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1755
 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1756
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1757
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1758
 * @see serialize_block()
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1759
 *
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1760
 * @param array    $block         An associative array of a single parsed block object. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1761
 * @param callable $pre_callback  Callback to run on each block in the tree before it is traversed and serialized.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1762
 *                                It is called with the following arguments: &$block, $parent_block, $previous_block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1763
 *                                Its string return value will be prepended to the serialized block markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1764
 * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1765
 *                                It is called with the following arguments: &$block, $parent_block, $next_block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1766
 *                                Its string return value will be appended to the serialized block markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1767
 * @return string Serialized block markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1768
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1769
function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1770
	$block_content = '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1771
	$block_index   = 0;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1772
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1773
	foreach ( $block['innerContent'] as $chunk ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1774
		if ( is_string( $chunk ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1775
			$block_content .= $chunk;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1776
		} else {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1777
			$inner_block = $block['innerBlocks'][ $block_index ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1778
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1779
			if ( is_callable( $pre_callback ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1780
				$prev = 0 === $block_index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1781
					? null
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1782
					: $block['innerBlocks'][ $block_index - 1 ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1783
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1784
				$block_content .= call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1785
					$pre_callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1786
					array( &$inner_block, &$block, $prev )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1787
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1788
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1789
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1790
			if ( is_callable( $post_callback ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1791
				$next = count( $block['innerBlocks'] ) - 1 === $block_index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1792
					? null
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1793
					: $block['innerBlocks'][ $block_index + 1 ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1794
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1795
				$post_markup = call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1796
					$post_callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1797
					array( &$inner_block, &$block, $next )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1798
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1799
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1800
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1801
			$block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1802
			$block_content .= isset( $post_markup ) ? $post_markup : '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1803
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1804
			++$block_index;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1805
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1806
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1807
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1808
	if ( ! is_array( $block['attrs'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1809
		$block['attrs'] = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1810
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1811
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1812
	return get_comment_delimited_block_content(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1813
		$block['blockName'],
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1814
		$block['attrs'],
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1815
		$block_content
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1816
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1817
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1818
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1819
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1820
 * Replaces patterns in a block tree with their content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1821
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1822
 * @since 6.6.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1823
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1824
 * @param array $blocks An array blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1825
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1826
 * @return array An array of blocks with patterns replaced by their content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1827
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1828
function resolve_pattern_blocks( $blocks ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1829
	static $inner_content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1830
	// Keep track of seen references to avoid infinite loops.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1831
	static $seen_refs = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1832
	$i                = 0;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1833
	while ( $i < count( $blocks ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1834
		if ( 'core/pattern' === $blocks[ $i ]['blockName'] ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1835
			$attrs = $blocks[ $i ]['attrs'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1836
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1837
			if ( empty( $attrs['slug'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1838
				++$i;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1839
				continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1840
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1841
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1842
			$slug = $attrs['slug'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1843
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1844
			if ( isset( $seen_refs[ $slug ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1845
				// Skip recursive patterns.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1846
				array_splice( $blocks, $i, 1 );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1847
				continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1848
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1849
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1850
			$registry = WP_Block_Patterns_Registry::get_instance();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1851
			$pattern  = $registry->get_registered( $slug );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1852
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1853
			// Skip unknown patterns.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1854
			if ( ! $pattern ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1855
				++$i;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1856
				continue;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1857
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1858
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1859
			$blocks_to_insert   = parse_blocks( $pattern['content'] );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1860
			$seen_refs[ $slug ] = true;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1861
			$prev_inner_content = $inner_content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1862
			$inner_content      = null;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1863
			$blocks_to_insert   = resolve_pattern_blocks( $blocks_to_insert );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1864
			$inner_content      = $prev_inner_content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1865
			unset( $seen_refs[ $slug ] );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1866
			array_splice( $blocks, $i, 1, $blocks_to_insert );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1867
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1868
			// If we have inner content, we need to insert nulls in the
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1869
			// inner content array, otherwise serialize_blocks will skip
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1870
			// blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1871
			if ( $inner_content ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1872
				$null_indices  = array_keys( $inner_content, null, true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1873
				$content_index = $null_indices[ $i ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1874
				$nulls         = array_fill( 0, count( $blocks_to_insert ), null );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1875
				array_splice( $inner_content, $content_index, 1, $nulls );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1876
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1877
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1878
			// Skip inserted blocks.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1879
			$i += count( $blocks_to_insert );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1880
		} else {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1881
			if ( ! empty( $blocks[ $i ]['innerBlocks'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1882
				$prev_inner_content           = $inner_content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1883
				$inner_content                = $blocks[ $i ]['innerContent'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1884
				$blocks[ $i ]['innerBlocks']  = resolve_pattern_blocks(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1885
					$blocks[ $i ]['innerBlocks']
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1886
				);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1887
				$blocks[ $i ]['innerContent'] = $inner_content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1888
				$inner_content                = $prev_inner_content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1889
			}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1890
			++$i;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1891
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1892
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1893
	return $blocks;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1894
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1895
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1896
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1897
 * Given an array of parsed block trees, applies callbacks before and after serializing them and
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1898
 * returns their concatenated output.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1899
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1900
 * Recursively traverses the blocks and their inner blocks and applies the two callbacks provided as
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1901
 * arguments, the first one before serializing a block, and the second one after serializing.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1902
 * If either callback returns a string value, it will be prepended and appended to the serialized
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1903
 * block markup, respectively.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1904
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1905
 * The callbacks will receive a reference to the current block as their first argument, so that they
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1906
 * can also modify it, and the current block's parent block as second argument. Finally, the
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1907
 * `$pre_callback` receives the previous block, whereas the `$post_callback` receives
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1908
 * the next block as third argument.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1909
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1910
 * Serialized blocks are returned including comment delimiters, and with all attributes serialized.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1911
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1912
 * This function should be used when there is a need to modify the saved blocks, or to inject markup
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1913
 * into the return value. Prefer `serialize_blocks` when preparing blocks to be saved to post content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1914
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1915
 * This function is meant for internal use only.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1916
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1917
 * @since 6.4.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1918
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1919
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1920
 * @see serialize_blocks()
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1921
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1922
 * @param array[]  $blocks        An array of parsed blocks. See WP_Block_Parser_Block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1923
 * @param callable $pre_callback  Callback to run on each block in the tree before it is traversed and serialized.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1924
 *                                It is called with the following arguments: &$block, $parent_block, $previous_block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1925
 *                                Its string return value will be prepended to the serialized block markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1926
 * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1927
 *                                It is called with the following arguments: &$block, $parent_block, $next_block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1928
 *                                Its string return value will be appended to the serialized block markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1929
 * @return string Serialized block markup.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1930
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1931
function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1932
	$result       = '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1933
	$parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1934
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1935
	$pre_callback_is_callable  = is_callable( $pre_callback );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1936
	$post_callback_is_callable = is_callable( $post_callback );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1937
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1938
	foreach ( $blocks as $index => $block ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1939
		if ( $pre_callback_is_callable ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1940
			$prev = 0 === $index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1941
				? null
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1942
				: $blocks[ $index - 1 ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1943
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1944
			$result .= call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1945
				$pre_callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1946
				array( &$block, &$parent_block, $prev )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1947
			);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1948
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1949
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  1950
		if ( $post_callback_is_callable ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1951
			$next = count( $blocks ) - 1 === $index
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1952
				? null
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1953
				: $blocks[ $index + 1 ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1954
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1955
			$post_markup = call_user_func_array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1956
				$post_callback,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1957
				array( &$block, &$parent_block, $next )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1958
			);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1959
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1960
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1961
		$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1962
		$result .= isset( $post_markup ) ? $post_markup : '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1963
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1964
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1965
	return $result;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1966
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1967
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1968
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1969
 * Filters and sanitizes block content to remove non-allowable HTML
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1970
 * from parsed block attribute values.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1971
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1972
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1973
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1974
 * @param string         $text              Text that may contain block content.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1975
 * @param array[]|string $allowed_html      Optional. An array of allowed HTML elements and attributes,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1976
 *                                          or a context name such as 'post'. See wp_kses_allowed_html()
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1977
 *                                          for the list of accepted context names. Default 'post'.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1978
 * @param string[]       $allowed_protocols Optional. Array of allowed URL protocols.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1979
 *                                          Defaults to the result of wp_allowed_protocols().
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1980
 * @return string The filtered and sanitized content result.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1981
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1982
function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1983
	$result = '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1984
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1985
	if ( str_contains( $text, '<!--' ) && str_contains( $text, '--->' ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1986
		$text = preg_replace_callback( '%<!--(.*?)--->%', '_filter_block_content_callback', $text );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1987
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1988
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1989
	$blocks = parse_blocks( $text );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1990
	foreach ( $blocks as $block ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1991
		$block   = filter_block_kses( $block, $allowed_html, $allowed_protocols );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1992
		$result .= serialize_block( $block );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1993
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1994
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1995
	return $result;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1996
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1997
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  1998
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  1999
 * Callback used for regular expression replacement in filter_block_content().
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2000
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2001
 * @since 6.2.1
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2002
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2003
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2004
 * @param array $matches Array of preg_replace_callback matches.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2005
 * @return string Replacement string.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2006
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2007
function _filter_block_content_callback( $matches ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2008
	return '<!--' . rtrim( $matches[1], '-' ) . '-->';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2009
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2010
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2011
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2012
 * Filters and sanitizes a parsed block to remove non-allowable HTML
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2013
 * from block attribute values.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2014
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2015
 * @since 5.3.1
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2016
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2017
 * @param WP_Block_Parser_Block $block             The parsed block object.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2018
 * @param array[]|string        $allowed_html      An array of allowed HTML elements and attributes,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2019
 *                                                 or a context name such as 'post'. See wp_kses_allowed_html()
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2020
 *                                                 for the list of accepted context names.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2021
 * @param string[]              $allowed_protocols Optional. Array of allowed URL protocols.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2022
 *                                                 Defaults to the result of wp_allowed_protocols().
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2023
 * @return array The filtered and sanitized block object result.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2024
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2025
function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2026
	$block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols, $block );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2027
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2028
	if ( is_array( $block['innerBlocks'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2029
		foreach ( $block['innerBlocks'] as $i => $inner_block ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2030
			$block['innerBlocks'][ $i ] = filter_block_kses( $inner_block, $allowed_html, $allowed_protocols );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2031
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2032
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2033
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2034
	return $block;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2035
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2036
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2037
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2038
 * Filters and sanitizes a parsed block attribute value to remove
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2039
 * non-allowable HTML.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2040
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2041
 * @since 5.3.1
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2042
 * @since 6.5.5 Added the `$block_context` parameter.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2043
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2044
 * @param string[]|string $value             The attribute value to filter.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2045
 * @param array[]|string  $allowed_html      An array of allowed HTML elements and attributes,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2046
 *                                           or a context name such as 'post'. See wp_kses_allowed_html()
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2047
 *                                           for the list of accepted context names.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2048
 * @param string[]        $allowed_protocols Optional. Array of allowed URL protocols.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2049
 *                                           Defaults to the result of wp_allowed_protocols().
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2050
 * @param array           $block_context     Optional. The block the attribute belongs to, in parsed block array format.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2051
 * @return string[]|string The filtered and sanitized result.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2052
 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2053
function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array(), $block_context = null ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2054
	if ( is_array( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2055
		foreach ( $value as $key => $inner_value ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2056
			$filtered_key   = filter_block_kses_value( $key, $allowed_html, $allowed_protocols, $block_context );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2057
			$filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols, $block_context );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2058
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2059
			if ( isset( $block_context['blockName'] ) && 'core/template-part' === $block_context['blockName'] ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2060
				$filtered_value = filter_block_core_template_part_attributes( $filtered_value, $filtered_key, $allowed_html );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2061
			}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2062
			if ( $filtered_key !== $key ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2063
				unset( $value[ $key ] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2064
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2065
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2066
			$value[ $filtered_key ] = $filtered_value;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2067
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2068
	} elseif ( is_string( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2069
		return wp_kses( $value, $allowed_html, $allowed_protocols );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2070
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2071
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2072
	return $value;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2073
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2074
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2075
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2076
 * Sanitizes the value of the Template Part block's `tagName` attribute.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2077
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2078
 * @since 6.5.5
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2079
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2080
 * @param string         $attribute_value The attribute value to filter.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2081
 * @param string         $attribute_name  The attribute name.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2082
 * @param array[]|string $allowed_html    An array of allowed HTML elements and attributes,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2083
 *                                        or a context name such as 'post'. See wp_kses_allowed_html()
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2084
 *                                        for the list of accepted context names.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2085
 * @return string The sanitized attribute value.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2086
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2087
function filter_block_core_template_part_attributes( $attribute_value, $attribute_name, $allowed_html ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2088
	if ( empty( $attribute_value ) || 'tagName' !== $attribute_name ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2089
		return $attribute_value;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2090
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2091
	if ( ! is_array( $allowed_html ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2092
		$allowed_html = wp_kses_allowed_html( $allowed_html );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2093
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2094
	return isset( $allowed_html[ $attribute_value ] ) ? $attribute_value : '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2095
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2096
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2097
/**
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
 * Parses blocks out of a content string, and renders those appropriate for the excerpt.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
 * As the excerpt should be a small string of text relevant to the full post content,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
 * this function renders the blocks that are most likely to contain such text.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2105
 * @param string $content The content to parse.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
 * @return string The parsed and filtered content.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
function excerpt_remove_blocks( $content ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2109
	if ( ! has_blocks( $content ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2110
		return $content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2111
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2112
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
	$allowed_inner_blocks = array(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
		// Classic blocks have their blockName set to null.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2115
		null,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2116
		'core/freeform',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2117
		'core/heading',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2118
		'core/html',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2119
		'core/list',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
		'core/media-text',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2121
		'core/paragraph',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
		'core/preformatted',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2123
		'core/pullquote',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2124
		'core/quote',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2125
		'core/table',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
		'core/verse',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2127
	);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2129
	$allowed_wrapper_blocks = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2130
		'core/columns',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2131
		'core/column',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2132
		'core/group',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2133
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2134
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2135
	/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2136
	 * Filters the list of blocks that can be used as wrapper blocks, allowing
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2137
	 * excerpts to be generated from the `innerBlocks` of these wrappers.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2138
	 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2139
	 * @since 5.8.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2140
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2141
	 * @param string[] $allowed_wrapper_blocks The list of names of allowed wrapper blocks.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2142
	 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2143
	$allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2144
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2145
	$allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
	/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
	 * Filters the list of blocks that can contribute to the excerpt.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
	 * If a dynamic block is added to this list, it must not generate another
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
	 * excerpt, as this will cause an infinite loop to occur.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
	 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2155
	 * @param string[] $allowed_blocks The list of names of allowed blocks.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
	 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
	$allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
	$blocks         = parse_blocks( $content );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
	$output         = '';
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
	foreach ( $blocks as $block ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
		if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
			if ( ! empty( $block['innerBlocks'] ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2164
				if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2165
					$output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2166
					continue;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2167
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2168
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
				// Skip the block if it has disallowed or nested inner blocks.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
				foreach ( $block['innerBlocks'] as $inner_block ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
					if (
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2172
						! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2173
						! empty( $inner_block['innerBlocks'] )
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2174
					) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2175
						continue 2;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2176
					}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2177
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
			$output .= render_block( $block );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2183
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
	return $output;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2188
 * Parses footnotes markup out of a content string,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2189
 * and renders those appropriate for the excerpt.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2190
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2191
 * @since 6.3.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2192
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2193
 * @param string $content The content to parse.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2194
 * @return string The parsed and filtered content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2195
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2196
function excerpt_remove_footnotes( $content ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2197
	if ( ! str_contains( $content, 'data-fn=' ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2198
		return $content;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2199
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2200
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2201
	return preg_replace(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2202
		'_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2203
		'',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2204
		$content
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2205
	);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2206
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2207
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2208
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2209
 * Renders inner blocks from the allowed wrapper blocks
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2210
 * for generating an excerpt.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2212
 * @since 5.8.0
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
 * @access private
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2215
 * @param array $parsed_block   The parsed block.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
 * @param array $allowed_blocks The list of allowed inner blocks.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2217
 * @return string The rendered inner blocks.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2218
 */
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2219
function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2220
	$output = '';
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2221
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2222
	foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2223
		if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2224
			continue;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2225
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2226
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2227
		if ( empty( $inner_block['innerBlocks'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2228
			$output .= render_block( $inner_block );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2229
		} else {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2230
			$output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
	return $output;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2235
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2236
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2237
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2238
 * Renders a single block into a HTML string.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2239
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2240
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2241
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2242
 * @global WP_Post $post The post to edit.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2243
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2244
 * @param array $parsed_block {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2245
 *     An associative array of the block being rendered. See WP_Block_Parser_Block.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2246
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2247
 *     @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2248
 *     @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2249
 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2250
 *                                  have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2251
 *     @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2252
 *     @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2253
 *                                  inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2254
 * }
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2255
 * @return string String of rendered HTML.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2256
 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2257
function render_block( $parsed_block ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2258
	global $post;
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2259
	$parent_block = null;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
	/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2262
	 * Allows render_block() to be short-circuited, by returning a non-null value.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
	 * @since 5.1.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2265
	 * @since 5.9.0 The `$parent_block` parameter was added.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2267
	 * @param string|null   $pre_render   The pre-rendered content. Default null.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2268
	 * @param array         $parsed_block {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2269
	 *     An associative array of the block being rendered. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2270
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2271
	 *     @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2272
	 *     @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2273
	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2274
	 *                                  have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2275
	 *     @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2276
	 *     @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2277
	 *                                  inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2278
	 * }
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2279
	 * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2280
	 */
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2281
	$pre_render = apply_filters( 'pre_render_block', null, $parsed_block, $parent_block );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2282
	if ( ! is_null( $pre_render ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2283
		return $pre_render;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2284
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2286
	$source_block = $parsed_block;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2287
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
	/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
	 * Filters the block being rendered in render_block(), before it's processed.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2290
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2291
	 * @since 5.1.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2292
	 * @since 5.9.0 The `$parent_block` parameter was added.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2293
	 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2294
	 * @param array         $parsed_block {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2295
	 *     An associative array of the block being rendered. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2296
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2297
	 *     @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2298
	 *     @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2299
	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2300
	 *                                  have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2301
	 *     @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2302
	 *     @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2303
	 *                                  inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2304
	 * }
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2305
	 * @param array         $source_block {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2306
	 *     An un-modified copy of `$parsed_block`, as it appeared in the source content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2307
	 *     See WP_Block_Parser_Block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2308
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2309
	 *     @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2310
	 *     @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2311
	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2312
	 *                                  have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2313
	 *     @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2314
	 *     @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2315
	 *                                  inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2316
	 * }
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2317
	 * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2318
	 */
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2319
	$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block, $parent_block );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2320
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2321
	$context = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2322
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2323
	if ( $post instanceof WP_Post ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2324
		$context['postId'] = $post->ID;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2325
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2326
		/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2327
		 * The `postType` context is largely unnecessary server-side, since the ID
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2328
		 * is usually sufficient on its own. That being said, since a block's
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2329
		 * manifest is expected to be shared between the server and the client,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2330
		 * it should be included to consistently fulfill the expectation.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2331
		 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2332
		$context['postType'] = $post->post_type;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2333
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2334
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2335
	/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2336
	 * Filters the default context provided to a rendered block.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2337
	 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2338
	 * @since 5.5.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2339
	 * @since 5.9.0 The `$parent_block` parameter was added.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2340
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2341
	 * @param array         $context      Default context.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2342
	 * @param array         $parsed_block {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2343
	 *     An associative array of the block being rendered. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2344
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2345
	 *     @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2346
	 *     @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2347
	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2348
	 *                                  have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2349
	 *     @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2350
	 *     @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2351
	 *                                  inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2352
	 * }
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2353
	 * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
	 */
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2355
	$context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2356
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2357
	$block = new WP_Block( $parsed_block, $context );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2358
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2359
	return $block->render();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2360
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2361
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2362
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
 * Parses blocks out of a content string.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2364
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2367
 * @param string $content Post content.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2368
 * @return array[] {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2369
 *     Array of block structures.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2370
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2371
 *     @type array ...$0 {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2372
 *         An associative array of a single parsed block object. See WP_Block_Parser_Block.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2373
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2374
 *         @type string   $blockName    Name of block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2375
 *         @type array    $attrs        Attributes from block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2376
 *         @type array[]  $innerBlocks  List of inner blocks. An array of arrays that
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2377
 *                                      have the same structure as this one.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2378
 *         @type string   $innerHTML    HTML from inside block comment delimiters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2379
 *         @type array    $innerContent List of string fragments and null markers where
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2380
 *                                      inner blocks were found.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2381
 *     }
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2382
 * }
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2383
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2384
function parse_blocks( $content ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2385
	/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2386
	 * Filter to allow plugins to replace the server-side block parser.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
	 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2390
	 * @param string $parser_class Name of block parser class.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2391
	 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2392
	$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2393
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2394
	$parser = new $parser_class();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
	return $parser->parse( $content );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
 * Parses dynamic blocks out of `post_content` and re-renders them.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2403
 * @param string $content Post content.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2404
 * @return string Updated post content.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2405
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2406
function do_blocks( $content ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2407
	$blocks                = parse_blocks( $content );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2408
	$top_level_block_count = count( $blocks );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2409
	$output                = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2410
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2411
	/**
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2412
	 * Parsed blocks consist of a list of top-level blocks. Those top-level
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2413
	 * blocks may themselves contain nested inner blocks. However, every
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2414
	 * top-level block is rendered independently, meaning there are no data
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2415
	 * dependencies between them.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2416
	 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2417
	 * Ideally, therefore, the parser would only need to parse one complete
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2418
	 * top-level block at a time, render it, and move on. Unfortunately, this
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2419
	 * is not possible with {@see \parse_blocks()} because it must parse the
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2420
	 * entire given document at once.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2421
	 *
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2422
	 * While the current implementation prevents this optimization, it’s still
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2423
	 * possible to reduce the peak memory use when calls to `render_block()`
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2424
	 * on those top-level blocks are memory-heavy (which many of them are).
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2425
	 * By setting each parsed block to `NULL` after rendering it, any memory
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2426
	 * allocated during the render will be freed and reused for the next block.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2427
	 * Before making this change, that memory was retained and would lead to
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2428
	 * out-of-memory crashes for certain posts that now run with this change.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2429
	 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2430
	for ( $i = 0; $i < $top_level_block_count; $i++ ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2431
		$output      .= render_block( $blocks[ $i ] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2432
		$blocks[ $i ] = null;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2433
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2434
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2435
	// If there are blocks in this content, we shouldn't run wpautop() on it later.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2436
	$priority = has_filter( 'the_content', 'wpautop' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2437
	if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2438
		remove_filter( 'the_content', 'wpautop', $priority );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2439
		add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2440
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2441
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2442
	return $output;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2443
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2444
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2445
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2446
 * If do_blocks() needs to remove wpautop() from the `the_content` filter, this re-adds it afterwards,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2447
 * for subsequent `the_content` usage.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2448
 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2449
 * @since 5.0.0
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2450
 * @access private
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2451
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2452
 * @param string $content The post content running through this filter.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2453
 * @return string The unmodified content.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2454
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2455
function _restore_wpautop_hook( $content ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2456
	$current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2457
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2458
	add_filter( 'the_content', 'wpautop', $current_priority - 1 );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2459
	remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2460
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2461
	return $content;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2462
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2463
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2464
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2465
 * Returns the current version of the block format that the content string is using.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2466
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2467
 * If the string doesn't contain blocks, it returns 0.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2468
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2469
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2470
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2471
 * @param string $content Content to test.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2472
 * @return int The block format version is 1 if the content contains one or more blocks, 0 otherwise.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2473
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2474
function block_version( $content ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2475
	return has_blocks( $content ) ? 1 : 0;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2476
}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2477
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2478
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2479
 * Registers a new block style.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2480
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2481
 * @since 5.3.0
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2482
 * @since 6.6.0 Added support for registering styles for multiple block types.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2483
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2484
 * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2485
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2486
 * @param string|string[] $block_name       Block type name including namespace or array of namespaced block type names.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2487
 * @param array           $style_properties Array containing the properties of the style name, label,
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2488
 *                                          style_handle (name of the stylesheet to be enqueued),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2489
 *                                          inline_style (string containing the CSS to be added),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2490
 *                                          style_data (theme.json-like array to generate CSS from).
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2491
 *                                          See WP_Block_Styles_Registry::register().
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2492
 * @return bool True if the block style was registered with success and false otherwise.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2493
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2494
function register_block_style( $block_name, $style_properties ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2495
	return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2496
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2497
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2498
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2499
 * Unregisters a block style.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2500
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2501
 * @since 5.3.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2502
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2503
 * @param string $block_name       Block type name including namespace.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2504
 * @param string $block_style_name Block style name.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2505
 * @return bool True if the block style was unregistered with success and false otherwise.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2506
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2507
function unregister_block_style( $block_name, $block_style_name ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2508
	return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
  2509
}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2510
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2511
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2512
 * Checks whether the current block type supports the feature requested.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2513
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2514
 * @since 5.8.0
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2515
 * @since 6.4.0 The `$feature` parameter now supports a string.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2516
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2517
 * @param WP_Block_Type $block_type    Block type to check for support.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2518
 * @param string|array  $feature       Feature slug, or path to a specific feature to check support for.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2519
 * @param mixed         $default_value Optional. Fallback value for feature support. Default false.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2520
 * @return bool Whether the feature is supported.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2521
 */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2522
function block_has_support( $block_type, $feature, $default_value = false ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2523
	$block_support = $default_value;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2524
	if ( $block_type instanceof WP_Block_Type ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2525
		if ( is_array( $feature ) && count( $feature ) === 1 ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2526
			$feature = $feature[0];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2527
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2528
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2529
		if ( is_array( $feature ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2530
			$block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2531
		} elseif ( isset( $block_type->supports[ $feature ] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2532
			$block_support = $block_type->supports[ $feature ];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2533
		}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2534
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2535
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2536
	return true === $block_support || is_array( $block_support );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2537
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2538
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2539
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2540
 * Converts typography keys declared under `supports.*` to `supports.typography.*`.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2541
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2542
 * Displays a `_doing_it_wrong()` notice when a block using the older format is detected.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2543
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2544
 * @since 5.8.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2545
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2546
 * @param array $metadata Metadata for registering a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2547
 * @return array Filtered metadata for registering a block type.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2548
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2549
function wp_migrate_old_typography_shape( $metadata ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2550
	if ( ! isset( $metadata['supports'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2551
		return $metadata;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2552
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2553
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2554
	$typography_keys = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2555
		'__experimentalFontFamily',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2556
		'__experimentalFontStyle',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2557
		'__experimentalFontWeight',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2558
		'__experimentalLetterSpacing',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2559
		'__experimentalTextDecoration',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2560
		'__experimentalTextTransform',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2561
		'fontSize',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2562
		'lineHeight',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2563
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2564
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2565
	foreach ( $typography_keys as $typography_key ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2566
		$support_for_key = isset( $metadata['supports'][ $typography_key ] ) ? $metadata['supports'][ $typography_key ] : null;
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2567
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2568
		if ( null !== $support_for_key ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2569
			_doing_it_wrong(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2570
				'register_block_type_from_metadata()',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2571
				sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2572
					/* translators: 1: Block type, 2: Typography supports key, e.g: fontSize, lineHeight, etc. 3: block.json, 4: Old metadata key, 5: New metadata key. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2573
					__( 'Block "%1$s" is declaring %2$s support in %3$s file under %4$s. %2$s support is now declared under %5$s.' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2574
					$metadata['name'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2575
					"<code>$typography_key</code>",
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2576
					'<code>block.json</code>',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2577
					"<code>supports.$typography_key</code>",
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2578
					"<code>supports.typography.$typography_key</code>"
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2579
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2580
				'5.8.0'
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2581
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2582
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2583
			_wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2584
			unset( $metadata['supports'][ $typography_key ] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2585
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2586
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2587
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2588
	return $metadata;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2589
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2590
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2591
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2592
 * Helper function that constructs a WP_Query args array from
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2593
 * a `Query` block properties.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2594
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2595
 * It's used in Query Loop, Query Pagination Numbers and Query Pagination Next blocks.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2596
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2597
 * @since 5.8.0
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2598
 * @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query.
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2599
 * @since 6.7.0 Added support for the `format` property in query.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2600
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2601
 * @param WP_Block $block Block instance.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2602
 * @param int      $page  Current query's page.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2603
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2604
 * @return array Returns the constructed WP_Query arguments.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2605
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2606
function build_query_vars_from_query_block( $block, $page ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2607
	$query = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2608
		'post_type'    => 'post',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2609
		'order'        => 'DESC',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2610
		'orderby'      => 'date',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2611
		'post__not_in' => array(),
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2612
		'tax_query'    => array(),
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2613
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2614
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2615
	if ( isset( $block->context['query'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2616
		if ( ! empty( $block->context['query']['postType'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2617
			$post_type_param = $block->context['query']['postType'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2618
			if ( is_post_type_viewable( $post_type_param ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2619
				$query['post_type'] = $post_type_param;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2620
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2621
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2622
		if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2623
			$sticky = get_option( 'sticky_posts' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2624
			if ( 'only' === $block->context['query']['sticky'] ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2625
				/*
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2626
				 * Passing an empty array to post__in will return have_posts() as true (and all posts will be returned).
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2627
				 * Logic should be used before hand to determine if WP_Query should be used in the event that the array
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2628
				 * being passed to post__in is empty.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2629
				 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2630
				 * @see https://core.trac.wordpress.org/ticket/28099
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2631
				 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2632
				$query['post__in']            = ! empty( $sticky ) ? $sticky : array( 0 );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2633
				$query['ignore_sticky_posts'] = 1;
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2634
			} elseif ( 'exclude' === $block->context['query']['sticky'] ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2635
				$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2636
			} elseif ( 'ignore' === $block->context['query']['sticky'] ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2637
				$query['ignore_sticky_posts'] = 1;
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2638
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2639
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2640
		if ( ! empty( $block->context['query']['exclude'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2641
			$excluded_post_ids     = array_map( 'intval', $block->context['query']['exclude'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2642
			$excluded_post_ids     = array_filter( $excluded_post_ids );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2643
			$query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2644
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2645
		if (
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2646
			isset( $block->context['query']['perPage'] ) &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2647
			is_numeric( $block->context['query']['perPage'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2648
		) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2649
			$per_page = absint( $block->context['query']['perPage'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2650
			$offset   = 0;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2651
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2652
			if (
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2653
				isset( $block->context['query']['offset'] ) &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2654
				is_numeric( $block->context['query']['offset'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2655
			) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2656
				$offset = absint( $block->context['query']['offset'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2657
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2658
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2659
			$query['offset']         = ( $per_page * ( $page - 1 ) ) + $offset;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2660
			$query['posts_per_page'] = $per_page;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2661
		}
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2662
		// Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2663
		if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2664
			$tax_query_back_compat = array();
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2665
			if ( ! empty( $block->context['query']['categoryIds'] ) ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2666
				$tax_query_back_compat[] = array(
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2667
					'taxonomy'         => 'category',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2668
					'terms'            => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2669
					'include_children' => false,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2670
				);
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2671
			}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2672
			if ( ! empty( $block->context['query']['tagIds'] ) ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2673
				$tax_query_back_compat[] = array(
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2674
					'taxonomy'         => 'post_tag',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2675
					'terms'            => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2676
					'include_children' => false,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2677
				);
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2678
			}
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2679
			$query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2680
		}
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2681
		if ( ! empty( $block->context['query']['taxQuery'] ) ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2682
			$tax_query = array();
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2683
			foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2684
				if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2685
					$tax_query[] = array(
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2686
						'taxonomy'         => $taxonomy,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2687
						'terms'            => array_filter( array_map( 'intval', $terms ) ),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2688
						'include_children' => false,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2689
					);
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2690
				}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2691
			}
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2692
			$query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2693
		}
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2694
		if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2695
			$formats = $block->context['query']['format'];
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2696
			/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2697
			 * Validate that the format is either `standard` or a supported post format.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2698
			 * - First, add `standard` to the array of valid formats.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2699
			 * - Then, remove any invalid formats.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2700
			 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2701
			$valid_formats = array_merge( array( 'standard' ), get_post_format_slugs() );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2702
			$formats       = array_intersect( $formats, $valid_formats );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2703
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2704
			/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2705
			 * The relation needs to be set to `OR` since the request can contain
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2706
			 * two separate conditions. The user may be querying for items that have
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2707
			 * either the `standard` format or a specific format.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2708
			 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2709
			$formats_query = array( 'relation' => 'OR' );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2710
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2711
			/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2712
			 * The default post format, `standard`, is not stored in the database.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2713
			 * If `standard` is part of the request, the query needs to exclude all post items that
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2714
			 * have a format assigned.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2715
			 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2716
			if ( in_array( 'standard', $formats, true ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2717
				$formats_query[] = array(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2718
					'taxonomy' => 'post_format',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2719
					'field'    => 'slug',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2720
					'operator' => 'NOT EXISTS',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2721
				);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2722
				// Remove the `standard` format, since it cannot be queried.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2723
				unset( $formats[ array_search( 'standard', $formats, true ) ] );
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2724
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2725
			// Add any remaining formats to the formats query.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2726
			if ( ! empty( $formats ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2727
				// Add the `post-format-` prefix.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2728
				$terms           = array_map(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2729
					static function ( $format ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2730
						return "post-format-$format";
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2731
					},
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2732
					$formats
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2733
				);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2734
				$formats_query[] = array(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2735
					'taxonomy' => 'post_format',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2736
					'field'    => 'slug',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2737
					'terms'    => $terms,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2738
					'operator' => 'IN',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2739
				);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2740
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2741
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2742
			/*
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2743
			 * Add `$formats_query` to `$query`, as long as it contains more than one key:
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2744
			 * If `$formats_query` only contains the initial `relation` key, there are no valid formats to query,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2745
			 * and the query should not be modified.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2746
			 */
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2747
			if ( count( $formats_query ) > 1 ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2748
				// Enable filtering by both post formats and other taxonomies by combining them with `AND`.
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2749
				if ( empty( $query['tax_query'] ) ) {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2750
					$query['tax_query'] = $formats_query;
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2751
				} else {
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2752
					$query['tax_query'] = array(
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2753
						'relation' => 'AND',
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2754
						$query['tax_query'],
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2755
						$formats_query,
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2756
					);
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2757
				}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2758
			}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2759
		}
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2760
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2761
		if (
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2762
			isset( $block->context['query']['order'] ) &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2763
				in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2764
		) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2765
			$query['order'] = strtoupper( $block->context['query']['order'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2766
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2767
		if ( isset( $block->context['query']['orderBy'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2768
			$query['orderby'] = $block->context['query']['orderBy'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2769
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2770
		if (
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2771
			isset( $block->context['query']['author'] )
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2772
		) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2773
			if ( is_array( $block->context['query']['author'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2774
				$query['author__in'] = array_filter( array_map( 'intval', $block->context['query']['author'] ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2775
			} elseif ( is_string( $block->context['query']['author'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2776
				$query['author__in'] = array_filter( array_map( 'intval', explode( ',', $block->context['query']['author'] ) ) );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2777
			} elseif ( is_int( $block->context['query']['author'] ) && $block->context['query']['author'] > 0 ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2778
				$query['author'] = $block->context['query']['author'];
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2779
			}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2780
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2781
		if ( ! empty( $block->context['query']['search'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2782
			$query['s'] = $block->context['query']['search'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2783
		}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2784
		if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
22
8c2e4d02f4ef Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents: 21
diff changeset
  2785
			$query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) );
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2786
		}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2787
	}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2788
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2789
	/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2790
	 * Filters the arguments which will be passed to `WP_Query` for the Query Loop Block.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2791
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2792
	 * Anything to this filter should be compatible with the `WP_Query` API to form
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2793
	 * the query context which will be passed down to the Query Loop Block's children.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2794
	 * This can help, for example, to include additional settings or meta queries not
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2795
	 * directly supported by the core Query Loop Block, and extend its capabilities.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2796
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2797
	 * Please note that this will only influence the query that will be rendered on the
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2798
	 * front-end. The editor preview is not affected by this filter. Also, worth noting
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2799
	 * that the editor preview uses the REST API, so, ideally, one should aim to provide
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2800
	 * attributes which are also compatible with the REST API, in order to be able to
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2801
	 * implement identical queries on both sides.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2802
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2803
	 * @since 6.1.0
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2804
	 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2805
	 * @param array    $query Array containing parameters for `WP_Query` as parsed by the block context.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2806
	 * @param WP_Block $block Block instance.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2807
	 * @param int      $page  Current query's page.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2808
	 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2809
	return apply_filters( 'query_loop_block_query_vars', $query, $block, $page );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2810
}
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2811
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2812
/**
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2813
 * Helper function that returns the proper pagination arrow HTML for
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2814
 * `QueryPaginationNext` and `QueryPaginationPrevious` blocks based
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2815
 * on the provided `paginationArrow` from `QueryPagination` context.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2816
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2817
 * It's used in QueryPaginationNext and QueryPaginationPrevious blocks.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2818
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2819
 * @since 5.9.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2820
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2821
 * @param WP_Block $block   Block instance.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2822
 * @param bool     $is_next Flag for handling `next/previous` blocks.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2823
 * @return string|null The pagination arrow HTML or null if there is none.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2824
 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2825
function get_query_pagination_arrow( $block, $is_next ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2826
	$arrow_map = array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2827
		'none'    => '',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2828
		'arrow'   => array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2829
			'next'     => '→',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2830
			'previous' => '←',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2831
		),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2832
		'chevron' => array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2833
			'next'     => '»',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2834
			'previous' => '«',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2835
		),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2836
	);
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2837
	if ( ! empty( $block->context['paginationArrow'] ) && array_key_exists( $block->context['paginationArrow'], $arrow_map ) && ! empty( $arrow_map[ $block->context['paginationArrow'] ] ) ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2838
		$pagination_type = $is_next ? 'next' : 'previous';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2839
		$arrow_attribute = $block->context['paginationArrow'];
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2840
		$arrow           = $arrow_map[ $block->context['paginationArrow'] ][ $pagination_type ];
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2841
		$arrow_classes   = "wp-block-query-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2842
		return "<span class='$arrow_classes' aria-hidden='true'>$arrow</span>";
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2843
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2844
	return null;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2845
}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2846
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2847
/**
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2848
 * Helper function that constructs a comment query vars array from the passed
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2849
 * block properties.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2850
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2851
 * It's used with the Comment Query Loop inner blocks.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2852
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2853
 * @since 6.0.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2854
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2855
 * @param WP_Block $block Block instance.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2856
 * @return array Returns the comment query parameters to use with the
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2857
 *               WP_Comment_Query constructor.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2858
 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2859
function build_comment_query_vars_from_block( $block ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2860
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2861
	$comment_args = array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2862
		'orderby'       => 'comment_date_gmt',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2863
		'order'         => 'ASC',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2864
		'status'        => 'approve',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2865
		'no_found_rows' => false,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2866
	);
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2867
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2868
	if ( is_user_logged_in() ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2869
		$comment_args['include_unapproved'] = array( get_current_user_id() );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2870
	} else {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2871
		$unapproved_email = wp_get_unapproved_comment_author_email();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2872
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2873
		if ( $unapproved_email ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2874
			$comment_args['include_unapproved'] = array( $unapproved_email );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2875
		}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2876
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2877
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2878
	if ( ! empty( $block->context['postId'] ) ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2879
		$comment_args['post_id'] = (int) $block->context['postId'];
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2880
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2881
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2882
	if ( get_option( 'thread_comments' ) ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2883
		$comment_args['hierarchical'] = 'threaded';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2884
	} else {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2885
		$comment_args['hierarchical'] = false;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2886
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2887
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2888
	if ( get_option( 'page_comments' ) === '1' || get_option( 'page_comments' ) === true ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2889
		$per_page     = get_option( 'comments_per_page' );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2890
		$default_page = get_option( 'default_comments_page' );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2891
		if ( $per_page > 0 ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2892
			$comment_args['number'] = $per_page;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2893
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2894
			$page = (int) get_query_var( 'cpage' );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2895
			if ( $page ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2896
				$comment_args['paged'] = $page;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2897
			} elseif ( 'oldest' === $default_page ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2898
				$comment_args['paged'] = 1;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2899
			} elseif ( 'newest' === $default_page ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2900
				$max_num_pages = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2901
				if ( 0 !== $max_num_pages ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2902
					$comment_args['paged'] = $max_num_pages;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2903
				}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2904
			}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2905
		}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2906
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2907
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2908
	return $comment_args;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2909
}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2910
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2911
/**
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2912
 * Helper function that returns the proper pagination arrow HTML for
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2913
 * `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based on the
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2914
 * provided `paginationArrow` from `CommentsPagination` context.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2915
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2916
 * It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2917
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2918
 * @since 6.0.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2919
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2920
 * @param WP_Block $block           Block instance.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2921
 * @param string   $pagination_type Optional. Type of the arrow we will be rendering.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2922
 *                                  Accepts 'next' or 'previous'. Default 'next'.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2923
 * @return string|null The pagination arrow HTML or null if there is none.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2924
 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2925
function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2926
	$arrow_map = array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2927
		'none'    => '',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2928
		'arrow'   => array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2929
			'next'     => '→',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2930
			'previous' => '←',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2931
		),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2932
		'chevron' => array(
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2933
			'next'     => '»',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2934
			'previous' => '«',
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2935
		),
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2936
	);
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2937
	if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2938
		$arrow_attribute = $block->context['comments/paginationArrow'];
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2939
		$arrow           = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ];
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2940
		$arrow_classes   = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2941
		return "<span class='$arrow_classes' aria-hidden='true'>$arrow</span>";
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2942
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2943
	return null;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2944
}
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2945
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2946
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2947
 * Strips all HTML from the content of footnotes, and sanitizes the ID.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2948
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2949
 * This function expects slashed data on the footnotes content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2950
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2951
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2952
 * @since 6.3.2
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2953
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2954
 * @param string $footnotes JSON-encoded string of an array containing the content and ID of each footnote.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2955
 * @return string Filtered content without any HTML on the footnote content and with the sanitized ID.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2956
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2957
function _wp_filter_post_meta_footnotes( $footnotes ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2958
	$footnotes_decoded = json_decode( $footnotes, true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2959
	if ( ! is_array( $footnotes_decoded ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2960
		return '';
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2961
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2962
	$footnotes_sanitized = array();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2963
	foreach ( $footnotes_decoded as $footnote ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2964
		if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2965
			$footnotes_sanitized[] = array(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2966
				'id'      => sanitize_key( $footnote['id'] ),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2967
				'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2968
			);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2969
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2970
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2971
	return wp_json_encode( $footnotes_sanitized );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2972
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2973
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2974
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2975
 * Adds the filters for footnotes meta field.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2976
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2977
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2978
 * @since 6.3.2
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2979
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2980
function _wp_footnotes_kses_init_filters() {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2981
	add_filter( 'sanitize_post_meta_footnotes', '_wp_filter_post_meta_footnotes' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2982
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2983
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2984
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2985
 * Removes the filters for footnotes meta field.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2986
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2987
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2988
 * @since 6.3.2
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2989
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2990
function _wp_footnotes_remove_filters() {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2991
	remove_filter( 'sanitize_post_meta_footnotes', '_wp_filter_post_meta_footnotes' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2992
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2993
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2994
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2995
 * Registers the filter of footnotes meta field if the user does not have `unfiltered_html` capability.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2996
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2997
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2998
 * @since 6.3.2
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  2999
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3000
function _wp_footnotes_kses_init() {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3001
	_wp_footnotes_remove_filters();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3002
	if ( ! current_user_can( 'unfiltered_html' ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3003
		_wp_footnotes_kses_init_filters();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3004
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3005
}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3006
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3007
/**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3008
 * Initializes the filters for footnotes meta field when imported data should be filtered.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3009
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3010
 * This filter is the last one being executed on {@see 'force_filtered_html_on_import'}.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3011
 * If the input of the filter is true, it means we are in an import situation and should
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3012
 * enable kses, independently of the user capabilities. So in that case we call
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3013
 * _wp_footnotes_kses_init_filters().
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3014
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3015
 * @access private
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3016
 * @since 6.3.2
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3017
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3018
 * @param string $arg Input argument of the filter.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3019
 * @return string Input argument of the filter.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3020
 */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3021
function _wp_footnotes_force_filtered_html_on_import_filter( $arg ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3022
	// If `force_filtered_html_on_import` is true, we need to init the global styles kses filters.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3023
	if ( $arg ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3024
		_wp_footnotes_kses_init_filters();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3025
	}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3026
	return $arg;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
  3027
}