From 9587798a175b88499058d57bb41b36fb69d1f9e2 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 29 Jan 2024 15:14:18 +0100 Subject: [PATCH] Remove unnecessary `function_exists` checks --- .../block-bindings/sources/pattern.php | 64 +++++++++---------- .../block-bindings/sources/post-meta.php | 50 +++++++-------- 2 files changed, 55 insertions(+), 59 deletions(-) diff --git a/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php b/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php index 956739bba324e..b43484660f1f8 100644 --- a/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php +++ b/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php @@ -4,43 +4,41 @@ * * @package gutenberg */ -if ( ! function_exists( 'gutenberg_register_block_bindings_pattern_overrides_source' ) && ! function_exists( 'gutenberg_block_bindings_pattern_overrides_callback' ) ) { - function gutenberg_block_bindings_pattern_overrides_callback( $source_attrs, $block_instance, $attribute_name ) { - if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) { - return null; - } - $block_id = $block_instance->attributes['metadata']['id']; - $attribute_override = _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null ); - if ( null === $attribute_override ) { +function gutenberg_block_bindings_pattern_overrides_callback( $source_attrs, $block_instance, $attribute_name ) { + if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) { + return null; + } + $block_id = $block_instance->attributes['metadata']['id']; + $attribute_override = _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null ); + if ( null === $attribute_override ) { + return null; + } + switch ( $attribute_override[0] ) { + case 0: // remove + /** + * TODO: This currently doesn't remove the attribute, but only set it to an empty string. + * It's a temporary solution until the block binding API supports different operations. + */ + return ''; + case 1: // replace + return $attribute_override[1]; + default: return null; - } - switch ( $attribute_override[0] ) { - case 0: // remove - /** - * TODO: This currently doesn't remove the attribute, but only set it to an empty string. - * It's a temporary solution until the block binding API supports different operations. - */ - return ''; - case 1: // replace - return $attribute_override[1]; - default: - return null; - } } +} - function gutenberg_register_block_bindings_pattern_overrides_source() { - // Override the "core/pattern-attributes" source from core. - if ( array_key_exists( 'core/pattern-attributes', get_all_registered_block_bindings_sources() ) ) { - unregister_block_bindings_source( 'core/pattern-attributes' ); - } - register_block_bindings_source( - 'core/pattern-attributes', - array( - 'label' => __( 'Pattern Attributes' ), - 'get_value_callback' => 'gutenberg_block_bindings_pattern_overrides_callback', - ) - ); +function gutenberg_register_block_bindings_pattern_overrides_source() { + // Override the "core/pattern-attributes" source from core. + if ( array_key_exists( 'core/pattern-attributes', get_all_registered_block_bindings_sources() ) ) { + unregister_block_bindings_source( 'core/pattern-attributes' ); } + register_block_bindings_source( + 'core/pattern-attributes', + array( + 'label' => __( 'Pattern Attributes' ), + 'get_value_callback' => 'gutenberg_block_bindings_pattern_overrides_callback', + ) + ); } add_action( 'init', 'gutenberg_register_block_bindings_pattern_overrides_source' ); diff --git a/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php b/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php index 48e8598b208f6..4166ed3243ef0 100644 --- a/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php +++ b/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php @@ -4,36 +4,34 @@ * * @package gutenberg */ -if ( ! function_exists( 'gutenberg_register_block_bindings_post_meta_source' ) && ! function_exists( 'gutenberg_block_bindings_post_meta_callback' ) ) { - function gutenberg_block_bindings_post_meta_callback( $source_attrs ) { - if ( ! isset( $source_attrs['key'] ) ) { - return null; - } - - // Use the postId attribute if available - if ( isset( $source_attrs['postId'] ) ) { - $post_id = $source_attrs['postId']; - } else { - // I tried using $block_instance->context['postId'] but it wasn't available in the image block. - $post_id = get_the_ID(); - } +function gutenberg_block_bindings_post_meta_callback( $source_attrs ) { + if ( ! isset( $source_attrs['key'] ) ) { + return null; + } - return get_post_meta( $post_id, $source_attrs['key'], true ); + // Use the postId attribute if available + if ( isset( $source_attrs['postId'] ) ) { + $post_id = $source_attrs['postId']; + } else { + // I tried using $block_instance->context['postId'] but it wasn't available in the image block. + $post_id = get_the_ID(); } - function gutenberg_register_block_bindings_post_meta_source() { - // Override the "core/post-meta" source from core. - if ( array_key_exists( 'core/post-meta', get_all_registered_block_bindings_sources() ) ) { - unregister_block_bindings_source( 'core/post-meta' ); - } - register_block_bindings_source( - 'core/post-meta', - array( - 'label' => __( 'Post Meta' ), - 'get_value_callback' => 'gutenberg_block_bindings_post_meta_callback', - ) - ); + return get_post_meta( $post_id, $source_attrs['key'], true ); +} + +function gutenberg_register_block_bindings_post_meta_source() { + // Override the "core/post-meta" source from core. + if ( array_key_exists( 'core/post-meta', get_all_registered_block_bindings_sources() ) ) { + unregister_block_bindings_source( 'core/post-meta' ); } + register_block_bindings_source( + 'core/post-meta', + array( + 'label' => __( 'Post Meta' ), + 'get_value_callback' => 'gutenberg_block_bindings_post_meta_callback', + ) + ); } add_action( 'init', 'gutenberg_register_block_bindings_post_meta_source' );