PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.4
Pods – Custom Content Types and Fields v3.3.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 / Meta.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
Meta.php
200 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 $pod_has_revisions = (
119 'post_type' === $pod_type
120 && post_type_supports( $pod_name, 'revisions' )
121 );
122
123 foreach ( $fields as $field ) {
124 $field_is_repeatable = $field->is_repeatable();
125
126 // REST API field config.
127 $field_has_rest = (
128 'meta' === $pod_rest_field_location
129 && $post_type_has_custom_fields
130 && $can_use_dynamic_feature_display
131 && (
132 $all_fields_have_rest
133 || (
134 $pod_rest_enabled
135 && filter_var( $field->get_arg( 'rest_read', false ), FILTER_VALIDATE_BOOLEAN )
136 )
137 )
138 );
139
140 // Revision field config.
141 $field_has_revisions = (
142 $pod_has_revisions
143 && (
144 $all_fields_have_revisions
145 || (
146 'post_type' === $pod_type
147 && 'meta' === $pod_storage
148 && filter_var( $pod->get_arg( 'revisions_revision_field', false ), FILTER_VALIDATE_BOOLEAN )
149 )
150 )
151 );
152
153 $meta_args = [
154 'object_subtype' => $pod_name,
155 'type' => 'string',
156 'description' => $field['label'],
157 'default' => $field->get_arg( 'default', '' ),
158 'single' => ! $field_is_repeatable,
159 'show_in_rest' => $field_has_rest,
160 ];
161
162 if ( $field_has_revisions ) {
163 $meta_args['revisions_enabled'] = true;
164 }
165
166 register_meta(
167 $type,
168 $field['name'],
169 $meta_args
170 );
171
172 if ( ! isset( $this->registered_meta[ $type ] ) ) {
173 $this->registered_meta[ $type ] = [];
174 }
175
176 $this->registered_meta[] = [
177 'object_type' => $type,
178 'meta_key' => $field['name'],
179 'object_subtype' => $pod_name,
180 ];
181 }
182 }
183 }
184 }
185
186 /**
187 * Unregister the meta fields.
188 *
189 * @since 3.2.0
190 */
191 public function unregister_meta() {
192 foreach ( $this->registered_meta as $field ) {
193 unregister_meta_key( $field['object_type'], $field['meta_key'], $field['object_subtype'] );
194 }
195
196 $this->registered_meta = [];
197 }
198
199 }
200