diff --git a/core/Block.php b/core/Block.php index d27491aa..1f3ac42f 100644 --- a/core/Block.php +++ b/core/Block.php @@ -8,7 +8,8 @@ */ class Block extends Container { /** - * {@inheritDoc} + * @see \Carbon_Fields\Container\Container::factory() + * @return \Carbon_Fields\Container\Block_Container */ public static function make() { return call_user_func_array( array( parent::class, 'make' ), array_merge( array( 'block' ), func_get_args() ) ); diff --git a/core/Container/Block_Container.php b/core/Container/Block_Container.php index b5305387..a9e2f7b9 100644 --- a/core/Container/Block_Container.php +++ b/core/Container/Block_Container.php @@ -25,6 +25,7 @@ class Block_Container extends Container { 'category' => array( 'slug' => 'common', ), + 'api_version' => null, ); /** @@ -64,6 +65,12 @@ public function __construct( $id, $title, $type, $condition_collection, $conditi if ( ! $this->get_datastore() ) { $this->set_datastore( Datastore::make( 'empty' ), $this->has_default_datastore() ); } + + // check to see if block api_version is set globally. + $filtered_block_api_version = apply_filters( 'carbon_fields_set_block_api_version', null ); + if ( ! is_null( $filtered_block_api_version ) ) { + $this->settings[ 'api_version' ] = $filtered_block_api_version; + } } /** @@ -399,6 +406,20 @@ public function set_render_callback( $render_callback ) { return $this; } + /** + * Set the Block Api Version. + * + * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/ + * + * @param float|null $version - null will use the default. + * @return Block_Container + */ + public function set_api_version( $version = null ) { + $this->settings[ 'api_version' ] = $version; + + return $this; + } + /** * Get the post id where the block is used in. * Try with the GET param, and if this is an @@ -488,11 +509,18 @@ protected function register_block() { ), ) ); - register_block_type( $this->get_block_type_name(), array( + $block_type_settings = array( 'style' => $style, 'editor_style' => $editor_style, 'attributes' => $attributes, 'render_callback' => array( $this, 'render_block' ), - ) ); + ); + + // pass api_version if set. + if ( ! is_null( $this->settings[ 'api_version' ] ) && is_numeric( $this->settings[ 'api_version' ] ) ) { + $block_type_settings[ 'api_version' ] = floatval( $this->settings[ 'api_version' ] ); + } + + register_block_type( $this->get_block_type_name(), $block_type_settings ); } }