PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / Blocks / Bindings.php
secure-custom-fields / includes / Blocks Last commit date
Bindings.php 1 year ago index.php 1 year ago
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