PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
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 week ago Bindings_Editor.php 1 month ago
Bindings.php
126 lines
1 <?php
2 /**
3 * SCF Block Bindings
4 *
5 * @since ACF 6.2.8
6 * @package wordpress/secure-custom-fields
7 */
8
9 namespace SCF\Blocks;
10
11 // Exit if accessed directly.
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * The core SCF Blocks binding class.
16 */
17 class Bindings {
18 /**
19 * Block Bindings constructor.
20 */
21 public function __construct() {
22 // Final check we're on WP 6.5 or newer.
23 if ( ! function_exists( 'register_block_bindings_source' ) ) {
24 return;
25 }
26
27 add_action( 'acf/init', array( $this, 'register_binding_sources' ) );
28 }
29
30 /**
31 * Hooked to acf/init, register our binding sources.
32 */
33 public function register_binding_sources() {
34 if ( acf_get_setting( 'enable_block_bindings' ) ) {
35 register_block_bindings_source(
36 'acf/field',
37 array(
38 'label' => _x( 'Custom Fields', 'The core SCF block binding source name for fields on the current page', 'secure-custom-fields' ),
39 'get_value_callback' => array( $this, 'get_value' ),
40 'uses_context' => array( 'postId', 'postType' ),
41 )
42 );
43 }
44 }
45
46 /**
47 * Handle returning the block binding value for an ACF meta value.
48 *
49 * @since ACF 6.2.8
50 *
51 * @param array $source_attrs An array of the source attributes requested.
52 * @param \WP_Block $block_instance The block instance.
53 * @param string $attribute_name The block's bound attribute name.
54 * @return string|null The block binding value or an empty string on failure.
55 */
56 public function get_value( array $source_attrs, \WP_Block $block_instance, string $attribute_name ) {
57 if ( ! isset( $source_attrs['key'] ) || ! is_string( $source_attrs['key'] ) ) {
58 $value = '';
59 } else {
60 $field = get_field_object( $source_attrs['key'], false, true, true, true );
61
62 if ( ! $field ) {
63 return '';
64 }
65
66 if ( ! acf_field_type_supports( $field['type'], 'bindings', true ) ) {
67 if ( is_preview() ) {
68 return apply_filters( 'acf/bindings/field_not_supported_message', '[' . esc_html__( 'The requested SCF field type does not support output in Block Bindings or the SCF shortcode.', 'secure-custom-fields' ) . ']' );
69 } else {
70 return '';
71 }
72 }
73
74 if ( isset( $field['allow_in_bindings'] ) && ! $field['allow_in_bindings'] ) {
75 if ( is_preview() ) {
76 return apply_filters( 'acf/bindings/field_not_allowed_message', '[' . esc_html__( 'The requested SCF field is not allowed to be output in bindings or the SCF Shortcode.', 'secure-custom-fields' ) . ']' );
77 } else {
78 return '';
79 }
80 }
81
82 $field_value = $field['value'];
83
84 switch ( $attribute_name ) {
85 case 'id':
86 case 'alt':
87 case 'title':
88 // The value is in the field of the same name.
89 $value = is_array( $field_value ) ? $field_value[ $attribute_name ] ?? '' : '';
90 break;
91 case 'url':
92 if ( is_array( $field_value ) ) {
93 // The URL is in the array returned by media-like fields.
94 $value = $field_value['url'] ?? '';
95 } elseif ( is_scalar( $field_value ) || null === $field_value ) {
96 // Scalar URL-like fields use the field value directly.
97 $value = $field_value ?? '';
98 } else {
99 $value = '';
100 }
101 break;
102 case 'rel':
103 // Handle checkbox field for rel attribute by joining array values.
104 if ( is_array( $field_value ) ) {
105 $value = implode( ' ', $field_value );
106 } elseif ( is_scalar( $field_value ) || null === $field_value ) {
107 $value = $field_value ?? '';
108 } else {
109 $value = '';
110 }
111 break;
112 default:
113 $value = $field_value;
114
115 if ( is_array( $value ) ) {
116 $value = wp_json_encode( $value );
117 } elseif ( ! is_scalar( $value ) && null !== $value ) {
118 $value = '';
119 }
120 }
121 }
122
123 return apply_filters( 'acf/blocks/binding_value', $value, $source_attrs, $block_instance, $attribute_name );
124 }
125 }
126