Skip to content

Commit

Permalink
Allow extending taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Jun 12, 2024
1 parent 98cddd4 commit f1c1781
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace ExtCPTs;

use WP_Taxonomy;

class Taxonomy {

/**
Expand Down Expand Up @@ -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.
Expand All @@ -209,6 +217,18 @@ 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 ) {

Check failure on line 225 in src/Taxonomy.php

View workflow job for this annotation

GitHub Actions / PHP / PHP 7.4 / PHP / PHP 7.4

Method ExtCPTs\Taxonomy::extend() has no return type specified.

Check failure on line 225 in src/Taxonomy.php

View workflow job for this annotation

GitHub Actions / PHP / PHP 8.3 / PHP / PHP 8.3

Method ExtCPTs\Taxonomy::extend() has no return type specified.
# 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 f1c1781

Please sign in to comment.