<?php

/* Theme setup section
-------------------------------------------------------------------- */
if (!function_exists('seohub_sc_accordion_theme_setup')) {
	add_action( 'seohub_action_before_init_theme', 'seohub_sc_accordion_theme_setup' );
	function seohub_sc_accordion_theme_setup() {
		add_action('seohub_action_shortcodes_list', 		'seohub_sc_accordion_reg_shortcodes');
		if (function_exists('seohub_exists_visual_composer') && seohub_exists_visual_composer())
			add_action('seohub_action_shortcodes_list_vc','seohub_sc_accordion_reg_shortcodes_vc');
	}
}



/* Shortcode implementation
-------------------------------------------------------------------- */

/*
[trx_accordion counter="off" initial="1"]
	[trx_accordion_item title="Accordion Title 1"]Lorem ipsum dolor sit amet, consectetur adipisicing elit[/trx_accordion_item]
	[trx_accordion_item title="Accordion Title 2"]Proin dignissim commodo magna at luctus. Nam molestie justo augue, nec eleifend urna laoreet non.[/trx_accordion_item]
	[trx_accordion_item title="Accordion Title 3 with custom icons" icon_closed="icon-check" icon_opened="icon-delete"]Curabitur tristique tempus arcu a placerat.[/trx_accordion_item]
[/trx_accordion]
*/
if (!function_exists('seohub_sc_accordion')) {	
	function seohub_sc_accordion($atts, $content=null){	
		if (seohub_in_shortcode_blogger()) return '';
		extract(seohub_html_decode(shortcode_atts(array(
			// Individual params
			"initial" => "1",
			"counter" => "off",
			"icon_closed" => "icon-plus",
			"icon_opened" => "icon-minus",
			// Common params
			"id" => "",
			"class" => "",
			"css" => "",
			"animation" => "",
			"top" => "",
			"bottom" => "",
			"left" => "",
			"right" => ""
		), $atts)));
		$class .= ($class ? ' ' : '') . seohub_get_css_position_as_classes($top, $right, $bottom, $left);
		$initial = max(0, (int) $initial);
		global $SEOHUB_GLOBALS;
		$SEOHUB_GLOBALS['sc_accordion_counter'] = 0;
		$SEOHUB_GLOBALS['sc_accordion_show_counter'] = seohub_param_is_on($counter);
		$SEOHUB_GLOBALS['sc_accordion_icon_closed'] = empty($icon_closed) || seohub_param_is_inherit($icon_closed) ? "icon-plus" : $icon_closed;
		$SEOHUB_GLOBALS['sc_accordion_icon_opened'] = empty($icon_opened) || seohub_param_is_inherit($icon_opened) ? "icon-minus" : $icon_opened;
		seohub_enqueue_script('jquery-ui-accordion', false, array('jquery','jquery-ui-core'), null, true);
		$output = '<div' . ($id ? ' id="'.esc_attr($id).'"' : '') 
				. ' class="sc_accordion'
					. (!empty($class) ? ' '.esc_attr($class) : '')
					. (seohub_param_is_on($counter) ? ' sc_show_counter' : '') 
				. '"'
				. ($css!='' ? ' style="'.esc_attr($css).'"' : '') 
				. ' data-active="' . ($initial-1) . '"'
				. (!seohub_param_is_off($animation) ? ' data-animation="'.esc_attr(seohub_get_animation_classes($animation)).'"' : '')
				. '>'
				. do_shortcode($content)
				. '</div>';
		return apply_filters('seohub_shortcode_output', $output, 'trx_accordion', $atts, $content);
	}
	seohub_require_shortcode('trx_accordion', 'seohub_sc_accordion');
}


