<?php
/**
 * Product_CPT Class.
 * CPT for Custom Post Type
 *
 * @since 1.0.0
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! class_exists( 'SBUY_Product_CPT' ) ) {
	class SBUY_Product_CPT {

		/**
		 * Constructor
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			$this->custom_post_type();

			add_filter( 'display_post_states', array( $this, 'add_display_post_states' ), 10, 2 );
		}

		/**
		 * Custom post type
		 *
		 * @since 1.0.0
		 */
		public function custom_post_type(): void {
			register_post_type( 'sbuy_product', array(
				'labels'      => array(
					'name'          => esc_html__( 'Products', 'simply-buy' ),
					'singular_name' => esc_html__( 'Product', 'simply-buy' ),
					'add_new'       => _x( 'Add New', 'product', 'simply-buy' ),
					'add_new_item'  => esc_html__( 'Add New Product', 'simply-buy' ),
				),
				'public'      => true,
				'has_archive' => true,
				'menu_icon'   => 'dashicons-cart',
			) );
		}

		/**
		 * Add a post-display state for special SimplyBuy pages in the page list table.
		 *
		 * @param array $post_states An array of post-display states.
		 * @param WP_Post $post The current post-object.
		 *
		 * @since 1.0.0
		 */
		public function add_display_post_states( array $post_states, WP_Post $post ): array {

			$options_pages = get_option( 'sbuy_settings_pages' );

			foreach ( $options_pages as $page => $page_id ) {

				if ( $post->ID == $page_id ) {

					switch ( $page ) :
						case 'simply_buy_cart_page':
							$post_states['sbuy_page_for_cart'] = esc_html__( 'Cart Page', 'simply-buy' );
							break;
						case 'simply_buy_checkout_page':
							$post_states['sbuy_page_for_checkout'] = esc_html__( 'Checkout Page', 'simply-buy' );
							break;
						case 'simply_buy_thankyou_page':
							$post_states['sbuy_page_for_thankyou'] = esc_html__( 'Thank You Page', 'simply-buy' );
							break;
						case 'planting_tree_page':
							$post_states['sbuy_page_for_checkout'] = esc_html__( 'Planting Tree Page', 'simply-buy' );
							break;
					endswitch;

				}
			}

			return $post_states;
		}
	}
}
