Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
guard deprecated functions against redeclaration in core
  • Loading branch information
gaambo committed Feb 9, 2024
commit cca0b015258f2e46791ffcd427446dfa369b0e29
68 changes: 64 additions & 4 deletions lib/compat/wordpress-6.5/navigation-block-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function gutenberg_navigation_link_variations_compat( $args ) {
* @param string $post_type The post type name passed from registered_post_type action hook.
* @param WP_Post_Type $post_type_object The post type object passed from registered_post_type.
*/
function block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration
function gutenberg_block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
if ( $post_type_object->show_in_nav_menus ) {
$variation = build_variation_for_navigation_link( $post_type_object, 'post-type' );
Expand All @@ -48,7 +48,7 @@ function block_core_navigation_link_register_post_type_variation( $post_type, $p
* @param array|string $object_type Object type or array of object types.
* @param array $args Array of taxonomy registration arguments.
*/
function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration
function gutenberg_block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) {
$variation = build_variation_for_navigation_link( (object) $args, 'post-type' );
Expand All @@ -64,7 +64,7 @@ function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $obj
*
* @param string $post_type The post type name passed from unregistered_post_type action hook.
*/
function block_core_navigation_link_unregister_post_type_variation( $post_type ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration
function gutenberg_block_core_navigation_link_unregister_post_type_variation( $post_type ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
block_core_navigation_link_unregister_variation( $post_type );
}
Expand All @@ -77,7 +77,67 @@ function block_core_navigation_link_unregister_post_type_variation( $post_type )
*
* @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook.
*/
function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration
function gutenberg_block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
block_core_navigation_link_unregister_variation( $taxonomy );
}

/**
* Registers a variation for a post type / taxonomy for the navigation link block.
*
* @since 6.5.0
* @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead.
*
* @param array $variation Variation array from build_variation_for_navigation_link.
*/
function gutenberg_eblock_core_navigation_link_register_variation( $variation ) {
Comment thread
getdave marked this conversation as resolved.
Outdated
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
/*
* Directly set the variations on the registered block type
* because there's no server side registration for variations (see #47170).
*/
$navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' );
/*
* If the block is not registered yet, bail early.
* Variation will be registered in register_block_core_navigation_link then.
*/
if ( ! $navigation_block_type ) {
return;
}

$navigation_block_type->variations = array_merge(
$navigation_block_type->variations,
array( $variation )
);
}

/**
* Unregisters a variation for a post type / taxonomy for the navigation link block.
*
* @since 6.5.0
* @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead.
*
* @param string $name Name of the post type / taxonomy (which was used as variation name).
*/
function gutenberg_block_core_navigation_link_unregister_variation( $name ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
/*
* Directly get the variations from the registered block type
* because there's no server side (un)registration for variations (see #47170).
*/
$navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' );
// If the block is not registered (yet), there's no need to remove a variation.
if ( ! $navigation_block_type || empty( $navigation_block_type->variations ) ) {
return;
}
$variations = $navigation_block_type->variations;
// Search for the variation and remove it from the array.
foreach ( $variations as $i => $variation ) {
if ( $variation['name'] === $name ) {
unset( $variations[ $i ] );
break;
}
}
// Reindex array after removing one variation.
$navigation_block_type->variations = array_values( $variations );
}
64 changes: 0 additions & 64 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,70 +266,6 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
return $html;
}

/**
* Registers a variation for a post type / taxonomy for the navigation link block.
*
* @since 6.5.0
* @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead.
*
* TODO: After two WP versions (6.7), we can remove this.
*
* @param array $variation Variation array from build_variation_for_navigation_link.
*/
function block_core_navigation_link_register_variation( $variation ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
/*
* Directly set the variations on the registered block type
* because there's no server side registration for variations (see #47170).
*/
$navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' );
/*
* If the block is not registered yet, bail early.
* Variation will be registered in register_block_core_navigation_link then.
*/
if ( ! $navigation_block_type ) {
return;
}

$navigation_block_type->variations = array_merge(
$navigation_block_type->variations,
array( $variation )
);
}

/**
* Unregisters a variation for a post type / taxonomy for the navigation link block.
*
* @since 6.5.0
* @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead.
*
* TODO: After two WP versions (6.7), we can remove this.
*
* @param string $name Name of the post type / taxonomy (which was used as variation name).
*/
function block_core_navigation_link_unregister_variation( $name ) {
_deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' );
/*
* Directly get the variations from the registered block type
* because there's no server side (un)registration for variations (see #47170).
*/
$navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' );
// If the block is not registered (yet), there's no need to remove a variation.
if ( ! $navigation_block_type || empty( $navigation_block_type->variations ) ) {
return;
}
$variations = $navigation_block_type->variations;
// Search for the variation and remove it from the array.
foreach ( $variations as $i => $variation ) {
if ( $variation['name'] === $name ) {
unset( $variations[ $i ] );
break;
}
}
// Reindex array after removing one variation.
$navigation_block_type->variations = array_values( $variations );
}

/**
* Returns a navigation link variation
*
Expand Down