@@ -224,6 +224,10 @@ public function get( $args, $assoc_args ) {
224224 * @subcommand update
225225 */
226226 public function update ( $ args , $ assoc_args ) {
227+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
228+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
229+ }
230+
227231 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
228232 $ index = (int ) $ args [1 ];
229233 $ blocks = parse_blocks ( $ post ->post_content );
@@ -280,7 +284,7 @@ public function update( $args, $assoc_args ) {
280284 $ blocks [ $ original_idx ] = $ block ;
281285
282286 // @phpstan-ignore argument.type
283- $ new_content = $ this -> serialize_blocks ( $ blocks );
287+ $ new_content = serialize_blocks ( $ blocks );
284288 $ result = wp_update_post (
285289 [
286290 'ID ' => $ post ->ID ,
@@ -340,6 +344,10 @@ public function update( $args, $assoc_args ) {
340344 * @subcommand move
341345 */
342346 public function move ( $ args , $ assoc_args ) {
347+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
348+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
349+ }
350+
343351 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
344352 $ from_index = (int ) $ args [1 ];
345353 $ to_index = (int ) $ args [2 ];
@@ -398,7 +406,7 @@ public function move( $args, $assoc_args ) {
398406 // Insert at new position.
399407 array_splice ( $ blocks , $ insert_pos , 0 , [ $ block_to_move ] );
400408
401- $ new_content = $ this -> serialize_blocks ( $ blocks );
409+ $ new_content = serialize_blocks ( $ blocks );
402410 $ result = wp_update_post (
403411 [
404412 'ID ' => $ post ->ID ,
@@ -607,6 +615,10 @@ function ( $block ) {
607615 * @subcommand import
608616 */
609617 public function import ( $ args , $ assoc_args ) {
618+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
619+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
620+ }
621+
610622 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
611623 $ file = Utils \get_flag_value ( $ assoc_args , 'file ' , null );
612624 $ position = Utils \get_flag_value ( $ assoc_args , 'position ' , 'end ' );
@@ -691,7 +703,7 @@ function ( $block ) {
691703 }
692704 }
693705
694- $ new_content = $ this -> serialize_blocks ( $ blocks );
706+ $ new_content = serialize_blocks ( $ blocks );
695707 $ result = wp_update_post (
696708 [
697709 'ID ' => $ post ->ID ,
@@ -914,6 +926,10 @@ public function count( $args, $assoc_args ) {
914926 * @subcommand clone
915927 */
916928 public function clone_block ( $ args , $ assoc_args ) {
929+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
930+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
931+ }
932+
917933 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
918934 $ source_index = (int ) $ args [1 ];
919935 $ position = Utils \get_flag_value ( $ assoc_args , 'position ' , 'after ' );
@@ -975,7 +991,7 @@ public function clone_block( $args, $assoc_args ) {
975991 array_splice ( $ blocks , (int ) $ insert_pos , 0 , [ $ cloned_block ] );
976992
977993 // @phpstan-ignore argument.type
978- $ new_content = $ this -> serialize_blocks ( $ blocks );
994+ $ new_content = serialize_blocks ( $ blocks );
979995
980996 $ result = wp_update_post (
981997 [
@@ -1393,6 +1409,10 @@ public function list_( $args, $assoc_args ) {
13931409 * @subcommand insert
13941410 */
13951411 public function insert ( $ args , $ assoc_args ) {
1412+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
1413+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
1414+ }
1415+
13961416 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
13971417 $ block_name = $ args [1 ];
13981418 $ content = Utils \get_flag_value ( $ assoc_args , 'content ' , '' );
@@ -1424,7 +1444,7 @@ public function insert( $args, $assoc_args ) {
14241444 }
14251445
14261446 // @phpstan-ignore argument.type
1427- $ new_content = $ this -> serialize_blocks ( $ blocks );
1447+ $ new_content = serialize_blocks ( $ blocks );
14281448 $ result = wp_update_post (
14291449 [
14301450 'ID ' => $ post ->ID ,
@@ -1492,6 +1512,10 @@ public function insert( $args, $assoc_args ) {
14921512 * @subcommand remove
14931513 */
14941514 public function remove ( $ args , $ assoc_args ) {
1515+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
1516+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
1517+ }
1518+
14951519 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
14961520 $ block_name = isset ( $ args [1 ] ) ? $ args [1 ] : null ;
14971521 $ indices = Utils \get_flag_value ( $ assoc_args , 'index ' , null );
@@ -1567,7 +1591,7 @@ function ( $idx ) use ( $index_map ) {
15671591 return ;
15681592 }
15691593
1570- $ new_content = $ this -> serialize_blocks ( $ blocks );
1594+ $ new_content = serialize_blocks ( $ blocks );
15711595 $ result = wp_update_post (
15721596 [
15731597 'ID ' => $ post ->ID ,
@@ -1637,6 +1661,10 @@ function ( $idx ) use ( $index_map ) {
16371661 * @subcommand replace
16381662 */
16391663 public function replace ( $ args , $ assoc_args ) {
1664+ if ( ! function_exists ( 'serialize_blocks ' ) ) {
1665+ WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
1666+ }
1667+
16401668 $ post = $ this ->fetcher ->get_check ( $ args [0 ] );
16411669 $ old_block_name = $ args [1 ];
16421670 $ new_block_name = $ args [2 ];
@@ -1681,7 +1709,7 @@ public function replace( $args, $assoc_args ) {
16811709 }
16821710
16831711 // @phpstan-ignore argument.type
1684- $ new_content = $ this -> serialize_blocks ( $ blocks );
1712+ $ new_content = serialize_blocks ( $ blocks );
16851713 $ result = wp_update_post (
16861714 [
16871715 'ID ' => $ post ->ID ,
@@ -1702,25 +1730,6 @@ public function replace( $args, $assoc_args ) {
17021730 }
17031731 }
17041732
1705- /**
1706- * Serializes blocks back into post content.
1707- *
1708- * The command itself requires WordPress 5.0, but `serialize_blocks()` was only
1709- * introduced in WordPress 5.3.1.
1710- *
1711- * @param array $blocks Array of blocks.
1712- * @return string Serialized post content.
1713- *
1714- * @phpstan-param array<int|string, array{blockName: string|null, attrs: array, innerBlocks: array[], innerHTML: string, innerContent: array}> $blocks
1715- */
1716- private function serialize_blocks ( $ blocks ) {
1717- if ( ! function_exists ( 'serialize_blocks ' ) ) {
1718- WP_CLI ::error ( 'Requires WordPress 5.3.1 or greater. ' );
1719- }
1720-
1721- return serialize_blocks ( $ blocks );
1722- }
1723-
17241733 /**
17251734 * Strips innerHTML and innerContent from blocks recursively.
17261735 *
0 commit comments