|
7 | 7 |
|
8 | 8 | namespace Alley\WP\Create_WordPress_Plugin; |
9 | 9 |
|
10 | | -/** |
11 | | - * Register meta for posts or terms with sensible defaults and sanitization. |
12 | | - * |
13 | | - * @throws \InvalidArgumentException For unmet requirements. |
14 | | - * |
15 | | - * @see \register_post_meta |
16 | | - * @see \register_term_meta |
17 | | - * |
18 | | - * @param string $object_type The type of meta to register, which must be one of 'post' or 'term'. |
19 | | - * @param string|string[] $object_slugs The post type or taxonomy slugs to register with. |
20 | | - * @param string $meta_key The meta key to register. |
21 | | - * @param array<string, mixed> $args Optional. Additional arguments for register_post_meta or register_term_meta. Defaults to an empty array. |
22 | | - * @return bool True if the meta key was successfully registered in the global array, false if not. |
23 | | - */ |
24 | | -function register_meta_helper( |
25 | | - string $object_type, |
26 | | - string|array $object_slugs, |
27 | | - string $meta_key, |
28 | | - array $args = [] |
29 | | -): bool { |
30 | | - |
31 | | - // Object type must be either post or term. |
32 | | - if ( ! in_array( $object_type, [ 'post', 'term' ], true ) ) { |
33 | | - throw new \InvalidArgumentException( |
34 | | - esc_html__( |
35 | | - 'Object type must be one of "post", "term".', |
36 | | - 'create-wordpress-plugin' |
37 | | - ) |
38 | | - ); |
39 | | - } |
40 | | - |
41 | | - /** |
42 | | - * Merge provided arguments with defaults and filter register_meta() args. |
43 | | - * |
44 | | - * @link https://developer.wordpress.org/reference/functions/register_meta/ |
45 | | - * |
46 | | - * @param array $args { |
47 | | - * Array of args to be passed to register_meta(). |
48 | | - * |
49 | | - * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, |
50 | | - * the meta key will be registered on the entire object type. Default empty. |
51 | | - * @type string $type The type of data associated with this meta key. Valid values are |
52 | | - * 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
53 | | - * @type string $description A description of the data attached to this meta key. |
54 | | - * @type bool $single Whether the meta key has one value per object, or an array of values per object. |
55 | | - * @type mixed $default The default value returned from get_metadata() if no value has been set yet. |
56 | | - * When using a non-single meta key, the default value is for the first entry. In other words, |
57 | | - * when calling get_metadata() with $single set to false, the default value given here will be wrapped in an array. |
58 | | - * @type callable $sanitize_callback A function or method to call when sanitizing $meta_key data. |
59 | | - * @type callable $auth_callback Optional. A function or method to call when performing edit_post_meta, |
60 | | - * add_post_meta, and delete_post_meta capability checks. |
61 | | - * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and should be |
62 | | - * accessible via the REST API. A custom post type must also declare support |
63 | | - * for custom fields for registered meta to be accessible via REST. When registering |
64 | | - * complex meta values this argument may optionally be an array with 'schema' |
65 | | - * or 'prepare_callback' keys instead of a boolean. |
66 | | - * } |
67 | | - * @param string $object_type The type of meta to register, which must be one of 'post' or 'term'. |
68 | | - * @param string|string[] $object_slugs The post type or taxonomy slugs to register with. |
69 | | - * @param string $meta_key The meta key to register. |
70 | | - */ |
71 | | - $args = apply_filters( |
72 | | - 'create_wordpress_plugin_register_meta_helper_args', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
73 | | - wp_parse_args( |
74 | | - $args, |
75 | | - [ |
76 | | - 'show_in_rest' => true, |
77 | | - 'single' => true, |
78 | | - 'type' => 'string', |
79 | | - ] |
80 | | - ), |
81 | | - $object_type, |
82 | | - $object_slugs, |
83 | | - $meta_key |
84 | | - ); |
85 | | - |
86 | | - // Allow setting meta for all of an object type. |
87 | | - if ( |
88 | | - ( |
89 | | - is_array( $object_slugs ) && |
90 | | - 1 === count( $object_slugs ) && |
91 | | - 'all' === $object_slugs[0] |
92 | | - ) || |
93 | | - ( |
94 | | - is_string( $object_slugs ) && |
95 | | - 'all' === $object_slugs |
96 | | - ) |
97 | | - ) { |
98 | | - return register_meta( $object_type, $meta_key, $args ); // @phpstan-ignore-line array given |
99 | | - } |
100 | | - |
101 | | - // Fix potential errors since we're allowing `$object_slugs` to be a string or array. |
102 | | - if ( is_string( $object_slugs ) ) { |
103 | | - $object_slugs = [ $object_slugs ]; |
104 | | - } |
105 | | - |
106 | | - // Fork for object type. |
107 | | - switch ( $object_type ) { |
108 | | - case 'post': |
109 | | - foreach ( $object_slugs as $object_slug ) { |
110 | | - if ( ! register_post_meta( $object_slug, $meta_key, $args ) ) { // @phpstan-ignore-line array given |
111 | | - return false; |
112 | | - } |
113 | | - } |
114 | | - break; |
115 | | - case 'term': |
116 | | - foreach ( $object_slugs as $object_slug ) { |
117 | | - if ( ! register_term_meta( $object_slug, $meta_key, $args ) ) { // @phpstan-ignore-line array given |
118 | | - return false; |
119 | | - } |
120 | | - } |
121 | | - break; |
122 | | - default: |
123 | | - return false; |
124 | | - } |
125 | | - |
126 | | - return true; |
127 | | -} |
128 | | - |
129 | | -/** |
130 | | - * Reads the meta definitions from config and registers them. |
131 | | - * |
132 | | - * @param 'post'|'term' $meta_context The context in which to register the definitions. |
133 | | - */ |
134 | | -function register_meta_from_defs( string $meta_context = 'post' ): void { |
135 | | - $filepath = dirname( __DIR__ ) . '/config/' . $meta_context . '-meta.json'; |
136 | | - |
137 | | - // Ensure the config file exists and is valid. |
138 | | - if ( ! file_exists( $filepath ) || ! in_array( validate_file( $filepath ), [ 0, 2 ], true ) ) { |
139 | | - return; |
140 | | - } |
141 | | - |
142 | | - // Try to read the file's contents. We can dismiss the "uncached" warning here because it is a local file. |
143 | | - // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown |
144 | | - $definitions = json_decode( (string) file_get_contents( $filepath ), true ); |
145 | | - if ( empty( $definitions ) || ! is_array( $definitions ) ) { |
146 | | - return; |
147 | | - } |
148 | | - |
149 | | - // Loop through definitions and register each. |
150 | | - foreach ( $definitions as $meta_key => $definition ) { |
151 | | - if ( ! is_array( $definition ) ) { |
152 | | - _doing_it_wrong( __FUNCTION__, 'Post meta definition items must be an array.', '1.0.0' ); |
153 | | - |
154 | | - continue; |
155 | | - } |
156 | | - |
157 | | - // Extract post types or terms. |
158 | | - $definition_key = ( 'post' === $meta_context ) ? 'post_types' : 'terms'; |
159 | | - $object_types = $definition[ $definition_key ] ?? []; |
160 | | - |
161 | | - // Unset since $definition is passed as register_meta args. |
162 | | - unset( $definition[ $definition_key ] ); |
163 | | - |
164 | | - // Relocate schema, if specified at the top level. |
165 | | - if ( ! empty( $definition['schema'] ) ) { |
166 | | - if ( ! isset( $definition['show_in_rest'] ) || ! is_array( $definition['show_in_rest'] ) ) { |
167 | | - $definition['show_in_rest'] = []; |
168 | | - } |
169 | | - |
170 | | - $definition['show_in_rest']['schema'] = $definition['schema']; |
171 | | - // Unset since $definition is passed as register_meta args. |
172 | | - unset( $definition['schema'] ); |
173 | | - } |
174 | | - |
175 | | - // Register the meta. |
176 | | - register_meta_helper( |
177 | | - $meta_context, |
178 | | - $object_types, // @phpstan-ignore-line array given |
179 | | - $meta_key, |
180 | | - $definition, // @phpstan-ignore-line array given |
181 | | - ); |
182 | | - } |
183 | | -} |
| 10 | +use function Mantle\Support\Helpers\register_meta_from_file; |
184 | 11 |
|
185 | 12 | /** |
186 | 13 | * Reads the post meta definitions from config and registers them. |
187 | 14 | */ |
188 | 15 | function register_post_meta_from_defs(): void { |
189 | | - register_meta_from_defs( 'post' ); |
| 16 | + if ( file_exists( dirname( __DIR__ ) . '/config/post-meta.json' ) ) { |
| 17 | + register_meta_from_file( dirname( __DIR__ ) . '/config/post-meta.json', 'post' ); |
| 18 | + } |
190 | 19 | } |
191 | 20 |
|
192 | 21 | /** |
193 | 22 | * Reads the term meta definitions from config and registers them. |
194 | 23 | */ |
195 | 24 | function register_term_meta_from_defs(): void { |
196 | | - register_meta_from_defs( 'term' ); |
| 25 | + if ( file_exists( dirname( __DIR__ ) . '/config/term-meta.json' ) ) { |
| 26 | + register_meta_from_file( dirname( __DIR__ ) . '/config/term-meta.json', 'term' ); |
| 27 | + } |
197 | 28 | } |
0 commit comments