PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.4
Pods – Custom Content Types and Fields v3.2.4
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / WP / Bindings.php
pods / src / Pods / WP Last commit date
UI 2 years ago Bindings.php 2 years ago Meta.php 1 year ago Revisions.php 2 years ago
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