Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove building wp_template_parts variation call for frontend #5718

Closed
Closed
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7a95c4f
remove varation
kt-12 Nov 29, 2023
70fb7aa
introduce get all variation
kt-12 Dec 1, 2023
a41c600
cs fixes
kt-12 Dec 1, 2023
04de489
cs fixes
kt-12 Dec 1, 2023
9146a55
filter to callback
kt-12 Dec 5, 2023
d495db0
Add Variation to block-types rest api
kt-12 Dec 7, 2023
3f8b154
navigation link and post term to block varation to callback
kt-12 Dec 7, 2023
d382bb9
Update src/wp-includes/class-wp-block-type.php
kt-12 Dec 7, 2023
26baa27
Merge branch 'enhancement/remove-variation-fontend' of https://github…
kt-12 Dec 7, 2023
cc598f2
doc block
kt-12 Dec 7, 2023
c174980
dock block
kt-12 Dec 7, 2023
8ad9728
phpcs fixes
kt-12 Dec 7, 2023
d9dabaa
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
kt-12 Dec 7, 2023
e8ebb6f
Update src/wp-includes/class-wp-block-type.php
kt-12 Dec 11, 2023
9d5812d
Making name consistant
kt-12 Dec 11, 2023
6a4433d
Merge branch 'enhancement/remove-variation-fontend' of https://github…
kt-12 Dec 11, 2023
5c3ad2c
callback support with backward compatibility
kt-12 Jan 5, 2024
b1d3664
Allow setting of value. Doc block for variation callback
kt-12 Jan 5, 2024
9c475a9
variations set to null initially
kt-12 Jan 8, 2024
3cb7d40
isset logic
kt-12 Jan 9, 2024
0351ae6
test cases for variation callback
kt-12 Jan 10, 2024
5480c0f
remove isset as variations will never be returned as null.
kt-12 Jan 10, 2024
a68e5e0
isset always true for variations
kt-12 Jan 10, 2024
cd38417
revert back navigation-link to
kt-12 Jan 16, 2024
b604464
revert back to trunk
kt-12 Jan 16, 2024
93e4457
Merge branch 'trunk' into enhancement/remove-variation-fontend
kt-12 Jan 16, 2024
39ee3ee
set to null
kt-12 Jan 17, 2024
34bcc84
test cases for multiple cases
kt-12 Jan 18, 2024
94aa0fe
additional test cases
kt-12 Jan 18, 2024
bae4e71
change message
kt-12 Jan 18, 2024
2918539
cs fixes
kt-12 Jan 18, 2024
686c243
Update tests/phpunit/tests/blocks/wpBlockType.php
kt-12 Jan 18, 2024
689d48c
Update tests/phpunit/tests/blocks/wpBlockType.php
kt-12 Jan 18, 2024
aafceda
Move filter function one up
kt-12 Jan 19, 2024
b0730c7
remove filters and expand doc block defnetion
kt-12 Jan 19, 2024
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
53 changes: 51 additions & 2 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ class WP_Block_Type {
* Block variations.
*
* @since 5.8.0
* @var array[]
* @since 6.5.0 Only accessible through magic getter. null by default.
* @var array[]|null
*/
public $variations = array();
private $variations = null;

/**
* Block variations callback.
*
* @since 6.5.0
* @var callable
kt-12 marked this conversation as resolved.
Show resolved Hide resolved
*/
public $variation_callback = null;

/**
* Custom CSS selectors for theme.json style generation.
Expand Down Expand Up @@ -296,6 +305,7 @@ class WP_Block_Type {
* @type array|null $supports Supported features.
* @type array|null $example Structured data for the block preview.
* @type callable|null $render_callback Block type render callback.
* @type callable|null $variation_callback Block type variations callback.
* @type array|null $attributes Block type attributes property schemas.
* @type string[] $uses_context Context values inherited by blocks of this type.
* @type string[]|null $provides_context Context provided by blocks of this type.
Expand Down Expand Up @@ -325,6 +335,10 @@ public function __construct( $block_type, $args = array() ) {
* null when value not found, or void when unknown property name provided.
*/
public function __get( $name ) {
if ( 'variations' === $name ) {
return $this->get_variations();
}

if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
return;
}
Expand Down Expand Up @@ -353,6 +367,10 @@ public function __get( $name ) {
* or false otherwise.
*/
public function __isset( $name ) {
if ( 'variations' === $name ) {
return true;
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}

if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
return false;
}
Expand All @@ -372,6 +390,11 @@ public function __isset( $name ) {
* @param mixed $value Property value.
*/
public function __set( $name, $value ) {
if ( 'variations' === $name ) {
joemcgill marked this conversation as resolved.
Show resolved Hide resolved
$this->variations = $value;
return;
}

if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
$this->{$name} = $value;
return;
Expand Down Expand Up @@ -540,4 +563,30 @@ public function get_attributes() {
$this->attributes :
array();
}

/**
* Get block variations.
*
* @since 6.5.0
*
* @return array[]
*/
public function get_variations() {
if ( ! isset( $this->variations ) ) {
$this->variations = array();
if ( is_callable( $this->variation_callback ) ) {
$this->variations = call_user_func( $this->variation_callback );
}
}
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved

/**
* Filters the registered variations for a block type.
*
* @since 6.5.0
*
* @param array $variations Array of registered variations for a block type.
* @param WP_Block_Type $block_type The full block type object.
*/
return apply_filters( 'get_block_type_variations', $this->variations, $this );
kt-12 marked this conversation as resolved.
Show resolved Hide resolved
kt-12 marked this conversation as resolved.
Show resolved Hide resolved
}
}
43 changes: 43 additions & 0 deletions tests/phpunit/tests/blocks/wpBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,47 @@ public function data_block_version() {
array( '<!- - wp:core/separator -->', 0 ),
);
}

/**
* @ticket 59969
*/
public function test_variation_callback() {
$block_type = new WP_Block_Type(
'test/block',
array(
'title' => 'Test title',
'variation_callback' => array( $this, 'mock_variation_callback' ),
)
);

$this->assertSameSets( $this->mock_variation_callback(), $block_type->variations );
}

/**
* @ticket 59969
* @covers WP_Block_Type::get_variations
*/
public function test_get_variations() {
$block_type = new WP_Block_Type(
'test/block',
array(
'title' => 'Test title',
'variation_callback' => array( $this, 'mock_variation_callback' ),
)
);

$this->assertSameSets( $this->mock_variation_callback(), $block_type->get_variations() );
}
kt-12 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Mock variation callback.
*
* @return array
*/
public function mock_variation_callback() {
return array(
array( 'name' => 'var1' ),
array( 'name' => 'var2' ),
);
}
}
29 changes: 29 additions & 0 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,35 @@ protected function check_block_type_object( $block_type, $data, $links ) {
}
}

/**
* @ticket 59969
*/
public function test_variation_callback() {
$block_type = 'test/block';
$settings = array(
'title' => true,
'variation_callback' => array( $this, 'mock_variation_callback' ),
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSameSets( $this->mock_variation_callback(), $data['variations'] );
}

/**
* Mock variation callback.
*
* @return array
*/
public function mock_variation_callback() {
return array(
array( 'name' => 'var1' ),
array( 'name' => 'var2' ),
);
}

/**
* The create_item() method does not exist for block types.
*
Expand Down
Loading