<?php
/* Contact Form 7 support functions
------------------------------------------------------------------------------- */

// Theme init priorities:
// 9 - register other filters (for installer, etc.)
if ( ! function_exists( 'tourimo_cf7_theme_setup9' ) ) {
	add_action( 'after_setup_theme', 'tourimo_cf7_theme_setup9', 9 );
	function tourimo_cf7_theme_setup9() {
		if ( tourimo_exists_cf7() ) {
			add_action( 'wp_enqueue_scripts', 'tourimo_cf7_frontend_scripts', 1100 );
			add_filter( 'tourimo_filter_merge_scripts', 'tourimo_cf7_merge_scripts' );
		}
		if ( is_admin() ) {
			add_filter( 'tourimo_filter_tgmpa_required_plugins', 'tourimo_cf7_tgmpa_required_plugins' );
			add_filter( 'tourimo_filter_theme_plugins', 'tourimo_cf7_theme_plugins' );
		}
	}
}

// Filter to add in the required plugins list
if ( ! function_exists( 'tourimo_cf7_tgmpa_required_plugins' ) ) {
	
	function tourimo_cf7_tgmpa_required_plugins( $list = array() ) {
		if ( tourimo_storage_isset( 'required_plugins', 'contact-form-7' ) && tourimo_storage_get_array( 'required_plugins', 'contact-form-7', 'install' ) !== false ) {
			// CF7 plugin
			$list[] = array(
				'name'     => tourimo_storage_get_array( 'required_plugins', 'contact-form-7', 'title' ),
				'slug'     => 'contact-form-7',
				'required' => false,
			);
			// CF7 extension - datepicker
			if ( ! TOURIMO_THEME_FREE && tourimo_is_theme_activated() ) {
				$params = array(
					'name'     => esc_html__( 'Contact Form 7 Datepicker', 'tourimo' ),
					'slug'     => 'contact-form-7-datepicker',
					'version'  => '2.6.0',
					'required' => false,
				);
				$path   = tourimo_get_plugin_source_path( 'plugins/contact-form-7/contact-form-7-datepicker.zip' );
				if ( '' != $path ) {
					$params['source'] = $path;
				}
				$list[] = $params;
			}
		}
		return $list;
	}
}

// Filter theme-supported plugins list
if ( ! function_exists( 'tourimo_cf7_theme_plugins' ) ) {
	
	function tourimo_cf7_theme_plugins( $list = array() ) {
		if ( ! empty( $list['contact-form-7']['group'] ) ) {
			foreach ( $list as $k => $v ) {
				if ( substr( $k, 0, 15 ) == 'contact-form-7-' ) {
					if ( empty( $v['group'] ) ) {
						$list[ $k ]['group'] = $list['contact-form-7']['group'];
					}
					if ( empty( $v['logo'] ) ) {
						$logo = tourimo_get_file_url( "plugins/contact-form-7/{$k}.png" );
						$list[ $k ]['logo'] = empty( $logo )
												? ( ! empty( $list['contact-form-7']['logo'] )
													? ( strpos( $list['contact-form-7']['logo'], '//' ) !== false
														? $list['contact-form-7']['logo']
														: tourimo_get_file_url( "plugins/contact-form-7/{$list['contact-form-7']['logo']}" )
														)
													: ''
													)
												: $logo;
					}
				}
			}
		}
		return $list;
	}
}



// Check if cf7 installed and activated
if ( ! function_exists( 'tourimo_exists_cf7' ) ) {
	function tourimo_exists_cf7() {
		return class_exists( 'WPCF7' );
	}
}

// Enqueue custom scripts
if ( ! function_exists( 'tourimo_cf7_frontend_scripts' ) ) {
	
	function tourimo_cf7_frontend_scripts() {
		if ( tourimo_is_on( tourimo_get_theme_option( 'debug_mode' ) ) ) {
			$tourimo_url = tourimo_get_file_url( 'plugins/contact-form-7/contact-form-7.js' );
			if ( '' != $tourimo_url ) {
				wp_enqueue_script( 'tourimo-contact-form-7', $tourimo_url, array( 'jquery' ), null, true );
			}
		}
	}
}

// Merge custom scripts
if ( ! function_exists( 'tourimo_cf7_merge_scripts' ) ) {
	
	function tourimo_cf7_merge_scripts( $list ) {
		$list[] = 'plugins/contact-form-7/contact-form-7.js';
		return $list;
	}
}
