<?php
/* Booked Appointments support functions
------------------------------------------------------------------------------- */

// Theme init priorities:
// 9 - register other filters (for installer, etc.)
if ( ! function_exists( 'tourimo_booked_theme_setup9' ) ) {
	add_action( 'after_setup_theme', 'tourimo_booked_theme_setup9', 9 );
	function tourimo_booked_theme_setup9() {
		if ( tourimo_exists_booked() ) {
			add_action( 'wp_enqueue_scripts', 'tourimo_booked_frontend_scripts', 1100 );
			add_filter( 'tourimo_filter_merge_styles', 'tourimo_booked_merge_styles' );
		}
		if ( is_admin() ) {
			add_filter( 'tourimo_filter_tgmpa_required_plugins', 'tourimo_booked_tgmpa_required_plugins' );
			add_filter( 'tourimo_filter_theme_plugins', 'tourimo_booked_theme_plugins' );
		}
	}
}

// Filter to add in the required plugins list
if ( ! function_exists( 'tourimo_booked_tgmpa_required_plugins' ) ) {
	
	function tourimo_booked_tgmpa_required_plugins( $list = array() ) {
		if ( tourimo_storage_isset( 'required_plugins', 'booked' ) && tourimo_storage_get_array( 'required_plugins', 'booked', 'install' ) !== false && tourimo_is_theme_activated() ) {
			$path = tourimo_get_plugin_source_path( 'plugins/booked/booked.zip' );
			if ( ! empty( $path ) || tourimo_get_theme_setting( 'tgmpa_upload' ) ) {
				$list[] = array(
					'name'     => tourimo_storage_get_array( 'required_plugins', 'booked', 'title' ),
					'slug'     => 'booked',
					'source'   => ! empty( $path ) ? $path : 'upload://booked.zip',
					'version'  => '2.1',
					'required' => false,
				);
			}
			$path = tourimo_get_plugin_source_path( 'plugins/booked/booked-calendar-feeds.zip' );
			if ( ! empty( $path ) || tourimo_get_theme_setting( 'tgmpa_upload' ) ) {
				$list[] = array(
					'name'     => esc_html__( 'Booked Calendar Feeds', 'tourimo' ),
					'slug'     => 'booked-calendar-feeds',
					'source'   => ! empty( $path ) ? $path : 'upload://booked-calendar-feeds.zip',
					'version'  => '1.1.5',
					'required' => false,
				);
			}
			$path = tourimo_get_plugin_source_path( 'plugins/booked/booked-frontend-agents.zip' );
			if ( ! empty( $path ) || tourimo_get_theme_setting( 'tgmpa_upload' ) ) {
				$list[] = array(
					'name'     => esc_html__( 'Booked Front-End Agents', 'tourimo' ),
					'slug'     => 'booked-frontend-agents',
					'source'   => ! empty( $path ) ? $path : 'upload://booked-frontend-agents.zip',
					'version'  => '1.1.15',
					'required' => false,
				);
			}
			if ( tourimo_storage_isset( 'required_plugins', 'woocommerce' ) ) {
				$path = tourimo_get_plugin_source_path( 'plugins/booked/booked-woocommerce-payments.zip' );
				if ( ! empty( $path ) || tourimo_get_theme_setting( 'tgmpa_upload' ) ) {
					$list[] = array(
						'name'     => esc_html__( 'Booked Payments with WooCommerce', 'tourimo' ),
						'slug'     => 'booked-woocommerce-payments',
						'source'   => ! empty( $path ) ? $path : 'upload://booked-woocommerce-payments.zip',
						'version'  => '1.4.9',
						'required' => false,
					);
				}
			}
		}
		return $list;
	}
}

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



// Check if plugin installed and activated
if ( ! function_exists( 'tourimo_exists_booked' ) ) {
	function tourimo_exists_booked() {
		return class_exists( 'booked_plugin' );
	}
}


// Enqueue styles for frontend
if ( ! function_exists( 'tourimo_booked_frontend_scripts' ) ) {
	
	function tourimo_booked_frontend_scripts() {
		if ( tourimo_is_on( tourimo_get_theme_option( 'debug_mode' ) ) ) {
			$tourimo_url = tourimo_get_file_url( 'plugins/booked/booked.css' );
			if ( '' != $tourimo_url ) {
				wp_enqueue_style( 'tourimo-booked', $tourimo_url, array(), null );
			}
		}
	}
}


// Merge custom styles
if ( ! function_exists( 'tourimo_booked_merge_styles' ) ) {
	
	function tourimo_booked_merge_styles( $list ) {
		$list[] = 'plugins/booked/booked.css';
		return $list;
	}
}


// Add plugin-specific colors and fonts to the custom CSS
if ( tourimo_exists_booked() ) {
	require_once tourimo_get_file_dir( 'plugins/booked/booked-style.php' );
}
