From 1109637befb73b223512dc0ba15105f7a8f002c9 Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Tue, 23 Mar 2021 21:51:53 +0100 Subject: [PATCH] Allow extending taxonomy --- src/Taxonomy.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 3e8b958..b2c6230 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -3,6 +3,8 @@ namespace ExtCPTs; +use WP_Taxonomy; + class Taxonomy { /** @@ -197,8 +199,14 @@ public function init(): void { add_filter( 'rewrite_testing_tests', [ $this, 'rewrite_testing_tests' ], 1 ); } - # Register taxonomy: - $this->register_taxonomy(); + $existing = get_taxonomy( $this->taxonomy ); + + if ( empty( $existing ) ) { + # Register taxonomy: + $this->register_taxonomy(); + } else { + $this->extend( $existing ); + } /** * Fired when the extended taxonomy instance is set up. @@ -210,6 +218,23 @@ public function init(): void { do_action( "ext-taxos/{$this->taxonomy}/instance", $this ); } + /** + * Extends an existing taxonomy object. Currently only handles labels. + * + * @param WP_Taxonomy $taxonomy A taxonomy object. + */ + public function extend( WP_Taxonomy $taxonomy ): void { + # Merge core with overridden labels + $this->args['labels'] = array_merge( (array) get_taxonomy_labels( $taxonomy ), $this->args['labels'] ); + + $GLOBALS['wp_taxonomies'][ $taxonomy->name ]->labels = (object) $this->args['labels']; + + // Register taxonomy for object types + foreach ( $this->object_type as $object_type ) { + register_taxonomy_for_object_type( $this->taxonomy, $object_type ); + } + } + /** * Add our rewrite tests to the Rewrite Rule Testing tests array. *