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