<?php

namespace Gigneo;

use Carbon_Fields\Container\Container;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Term_Meta
 *
 * Plugin term-meta class for Gigneo.
 *
 * @since 0.0.1
 */
class Term_Meta {

	/**
	 * The single instance of the class.
	 *
	 * @var Term_Meta|null
	 * @since 0.0.1
	 */
	private static $instance = null;

	/**
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @return Term_Meta
	 * @since 0.0.1
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor for the class.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {

		// Register term meta
		add_action( 'carbon_fields_register_fields', array( $this, 'register_term_meta' ) );
	}

	/**
	 * Register term meta fields.
	 *
	 * @since 0.0.1
	 */
	public function register_term_meta() {

		//Register city term meta
		$this->add_taxonomy_city_term_meta();

		//Register category term meta
		$this->add_taxonomy_category_term_meta();
	}

	/**
	 * Register city term meta fields.
	 *
	 * @since 0.0.1
	 */
	public function add_taxonomy_city_term_meta() {

		$gigneo_city_disabled = gigneo_option( '_gigneo_ts_disable_cities', false );

		if ( $gigneo_city_disabled ) {
			return;
		}

		$gigneo_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/term-meta/fields/city.php';
		$gigneo_fields = apply_filters( 'gigneo_tm_city_fields', $gigneo_fields );

		Container::make( 'term_meta', 'City fields' )
			->set_classes( 'gn-term-meta-container' )
			->where( 'term_taxonomy', '=', 'gigneo_city' )
			->add_fields( $gigneo_fields );
	}

	/**
	 * Register city term meta fields.
	 *
	 * @since 0.0.1
	 */
	public function add_taxonomy_category_term_meta() {

		$gigneo_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/term-meta/fields/category.php';
		$gigneo_fields = apply_filters( 'gigneo_tm_category_fields', $gigneo_fields );

		Container::make( 'term_meta', 'Category fields' )
				->set_classes( 'gn-term-meta-container' )
				->where( 'term_taxonomy', '=', 'gigneo_facet' )
				->add_fields( $gigneo_fields );
	}
}
