Skip to content

Commit 7ff3625

Browse files
Copilotswissspidy
andcommitted
Add WordPress version checks for post block commands and improve error handling
Co-authored-by: swissspidy <[email protected]>
1 parent 0f5c31a commit 7ff3625

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

features/post-blocks.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,8 @@ Feature: Manage WordPress post blocks
108108
@require-wp-4.9
109109
Scenario: Post block commands require WordPress 5.0+
110110
When I try `wp post has-blocks 1`
111-
Then the return code should be 1
111+
Then STDERR should contain:
112+
"""
113+
Error: The has_blocks() function requires WordPress 5.0 or greater.
114+
"""
115+
And the return code should be 1

src/Post_Command.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,10 @@ public function exists( $args ) {
10811081
* @subcommand has-blocks
10821082
*/
10831083
public function has_blocks( $args, $assoc_args ) {
1084+
if ( ! function_exists( 'has_blocks' ) ) {
1085+
WP_CLI::error( 'The has_blocks() function requires WordPress 5.0 or greater.' );
1086+
}
1087+
10841088
$post = $this->fetcher->get_check( $args[0] );
10851089

10861090
if ( has_blocks( $post->post_content ) ) {
@@ -1110,6 +1114,10 @@ public function has_blocks( $args, $assoc_args ) {
11101114
* @subcommand has-block
11111115
*/
11121116
public function has_block( $args, $assoc_args ) {
1117+
if ( ! function_exists( 'has_block' ) ) {
1118+
WP_CLI::error( 'The has_block() function requires WordPress 5.0 or greater.' );
1119+
}
1120+
11131121
$post = $this->fetcher->get_check( $args[0] );
11141122
$block_name = $args[1];
11151123

@@ -1149,6 +1157,10 @@ public function has_block( $args, $assoc_args ) {
11491157
* @subcommand parse-blocks
11501158
*/
11511159
public function parse_blocks( $args, $assoc_args ) {
1160+
if ( ! function_exists( 'parse_blocks' ) ) {
1161+
WP_CLI::error( 'The parse_blocks() function requires WordPress 5.0 or greater.' );
1162+
}
1163+
11521164
$post = $this->fetcher->get_check( $args[0] );
11531165
$blocks = parse_blocks( $post->post_content );
11541166

@@ -1157,7 +1169,9 @@ public function parse_blocks( $args, $assoc_args ) {
11571169
if ( 'json' === $format ) {
11581170
WP_CLI::line( json_encode( $blocks, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
11591171
} elseif ( 'yaml' === $format ) {
1160-
WP_CLI::line( WP_CLI\Utils\mustache_render( 'yaml_dump.mustache', array( 'output' => $blocks ) ) );
1172+
foreach ( $blocks as $block ) {
1173+
WP_CLI::line( \WP_CLI\Utils\yaml_dump( $block ) );
1174+
}
11611175
}
11621176
}
11631177

@@ -1178,6 +1192,10 @@ public function parse_blocks( $args, $assoc_args ) {
11781192
* @subcommand render-blocks
11791193
*/
11801194
public function render_blocks( $args, $assoc_args ) {
1195+
if ( ! function_exists( 'do_blocks' ) ) {
1196+
WP_CLI::error( 'The do_blocks() function requires WordPress 5.0 or greater.' );
1197+
}
1198+
11811199
$post = $this->fetcher->get_check( $args[0] );
11821200
$html = do_blocks( $post->post_content );
11831201

0 commit comments

Comments
 (0)