if (!function_exists('seohub_sc_accordion_item')) {	
	function seohub_sc_accordion_item($atts, $content=null) {
		if (seohub_in_shortcode_blogger()) return '';
		extract(seohub_html_decode(shortcode_atts( array(
			// Individual params
			"icon_closed" => "",
			"icon_opened" => "",
			"title" => "",
			// Common params
			"id" => "",
			"class" => "",
			"css" => ""
		), $atts)));
		global $SEOHUB_GLOBALS;
		$SEOHUB_GLOBALS['sc_accordion_counter']++;
		if (empty($icon_closed) || seohub_param_is_inherit($icon_closed)) $icon_closed = $SEOHUB_GLOBALS['sc_accordion_icon_closed'] ? $SEOHUB_GLOBALS['sc_accordion_icon_closed'] : "icon-plus";
		if (empty($icon_opened) || seohub_param_is_inherit($icon_opened)) $icon_opened = $SEOHUB_GLOBALS['sc_accordion_icon_opened'] ? $SEOHUB_GLOBALS['sc_accordion_icon_opened'] : "icon-minus";
		$output = '<div' . ($id ? ' id="'.esc_attr($id).'"' : '') 
				. ' class="sc_accordion_item' 
				. (!empty($class) ? ' '.esc_attr($class) : '')
				. ($SEOHUB_GLOBALS['sc_accordion_counter'] % 2 == 1 ? ' odd' : ' even') 
				. ($SEOHUB_GLOBALS['sc_accordion_counter'] == 1 ? ' first' : '') 
				. '">'
				. '<h5 class="sc_accordion_title">'
				. (!seohub_param_is_off($icon_closed) ? '<span class="sc_accordion_icon sc_accordion_icon_closed '.esc_attr($icon_closed).'"></span>' : '')
				. (!seohub_param_is_off($icon_opened) ? '<span class="sc_accordion_icon sc_accordion_icon_opened '.esc_attr($icon_opened).'"></span>' : '')
				. ($SEOHUB_GLOBALS['sc_accordion_show_counter'] ? '<span class="sc_items_counter">'.($SEOHUB_GLOBALS['sc_accordion_counter']).'</span>' : '')
				. ($title)
				. '</h5>'
				. '<div class="sc_accordion_content"'
					. ($css!='' ? ' style="'.esc_attr($css).'"' : '') 
					. '>'
					. do_shortcode($content) 
				. '</div>'
				. '</div>';
		return apply_filters('seohub_shortcode_output', $output, 'trx_accordion_item', $atts, $content);
	}
	seohub_require_shortcode('trx_accordion_item', 'seohub_sc_accordion_item');
}



