PluginProbe
Advanced Custom Fields (ACF®) / 6.8.2
Advanced Custom Fields (ACF®) v6.8.2
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / src / Blocks / Bindings.php
Bindings.php
99 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package ACF
4 * @author WP Engine
5 *
6 * © 2026 Advanced Custom Fields (ACF®). All rights reserved.
7 * "ACF" is a trademark of WP Engine.
8 * Licensed under the GNU General Public License v2 or later.
9 * https://www.gnu.org/licenses/gpl-2.0.html
10 */
11
12 namespace ACF\Blocks;
13
14 // Exit if accessed directly.
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * The core ACF Blocks binding class.
19 */
20 class Bindings {
21 /**
22 * Block Bindings constructor.
23 */
24 public function __construct() {
25 // Final check we're on WP 6.5 or newer.
26 if ( ! function_exists( 'register_block_bindings_source' ) ) {
27 return;
28 }
29
30 add_action( 'acf/init', array( $this, 'register_binding_sources' ) );
31 }
32
33 /**
34 * Hooked to acf/init, register our binding sources.
35 */
36 public function register_binding_sources() {
37 if ( acf_get_setting( 'enable_block_bindings' ) ) {
38 register_block_bindings_source(
39 'acf/field',
40 array(
41 'label' => _x( 'Custom Fields', 'The core ACF block binding source name for fields on the current page', 'acf' ),
42 'get_value_callback' => array( $this, 'get_value' ),
43 )
44 );
45 }
46 }
47
48 /**
49 * Handle returing the block binding value for an ACF meta value.
50 *
51 * @since 6.2.8
52 *
53 * @param array $source_attrs An array of the source attributes requested.
54 * @param \WP_Block $block_instance The block instance.
55 * @param string $attribute_name The block's bound attribute name.
56 * @return string|null The block binding value or an empty string on failure.
57 */
58 public function get_value( array $source_attrs, \WP_Block $block_instance, string $attribute_name ) {
59 if ( ! isset( $source_attrs['key'] ) || ! is_string( $source_attrs['key'] ) ) {
60 $value = '';
61 } else {
62 $field = get_field_object( $source_attrs['key'], false, true, true, true );
63
64 if ( ! $field ) {
65 return '';
66 }
67
68 if ( ! acf_field_type_supports( $field['type'], 'bindings', true ) ) {
69 if ( is_preview() ) {
70 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' ) . ']' );
71 } else {
72 return '';
73 }
74 }
75
76 if ( isset( $field['allow_in_bindings'] ) && ! $field['allow_in_bindings'] ) {
77 if ( is_preview() ) {
78 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' ) . ']' );
79 } else {
80 return '';
81 }
82 }
83
84 $value = $field['value'];
85
86 if ( is_array( $value ) ) {
87 $value = implode( ', ', $value );
88 }
89
90 // If we're not a scalar we'd throw an error, so return early for safety.
91 if ( ! is_scalar( $value ) ) {
92 $value = null;
93 }
94 }
95
96 return apply_filters( 'acf/blocks/binding_value', $value, $source_attrs, $block_instance, $attribute_name );
97 }
98 }
99