Skip to content

Commit a034474

Browse files
authored
Merge pull request #450 from alleyinteractive/feature/term-meta-support
Adds support for reading `config/term-meta.json`
2 parents c45dadf + 15786e1 commit a034474

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ function () {
6363

6464
load_scripts();
6565
register_post_meta_from_defs();
66+
register_term_meta_from_defs();
6667
main();

src/meta.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ function register_meta_helper(
127127
}
128128

129129
/**
130-
* Reads the post meta definitions from config and registers them.
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.
131133
*/
132-
function register_post_meta_from_defs(): void {
133-
$filepath = dirname( __DIR__ ) . '/config/post-meta.json';
134+
function register_meta_from_defs( string $meta_context = 'post' ): void {
135+
$filepath = dirname( __DIR__ ) . '/config/' . $meta_context . '-meta.json';
134136

135137
// Ensure the config file exists and is valid.
136138
if ( ! file_exists( $filepath ) || ! in_array( validate_file( $filepath ), [ 0, 2 ], true ) ) {
@@ -152,11 +154,12 @@ function register_post_meta_from_defs(): void {
152154
continue;
153155
}
154156

155-
// Extract post types.
156-
$post_types = $definition['post_types'] ?? [];
157+
// Extract post types or terms.
158+
$definition_key = ( 'post' === $meta_context ) ? 'post_types' : 'terms';
159+
$object_types = $definition[ $definition_key ] ?? [];
157160

158161
// Unset since $definition is passed as register_meta args.
159-
unset( $definition['post_types'] );
162+
unset( $definition[ $definition_key ] );
160163

161164
// Relocate schema, if specified at the top level.
162165
if ( ! empty( $definition['schema'] ) ) {
@@ -171,10 +174,24 @@ function register_post_meta_from_defs(): void {
171174

172175
// Register the meta.
173176
register_meta_helper(
174-
'post',
175-
$post_types, // @phpstan-ignore-line array given
177+
$meta_context,
178+
$object_types, // @phpstan-ignore-line array given
176179
$meta_key,
177-
$definition, // @phpstan-ignore-line array given
180+
$definition, // @phpstan-ignore-line array given
178181
);
179182
}
180183
}
184+
185+
/**
186+
* Reads the post meta definitions from config and registers them.
187+
*/
188+
function register_post_meta_from_defs(): void {
189+
register_meta_from_defs( 'post' );
190+
}
191+
192+
/**
193+
* Reads the term meta definitions from config and registers them.
194+
*/
195+
function register_term_meta_from_defs(): void {
196+
register_meta_from_defs( 'term' );
197+
}

0 commit comments

Comments
 (0)