mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-08 19:20:03 +00:00
Blocks: Introduce WP_Block_Type and WP_Block_Type_Registry classes.
These are the foundational classes allowing blocks to be registered and used throughout WordPress. This commit also includes the `has_block()` and `has_blocks()` functions, which are required for unit testing these classes. Merges [43742] from the 5.0 branch to trunk. Props adamsilverstein, danielbachhuber, desrosj. Fixes #45097. See #45109. git-svn-id: https://develop.svn.wordpress.org/trunk@44108 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
61
src/wp-includes/blocks.php
Normal file
61
src/wp-includes/blocks.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Functions related to registering and parsing blocks.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
* @since 5.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determine whether a post or content string has blocks.
|
||||
*
|
||||
* This test optimizes for performance rather than strict accuracy, detecting
|
||||
* the pattern of a block but not validating its structure. For strict accuracy,
|
||||
* you should use the block parser on post content.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @see parse_blocks()
|
||||
*
|
||||
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post.
|
||||
* @return bool Whether the post has blocks.
|
||||
*/
|
||||
function has_blocks( $post = null ) {
|
||||
if ( ! is_string( $post ) ) {
|
||||
$wp_post = get_post( $post );
|
||||
if ( $wp_post instanceof WP_Post ) {
|
||||
$post = $wp_post->post_content;
|
||||
}
|
||||
}
|
||||
|
||||
return false !== strpos( (string) $post, '<!-- wp:' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a $post or a string contains a specific block type.
|
||||
*
|
||||
* This test optimizes for performance rather than strict accuracy, detecting
|
||||
* the block type exists but not validating its structure. For strict accuracy,
|
||||
* you should use the block parser on post content.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @see parse_blocks()
|
||||
*
|
||||
* @param string $block_type Full Block type to look for.
|
||||
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post.
|
||||
* @return bool Whether the post content contains the specified block.
|
||||
*/
|
||||
function has_block( $block_type, $post = null ) {
|
||||
if ( ! has_blocks( $post ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! is_string( $post ) ) {
|
||||
$wp_post = get_post( $post );
|
||||
if ( $wp_post instanceof WP_Post ) {
|
||||
$post = $wp_post->post_content;
|
||||
}
|
||||
}
|
||||
|
||||
return false !== strpos( $post, '<!-- wp:' . $block_type . ' ' );
|
||||
}
|
||||
Reference in New Issue
Block a user