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

Lazy load varients #5825

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,6 @@
'ancestor' => 'ancestor',
'keywords' => 'keywords',
'example' => 'example',
'variations' => 'variations',
);

foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
Expand All @@ -2317,6 +2316,7 @@

$blocks[ $block_name ][ $key ] = $block_type->{ $field };
}
$blocks[ $block_name ][ 'variations' ] = $block_type->get_variations();

Check failure on line 2319 in src/wp-admin/includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
}

return $blocks;
Expand Down
21 changes: 15 additions & 6 deletions src/wp-includes/blocks/navigation-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,11 @@ function build_variation_for_navigation_link( $entity, $kind ) {
}

/**
* Register the navigation link block.
* Returns an array of variations for the navigation link block.
*
* @uses render_block_core_navigation()
* @throws WP_Error An WP_Error exception parsing the block definition.
* @return array
*/
function register_block_core_navigation_link() {
function build_navigation_link_block_variations() {
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );

Expand Down Expand Up @@ -360,11 +359,21 @@ function register_block_core_navigation_link() {
}
}

return array_merge( $built_ins, $variations );
}

/**
* Register the navigation link block.
*
* @uses render_block_core_navigation()
* @throws WP_Error An WP_Error exception parsing the block definition.
*/
function register_block_core_navigation_link() {
register_block_type_from_metadata(
__DIR__ . '/navigation-link',
array(
'render_callback' => 'render_block_core_navigation_link',
'variations' => array_merge( $built_ins, $variations ),
'render_callback' => 'render_block_core_navigation_link',
'variation_callback' => 'build_navigation_link_block_variations',
)
);
}
Expand Down
16 changes: 11 additions & 5 deletions src/wp-includes/blocks/post-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ function render_block_core_post_terms( $attributes, $content, $block ) {
}

/**
* Registers the `core/post-terms` block on the server.
* @return array
*/
function register_block_core_post_terms() {
function build_post_term_block_variations() {
$taxonomies = get_taxonomies(
array(
'publicly_queryable' => true,
Expand All @@ -82,7 +82,7 @@ function register_block_core_post_terms() {
'name' => $taxonomy->name,
'title' => $taxonomy->label,
'description' => sprintf(
/* translators: %s: taxonomy's label */
/* translators: %s: taxonomy's label */
__( 'Display a list of assigned terms from the taxonomy: %s' ),
$taxonomy->label
),
Expand All @@ -102,12 +102,18 @@ function register_block_core_post_terms() {
$custom_variations[] = $variation;
}
}
return array_merge( $built_ins, $custom_variations );
}

/**
* Registers the `core/post-terms` block on the server.
*/
function register_block_core_post_terms() {
register_block_type_from_metadata(
__DIR__ . '/post-terms',
array(
'render_callback' => 'render_block_core_post_terms',
'variations' => array_merge( $built_ins, $custom_variations ),
'render_callback' => 'render_block_core_post_terms',
'variation_callback' => 'build_post_term_block_variations',
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/blocks/template-part.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ function register_block_core_template_part() {
register_block_type_from_metadata(
__DIR__ . '/template-part',
array(
'render_callback' => 'render_block_core_template_part',
'variations' => build_template_part_block_variations(),
'render_callback' => 'render_block_core_template_part',
'variation_callback' => 'build_template_part_block_variations',
)
);
}
Expand Down
35 changes: 34 additions & 1 deletion src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ class WP_Block_Type {
* @since 5.8.0
* @var array[]
*/
public $variations = array();
protected $variations = array();

/**
* Block variations callback.
*
* @since 6.5.0
* @var callable[]
*/
public $variation_callback;

/**
* Custom CSS selectors for theme.json style generation.
Expand Down Expand Up @@ -292,6 +300,7 @@ class WP_Block_Type {
* @type string|null $textdomain The translation textdomain.
* @type array[] $styles Alternative block styles.
* @type array[] $variations Block variations.
* @type callable $variation_callback Block variations callback.
* @type array $selectors Custom CSS selectors for theme.json style generation.
* @type array|null $supports Supported features.
* @type array|null $example Structured data for the block preview.
Expand Down Expand Up @@ -325,6 +334,11 @@ 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 ) {
_doing_it_wrong( __CLASS__, __( 'Calling the variation property is not recommended, call get_variations instead.' ), '6.5.0' );
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 && isset( $this->variation_callback ) ) {
return true;
}

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

/**
* Get block variations.
*
* @since 6.5.0
*
* @return array[]
*/
public function get_variations() {
if ( isset( $this->variation_callback ) && is_callable( $this->variation_callback ) ) {
return call_user_func( $this->variation_callback );
}

return $this->variations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ public function prepare_item_for_response( $item, $request ) {
'view_script_handles',
'editor_style_handles',
'style_handles',
'variations',
'block_hooks',
),
$deprecated_fields
Expand All @@ -314,6 +313,10 @@ public function prepare_item_for_response( $item, $request ) {
}
}

if ( rest_is_field_included( 'variations', $fields ) ) {
$data['variations'] = $block_type->get_variations();
}

if ( rest_is_field_included( 'styles', $fields ) ) {
$styles = $this->style_registry->get_registered_styles_for_block( $block_type->name );
$styles = array_values( $styles );
Expand Down
Loading