/* Add shortcode in the internal SC Builder
-------------------------------------------------------------------- */
if ( !function_exists( 'seohub_sc_accordion_reg_shortcodes' ) ) {
	//add_action('seohub_action_shortcodes_list', 'seohub_sc_accordion_reg_shortcodes');
	function seohub_sc_accordion_reg_shortcodes() {
		global $SEOHUB_GLOBALS;
	
		$SEOHUB_GLOBALS['shortcodes']["trx_accordion"] = array(
			"title" => esc_html__("Accordion", "seohub"),
			"desc" => wp_kses( __("Accordion items", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
			"decorate" => true,
			"container" => false,
			"params" => array(
				"counter" => array(
					"title" => esc_html__("Counter", "seohub"),
					"desc" => wp_kses( __("Display counter before each accordion title", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"value" => "off",
					"type" => "switch",
					"options" => $SEOHUB_GLOBALS['sc_params']['on_off']
				),
				"initial" => array(
					"title" => esc_html__("Initially opened item", "seohub"),
					"desc" => wp_kses( __("Number of initially opened item", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"value" => 1,
					"min" => 0,
					"type" => "spinner"
				),
				"icon_closed" => array(
					"title" => esc_html__("Icon while closed",  'seohub'),
					"desc" => wp_kses( __('Select icon for the closed accordion item from Fontello icons set',  'seohub'), $SEOHUB_GLOBALS['allowed_tags'] ),
					"value" => "",
					"type" => "icons",
					"options" => $SEOHUB_GLOBALS['sc_params']['icons']
				),
				"icon_opened" => array(
					"title" => esc_html__("Icon while opened",  'seohub'),
					"desc" => wp_kses( __('Select icon for the opened accordion item from Fontello icons set',  'seohub'), $SEOHUB_GLOBALS['allowed_tags'] ),
					"value" => "",
					"type" => "icons",
					"options" => $SEOHUB_GLOBALS['sc_params']['icons']
				),
				"top" => $SEOHUB_GLOBALS['sc_params']['top'],
				"bottom" => $SEOHUB_GLOBALS['sc_params']['bottom'],
				"left" => $SEOHUB_GLOBALS['sc_params']['left'],
				"right" => $SEOHUB_GLOBALS['sc_params']['right'],
				"id" => $SEOHUB_GLOBALS['sc_params']['id'],
				"class" => $SEOHUB_GLOBALS['sc_params']['class'],
				"animation" => $SEOHUB_GLOBALS['sc_params']['animation'],
				"css" => $SEOHUB_GLOBALS['sc_params']['css']
			),
			"children" => array(
				"name" => "trx_accordion_item",
				"title" => esc_html__("Item", "seohub"),
				"desc" => wp_kses( __("Accordion item", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
				"container" => true,
				"params" => array(
					"title" => array(
						"title" => esc_html__("Accordion item title", "seohub"),
						"desc" => wp_kses( __("Title for current accordion item", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"value" => "",
						"type" => "text"
					),
					"icon_closed" => array(
						"title" => esc_html__("Icon while closed",  'seohub'),
						"desc" => wp_kses( __('Select icon for the closed accordion item from Fontello icons set',  'seohub'), $SEOHUB_GLOBALS['allowed_tags'] ),
						"value" => "",
						"type" => "icons",
						"options" => $SEOHUB_GLOBALS['sc_params']['icons']
					),
					"icon_opened" => array(
						"title" => esc_html__("Icon while opened",  'seohub'),
						"desc" => wp_kses( __('Select icon for the opened accordion item from Fontello icons set',  'seohub'), $SEOHUB_GLOBALS['allowed_tags'] ),
						"value" => "",
						"type" => "icons",
						"options" => $SEOHUB_GLOBALS['sc_params']['icons']
					),
					"_content_" => array(
						"title" => esc_html__("Accordion item content", "seohub"),
						"desc" => wp_kses( __("Current accordion item content", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"rows" => 4,
						"value" => "",
						"type" => "textarea"
					),
					"id" => $SEOHUB_GLOBALS['sc_params']['id'],
					"class" => $SEOHUB_GLOBALS['sc_params']['class'],
					"css" => $SEOHUB_GLOBALS['sc_params']['css']
				)
			)
		);
	}
}


/* Add shortcode in the VC Builder
-------------------------------------------------------------------- */
if ( !function_exists( 'seohub_sc_accordion_reg_shortcodes_vc' ) ) {
	//add_action('seohub_action_shortcodes_list_vc', 'seohub_sc_accordion_reg_shortcodes_vc');
	function seohub_sc_accordion_reg_shortcodes_vc() {
		global $SEOHUB_GLOBALS;
	
		vc_map( array(
			"base" => "trx_accordion",
			"name" => esc_html__("Accordion", "seohub"),
			"description" => wp_kses( __("Accordion items", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
			"category" => esc_html__('Content', 'js_composer'),
			'icon' => 'icon_trx_accordion',
			"class" => "trx_sc_collection trx_sc_accordion",
			"content_element" => true,
			"is_container" => true,
			"show_settings_on_create" => false,
			"as_parent" => array('only' => 'trx_accordion_item'),	// Use only|except attributes to limit child shortcodes (separate multiple values with comma)
			"params" => array(
				array(
					"param_name" => "counter",
					"heading" => esc_html__("Counter", "seohub"),
					"description" => wp_kses( __("Display counter before each accordion title", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => array("Add item numbers before each element" => "on" ),
					"type" => "checkbox"
				),
				array(
					"param_name" => "initial",
					"heading" => esc_html__("Initially opened item", "seohub"),
					"description" => wp_kses( __("Number of initially opened item", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => 1,
					"type" => "textfield"
				),
				array(
					"param_name" => "icon_closed",
					"heading" => esc_html__("Icon while closed", "seohub"),
					"description" => wp_kses( __("Select icon for the closed accordion item from Fontello icons set", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => $SEOHUB_GLOBALS['sc_params']['icons'],
					"type" => "dropdown"
				),
				array(
					"param_name" => "icon_opened",
					"heading" => esc_html__("Icon while opened", "seohub"),
					"description" => wp_kses( __("Select icon for the opened accordion item from Fontello icons set", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => $SEOHUB_GLOBALS['sc_params']['icons'],
					"type" => "dropdown"
				),
				$SEOHUB_GLOBALS['vc_params']['id'],
				$SEOHUB_GLOBALS['vc_params']['class'],
				$SEOHUB_GLOBALS['vc_params']['animation'],
				$SEOHUB_GLOBALS['vc_params']['css'],
				$SEOHUB_GLOBALS['vc_params']['margin_top'],
				$SEOHUB_GLOBALS['vc_params']['margin_bottom'],
				$SEOHUB_GLOBALS['vc_params']['margin_left'],
				$SEOHUB_GLOBALS['vc_params']['margin_right']
			),
			'default_content' => '
				[trx_accordion_item title="' . esc_html__( 'Item 1 title', 'seohub' ) . '"][/trx_accordion_item]
				[trx_accordion_item title="' . esc_html__( 'Item 2 title', 'seohub' ) . '"][/trx_accordion_item]
			',
			"custom_markup" => '
				<div class="wpb_accordion_holder wpb_holder clearfix vc_container_for_children">
					%content%
				</div>
				<div class="tab_controls">
					<button class="add_tab" title="'.esc_attr__("Add item", "seohub").'">'.esc_html__("Add item", "seohub").'</button>
				</div>
			',
			'js_view' => 'VcTrxAccordionView'
		) );
		
		
		vc_map( array(
			"base" => "trx_accordion_item",
			"name" => esc_html__("Accordion item", "seohub"),
			"description" => wp_kses( __("Inner accordion item", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
			"show_settings_on_create" => true,
			"content_element" => true,
			"is_container" => true,
			'icon' => 'icon_trx_accordion_item',
			"as_child" => array('only' => 'trx_accordion'), 	// Use only|except attributes to limit parent (separate multiple values with comma)
			"as_parent" => array('except' => 'trx_accordion'),
			"params" => array(
				array(
					"param_name" => "title",
					"heading" => esc_html__("Title", "seohub"),
					"description" => wp_kses( __("Title for current accordion item", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"admin_label" => true,
					"class" => "",
					"value" => "",
					"type" => "textfield"
				),
				array(
					"param_name" => "icon_closed",
					"heading" => esc_html__("Icon while closed", "seohub"),
					"description" => wp_kses( __("Select icon for the closed accordion item from Fontello icons set", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => $SEOHUB_GLOBALS['sc_params']['icons'],
					"type" => "dropdown"
				),
				array(
					"param_name" => "icon_opened",
					"heading" => esc_html__("Icon while opened", "seohub"),
					"description" => wp_kses( __("Select icon for the opened accordion item from Fontello icons set", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => $SEOHUB_GLOBALS['sc_params']['icons'],
					"type" => "dropdown"
				),
				$SEOHUB_GLOBALS['vc_params']['id'],
				$SEOHUB_GLOBALS['vc_params']['class'],
				$SEOHUB_GLOBALS['vc_params']['css']
			),
		  'js_view' => 'VcTrxAccordionTabView'
		) );

		class WPBakeryShortCode_Trx_Accordion extends SEOHUB_VC_ShortCodeAccordion {}
		class WPBakeryShortCode_Trx_Accordion_Item extends SEOHUB_VC_ShortCodeAccordionItem {}
	}
}
?>