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 |