Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) );
Expand Down
32 changes: 30 additions & 2 deletions core/Container/Block_Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Block_Container extends Container {
'category' => array(
'slug' => 'common',
),
'api_version' => null,
);

/**
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}
}