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

165 lines
6.5 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* License custom taxonomy.
*
* @package WP_FediStream
*/
namespace WP_FediStream\Taxonomies;
// Prevent direct file access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* License taxonomy class.
*
* Hierarchical taxonomy for content licenses.
*/
class License extends AbstractTaxonomy {
/**
* Taxonomy key.
*
* @var string
*/
protected string $taxonomy = 'fedistream_license';
/**
* Post types this taxonomy applies to.
*
* @var array
*/
protected array $post_types = array(
'fedistream_album',
'fedistream_track',
);
/**
* Register the taxonomy.
*
* @return void
*/
public function register(): void {
$labels = array(
'name' => _x( 'Licenses', 'taxonomy general name', 'wp-fedistream' ),
'singular_name' => _x( 'License', 'taxonomy singular name', 'wp-fedistream' ),
'search_items' => __( 'Search Licenses', 'wp-fedistream' ),
'popular_items' => __( 'Popular Licenses', 'wp-fedistream' ),
'all_items' => __( 'All Licenses', 'wp-fedistream' ),
'parent_item' => __( 'Parent License', 'wp-fedistream' ),
'parent_item_colon' => __( 'Parent License:', 'wp-fedistream' ),
'edit_item' => __( 'Edit License', 'wp-fedistream' ),
'view_item' => __( 'View License', 'wp-fedistream' ),
'update_item' => __( 'Update License', 'wp-fedistream' ),
'add_new_item' => __( 'Add New License', 'wp-fedistream' ),
'new_item_name' => __( 'New License Name', 'wp-fedistream' ),
'separate_items_with_commas' => __( 'Separate licenses with commas', 'wp-fedistream' ),
'add_or_remove_items' => __( 'Add or remove licenses', 'wp-fedistream' ),
'choose_from_most_used' => __( 'Choose from the most used licenses', 'wp-fedistream' ),
'not_found' => __( 'No licenses found.', 'wp-fedistream' ),
'no_terms' => __( 'No licenses', 'wp-fedistream' ),
'menu_name' => __( 'Licenses', 'wp-fedistream' ),
'items_list_navigation' => __( 'Licenses list navigation', 'wp-fedistream' ),
'items_list' => __( 'Licenses list', 'wp-fedistream' ),
'back_to_items' => __( '&larr; Back to Licenses', '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' => false,
'show_tagcloud' => false,
'show_in_rest' => true,
'rest_base' => 'licenses',
'query_var' => true,
'rewrite' => array( 'slug' => 'license' ),
'capabilities' => array(
'manage_terms' => 'manage_fedistream_licenses',
'edit_terms' => 'manage_fedistream_licenses',
'delete_terms' => 'manage_fedistream_licenses',
'assign_terms' => 'edit_fedistream_tracks',
),
);
register_taxonomy( $this->taxonomy, $this->post_types, $args );
}
/**
* Get default licenses.
*
* @return array Array of licenses with descriptions.
*/
public static function get_default_licenses(): array {
return array(
'All Rights Reserved' => array(
'description' => __( 'Standard copyright. All rights reserved by the creator.', 'wp-fedistream' ),
'children' => array(),
),
'Creative Commons' => array(
'description' => __( 'Creative Commons licenses for sharing and reuse.', 'wp-fedistream' ),
'children' => array(
'CC0' => __( 'Public Domain Dedication - No rights reserved', 'wp-fedistream' ),
'CC BY' => __( 'Attribution - Credit must be given', 'wp-fedistream' ),
'CC BY-SA' => __( 'Attribution-ShareAlike - Credit and share under same terms', 'wp-fedistream' ),
'CC BY-ND' => __( 'Attribution-NoDerivs - Credit, no modifications', 'wp-fedistream' ),
'CC BY-NC' => __( 'Attribution-NonCommercial - Credit, non-commercial only', 'wp-fedistream' ),
'CC BY-NC-SA' => __( 'Attribution-NonCommercial-ShareAlike', 'wp-fedistream' ),
'CC BY-NC-ND' => __( 'Attribution-NonCommercial-NoDerivs', 'wp-fedistream' ),
),
),
'Public Domain' => array(
'description' => __( 'Works in the public domain with no copyright restrictions.', 'wp-fedistream' ),
'children' => array(),
),
);
}
/**
* Install default licenses.
*
* @return void
*/
public static function install_defaults(): void {
$licenses = self::get_default_licenses();
foreach ( $licenses as $name => $data ) {
// Check if parent exists.
$parent_term = term_exists( $name, 'fedistream_license' );
if ( ! $parent_term ) {
$parent_term = wp_insert_term(
$name,
'fedistream_license',
array( 'description' => $data['description'] )
);
}
if ( is_wp_error( $parent_term ) ) {
continue;
}
$parent_id = is_array( $parent_term ) ? $parent_term['term_id'] : $parent_term;
// Insert children.
foreach ( $data['children'] as $child_name => $child_desc ) {
if ( ! term_exists( $child_name, 'fedistream_license' ) ) {
wp_insert_term(
$child_name,
'fedistream_license',
array(
'parent' => (int) $parent_id,
'description' => $child_desc,
)
);
}
}
}
}
}