Skip to content

Commit

Permalink
Allow extending taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Mar 23, 2021
1 parent f0b9d11 commit d6ad39e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/class-extended-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,14 @@ public function __construct( string $taxonomy, $object_type, array $args = [], a
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.
Expand All @@ -212,6 +218,18 @@ public function __construct( string $taxonomy, $object_type, array $args = [], a
do_action( "ext-taxos/{$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'];
}

/**
* Add our rewrite tests to the Rewrite Rule Testing tests array.
*
Expand Down

0 comments on commit d6ad39e

Please sign in to comment.