<?php
/**
 * Class Taxonomy_Review_Type
 *
 * @package dvcrs
 */

namespace DVCRS;

use MongoDB\BSON\ObjectId;

if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/**
 * Taxonomy_Review_Type Class
 */
class Taxonomy_Review_Type {

	/**
	 * Taxonomy_Review_Type constructor
	 */
	public function __construct() {

		\add_action( 'init', [ $this, 'register_taxonomy' ], 0 );
		\add_filter( 'rewrite_rules_array', [ $this, 'flip_rewrite_rules_order' ], 10, 1 );
		\add_filter( 'page_for_post_types_objects', [ $this, 'add_terms_to_post_type_object_pages' ], 10, 1 );
		\add_filter( 'term_link', [ $this, 'flip_url_order' ], 10, 3 );
	}

	/**
	 * Register custom taxonomy
	 */
	public function register_taxonomy() {

		$labels = [
			'name'                       => _x( 'Types', 'Taxonomy General Name', 'dvcrs-core' ),
			'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'dvcrs-core' ),
			'menu_name'                  => __( 'Types', 'dvcrs-core' ),
			'all_items'                  => __( 'All Types', 'dvcrs-core' ),
			'parent_item'                => __( 'Parent Type', 'dvcrs-core' ),
			'parent_item_colon'          => __( 'Parent Type:', 'dvcrs-core' ),
			'new_item_name'              => __( 'New Type', 'dvcrs-core' ),
			'add_new_item'               => __( 'Add New', 'dvcrs-core' ),
			'edit_item'                  => __( 'Edit Type', 'dvcrs-core' ),
			'update_item'                => __( 'Update Type', 'dvcrs-core' ),
			'view_item'                  => __( 'View Type', 'dvcrs-core' ),
			'separate_items_with_commas' => __( 'Separate review types with commas', 'dvcrs-core' ),
			'add_or_remove_items'        => __( 'Add or remove review types', 'dvcrs-core' ),
			'choose_from_most_used'      => __( 'Choose from the most used', 'dvcrs-core' ),
			'popular_items'              => __( 'Popular Types', 'dvcrs-core' ),
			'search_items'               => __( 'Search Types', 'dvcrs-core' ),
			'not_found'                  => __( 'Not Found', 'dvcrs-core' ),
			'no_terms'                   => __( 'No review-types', 'dvcrs-core' ),
			'items_list'                 => __( 'Types list', 'dvcrs-core' ),
			'items_list_navigation'      => __( 'Types list navigation', 'dvcrs-core' ),
		];
		$args   = [
			'labels'             => $labels,
			'hierarchical'       => true,
			'public'             => true,
			'publicly_queryable' => true,
			'show_in_nav_menus'  => false,
			'show_in_rest'       => false,
			'show_admin_column'  => true,
			'rewrite'            => [
				'slug'       => 'reviews',
				'with_front' => false,
			],
		];

		\register_taxonomy( 'review-type', [ 'review' ], $args );
	}

	public function flip_rewrite_rules_order( $rewrite_rules ) {

		foreach ( $rewrite_rules as $pattern => $match ) {

			if ( false === strpos( $pattern, 'reviews/([^/]+)' ) ) {
				continue;
			}

			unset( $rewrite_rules[ $pattern ] );
			$new_key       = str_replace( 'reviews/([^/]+)', '([^/]+)/reviews', $pattern );
			$rewrite_rules = array_merge( [ $new_key => $match ], $rewrite_rules );
		}

		return $rewrite_rules;
	}

	public function flip_url_order( $termlink, $term, $taxonomy ) {

		if ( 'review-type' !== $taxonomy ) {
			return $termlink;
		}
		$termlink = str_replace( 'reviews/' . $term->slug, $term->slug . '/reviews', $termlink );

		return $termlink;
	}

	public function add_terms_to_post_type_object_pages( $objs ) {

		$terms = get_terms( [ 'taxonomy' => 'review-type' ] );

		foreach ( $terms as $term ) {
			$term_obj                 = new \StdClass();
			$term_obj->name           = $term->slug;
			$term_obj->label          = $term->name;
			$term_obj->id             = $term->term_id;
			$term_obj->disable_editor = false;
			$term_obj->notice         = '';

			$objs[] = $term_obj;
		}

		return $objs;
	}
}

new Taxonomy_Review_Type();
