<?php
/**
 * Content Blocks Widget
 *
 * @package Hootkit
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Class HootKit_Content_Blocks_Widget
 */
class HootKit_Content_Blocks_Widget extends HK_Widget {

	function __construct() {

		$id = 'content-blocks';

		$settings['id'] = "hootkit-{$id}";
		$settings['name'] = hootkit()->get_string( $id );
		$settings['widget_options'] = array(
			'description'	=> __( 'Display Styled Content Blocks', 'hootkit' ),
		);
		$settings['control_options'] = array();
		$settings['form_options'] = array(
			'title' => array(
				'name'		=> __( 'Title (optional)', 'hootkit' ),
				'type'		=> 'text',
			),
			'subtitle' => array(
				'name'		=> __( 'Sub Title (optional)', 'hootkit' ),
				'type'		=> 'text',
			),
			'style' => array(
				'name'		=> __( 'Blocks Style', 'hootkit' ),
				'type'		=> 'images',
				'std'		=> 'style1',
				'options'	=> array(
					'style1'	=> hootkit()->uri . 'assets/images/content-block-style-1.png',
					'style2'	=> hootkit()->uri . 'assets/images/content-block-style-2.png',
					'style3'	=> hootkit()->uri . 'assets/images/content-block-style-3.png',
					'style4'	=> hootkit()->uri . 'assets/images/content-block-style-4.png',
					'style5'	=> hootkit()->uri . 'assets/images/content-block-style-5.png',
					'style6'	=> hootkit()->uri . 'assets/images/content-block-style-6.png',
				),
			),
			'columns' => array(
				'name'		=> __( 'Number Of Columns', 'hootkit' ),
				'type'		=> 'select',
				'std'		=> '4',
				'options'	=> array(
					'1'	=> __( '1', 'hootkit' ),
					'2'	=> __( '2', 'hootkit' ),
					'3'	=> __( '3', 'hootkit' ),
					'4'	=> __( '4', 'hootkit' ),
					'5'	=> __( '5', 'hootkit' ),
				),
			),
			'evenspacecol' => array(
				'name'		=> __( 'Evenly space posts vertically in multiple columns', 'hootkit' ),
				'type'		=> 'checkbox',
				'std'		=> 1,
			),
			'border' => array(
				'name'		=> __( 'Border', 'hootkit' ),
				'desc'		=> __( 'Top and bottom borders.', 'hootkit' ),
				'type'		=> 'select',
				'std'		=> 'none none',
				'options'	=> array(
					'line line'		=> __( 'Top - Line || Bottom - Line', 'hootkit' ),
					'line shadow'	=> __( 'Top - Line || Bottom - DoubleLine', 'hootkit' ),
					'line none'		=> __( 'Top - Line || Bottom - None', 'hootkit' ),
					'shadow line'	=> __( 'Top - DoubleLine || Bottom - Line', 'hootkit' ),
					'shadow shadow'	=> __( 'Top - DoubleLine || Bottom - DoubleLine', 'hootkit' ),
					'shadow none'	=> __( 'Top - DoubleLine || Bottom - None', 'hootkit' ),
					'none line'		=> __( 'Top - None || Bottom - Line', 'hootkit' ),
					'none shadow'	=> __( 'Top - None || Bottom - DoubleLine', 'hootkit' ),
					'none none'		=> __( 'Top - None || Bottom - None', 'hootkit' ),
				),
			),
			'boxes' => array(
				'name'		=> __( 'Content Boxes', 'hootkit' ),
				'type'		=> 'group',
				'options'	=> array(
					'item_name'	=> __( 'Content Box', 'hootkit' ),
					'dellimit'	=> true,
					'sortable'	=> true,
				),
				'fields'	=> array(
					'title' => array(
						'name'		=> __( 'Title', 'hootkit' ),
						'type'		=> 'text',
					),
					'subtitle' => array(
						'name'		=> __( 'Sub Title (optional)', 'hootkit' ),
						'type'		=> 'text',
					),
					'content' => array(
						'name'		=> __( 'Content', 'hootkit' ),
						'type'		=> 'textarea',
					),
					'image' => array(
						'name'		=> __( 'Image', 'hootkit' ),
						'desc'		=> __( 'Remove any icon below to use image', 'hootkit' ),
						'type'		=> 'image',
					),
					'icon' => array(
						'name'		=> __( 'Icon', 'hootkit' ),
						'desc'		=> __( 'Use an icon instead of image', 'hootkit' ),
						'type'		=> 'icon',
					),
					'icon_style' => array(
						'name'		=> __( 'Icon Style', 'hootkit' ),
						'type'		=> 'smallselect',
						'std'		=> 'circle',
						'options'	=> array(
							'none'		=> __( 'None', 'hootkit' ),
							'circle'	=> __( 'Circle', 'hootkit' ),
							'square'	=> __( 'Square', 'hootkit' ),
						),
					),
					'icon_color' => array(
						'name'		=> __( 'Icon Color', 'hootkit' ),
						'desc'		=> __( 'Leave empty for default accent color', 'hootkit' ),
						'type'		=> 'color',
					),
					'icon_size' => array(
						'name'		=> __( 'Icon Size', 'hootkit' ),
						'desc'		=> __( '(in pixels) Leave empty for default icon size', 'hootkit' ),
						'type'		=> 'text',
						'settings'	=> array( 'size' => 3 ),
						'sanitize'	=> 'absint',
					),
					'link' => array(
						'name'		=> __( 'Link Text (optional)', 'hootkit' ),
						'type'		=> 'text',
						'std'		=> __( 'Know More', 'hootkit' ),
					),
					'url'=> array(
						'name'		=> __( 'Link URL (optional)', 'hootkit' ),
						'type'		=> 'text',
						'sanitize'	=> 'url',
					),
					'target' => array(
						'name'		=> __( 'Open Link In New Window', 'hootkit' ),
						'type'		=> 'checkbox',
						'boxdivi'	=> 'div-ve0',
					),
				),
			),
			'customcss' => array(
				'name'		=> __( 'Widget Options', 'hootkit' ),
				'type'		=> 'collapse',
				'fields'	=> array(
					'class' => array(
						'name'		=> __( 'Custom CSS Class', 'hootkit' ),
						'desc'		=> __( 'Give this widget a custom css classname', 'hootkit' ),
						'type'		=> 'text',
					),
					'mt' => array(
						'name'		=> __( 'Margin Top', 'hootkit' ),
						'desc'		=> __( '(in pixels) Leave empty to load default margins.<br>Hint: You can use negative numbers also.', 'hootkit' ),
						'type'		=> 'text',
						'settings'	=> array( 'size' => 3 ),
						'sanitize'	=> 'integer',
					),
					'mb' => array(
						'name'		=> __( 'Margin Bottom', 'hootkit' ),
						'desc'		=> __( '(in pixels) Leave empty to load default margins.<br>Hint: You can use negative numbers also.', 'hootkit' ),
						'type'		=> 'text',
						'settings'	=> array( 'size' => 3 ),
						'sanitize'	=> 'integer',
					),
				),
			),
		);

		if ( ! hootkit()->supports( 'content-blocks-style5' ) ) {
			unset( $settings['form_options']['style']['options']['style5'] );
		}
		if ( ! hootkit()->supports( 'content-blocks-style6' ) ) {
			unset( $settings['form_options']['style']['options']['style6'] );
		}
		if ( ! hootkit()->supports( 'widget-subtitle' ) ) {
			unset( $settings['form_options']['subtitle'] );
		}
		if ( ! hootkit()->supports( 'cbox-evenspacecol' ) ) {
			unset( $settings['form_options']['evenspacecol'] );
		}
		if ( ! hootkit()->supports( 'content-blocks-iconoptions' ) ) {
			unset( $settings['form_options']['boxes']['fields']['icon_color'] );
			unset( $settings['form_options']['boxes']['fields']['icon_size'] );
		}

		$settings = apply_filters( 'hootkit_content_blocks_widget_settings', $settings );

		parent::__construct( $settings['id'], $settings['name'], $settings['widget_options'], $settings['control_options'], $settings['form_options'] );

	}

	/**
	 * Display the widget content
	 */
	function display_widget( $instance, $before_title = '', $title = '', $after_title = '' ) {
		// Allow theme/child-themes to use their own template
		$widget_template = hoot_get_widget( 'content-blocks', false );
		// Use Hootkit template if theme does not have one
		$widget_template = apply_filters( 'hootkit_widget_template', $widget_template, 'content-blocks' );
		$widget_template = ( $widget_template ) ? $widget_template : hootkit()->dir . 'widgets/content-blocks/view.php';
		// Option to overwrite variables to keep html tags in title later sanitized during display => skips 'widget_title' filter (esc_html hooked) action on title; (Possibly redundant as html is sanitized in title during save)
		if ( apply_filters( 'hootkit_display_widget_extract_overwrite', false, 'content-blocks' ) ) extract( $instance, EXTR_OVERWRITE ); else extract( $instance, EXTR_SKIP );
		// Fire up the template
		if ( is_string( $widget_template ) && file_exists( $widget_template ) ) include ( $widget_template );
	}

}

/**
 * Register Widget
 */
function hootkit_content_blocks_widget_register(){
	register_widget( 'HootKit_Content_Blocks_Widget' );
}
add_action( 'widgets_init', 'hootkit_content_blocks_widget_register' );