Last active
January 30, 2019 19:49
-
-
Save jazzsequence/54d5f5d5bba10596bbd4768fef8463ea to your computer and use it in GitHub Desktop.
Taxonomy base class. Uses extended-cpts (https://github.com/johnbillion/extended-cpts) by johnbillion (https://github.com/johnbillion).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Encompass Health Blog Taxonomy Base Class | |
* | |
* Base class for creating and modifying all taxonomies for Encompass Health Blog. | |
* | |
* @package EH\Taxonomies | |
* @author Chris Reynolds <chris@humanmade.com> | |
*/ | |
namespace EH\Taxonomies; | |
/** | |
* Base class for registering or modifying Encompass Health Blog taxonomies. | |
*/ | |
class Taxonomy_Base { | |
/** | |
* Init function used to load in any hooks in child classes. | |
*/ | |
public function init() {} | |
/** | |
* The taxonomy name. | |
* | |
* @return string Returns the taxonomy name. | |
*/ | |
public function taxonomy_name() { | |
return ''; | |
} | |
/** | |
* The terms for a taxonomy. | |
* | |
* @return array Associative array of taxonomy terms and arguments. | |
*/ | |
public function terms() { | |
return []; | |
} | |
/** | |
* The allowed post types for the taxonomy, if we're registering it. | |
* | |
* @return array An array of post types. | |
*/ | |
public function post_types() { | |
return [ 'post' ]; | |
} | |
/** | |
* An array of arguments to pass to Extended CPTs when registering the taxonomy. | |
* | |
* @return array An array of allowed arguments. | |
*/ | |
public function taxonomy_args() { | |
return []; | |
} | |
/** | |
* An array of labels (singular, plural and slug) used to register the taxonomy. | |
* | |
* @return array An array of labels. | |
*/ | |
public function taxonomy_labels() { | |
return []; | |
} | |
/** | |
* Register the taxonomy if it doesn't already exist. | |
*/ | |
public function register() { | |
if ( ! taxonomy_exists( $this->taxonomy_name() ) ) { | |
register_extended_taxonomy( $this->taxonomy_name(), $this->post_types(), $this->taxonomy_args(), $this->taxonomy_labels() ); | |
} | |
} | |
/** | |
* Insert the terms for the taxonomy. | |
* Allows for child terms to be added if structured inside a 'children' parameter in the array of terms. | |
*/ | |
public function init_terms() { | |
foreach ( $this->terms() as $term_name => $args ) { | |
$term = wp_insert_term( $term_name, $this->taxonomy_name(), $args ); | |
// Report if there's an error. | |
if ( is_wp_error( $term ) ) { | |
// translators: 1: the term, 2: the error message. | |
wp_die( esc_html( sprintf( __( 'Could not insert term %1$s. The error returned was: %2$s', 'encompass' ), $term_name, $term->get_error_message() ) ), esc_html__( 'Error inserting term.', 'encompass' ) ); | |
} | |
// If child terms were passed to this term, loop through those and add them, too. | |
if ( isset( $args['children'] ) && is_array( $args['children'] ) ) { | |
foreach ( $args['children'] as $child_term_name => $child_term_args ) { | |
$child_term_args = array_merge( $child_term_args, [ 'parent' => $term['term_taxonomy_id'] ] ); | |
wp_insert_term( $child_term_name, $this->taxonomy_name(), $child_term_args ); | |
} | |
} | |
} | |
} | |
/** | |
* Removes the taxonomy page if not needed. | |
*/ | |
public function remove_menu_page() { | |
remove_submenu_page( 'edit.php', sprintf( 'edit-tags.php?taxonomy=%s', $this->taxonomy_name() ) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example bootstrap. | |
*/ | |
namespace Taxonomies; | |
function bootstrap() { | |
$example_tax = new ExampleTax(); | |
$example_tax->init(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example taxonomy | |
* | |
* Creates the an example taxonomy and handles related functionality. | |
* | |
* @package Taxonomies | |
* @author Chris Reynolds <chris@humanmade.com> | |
*/ | |
namespace Taxonomies; | |
/** | |
* Example Taxonomy taxonomy class. | |
*/ | |
class ExampleTax extends Taxonomy_Base { | |
const TAXONOMY_NAME = 'example_taxo'; | |
/** | |
* Handle all the Audience Types hooks. | |
*/ | |
public function init() { | |
add_action( 'init', [ $this, 'register' ], 10 ); | |
add_action( 'init', [ $this, 'init_terms' ], 20 ); | |
add_action( 'admin_menu', [ $this, 'remove_menu_page' ] ); | |
} | |
/** | |
* Returns the taxonomy name. | |
* | |
* @return string The taxonomy name. | |
*/ | |
public function taxonomy_name() { | |
return self::TAXONOMY_NAME; | |
} | |
/** | |
* The list of audience types. | |
* | |
* @return array Array of taxonomy terms and arguments. | |
*/ | |
public function terms() { | |
return [ | |
__( 'Apples', 'textdomain' ) => [ | |
// Optional taxonomy term arguments go here. | |
], | |
__( 'Oranges', 'textdomain' ) => [ | |
// Optional taxonomy term arguments go here. | |
], | |
__( 'Avocados', 'textdomain' ) => [ | |
// Optional taxonomy term arguments go here. | |
], | |
__( 'Bananas', 'textdomain' ) => [ | |
// Optional taxonomy term arguments go here. | |
], | |
]; | |
} | |
/** | |
* The list of supported post types. | |
* | |
* @return array An array of post types supported by the tax. | |
*/ | |
public function post_types() { | |
return [ 'post' ]; | |
} | |
/** | |
* Defines the labels used in registering the taxonomy. | |
* | |
* @return array An array of audience type labels. | |
*/ | |
public function taxonomy_labels() { | |
return [ | |
'singular' => __( 'Example', 'textdomain' ), | |
'plural' => __( 'Examples', 'textdomain' ), | |
'slug' => 'example', | |
]; | |
} | |
/** | |
* Any allowed arguments for extended taxonomies. | |
* | |
* @see https://github.com/johnbillion/extended-cpts/wiki/Registering-taxonomies | |
* @return array An array of taxonomy arguments. | |
*/ | |
public function taxonomy_args() { | |
return [ 'meta_box' => 'radio' ]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment