<?php
/* Revolution Slider support functions
------------------------------------------------------------------------------- */

// Theme init
if (!function_exists('healthcoach_revslider_theme_setup')) {
	add_action( 'healthcoach_action_before_init_theme', 'healthcoach_revslider_theme_setup', 1 );
	function healthcoach_revslider_theme_setup() {
		if (healthcoach_exists_revslider()) {
			add_filter( 'healthcoach_filter_list_sliders',					'healthcoach_revslider_list_sliders' );
			add_filter( 'healthcoach_filter_shortcodes_params',			'healthcoach_revslider_shortcodes_params' );
			add_filter( 'healthcoach_filter_theme_options_params',			'healthcoach_revslider_theme_options_params' );

		}
		if (is_admin()) {
			add_filter( 'healthcoach_filter_importer_required_plugins',	'healthcoach_revslider_importer_required_plugins', 10, 2 );
			add_filter( 'healthcoach_filter_required_plugins',				'healthcoach_revslider_required_plugins' );
		}
	}
}

if ( !function_exists( 'healthcoach_revslider_settings_theme_setup2' ) ) {
	add_action( 'healthcoach_action_before_init_theme', 'healthcoach_revslider_settings_theme_setup2', 3 );
	function healthcoach_revslider_settings_theme_setup2() {
		if (healthcoach_exists_revslider()) {

			// Add Revslider specific options in the Theme Options
			healthcoach_storage_set_array_after('options', 'slider_engine', "slider_alias", array(
				"title" => esc_html__('Revolution Slider: Select slider',  'healthcoach'),
				"desc" => wp_kses_data( __("Select slider to show (if engine=revo in the field above)", 'healthcoach') ),
				"override" => "category,services_group,page",
				"dependency" => array(
					'show_slider' => array('yes'),
					'slider_engine' => array('revo')
				),
				"std" => "",
				"options" => healthcoach_get_options_param('list_revo_sliders'),
				"type" => "select"
				)
			);

		}
	}
}

// Check if RevSlider installed and activated
if ( !function_exists( 'healthcoach_exists_revslider' ) ) {
	function healthcoach_exists_revslider() {
		return function_exists('rev_slider_shortcode');
	}
}

// Filter to add in the required plugins list
if ( !function_exists( 'healthcoach_revslider_required_plugins' ) ) {
	
	function healthcoach_revslider_required_plugins($list=array()) {
		if (in_array('revslider', healthcoach_storage_get('required_plugins'))) {
			$path = healthcoach_get_file_dir('plugins/install/revslider.zip');
			if (file_exists($path)) {
				$list[] = array(
					'name' 		=> esc_html__('Revolution Slider', 'healthcoach'),
					'slug' 		=> 'revslider',
                    'version'	=> '6.2.2',
					'source'	=> $path,
					'required' 	=> false
					);
			}
		}
		return $list;
	}
}



// One-click import support
//------------------------------------------------------------------------

// Check RevSlider in the required plugins
if ( !function_exists( 'healthcoach_revslider_importer_required_plugins' ) ) {
	
	function healthcoach_revslider_importer_required_plugins($not_installed='', $list='') {
		
		if (healthcoach_strpos($list, 'revslider')!==false && !healthcoach_exists_revslider() )
			$not_installed .= '<br>' . esc_html__('Revolution Slider', 'healthcoach');
		return $not_installed;
	}
}


// Lists
//------------------------------------------------------------------------

// Add RevSlider in the sliders list, prepended inherit (if need)
if ( !function_exists( 'healthcoach_revslider_list_sliders' ) ) {
	
	function healthcoach_revslider_list_sliders($list=array()) {
		$list["revo"] = esc_html__("Layer slider (Revolution)", 'healthcoach');
		return $list;
	}
}

// Return Revo Sliders list, prepended inherit (if need)
if ( !function_exists( 'healthcoach_get_list_revo_sliders' ) ) {
	function healthcoach_get_list_revo_sliders($prepend_inherit=false) {
		if (($list = healthcoach_storage_get('list_revo_sliders'))=='') {
			$list = array();
			if (healthcoach_exists_revslider()) {
				global $wpdb;
				$rows = $wpdb->get_results( "SELECT alias, title FROM " . esc_sql($wpdb->prefix) . "revslider_sliders" );
				if (is_array($rows) && count($rows) > 0) {
					foreach ($rows as $row) {
						$list[$row->alias] = $row->title;
					}
				}
			}
			$list = apply_filters('healthcoach_filter_list_revo_sliders', $list);
			if (healthcoach_get_theme_setting('use_list_cache')) healthcoach_storage_set('list_revo_sliders', $list);
		}
		return $prepend_inherit ? healthcoach_array_merge(array('inherit' => esc_html__("Inherit", 'healthcoach')), $list) : $list;
	}
}

// Add RevSlider in the shortcodes params
if ( !function_exists( 'healthcoach_revslider_shortcodes_params' ) ) {
	
	function healthcoach_revslider_shortcodes_params($list=array()) {
		$list["revo_sliders"] = healthcoach_get_list_revo_sliders();
		return $list;
	}
}

// Add RevSlider in the Theme Options params
if ( !function_exists( 'healthcoach_revslider_theme_options_params' ) ) {
	
	function healthcoach_revslider_theme_options_params($list=array()) {
		$list["list_revo_sliders"] = array('$healthcoach_get_list_revo_sliders' => '');
		return $list;
	}
}
?>