Bindings.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\WP; |
| 4 | |
| 5 | use Pods\Blocks\Types\Field; |
| 6 | use WP_Block; |
| 7 | |
| 8 | /** |
| 9 | * Bindings specific functionality. |
| 10 | * |
| 11 | * @since 3.2.0 |
| 12 | */ |
| 13 | class Bindings { |
| 14 | |
| 15 | /** |
| 16 | * Add the class hooks. |
| 17 | * |
| 18 | * @since 3.2.0 |
| 19 | */ |
| 20 | public function hook() { |
| 21 | $this->register_block_bindings(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Remove the class hooks. |
| 26 | * |
| 27 | * @since 3.2.0 |
| 28 | */ |
| 29 | public function unhook() { |
| 30 | $this->unregister_block_bindings(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register the block bindings. |
| 35 | * |
| 36 | * @since 3.2.0 |
| 37 | */ |
| 38 | public function register_block_bindings() { |
| 39 | if ( ! function_exists( 'register_block_bindings_source' ) || ! pods_can_use_dynamic_feature( 'display' ) ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | register_block_bindings_source( 'pods/bindings-field', [ |
| 44 | 'label' => __( 'Pods Field', 'pods' ), |
| 45 | 'get_value_callback' => [ $this, 'get_value' ], |
| 46 | 'uses_context' => [ 'postId', 'postType' ], |
| 47 | ] ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Unregister the block bindings. |
| 52 | * |
| 53 | * @since 3.2.0 |
| 54 | */ |
| 55 | public function unregister_block_bindings() { |
| 56 | if ( ! function_exists( 'unregister_block_bindings_source' ) || ! pods_can_use_dynamic_feature( 'display' ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | unregister_block_bindings_source( 'pods/bindings-field' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the bound value for a bound block. |
| 65 | * |
| 66 | * @since 3.2.0 |
| 67 | * |
| 68 | * @param array $source_args List of source arguments from the block. |
| 69 | * @param WP_Block $block_instance The block instance. |
| 70 | * @param string $attribute_name The name of the block attribute. |
| 71 | * |
| 72 | * @return string The bound value. |
| 73 | */ |
| 74 | public function get_value( $source_args, $block_instance, $attribute_name ) { |
| 75 | if ( empty( $source_args['field'] ) ) { |
| 76 | if ( is_admin() || wp_is_rest_endpoint() || pods_is_admin() ) { |
| 77 | return __( 'You must provide the "field" of the field to bind.', 'pods' ); |
| 78 | } |
| 79 | |
| 80 | return ''; |
| 81 | } |
| 82 | |
| 83 | /** @var Field $field_block */ |
| 84 | $field_block = pods_container( 'pods.blocks.field' ); |
| 85 | |
| 86 | if ( ! $field_block ) { |
| 87 | if ( is_admin() || wp_is_rest_endpoint() || pods_is_admin() ) { |
| 88 | return __( 'Pods blocks are not enabled.', 'pods' ); |
| 89 | } |
| 90 | |
| 91 | return ''; |
| 92 | } |
| 93 | |
| 94 | $value = $field_block->render( $source_args, '', $block_instance ); |
| 95 | |
| 96 | // Only support full HTML for the content attribute. |
| 97 | if ( 'content' !== $attribute_name ) { |
| 98 | $value = wp_strip_all_tags( $value ); |
| 99 | } |
| 100 | |
| 101 | return $value; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |