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

// Theme init
if (!function_exists('writer_ancora_vc_theme_setup')) {
	add_action( 'writer_ancora_action_before_init_theme', 'writer_ancora_vc_theme_setup', 1 );
	function writer_ancora_vc_theme_setup() {
		if (writer_ancora_exists_visual_composer()) {
			add_action('writer_ancora_action_add_styles',		 				'writer_ancora_vc_frontend_scripts' );
		}
		if (is_admin()) {
			add_filter( 'writer_ancora_filter_required_plugins',					'writer_ancora_vc_required_plugins' );
		}
	}
}

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

// Check if WPBakery PageBuilder in frontend editor mode
if ( !function_exists( 'writer_ancora_vc_is_frontend' ) ) {
	function writer_ancora_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( 'writer_ancora_vc_required_plugins' ) ) {
	function writer_ancora_vc_required_plugins($list=array()) {
		if (in_array('visual_composer', writer_ancora_storage_get('required_plugins'))) {
			$path = writer_ancora_get_file_dir('plugins/install/js_composer.zip');
			if (file_exists($path)) {
				$list[] = array(
					'name' 		=> esc_html__('WPBakery PageBuilder', 'writer-ancora'),
					'slug' 		=> 'js_composer',
					'version'  => '6.7.0',
					'source'	=> $path,
					'required' 	=> false
				);
			}
		}
		return $list;
	}
}

// Enqueue VC custom styles
if ( !function_exists( 'writer_ancora_vc_frontend_scripts' ) ) {
	function writer_ancora_vc_frontend_scripts() {
		if (file_exists(writer_ancora_get_file_dir('css/plugin.visual-composer.css')))
			wp_enqueue_style( 'writer-ancora-plugin-visual-composer-style',  writer_ancora_get_file_url('css/plugin.visual-composer.css'), array(), null );
	}
}
?>