<?php
/**
 * Builder Template custom post type.
 *
 * Registers the `ambld_template` post type for reusable header/footer templates.
 *
 * @package Antimanual_Builder
 * @since   1.0.0
 */

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

/**
 * Class AMBLD_Post_Type_Builder_Template
 *
 * Registers and manages the builder template post type.
 *
 * @since 1.0.0
 */
class AMBLD_Post_Type_Builder_Template {

	/**
	 * Post type slug.
	 *
	 * @since 1.0.0
	 * @var   string
	 */
	const POST_TYPE = 'ambld_template';

	/**
	 * Constructor. Registers hooks.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'register' ) );
	}

	/**
	 * Register the custom post type.
	 *
	 * @since  1.0.0
	 * @return void
	 */
	public function register() {
		$labels = array(
			'name'               => _x( 'Templates', 'Post type general name', 'antimanual-builder' ),
			'singular_name'      => _x( 'Template', 'Post type singular name', 'antimanual-builder' ),
			'menu_name'          => _x( 'Templates', 'Admin menu text', 'antimanual-builder' ),
			'add_new'            => __( 'Add New', 'antimanual-builder' ),
			'add_new_item'       => __( 'Add New Template', 'antimanual-builder' ),
			'edit_item'          => __( 'Edit Template', 'antimanual-builder' ),
			'new_item'           => __( 'New Template', 'antimanual-builder' ),
			'view_item'          => __( 'View Template', 'antimanual-builder' ),
			'search_items'       => __( 'Search Templates', 'antimanual-builder' ),
			'not_found'          => __( 'No templates found', 'antimanual-builder' ),
			'not_found_in_trash' => __( 'No templates found in Trash', 'antimanual-builder' ),
		);

		$args = array(
			'labels'              => $labels,
			'public'              => false,
			'publicly_queryable'  => false,
			'show_ui'             => false,
			'show_in_menu'        => false,
			'show_in_rest'        => true,
			'rest_base'           => 'ambld-templates',
			'query_var'           => false,
			'capability_type'     => 'page',
			'has_archive'         => false,
			'hierarchical'        => false,
			'supports'            => array( 'title', 'author', 'thumbnail', 'revisions', 'custom-fields' ),
			'exclude_from_search' => true,
		);

		register_post_type( self::POST_TYPE, $args );
		$this->register_meta();
	}

	/**
	 * Register post meta fields.
	 *
	 * @since  1.0.0
	 * @return void
	 */
	private function register_meta() {
		$edit_auth = function () {
			return current_user_can( 'edit_pages' );
		};

		register_post_meta(
			self::POST_TYPE,
			'_ambld_blocks',
			array(
				'type'              => 'string',
				'single'            => true,
				'show_in_rest'      => true,
				'sanitize_callback' => 'sanitize_text_field',
				'auth_callback'     => $edit_auth,
			)
		);

		register_post_meta(
			self::POST_TYPE,
			'_ambld_rendered_html',
			array(
				'type'              => 'string',
				'single'            => true,
				'show_in_rest'      => true,
				'sanitize_callback' => function( $html ) {
					return current_user_can( 'unfiltered_html' ) ? $html : wp_kses_post( $html );
				},
				'auth_callback'     => $edit_auth,
			)
		);

		register_post_meta(
			self::POST_TYPE,
			'_ambld_style_map',
			array(
				'type'              => 'string',
				'single'            => true,
				'show_in_rest'      => true,
				'sanitize_callback' => 'sanitize_text_field',
				'auth_callback'     => $edit_auth,
			)
		);

		register_post_meta(
			self::POST_TYPE,
			'_ambld_page_settings',
			array(
				'type'              => 'string',
				'single'            => true,
				'show_in_rest'      => true,
				'sanitize_callback' => 'sanitize_text_field',
				'auth_callback'     => $edit_auth,
			)
		);

		register_post_meta(
			self::POST_TYPE,
			'_ambld_template_kind',
			array(
				'type'              => 'string',
				'single'            => true,
				'show_in_rest'      => true,
				'sanitize_callback' => array( $this, 'sanitize_template_kind' ),
				'auth_callback'     => $edit_auth,
			)
		);
	}

	/**
	 * Sanitize template kind values.
	 *
	 * @since  1.0.0
	 * @param  mixed $value Raw meta value.
	 * @return string
	 */
	public function sanitize_template_kind( $value ) {
		$value = sanitize_key( (string) $value );

		return in_array( $value, array( 'header', 'footer' ), true ) ? $value : '';
	}
}
