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

if ( ! class_exists( 'Qode_Badges_For_WooCommerce_Custom_Post_Types' ) ) {
	class Qode_Badges_For_WooCommerce_Custom_Post_Types {
		private static $instance;
		private $allowed_post_types = array();

		public function __construct() {

			// Set properties value.
			$this->set_enabled_post_types();

			// Allows custom meta box options for this CPT.
			add_filter( 'qode_badges_for_woocommerce_filter_has_meta_box_options', '__return_true' );

			// Include register post types file.
			add_action( 'qode_badges_for_woocommerce_framework_action_before_post_types_register', array( $this, 'include_register_files' ) );

			// Register post types.
			// Priority 11 set because include of files is called on default action 10.
			add_action( 'qode_badges_for_woocommerce_framework_action_before_post_types_register', array( $this, 'register_post_types' ), 11 );

			// Include taxonomies.
			add_action( 'qode_badges_for_woocommerce_action_framework_custom_taxonomy_fields', array( $this, 'include_taxonomies' ) );

			// Register taxonomies.
			add_action( 'qode_badges_for_woocommerce_action_framework_custom_taxonomy_fields', array( $this, 'register_taxonomies' ), 11 );

			// Include shortcodes.
			add_action( 'qode_badges_for_woocommerce_action_framework_before_shortcodes_register', array( $this, 'include_shortcodes' ) );

			// Include files.
			$this->include_files();
		}

		/**
		 * Instance of module class
		 *
		 * @return Qode_Badges_For_WooCommerce_Custom_Post_Types
		 */
		public static function get_instance() {
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		public function get_allowed_post_types() {
			return $this->allowed_post_types;
		}

		public function set_allowed_post_types( $allowed_post_types ) {
			$this->allowed_post_types[] = $allowed_post_types;
		}

		public function set_enabled_post_types() {

			foreach ( glob( QODE_BADGES_FOR_WOOCOMMERCE_CPT_PATH . '/*', GLOB_ONLYDIR ) as $post_type ) {
				$this->set_allowed_post_types( $post_type );
			}

			do_action( 'qode_badges_for_woocommerce_action_add_custom_post_type', $this );
		}

		public function include_register_files() {
			$post_types = $this->get_allowed_post_types();

			if ( ! empty( $post_types ) ) {
				foreach ( $post_types as $post_type ) {
					foreach ( glob( $post_type . '/*-cpt.php' ) as $post_type_load ) {
						include_once $post_type_load;
					}
				}
			}
		}

		public function register_post_types() {
			$qode_badges_for_woocommerce_framework = qode_badges_for_woocommerce_framework_get_framework_root();
			$cpts                                  = apply_filters( 'qode_badges_for_woocommerce_filter_register_custom_post_types', array() );

			if ( ! empty( $cpts ) ) {
				foreach ( $cpts as $cpt ) {
					$qode_badges_for_woocommerce_framework->add_custom_post_type( new $cpt() );
				}
			}
		}

		public function include_taxonomies() {
			// Hook to include custom post types taxonomy fields.
			do_action( 'qode_badges_for_woocommerce_action_include_cpt_tax_fields' );
		}

		public function register_taxonomies() {
			// Hook to include custom post types taxonomy fields.
			do_action( 'qode_badges_for_woocommerce_action_register_cpt_tax_fields' );
		}

		public function include_shortcodes() {
			$post_types = $this->get_allowed_post_types();

			if ( ! empty( $post_types ) ) {
				foreach ( $post_types as $post_type ) {
					foreach ( glob( $post_type . '/shortcodes/*/include.php' ) as $shortcode ) {
						include_once $shortcode;
					}
				}
			}
		}

		public function include_files() {
			$post_types = $this->get_allowed_post_types();

			if ( ! empty( $post_types ) ) {
				foreach ( $post_types as $post_type ) {
					include_once $post_type . '/include.php';
				}
			}
		}
	}

	Qode_Badges_For_WooCommerce_Custom_Post_Types::get_instance();
}
