Files
wp-fedistream/includes/Taxonomies/Genre.php

155 lines
6.1 KiB
PHP
Raw Normal View History

<?php
/**
* Genre custom taxonomy.
*
* @package WP_FediStream
*/
namespace WP_FediStream\Taxonomies;
// Prevent direct file access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Genre taxonomy class.
*
* Hierarchical taxonomy for music genres.
*/
class Genre extends AbstractTaxonomy {
/**
* Taxonomy key.
*
* @var string
*/
protected string $taxonomy = 'fedistream_genre';
/**
* Post types this taxonomy applies to.
*
* @var array
*/
protected array $post_types = array(
'fedistream_artist',
'fedistream_album',
'fedistream_track',
);
/**
* Register the taxonomy.
*
* @return void
*/
public function register(): void {
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name', 'wp-fedistream' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name', 'wp-fedistream' ),
'search_items' => __( 'Search Genres', 'wp-fedistream' ),
'popular_items' => __( 'Popular Genres', 'wp-fedistream' ),
'all_items' => __( 'All Genres', 'wp-fedistream' ),
'parent_item' => __( 'Parent Genre', 'wp-fedistream' ),
'parent_item_colon' => __( 'Parent Genre:', 'wp-fedistream' ),
'edit_item' => __( 'Edit Genre', 'wp-fedistream' ),
'view_item' => __( 'View Genre', 'wp-fedistream' ),
'update_item' => __( 'Update Genre', 'wp-fedistream' ),
'add_new_item' => __( 'Add New Genre', 'wp-fedistream' ),
'new_item_name' => __( 'New Genre Name', 'wp-fedistream' ),
'separate_items_with_commas' => __( 'Separate genres with commas', 'wp-fedistream' ),
'add_or_remove_items' => __( 'Add or remove genres', 'wp-fedistream' ),
'choose_from_most_used' => __( 'Choose from the most used genres', 'wp-fedistream' ),
'not_found' => __( 'No genres found.', 'wp-fedistream' ),
'no_terms' => __( 'No genres', 'wp-fedistream' ),
'menu_name' => __( 'Genres', 'wp-fedistream' ),
'items_list_navigation' => __( 'Genres list navigation', 'wp-fedistream' ),
'items_list' => __( 'Genres list', 'wp-fedistream' ),
'back_to_items' => __( '&larr; Back to Genres', 'wp-fedistream' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true, // Like categories.
'public' => true,
'show_ui' => true,
'show_in_menu' => false, // Will be added to custom menu.
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'rest_base' => 'genres',
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
'capabilities' => array(
'manage_terms' => 'manage_fedistream_genres',
'edit_terms' => 'manage_fedistream_genres',
'delete_terms' => 'manage_fedistream_genres',
'assign_terms' => 'edit_fedistream_tracks',
),
);
register_taxonomy( $this->taxonomy, $this->post_types, $args );
}
/**
* Get default genres.
*
* @return array Array of genres with children.
*/
public static function get_default_genres(): array {
return array(
'Rock' => array( 'Alternative Rock', 'Hard Rock', 'Indie Rock', 'Progressive Rock', 'Punk Rock' ),
'Pop' => array( 'Dance Pop', 'Electropop', 'Indie Pop', 'Synth Pop' ),
'Electronic' => array( 'Ambient', 'Drum and Bass', 'Dubstep', 'House', 'Techno', 'Trance' ),
'Hip Hop' => array( 'Rap', 'Trap', 'Lo-Fi Hip Hop' ),
'Jazz' => array( 'Bebop', 'Cool Jazz', 'Free Jazz', 'Fusion' ),
'Classical' => array( 'Baroque', 'Chamber Music', 'Opera', 'Orchestral', 'Romantic' ),
'R&B' => array( 'Contemporary R&B', 'Neo Soul', 'Soul' ),
'Country' => array( 'Americana', 'Bluegrass', 'Country Rock' ),
'Metal' => array( 'Black Metal', 'Death Metal', 'Heavy Metal', 'Thrash Metal' ),
'Folk' => array( 'Acoustic', 'Folk Rock', 'Traditional Folk' ),
'Blues' => array( 'Chicago Blues', 'Delta Blues', 'Electric Blues' ),
'Reggae' => array( 'Dancehall', 'Dub', 'Roots Reggae', 'Ska' ),
'Latin' => array( 'Bossa Nova', 'Salsa', 'Reggaeton', 'Tango' ),
'World' => array( 'African', 'Asian', 'Celtic', 'Middle Eastern' ),
'Soundtrack' => array( 'Film Score', 'Video Game', 'Musical Theatre' ),
'Other' => array(),
);
}
/**
* Install default genres.
*
* @return void
*/
public static function install_defaults(): void {
$genres = self::get_default_genres();
foreach ( $genres as $parent => $children ) {
// Check if parent exists.
$parent_term = term_exists( $parent, 'fedistream_genre' );
if ( ! $parent_term ) {
$parent_term = wp_insert_term( $parent, 'fedistream_genre' );
}
if ( is_wp_error( $parent_term ) ) {
continue;
}
$parent_id = is_array( $parent_term ) ? $parent_term['term_id'] : $parent_term;
// Insert children.
foreach ( $children as $child ) {
if ( ! term_exists( $child, 'fedistream_genre' ) ) {
wp_insert_term(
$child,
'fedistream_genre',
array( 'parent' => (int) $parent_id )
);
}
}
}
}
}