PluginProbe
Advanced Custom Fields (ACF®) / 6.2.8
Advanced Custom Fields (ACF®) v6.2.8
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 / includes / Blocks / Bindings.php
Bindings.php
71 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 The block binding value.
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 = null;
55 } else {
56 $value = get_field( $source_attrs['key'] );
57
58 if ( is_array( $value ) ) {
59 $value = implode( ', ', $value );
60 }
61
62 // If we're not a scalar we'd throw an error, so return early for safety.
63 if ( ! is_scalar( $value ) ) {
64 $value = null;
65 }
66 }
67
68 return apply_filters( 'acf/blocks/binding_value', $value, $source_attrs, $block_instance, $attribute_name );
69 }
70 }
71