diff --git a/src/Taxonomy.php b/src/Taxonomy.php index c271983..6137d01 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -3,6 +3,8 @@ namespace ExtCPTs; +use WP_Taxonomy; + class Taxonomy { /** @@ -196,8 +198,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. @@ -209,6 +217,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 ) { + # 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. *