<?php
/**
 * Business Interface Blog » Blog Page / Archive section in Customizer.
 *
 * @package     Business Interface
 * @author      WPInterface Team
 * @since       1.0.0
 */

/**
 * Do not allow direct script access.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Business_Interface_Customizer_Blog_Page' ) ) :
	/**
	 * Business Interface Blog » Blog Page / Archive section in Customizer.
	 */
	class Business_Interface_Customizer_Blog_Page {

		/**
		 * Primary class constructor.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {

			/**
			 * Registers our custom options in Customizer.
			 */
			add_filter( 'business_interface_customizer_options', array( $this, 'register_options' ) );
		}

		/**
		 * Registers our custom options in Customizer.
		 *
		 * @since 1.0.0
		 * @param array $options Array of customizer options.
		 */
		public function register_options( $options ) {

			// Section.
			$options['section']['business_interface_section_blog_page'] = array(
				'title' => esc_html__( 'Blog Page / Archive', 'business-interface' ),
				'panel' => 'business_interface_panel_blog',
			);

			// Layout.
			$options['setting']['business_interface_blog_layout'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'label'       => esc_html__( 'Layout', 'business-interface' ),
					'description' => esc_html__( 'Choose blog layout. This will affect blog layout on archives, search results and posts page.', 'business-interface' ),
					'section'     => 'business_interface_section_blog_page',
					'choices'     => array(
						'blog-layout-1'   => esc_html__( 'Vertical', 'business-interface' ),
						'blog-horizontal' => esc_html__( 'Horizontal', 'business-interface' ),
					),
				),
			);

			$_image_sizes = business_interface_get_image_sizes();
			$size_choices = array();

			if ( ! empty( $_image_sizes ) ) {
				foreach ( $_image_sizes as $key => $value ) {
					$name = ucwords( str_replace( array( '-', '_' ), ' ', $key ) );

					$size_choices[ $key ] = $name;

					if ( $value['width'] || $value['height'] ) {
						$size_choices[ $key ] .= ' (' . $value['width'] . 'x' . $value['height'] . ')';
					}
				}
			}

			// Featured Image Size.
			$options['setting']['business_interface_blog_image_size'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'label'       => esc_html__( 'Featured Image Size', 'business-interface' ),
					'section'     => 'business_interface_section_blog_page',
					'choices'     => $size_choices,
				),
			);

			// Post Elements.
			$options['setting']['business_interface_blog_entry_elements'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_sortable',
				'control'           => array(
					'type'        => 'business-interface-sortable',
					'section'     => 'business_interface_section_blog_page',
					'label'       => esc_html__( 'Post Elements', 'business-interface' ),
					'description' => esc_html__( 'Set order and visibility for post elements.', 'business-interface' ),
					'choices'     => array(
						'summary'        => esc_html__( 'Summary', 'business-interface' ),
						'header'         => esc_html__( 'Title', 'business-interface' ),
						'meta'           => esc_html__( 'Post Meta', 'business-interface' ),
						'thumbnail'      => esc_html__( 'Featured Image', 'business-interface' ),
						'summary-footer' => esc_html__( 'Read More', 'business-interface' ),
					),
					'required'    => array(
						array(
							'control'  => 'business_interface_blog_layout',
							'value'    => 'blog-layout-1',
							'operator' => '==',
						),
					),
				),
			);

			// Meta/Post Details Layout.
			$options['setting']['business_interface_blog_entry_meta_elements'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_sortable',
				'control'           => array(
					'type'        => 'business-interface-sortable',
					'section'     => 'business_interface_section_blog_page',
					'label'       => esc_html__( 'Post Meta', 'business-interface' ),
					'description' => esc_html__( 'Set order and visibility for post meta details.', 'business-interface' ),
					'choices'     => array(
						'author'   => esc_html__( 'Author', 'business-interface' ),
						'date'     => esc_html__( 'Publish Date', 'business-interface' ),
						'comments' => esc_html__( 'Comments', 'business-interface' ),
						'category' => esc_html__( 'Categories', 'business-interface' ),
						'tag'      => esc_html__( 'Tags', 'business-interface' ),
					),
				),
			);

			// Post Categories.
			$options['setting']['business_interface_blog_horizontal_post_categories'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Show Post Categories', 'business-interface' ),
					'description' => esc_html__( 'A list of categories the post belongs to. Displayed above post title.', 'business-interface' ),
					'section'     => 'business_interface_section_blog_page',
					'required'    => array(
						array(
							'control'  => 'business_interface_blog_layout',
							'value'    => 'blog-horizontal',
							'operator' => '==',
						),
					),
				),
			);

			// Read More Button.
			$options['setting']['business_interface_blog_horizontal_read_more'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Show Read More Button', 'business-interface' ),
					'section'     => 'business_interface_section_blog_page',
					'required'    => array(
						array(
							'control'  => 'business_interface_blog_layout',
							'value'    => 'blog-horizontal',
							'operator' => '==',
						),
					),
				),
			);

			// Meta Author image.
			$options['setting']['business_interface_entry_meta_icons'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'    => 'business-interface-toggle',
					'section' => 'business_interface_section_blog_page',
					'label'   => esc_html__( 'Show avatar and icons in post meta', 'business-interface' ),
				),
			);

			// Featured Image Position.
			$options['setting']['business_interface_blog_image_position'] = array(
				'transport'         => 'postMessage',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'label'       => esc_html__( 'Featured Image Position', 'business-interface' ),
					'section'     => 'business_interface_section_blog_page',
					'choices'     => array(
						'left'  => esc_html__( 'Left', 'business-interface' ),
						'right' => esc_html__( 'Right', 'business-interface' ),
					),
					'required'    => array(
						array(
							'control'  => 'business_interface_blog_layout',
							'value'    => 'blog-horizontal',
							'operator' => '==',
						),
					),
				),
			);

			// Excerpt Length.
			$options['setting']['business_interface_excerpt_length'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_range',
				'control'           => array(
					'type'        => 'business-interface-range',
					'section'     => 'business_interface_section_blog_page',
					'label'       => esc_html__( 'Excerpt Length', 'business-interface' ),
					'description' => esc_html__( 'Number of words displayed in the excerpt.', 'business-interface' ),
					'min'         => 0,
					'max'         => 100,
					'step'        => 1,
					'unit'        => '',
					'responsive'  => false,
				),
			);

			// Excerpt more.
			$options['setting']['business_interface_excerpt_more'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'sanitize_text_field',
				'control'           => array(
					'type'        => 'business-interface-text',
					'section'     => 'business_interface_section_blog_page',
					'label'       => esc_html__( 'Excerpt More', 'business-interface' ),
					'description' => esc_html__( 'What to append to excerpt if the text is cut.', 'business-interface' ),
				),
			);

			return $options;
		}
	}
endif;

new Business_Interface_Customizer_Blog_Page();
