rebase on oct-10-2023
This commit is contained in:
@@ -17,14 +17,14 @@
|
||||
*/
|
||||
function remove_block_asset_path_prefix( $asset_handle_or_path ) {
|
||||
$path_prefix = 'file:';
|
||||
if ( 0 !== strpos( $asset_handle_or_path, $path_prefix ) ) {
|
||||
if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
|
||||
return $asset_handle_or_path;
|
||||
}
|
||||
$path = substr(
|
||||
$asset_handle_or_path,
|
||||
strlen( $path_prefix )
|
||||
);
|
||||
if ( strpos( $path, './' ) === 0 ) {
|
||||
if ( str_starts_with( $path, './' ) ) {
|
||||
$path = substr( $path, 2 );
|
||||
}
|
||||
return $path;
|
||||
@@ -44,12 +44,12 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) {
|
||||
* @return string Generated asset name for the block's field.
|
||||
*/
|
||||
function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
|
||||
if ( 0 === strpos( $block_name, 'core/' ) ) {
|
||||
if ( str_starts_with( $block_name, 'core/' ) ) {
|
||||
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
|
||||
if ( 0 === strpos( $field_name, 'editor' ) ) {
|
||||
if ( str_starts_with( $field_name, 'editor' ) ) {
|
||||
$asset_handle .= '-editor';
|
||||
}
|
||||
if ( 0 === strpos( $field_name, 'view' ) ) {
|
||||
if ( str_starts_with( $field_name, 'view' ) ) {
|
||||
$asset_handle .= '-view';
|
||||
}
|
||||
if ( $index > 0 ) {
|
||||
@@ -134,17 +134,34 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
|
||||
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
|
||||
}
|
||||
|
||||
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
|
||||
// Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
|
||||
static $template_path_norm = '';
|
||||
static $stylesheet_path_norm = '';
|
||||
if ( ! $template_path_norm || ! $stylesheet_path_norm ) {
|
||||
$template_path_norm = wp_normalize_path( get_template_directory() );
|
||||
$stylesheet_path_norm = wp_normalize_path( get_stylesheet_directory() );
|
||||
}
|
||||
|
||||
$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
|
||||
|
||||
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
|
||||
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
|
||||
$is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
|
||||
|
||||
/*
|
||||
* Determine if the block script was registered in a theme, by checking if the script path starts with either
|
||||
* the parent (template) or child (stylesheet) directory path.
|
||||
*/
|
||||
$is_parent_theme_block = str_starts_with( $script_path_norm, $template_path_norm );
|
||||
$is_child_theme_block = str_starts_with( $script_path_norm, $stylesheet_path_norm );
|
||||
$is_theme_block = ( $is_parent_theme_block || $is_child_theme_block );
|
||||
|
||||
$script_uri = plugins_url( $script_path, $metadata['file'] );
|
||||
if ( $is_core_block ) {
|
||||
$script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) );
|
||||
} elseif ( $is_theme_block ) {
|
||||
$script_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $script_path_norm ) );
|
||||
// Get the script path deterministically based on whether or not it was registered in a parent or child theme.
|
||||
$script_uri = $is_parent_theme_block
|
||||
? get_theme_file_uri( str_replace( $template_path_norm, '', $script_path_norm ) )
|
||||
: get_theme_file_uri( str_replace( $stylesheet_path_norm, '', $script_path_norm ) );
|
||||
}
|
||||
|
||||
$script_asset = require $script_asset_path;
|
||||
@@ -186,17 +203,6 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static $wpinc_path_norm = '';
|
||||
if ( ! $wpinc_path_norm ) {
|
||||
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
|
||||
}
|
||||
|
||||
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
|
||||
// Skip registering individual styles for each core block when a bundled version provided.
|
||||
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$style_handle = $metadata[ $field_name ];
|
||||
if ( is_array( $style_handle ) ) {
|
||||
if ( empty( $style_handle[ $index ] ) ) {
|
||||
@@ -205,6 +211,23 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
$style_handle = $style_handle[ $index ];
|
||||
}
|
||||
|
||||
$style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
|
||||
// If the style handle is already registered, skip re-registering.
|
||||
if ( wp_style_is( $style_handle_name, 'registered' ) ) {
|
||||
return $style_handle_name;
|
||||
}
|
||||
|
||||
static $wpinc_path_norm = '';
|
||||
if ( ! $wpinc_path_norm ) {
|
||||
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
|
||||
}
|
||||
|
||||
$is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
|
||||
// Skip registering individual styles for each core block when a bundled version provided.
|
||||
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$style_path = remove_block_asset_path_prefix( $style_handle );
|
||||
$is_style_handle = $style_handle === $style_path;
|
||||
// Allow only passing style handles for core blocks.
|
||||
@@ -219,7 +242,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
// Check whether styles should have a ".min" suffix or not.
|
||||
$suffix = SCRIPT_DEBUG ? '' : '.min';
|
||||
if ( $is_core_block ) {
|
||||
$style_path = "style$suffix.css";
|
||||
$style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css";
|
||||
}
|
||||
|
||||
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
|
||||
@@ -228,27 +251,36 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
if ( $has_style_file ) {
|
||||
$style_uri = plugins_url( $style_path, $metadata['file'] );
|
||||
|
||||
// Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
|
||||
static $theme_path_norm = '';
|
||||
if ( ! $theme_path_norm ) {
|
||||
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
|
||||
// Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
|
||||
static $template_path_norm = '';
|
||||
static $stylesheet_path_norm = '';
|
||||
if ( ! $template_path_norm || ! $stylesheet_path_norm ) {
|
||||
$template_path_norm = wp_normalize_path( get_template_directory() );
|
||||
$stylesheet_path_norm = wp_normalize_path( get_stylesheet_directory() );
|
||||
}
|
||||
|
||||
$is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );
|
||||
// Determine if the block style was registered in a theme, by checking if the script path starts with either
|
||||
// the parent (template) or child (stylesheet) directory path.
|
||||
$is_parent_theme_block = str_starts_with( $style_path_norm, $template_path_norm );
|
||||
$is_child_theme_block = str_starts_with( $style_path_norm, $stylesheet_path_norm );
|
||||
$is_theme_block = ( $is_parent_theme_block || $is_child_theme_block );
|
||||
|
||||
if ( $is_theme_block ) {
|
||||
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
|
||||
} elseif ( $is_core_block ) {
|
||||
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
|
||||
if ( $is_core_block ) {
|
||||
// All possible $style_path variants for core blocks are hard-coded above.
|
||||
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . '/' . $style_path );
|
||||
} elseif ( $is_theme_block ) {
|
||||
// Get the script path deterministically based on whether or not it was registered in a parent or child theme.
|
||||
$style_uri = $is_parent_theme_block
|
||||
? get_theme_file_uri( str_replace( $template_path_norm, '', $style_path_norm ) )
|
||||
: get_theme_file_uri( str_replace( $stylesheet_path_norm, '', $style_path_norm ) );
|
||||
}
|
||||
} else {
|
||||
$style_uri = false;
|
||||
}
|
||||
|
||||
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
|
||||
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
|
||||
$result = wp_register_style(
|
||||
$style_handle,
|
||||
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
|
||||
$result = wp_register_style(
|
||||
$style_handle_name,
|
||||
$style_uri,
|
||||
array(),
|
||||
$version
|
||||
@@ -258,7 +290,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
}
|
||||
|
||||
if ( $has_style_file ) {
|
||||
wp_style_add_data( $style_handle, 'path', $style_path_norm );
|
||||
wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
|
||||
|
||||
if ( $is_core_block ) {
|
||||
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
|
||||
@@ -267,13 +299,13 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
|
||||
}
|
||||
|
||||
if ( is_rtl() && file_exists( $rtl_file ) ) {
|
||||
wp_style_add_data( $style_handle, 'rtl', 'replace' );
|
||||
wp_style_add_data( $style_handle, 'suffix', $suffix );
|
||||
wp_style_add_data( $style_handle, 'path', $rtl_file );
|
||||
wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
|
||||
wp_style_add_data( $style_handle_name, 'suffix', $suffix );
|
||||
wp_style_add_data( $style_handle_name, 'path', $rtl_file );
|
||||
}
|
||||
}
|
||||
|
||||
return $style_handle;
|
||||
return $style_handle_name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -300,6 +332,7 @@ function get_block_metadata_i18n_schema() {
|
||||
* @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
|
||||
* @since 5.9.0 Added support for `variations` and `viewScript` fields.
|
||||
* @since 6.1.0 Added support for `render` field.
|
||||
* @since 6.3.0 Added `selectors` field.
|
||||
*
|
||||
* @param string $file_or_folder Path to the JSON file with metadata definition for
|
||||
* the block or path to the folder where the `block.json` file is located.
|
||||
@@ -318,20 +351,22 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
||||
*/
|
||||
static $core_blocks_meta;
|
||||
if ( ! $core_blocks_meta ) {
|
||||
$core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php';
|
||||
$core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
|
||||
}
|
||||
|
||||
$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
|
||||
trailingslashit( $file_or_folder ) . 'block.json' :
|
||||
$file_or_folder;
|
||||
|
||||
if ( ! file_exists( $metadata_file ) ) {
|
||||
$is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC );
|
||||
|
||||
if ( ! $is_core_block && ! file_exists( $metadata_file ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to get metadata from the static cache for core blocks.
|
||||
$metadata = false;
|
||||
if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) {
|
||||
if ( $is_core_block ) {
|
||||
$core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
|
||||
if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) {
|
||||
$metadata = $core_blocks_meta[ $core_block_name ];
|
||||
@@ -358,12 +393,16 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
||||
$metadata = apply_filters( 'block_type_metadata', $metadata );
|
||||
|
||||
// Add `style` and `editor_style` for core blocks if missing.
|
||||
if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) {
|
||||
if ( ! empty( $metadata['name'] ) && str_starts_with( $metadata['name'], 'core/' ) ) {
|
||||
$block_name = str_replace( 'core/', '', $metadata['name'] );
|
||||
|
||||
if ( ! isset( $metadata['style'] ) ) {
|
||||
$metadata['style'] = "wp-block-$block_name";
|
||||
}
|
||||
if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) {
|
||||
$metadata['style'] = (array) $metadata['style'];
|
||||
$metadata['style'][] = "wp-block-{$block_name}-theme";
|
||||
}
|
||||
if ( ! isset( $metadata['editorStyle'] ) ) {
|
||||
$metadata['editorStyle'] = "wp-block-{$block_name}-editor";
|
||||
}
|
||||
@@ -382,6 +421,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
||||
'attributes' => 'attributes',
|
||||
'providesContext' => 'provides_context',
|
||||
'usesContext' => 'uses_context',
|
||||
'selectors' => 'selectors',
|
||||
'supports' => 'supports',
|
||||
'styles' => 'styles',
|
||||
'variations' => 'variations',
|
||||
@@ -483,7 +523,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
||||
*
|
||||
* @return string Returns the block content.
|
||||
*/
|
||||
$settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
|
||||
$settings['render_callback'] = static function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
|
||||
ob_start();
|
||||
require $template_path;
|
||||
return ob_get_clean();
|
||||
@@ -579,7 +619,7 @@ function has_blocks( $post = null ) {
|
||||
$post = $wp_post->post_content;
|
||||
}
|
||||
|
||||
return false !== strpos( (string) $post, '<!-- wp:' );
|
||||
return str_contains( (string) $post, '<!-- wp:' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -615,12 +655,12 @@ function has_block( $block_name, $post = null ) {
|
||||
* This matches behavior for WordPress 5.0.0 - 5.3.0 in matching blocks by
|
||||
* their serialized names.
|
||||
*/
|
||||
if ( false === strpos( $block_name, '/' ) ) {
|
||||
if ( ! str_contains( $block_name, '/' ) ) {
|
||||
$block_name = 'core/' . $block_name;
|
||||
}
|
||||
|
||||
// Test for existence of block by its fully qualified name.
|
||||
$has_block = false !== strpos( $post, '<!-- wp:' . $block_name . ' ' );
|
||||
$has_block = str_contains( $post, '<!-- wp:' . $block_name . ' ' );
|
||||
|
||||
if ( ! $has_block ) {
|
||||
/*
|
||||
@@ -629,7 +669,7 @@ function has_block( $block_name, $post = null ) {
|
||||
*/
|
||||
$serialized_block_name = strip_core_block_namespace( $block_name );
|
||||
if ( $serialized_block_name !== $block_name ) {
|
||||
$has_block = false !== strpos( $post, '<!-- wp:' . $serialized_block_name . ' ' );
|
||||
$has_block = str_contains( $post, '<!-- wp:' . $serialized_block_name . ' ' );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -696,7 +736,7 @@ function serialize_block_attributes( $block_attributes ) {
|
||||
* @return string Block name to use for serialization.
|
||||
*/
|
||||
function strip_core_block_namespace( $block_name = null ) {
|
||||
if ( is_string( $block_name ) && 0 === strpos( $block_name, 'core/' ) ) {
|
||||
if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
|
||||
return substr( $block_name, 5 );
|
||||
}
|
||||
|
||||
@@ -798,7 +838,7 @@ function serialize_blocks( $blocks ) {
|
||||
function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) {
|
||||
$result = '';
|
||||
|
||||
if ( false !== strpos( $text, '<!--' ) && false !== strpos( $text, '--->' ) ) {
|
||||
if ( str_contains( $text, '<!--' ) && str_contains( $text, '--->' ) ) {
|
||||
$text = preg_replace_callback( '%<!--(.*?)--->%', '_filter_block_content_callback', $text );
|
||||
}
|
||||
|
||||
@@ -969,6 +1009,27 @@ function excerpt_remove_blocks( $content ) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses footnotes markup out of a content string,
|
||||
* and renders those appropriate for the excerpt.
|
||||
*
|
||||
* @since 6.3.0
|
||||
*
|
||||
* @param string $content The content to parse.
|
||||
* @return string The parsed and filtered content.
|
||||
*/
|
||||
function excerpt_remove_footnotes( $content ) {
|
||||
if ( ! str_contains( $content, 'data-fn=' ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return preg_replace(
|
||||
'_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_',
|
||||
'',
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders inner blocks from the allowed wrapper blocks
|
||||
* for generating an excerpt.
|
||||
@@ -1161,8 +1222,8 @@ function block_version( $content ) {
|
||||
* @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/
|
||||
*
|
||||
* @param string $block_name Block type name including namespace.
|
||||
* @param array $style_properties Array containing the properties of the style name,
|
||||
* label, style (name of the stylesheet to be enqueued),
|
||||
* @param array $style_properties Array containing the properties of the style name, label,
|
||||
* style_handle (name of the stylesheet to be enqueued),
|
||||
* inline_style (string containing the CSS to be added).
|
||||
* @return bool True if the block style was registered with success and false otherwise.
|
||||
*/
|
||||
@@ -1362,10 +1423,15 @@ function build_query_vars_from_query_block( $block, $page ) {
|
||||
$query['orderby'] = $block->context['query']['orderBy'];
|
||||
}
|
||||
if (
|
||||
isset( $block->context['query']['author'] ) &&
|
||||
(int) $block->context['query']['author'] > 0
|
||||
isset( $block->context['query']['author'] )
|
||||
) {
|
||||
$query['author'] = (int) $block->context['query']['author'];
|
||||
if ( is_array( $block->context['query']['author'] ) ) {
|
||||
$query['author__in'] = array_filter( array_map( 'intval', $block->context['query']['author'] ) );
|
||||
} elseif ( is_string( $block->context['query']['author'] ) ) {
|
||||
$query['author__in'] = array_filter( array_map( 'intval', explode( ',', $block->context['query']['author'] ) ) );
|
||||
} elseif ( is_int( $block->context['query']['author'] ) && $block->context['query']['author'] > 0 ) {
|
||||
$query['author'] = $block->context['query']['author'];
|
||||
}
|
||||
}
|
||||
if ( ! empty( $block->context['query']['search'] ) ) {
|
||||
$query['s'] = $block->context['query']['search'];
|
||||
|
||||
Reference in New Issue
Block a user