mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-22 15:31:42 +01:00
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. Built from https://develop.svn.wordpress.org/trunk@44108 git-svn-id: http://core.svn.wordpress.org/trunk@43938 1a063a9b-81f0-0310-95a4-ce76da25c4cd
62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?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 . ' ' );
|
|
}
|