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