Bindings.php
93 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ACF Block Bindings |
| 4 | * |
| 5 | * @since 6.2.8 |
| 6 | * @package ACF |
| 7 | */ |
| 8 | |
| 9 | namespace ACF\Blocks; |
| 10 | |
| 11 | /** |
| 12 | * The core ACF Blocks binding class. |
| 13 | */ |
| 14 | class Bindings { |
| 15 | /** |
| 16 | * Block Bindings constructor. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | // Final check we're on WP 6.5 or newer. |
| 20 | if ( ! function_exists( 'register_block_bindings_source' ) ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | add_action( 'acf/init', array( $this, 'register_binding_sources' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Hooked to acf/init, register our binding sources. |
| 29 | */ |
| 30 | public function register_binding_sources() { |
| 31 | if ( acf_get_setting( 'enable_block_bindings' ) ) { |
| 32 | register_block_bindings_source( |
| 33 | 'acf/field', |
| 34 | array( |
| 35 | 'label' => _x( 'ACF Fields', 'The core ACF block binding source name for fields on the current page', 'acf' ), |
| 36 | 'get_value_callback' => array( $this, 'get_value' ), |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Handle returing the block binding value for an ACF meta value. |
| 44 | * |
| 45 | * @since 6.2.8 |
| 46 | * |
| 47 | * @param array $source_attrs An array of the source attributes requested. |
| 48 | * @param \WP_Block $block_instance The block instance. |
| 49 | * @param string $attribute_name The block's bound attribute name. |
| 50 | * @return string|null The block binding value or an empty string on failure. |
| 51 | */ |
| 52 | public function get_value( array $source_attrs, \WP_Block $block_instance, string $attribute_name ) { |
| 53 | if ( ! isset( $source_attrs['key'] ) || ! is_string( $source_attrs['key'] ) ) { |
| 54 | $value = ''; |
| 55 | } else { |
| 56 | $field = get_field_object( $source_attrs['key'], false, true, true, true ); |
| 57 | |
| 58 | if ( ! $field ) { |
| 59 | return ''; |
| 60 | } |
| 61 | |
| 62 | if ( ! acf_field_type_supports( $field['type'], 'bindings', true ) ) { |
| 63 | if ( is_preview() ) { |
| 64 | return apply_filters( 'acf/bindings/field_not_supported_message', '[' . esc_html__( 'The requested ACF field type does not support output in Block Bindings or the ACF shortcode.', 'acf' ) . ']' ); |
| 65 | } else { |
| 66 | return ''; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ( isset( $field['allow_in_bindings'] ) && ! $field['allow_in_bindings'] ) { |
| 71 | if ( is_preview() ) { |
| 72 | return apply_filters( 'acf/bindings/field_not_allowed_message', '[' . esc_html__( 'The requested ACF field is not allowed to be output in bindings or the ACF Shortcode.', 'acf' ) . ']' ); |
| 73 | } else { |
| 74 | return ''; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $value = $field['value']; |
| 79 | |
| 80 | if ( is_array( $value ) ) { |
| 81 | $value = implode( ', ', $value ); |
| 82 | } |
| 83 | |
| 84 | // If we're not a scalar we'd throw an error, so return early for safety. |
| 85 | if ( ! is_scalar( $value ) ) { |
| 86 | $value = null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return apply_filters( 'acf/blocks/binding_value', $value, $source_attrs, $block_instance, $attribute_name ); |
| 91 | } |
| 92 | } |
| 93 |