PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.2
Pods – Custom Content Types and Fields v3.2.2
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 / Meta.php
pods / src / Pods / WP Last commit date
UI 2 years ago Bindings.php 2 years ago Meta.php 2 years ago Revisions.php 2 years ago
Meta.php
187 lines
1 <?php
2
3 namespace Pods\WP;
4
5 use Pods\Whatsit\Pod;
6 use PodsMeta;
7 use PodsForm;
8
9 /**
10 * Meta specific functionality.
11 *
12 * @since 3.2.0
13 */
14 class Meta {
15
16 /**
17 * The list of registered meta.
18 *
19 * @since 3.2.0
20 */
21 private $registered_meta = [];
22
23 /**
24 * Add the class hooks.
25 *
26 * @since 3.2.0
27 */
28 public function hook() {
29 $this->register_meta();
30 }
31
32 /**
33 * Remove the class hooks.
34 *
35 * @since 3.2.0
36 */
37 public function unhook() {
38 $this->unregister_meta();
39 }
40
41 /**
42 * Determine whether to register meta fields.
43 *
44 * @since 3.2.0
45 *
46 * @return bool Whether to register meta fields.
47 */
48 public function should_register_meta(): bool {
49 $should_register_meta = filter_var( pods_get_setting( 'register_meta_integration', false ), FILTER_VALIDATE_BOOLEAN );
50
51 /**
52 * Allow filtering whether to register meta fields.
53 *
54 * @since 3.2.0
55 *
56 * @param bool $should_register_meta Whether to register meta fields.
57 */
58 return (bool) apply_filters( 'pods_wp_meta_should_register_meta', $should_register_meta );
59 }
60
61 /**
62 * Register the meta fields only if needed.
63 *
64 * @since 3.2.0
65 */
66 public function register_meta() {
67 if ( ! $this->should_register_meta() ) {
68 return;
69 }
70
71 $can_use_dynamic_feature_display = pods_can_use_dynamic_feature( 'display' );
72
73 $pods_to_register = [
74 'post' => [],
75 ];
76
77 foreach ( PodsMeta::$post_types as $pod ) {
78 if ( ! $pod instanceof Pod || ! pods_can_use_dynamic_features( $pod ) ) {
79 continue;
80 }
81
82 $pods_to_register['post'][] = $pod;
83 }
84
85 foreach ( $pods_to_register as $type => $pods ) {
86 /** @var array<int,Pod> $pods */
87 foreach ( $pods as $pod ) {
88 $pod_name = $pod->get_name();
89 $pod_type = $pod->get_type();
90 $pod_storage = $pod->get_storage();
91 $fields = $pod->get_fields();
92
93 // REST API pod config.
94 $pod_rest_enabled = filter_var( $pod->get_arg( 'rest_enable', false ), FILTER_VALIDATE_BOOLEAN );
95
96 $pod_rest_field_location = $pod->get_arg( 'rest_api_field_location', 'object', true );
97
98 $post_type_has_custom_fields = (
99 $pod_rest_enabled
100 && 'post_type' === $pod_type
101 && post_type_supports( $pod_name, 'custom-fields' )
102 );
103
104 $all_fields_have_rest = (
105 $can_use_dynamic_feature_display
106 && $pod_rest_enabled
107 && $post_type_has_custom_fields
108 && filter_var( $pod->get_arg( 'read_all', false ), FILTER_VALIDATE_BOOLEAN )
109 );
110
111 // Revision pod config.
112 $all_fields_have_revisions = (
113 'post_type' === $pod_type
114 && 'meta' === $pod_storage
115 && filter_var( $pod->get_arg( 'read_all', false ), FILTER_VALIDATE_BOOLEAN )
116 );
117
118 foreach ( $fields as $field ) {
119 $field_is_repeatable = $field->is_repeatable();
120
121 // REST API field config.
122 $field_has_rest = (
123 'meta' === $pod_rest_field_location
124 && $post_type_has_custom_fields
125 && $can_use_dynamic_feature_display
126 && (
127 $all_fields_have_rest
128 || (
129 $pod_rest_enabled
130 && filter_var( $field->get_arg( 'rest_read', false ), FILTER_VALIDATE_BOOLEAN )
131 )
132 )
133 );
134
135 // Revision field config.
136 $field_has_revisions = (
137 $all_fields_have_revisions
138 || (
139 'post_type' === $pod_type
140 && 'meta' === $pod_storage
141 && filter_var( $pod->get_arg( 'revisions_revision_field', false ), FILTER_VALIDATE_BOOLEAN )
142 )
143 );
144
145 register_meta(
146 $type,
147 $field['name'],
148 [
149 'object_subtype' => $pod_name,
150 'type' => 'string',
151 'description' => $field['label'],
152 'default' => $field->get_arg( 'default' ),
153 'single' => $field_is_repeatable,
154 'show_in_rest' => $field_has_rest,
155 'revisions_enabled' => $field_has_revisions,
156 ]
157 );
158
159 if ( ! isset( $this->registered_meta[ $type ] ) ) {
160 $this->registered_meta[ $type ] = [];
161 }
162
163 $this->registered_meta[] = [
164 'object_type' => $type,
165 'meta_key' => $field['name'],
166 'object_subtype' => $pod_name,
167 ];
168 }
169 }
170 }
171 }
172
173 /**
174 * Unregister the meta fields.
175 *
176 * @since 3.2.0
177 */
178 public function unregister_meta() {
179 foreach ( $this->registered_meta as $field ) {
180 unregister_meta_key( $field['object_type'], $field['meta_key'], $field['object_subtype'] );
181 }
182
183 $this->registered_meta = [];
184 }
185
186 }
187