<?php
/* WPBakery PageBuilder support functions
------------------------------------------------------------------------------- */

// Theme init
if (!function_exists('healthcoach_vc_theme_setup')) {
	add_action( 'healthcoach_action_before_init_theme', 'healthcoach_vc_theme_setup', 1 );
	function healthcoach_vc_theme_setup() {
		if (healthcoach_exists_visual_composer()) {

			add_action('healthcoach_action_add_styles',		 				'healthcoach_vc_frontend_scripts' );
		}
		if (is_admin()) {
			add_filter( 'healthcoach_filter_importer_required_plugins',		'healthcoach_vc_importer_required_plugins', 10, 2 );
			add_filter( 'healthcoach_filter_required_plugins',					'healthcoach_vc_required_plugins' );
		}
	}
}

// Check if WPBakery PageBuilder installed and activated
if ( !function_exists( 'healthcoach_exists_visual_composer' ) ) {
	function healthcoach_exists_visual_composer() {
		return class_exists('Vc_Manager');
	}
}

// Check if WPBakery PageBuilder in frontend editor mode
if ( !function_exists( 'healthcoach_vc_is_frontend' ) ) {
	function healthcoach_vc_is_frontend() {
		return (isset($_GET['vc_editable']) && $_GET['vc_editable']=='true')
			|| (isset($_GET['vc_action']) && $_GET['vc_action']=='vc_inline');
	}
}

// Filter to add in the required plugins list
if ( !function_exists( 'healthcoach_vc_required_plugins' ) ) {
	
	function healthcoach_vc_required_plugins($list=array()) {
		if (in_array('visual_composer', healthcoach_storage_get('required_plugins'))) {
			$path = healthcoach_get_file_dir('plugins/install/js_composer.zip');
			if (file_exists($path)) {
				$list[] = array(
					'name' 		=> esc_html__('WPBakery PageBuilder', 'healthcoach'),
					'slug' 		=> 'js_composer',
                    'version'	=> '6.2',
					'source'	=> $path,
					'required' 	=> false
				);
			}
		}
		return $list;
	}
}

// Enqueue VC custom styles
if ( !function_exists( 'healthcoach_vc_frontend_scripts' ) ) {
	
	function healthcoach_vc_frontend_scripts() {
		if (file_exists(healthcoach_get_file_dir('css/plugin.visual-composer.css')))
			wp_enqueue_style( 'healthcoach-plugin-visual-composer-style',  healthcoach_get_file_url('css/plugin.visual-composer.css'), array(), null );
	}
}



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

// Check VC in the required plugins
if ( !function_exists( 'healthcoach_vc_importer_required_plugins' ) ) {
	
	function healthcoach_vc_importer_required_plugins($not_installed='', $list='') {
		if (!healthcoach_exists_visual_composer() )		
			$not_installed .= '<br>' . esc_html__('WPBakery PageBuilder', 'healthcoach');
		return $not_installed;
	}
}
?>