Skip to content

Commit

Permalink
Remove unnecessary function_exists checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jan 29, 2024
1 parent 27cb39f commit 9587798
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 59 deletions.
64 changes: 31 additions & 33 deletions lib/compat/wordpress-6.5/block-bindings/sources/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
50 changes: 24 additions & 26 deletions lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

0 comments on commit 9587798

Please sign in to comment.