<?php
defined( 'ABSPATH' ) || exit;

/**
 * Frontend CPT registration and shortcode.
 */
class Awp_Slider_Frontend {

	/**
	 * @var Awp_Slider_Frontend|null
	 */
	private static $instance = null;

	/**
	 * @return Awp_Slider_Frontend
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Hooks.
	 */
	public function init() {
		add_action( 'init', array( $this, 'register_post_type' ) );
		add_action( 'init', array( $this, 'register_shortcode' ) );
	}

	/**
	 * Register CPT.
	 */
	public function register_post_type() {
		$labels = array(
			'name'                  => _x( 'AWP Sliders', 'Post Type General Name', 'advance-wp-slider' ),
			'singular_name'         => _x( 'AWP Slider', 'Post Type Singular Name', 'advance-wp-slider' ),
			'menu_name'             => __( 'AWP Sliders', 'advance-wp-slider' ),
			'name_admin_bar'        => __( 'AWP Slider', 'advance-wp-slider' ),
			'archives'              => __( 'Item Archives', 'advance-wp-slider' ),
			'attributes'            => __( 'Item Attributes', 'advance-wp-slider' ),
			'parent_item_colon'     => __( 'Parent Item:', 'advance-wp-slider' ),
			'all_items'             => __( 'All Slides', 'advance-wp-slider' ),
			'add_new_item'          => __( 'Add New Slide', 'advance-wp-slider' ),
			'add_new'               => __( 'Add New', 'advance-wp-slider' ),
			'new_item'              => __( 'New Slide', 'advance-wp-slider' ),
			'edit_item'             => __( 'Edit Slide', 'advance-wp-slider' ),
			'update_item'           => __( 'Update Slide', 'advance-wp-slider' ),
			'view_item'             => __( 'View Slide', 'advance-wp-slider' ),
			'view_items'            => __( 'View Slides', 'advance-wp-slider' ),
			'search_items'          => __( 'Search Slides', 'advance-wp-slider' ),
			'not_found'             => __( 'No slides found', 'advance-wp-slider' ),
			'not_found_in_trash'    => __( 'No slides found in Trash', 'advance-wp-slider' ),
			'featured_image'        => __( 'Slide Image', 'advance-wp-slider' ),
			'set_featured_image'    => __( 'Set slide image', 'advance-wp-slider' ),
			'remove_featured_image' => __( 'Remove slide image', 'advance-wp-slider' ),
			'use_featured_image'    => __( 'Use as slide image', 'advance-wp-slider' ),
			'insert_into_item'      => __( 'Insert into slide', 'advance-wp-slider' ),
			'uploaded_to_this_item' => __( 'Uploaded to this slide', 'advance-wp-slider' ),
			'items_list'            => __( 'Slides list', 'advance-wp-slider' ),
			'items_list_navigation' => __( 'Slides list navigation', 'advance-wp-slider' ),
			'filter_items_list'     => __( 'Filter slides list', 'advance-wp-slider' ),
		);

		register_post_type(
			'awp-slider',
			array(
				'label'               => __( 'AWP Slider', 'advance-wp-slider' ),
				'description'         => __( 'Slides for Advance WP Slider', 'advance-wp-slider' ),
				'labels'              => $labels,
				'supports'            => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
				'hierarchical'        => false,
				'public'              => false,
				'show_ui'             => true,
				'show_in_menu'        => true,
				'menu_position'       => 5,
				'menu_icon'           => 'dashicons-images-alt2',
				'show_in_admin_bar'   => true,
				'show_in_nav_menus'   => false,
				'can_export'          => true,
				'has_archive'         => false,
				'exclude_from_search' => true,
				'publicly_queryable'  => false,
				'show_in_rest'        => true,
				'capability_type'     => 'post',
			)
		);
	}

	/**
	 * Register shortcode.
	 */
	public function register_shortcode() {
		add_shortcode( 'AWP-SLIDER', array( $this, 'render_shortcode' ) );
	}

	/**
	 * Shortcode callback.
	 *
	 * @return string
	 */
	public function render_shortcode() {
		$slides = AWP_Renderer::slides_from_cpt();
		return AWP_Renderer::render( $slides );
	}

	/**
	 * Back-compat alias used by activation hook.
	 */
	public function awp_slider_custom_post_type() {
		$this->register_post_type();
	}
}
