Bindings.php
106 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 | register_block_bindings_source( 'pods/bindings-field', [ |
| 46 | 'label' => __( 'Pods Field', 'pods' ), |
| 47 | 'get_value_callback' => [ $this, 'get_value' ], |
| 48 | 'uses_context' => [ 'postId', 'postType' ], |
| 49 | ] ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Unregister the block bindings. |
| 55 | * |
| 56 | * @since 3.2.0 |
| 57 | */ |
| 58 | public function unregister_block_bindings() { |
| 59 | if ( function_exists( 'unregister_block_bindings_source' ) && pods_can_use_dynamic_feature( 'display' ) ) { |
| 60 | unregister_block_bindings_source( 'pods/bindings-field' ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the bound value for a bound block. |
| 66 | * |
| 67 | * @since 3.2.0 |
| 68 | * |
| 69 | * @param array $source_args List of source arguments from the block. |
| 70 | * @param WP_Block $block_instance The block instance. |
| 71 | * @param string $attribute_name The name of the block attribute. |
| 72 | * |
| 73 | * @return string The bound value. |
| 74 | */ |
| 75 | public function get_value( $source_args, $block_instance, $attribute_name ) { |
| 76 | if ( empty( $source_args['field'] ) ) { |
| 77 | if ( is_admin() || ( function_exists( 'wp_is_rest_endpoint' ) && wp_is_rest_endpoint() ) || pods_is_admin() ) { |
| 78 | return __( 'You must provide the "field" of the field to bind.', 'pods' ); |
| 79 | } |
| 80 | |
| 81 | return ''; |
| 82 | } |
| 83 | |
| 84 | /** @var Field $field_block */ |
| 85 | $field_block = pods_container( 'pods.blocks.field' ); |
| 86 | |
| 87 | if ( ! $field_block ) { |
| 88 | if ( is_admin() || ( function_exists( 'wp_is_rest_endpoint' ) && wp_is_rest_endpoint() ) || pods_is_admin() ) { |
| 89 | return __( 'Pods blocks are not enabled.', 'pods' ); |
| 90 | } |
| 91 | |
| 92 | return ''; |
| 93 | } |
| 94 | |
| 95 | $value = $field_block->render( $source_args, '', $block_instance ); |
| 96 | |
| 97 | // Only support full HTML for the content attribute. |
| 98 | if ( 'content' !== $attribute_name ) { |
| 99 | $value = wp_strip_all_tags( $value ); |
| 100 | } |
| 101 | |
| 102 | return $value; |
| 103 | } |
| 104 | |
| 105 | } |
| 106 |