Skip to content

Commit

Permalink
Add callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnynews committed Dec 31, 2023
1 parent a9136b7 commit f3c7a5a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
41 changes: 25 additions & 16 deletions src/wp-includes/blocks/navigation-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,48 +323,57 @@ 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() {
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
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');

// Use two separate arrays as a way to order the variations in the UI.
// Known variations (like Post Link and Page Link) are added to the
// `built_ins` array. Variations for custom post types and taxonomies are
// added to the `variations` array and will always appear after `built-ins.
$built_ins = array();
$built_ins = array();
$variations = array();

if ( $post_types ) {
foreach ( $post_types as $post_type ) {
$variation = build_variation_for_navigation_link( $post_type, 'post-type' );
if ( $post_type->_builtin ) {
if ($post_types) {
foreach ($post_types as $post_type) {
$variation = build_variation_for_navigation_link($post_type, 'post-type');
if ($post_type->_builtin) {
$built_ins[] = $variation;
} else {
$variations[] = $variation;
}
}
}
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
$variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
if ( $taxonomy->_builtin ) {
if ($taxonomies) {
foreach ($taxonomies as $taxonomy) {
$variation = build_variation_for_navigation_link($taxonomy, 'taxonomy');
if ($taxonomy->_builtin) {
$built_ins[] = $variation;
} else {
$variations[] = $variation;
}
}
}

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 ),
'variation_callback' => 'build_navigation_link_block_variations',
)
);
}
Expand Down
36 changes: 21 additions & 15 deletions src/wp-includes/blocks/post-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,55 +59,61 @@ 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,
'show_in_rest' => true,
'show_in_rest' => true,
),
'objects'
);

// Split the available taxonomies to `built_in` and custom ones,
// in order to prioritize the `built_in` taxonomies at the
// search results.
$built_ins = array();
$built_ins = array();
$custom_variations = array();

// Create and register the eligible taxonomies variations.
foreach ( $taxonomies as $taxonomy ) {
foreach ($taxonomies as $taxonomy) {

Check failure on line 80 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space after opening parenthesis is prohibited

Check failure on line 80 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space before closing parenthesis is prohibited
$variation = array(
'name' => $taxonomy->name,
'title' => $taxonomy->label,
'name' => $taxonomy->name,
'title' => $taxonomy->label,
'description' => sprintf(
/* translators: %s: taxonomy's label */
__( 'Display a list of assigned terms from the taxonomy: %s' ),
/* translators: %s: taxonomy's label */
__('Display a list of assigned terms from the taxonomy: %s'),

Check failure on line 86 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 86 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found
$taxonomy->label
),
'attributes' => array(
'attributes' => array(
'term' => $taxonomy->name,
),
'isActive' => array( 'term' ),
'scope' => array( 'inserter', 'transform' ),
'isActive' => array('term'),

Check failure on line 92 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after the array opener in a single line array. Found: no spaces

Check failure on line 92 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space before the array closer in a single line array. Found: no spaces
'scope' => array('inserter', 'transform'),

Check failure on line 93 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after the array opener in a single line array. Found: no spaces

Check failure on line 93 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space before the array closer in a single line array. Found: no spaces
);
// Set the category variation as the default one.
if ( 'category' === $taxonomy->name ) {
if ('category' === $taxonomy->name) {

Check failure on line 96 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space after opening parenthesis is prohibited

Check failure on line 96 in src/wp-includes/blocks/post-terms.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space before closing parenthesis is prohibited
$variation['isDefault'] = true;
}
if ( $taxonomy->_builtin ) {
if ($taxonomy->_builtin) {
$built_ins[] = $variation;
} else {
$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 ),
'variation_callback' => 'build_post_term_block_variations',
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/blocks/template-part.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function register_block_core_template_part() {
__DIR__ . '/template-part',
array(
'render_callback' => 'render_block_core_template_part',
'variations' => build_template_part_block_variations(),
'variation_callback' => 'build_template_part_block_variations',
)
);
}
Expand Down

0 comments on commit f3c7a5a

Please sign in to comment.