<?php
/**
 * Functions and definitions
 *
 * @package Meridian One
 * @since 1.0
 */

/**
 * Table Of Contents
 *
 * meridian_one_setup ( Sets up theme defaults and registers support for various WordPress features )
 * meridian_one_content_width ( Set the content width global variable )
 * meridian_one_register_sidebars ( Register sidebars )
 * meridian_one_scripts ( Enqueue scripts and styles )
 * meridian_one_fonts_url ( Google Fonts URL generator )
 * Include other files
 */

define( 'MERIDIAN_ONE_CUSTOMIZER_PREPEND', 'meridian_one_' );
define( 'MERIDIAN_ONE_FEATURES_DISABLE_CSS', true );

if ( ! function_exists( 'meridian_one_setup' ) ) {

	/**
	 * Sets up theme defaults and registers support for various WordPress features
	 *
	 * @since 1.0
	 */
	function meridian_one_setup() {

		// translation
		load_theme_textdomain( 'meridian-one', get_parent_theme_file_path() . '/languages' );

		// theme support
		add_theme_support( 'custom-logo', array(
			'width' => 75,
			'height' => 27,
			'flex-width' => true,
			'flex-height' => true,
			'header-text' => array( 'site-title', 'site-description' ),
		));
		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'title-tag' );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) );
		add_theme_support( 'customize-selective-refresh-widgets' );

		// custom background support
		add_theme_support( 'custom-background', array(
			'default-color' => 'ffffff',
		));

		// editor style
		add_editor_style( array( 'css/editor-style.css', meridian_one_fonts_url() ) );

		// register menus
		register_nav_menus( array(
			'menu-1' => esc_html__( 'Header', 'meridian-one' ),
		) );

		// image sizes
		add_image_size( 'meridian-one-fourth', 420, 99999, false );
		add_image_size( 'meridian-one-posts-listing', 1170, 99999, false );

	}

} add_action( 'after_setup_theme', 'meridian_one_setup' );

if ( ! function_exists( 'meridian_one_content_width' ) ) {

	/**
	 * Set the content width global variable
	 *
	 * @since 1.0
	 * @global int $content_width
	 */
	function meridian_one_content_width() {

		$GLOBALS['content_width'] = apply_filters( 'meridian_one_content_width', 1170 );

	}

} add_action( 'after_setup_theme', 'meridian_one_content_width', 0 );

if ( ! function_exists( 'meridian_one_register_sidebars' ) ) {

	/**
	 * Register Sidebars
	 *
	 * @since 1.0
	 */
	function meridian_one_register_sidebars() {

		// sidebar
		register_sidebar( array(
			'name'          => esc_html__( 'Sidebar', 'meridian-one' ),
			'id'            => 'sidebar-1',
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// footer
		register_sidebar( array(
			'name'          => esc_html__( 'Footer', 'meridian-one' ),
			'id'            => 'sidebar-2',
			'before_widget' => '<div id="%1$s" class="widget col col-3 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// features
		register_sidebar( array(
			'name'          => esc_html__( 'Home Features', 'meridian-one' ),
			'id'            => 'sidebar-home-features',
			'before_widget' => '<div id="%1$s" class="widget col col-6 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// about
		register_sidebar( array(
			'name'          => esc_html__( 'Home About', 'meridian-one' ),
			'id'            => 'sidebar-home-about',
			'before_widget' => '<div id="%1$s" class="widget col col-4 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// services
		register_sidebar( array(
			'name'          => esc_html__( 'Home Services', 'meridian-one' ),
			'id'            => 'sidebar-home-services',
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// process
		register_sidebar( array(
			'name'          => esc_html__( 'Home Process', 'meridian-one' ),
			'id'            => 'sidebar-home-process',
			'before_widget' => '<div id="%1$s" class="widget col col-4 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// testimonials
		register_sidebar( array(
			'name'          => esc_html__( 'Home Testimonials', 'meridian-one' ),
			'id'            => 'sidebar-home-testimonials',
			'before_widget' => '<div id="%1$s" class="widget col col-4 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// clients
		register_sidebar( array(
			'name'          => esc_html__( 'Home Clients', 'meridian-one' ),
			'id'            => 'sidebar-home-clients',
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// team
		register_sidebar( array(
			'name'          => esc_html__( 'Home Team', 'meridian-one' ),
			'id'            => 'sidebar-home-team',
			'before_widget' => '<div id="%1$s" class="widget col col-3 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

		// contact
		register_sidebar( array(
			'name'          => esc_html__( 'Home Contact', 'meridian-one' ),
			'id'            => 'sidebar-home-contact',
			'before_widget' => '<div id="%1$s" class="widget col col-4 %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );

	}

} add_action( 'widgets_init', 'meridian_one_register_sidebars' );

if ( ! function_exists( 'meridian_one_scripts' ) ) {

	/**
	 * Enqueue scripts and styles
	 *
	 * @since 1.0
	 */
	function meridian_one_scripts() {

		// theme
		$theme = wp_get_theme();

		// styles
		wp_enqueue_style( 'meridian-one-style', get_stylesheet_uri(), array(), $theme->get( 'Version' ) );
		wp_enqueue_style( 'magnific-popup', get_parent_theme_file_uri() . '/css/plugin.magnific-popup.css', array(), $theme->get( 'Version' ) );
		wp_enqueue_style( 'font-awesome', get_parent_theme_file_uri() . '/css/fonts/font-awesome/font-awesome.css', array(), $theme->get( 'Version' ) );

		// scripts
		wp_enqueue_script( 'fitvids', get_parent_theme_file_uri() . '/js/plugin.fitvids.js', array( 'jquery' ), $theme->get( 'Version' ), true );
		wp_enqueue_script( 'magnific-popup', get_parent_theme_file_uri() . '/js/plugin.magnific-popup.js', array( 'jquery' ), $theme->get( 'Version' ), true );
		wp_enqueue_script( 'meridian-one-main-js', get_parent_theme_file_uri() . '/js/main.js', array( 'jquery', 'jquery-effects-core', 'wp-ajax-response' ), $theme->get( 'Version' ), true );

		// google fonts
		wp_enqueue_style( 'meridian-one-fonts', meridian_one_fonts_url(), array(), $theme->get( 'Version' ) );

		// comment reply
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}

	}

} add_action( 'wp_enqueue_scripts', 'meridian_one_scripts' );

if ( ! function_exists( 'meridian_one_fonts_url' ) ) {

	/**
	 * Returns the google fonts URL
	 *
	 * @since 1.0
	 */
	function meridian_one_fonts_url() {

		$fonts_url = '';

		/*
		Translators: If there are characters in your language that are not supported
		by Lora, translate this to 'off'. Do not translate into your own language.
		*/
		$lora = esc_html_x( 'on', 'Lora font: on or off', 'meridian-one' );

		/*
		Translators: If there are characters in your language that are not supported
		by Lato, translate this to 'off'. Do not translate into your own language.
		*/
		$lato = esc_html_x( 'on', 'Lato font: on or off', 'meridian-one' );

		/*
		Translators: If there are characters in your language that are not supported
		by Arbutus Slab, translate this to 'off'. Do not translate into your own language.
		*/
		$montserrat = esc_html_x( 'on', 'Montserrat font: on or off', 'meridian-one' );

		if ( 'off' !== $lora || 'off' !== $lato || 'off' !== $montserrat ) {

			$font_families = array();

			if ( 'off' !== $lora ) {
				$font_families[] = 'Lora:400,700';
			}

			if ( 'off' !== $lato ) {
				$font_families[] = 'Lato:400,100,300,700,900';
			}

			if ( 'off' !== $montserrat ) {
				$font_families[] = 'Montserrat:400,500,100,300,700,900';
			}

			$query_args = array(
				'family' => urlencode( implode( '|', $font_families ) ),
				'subset' => urlencode( 'latin,latin-ext' ),
			);

			$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );

		}

		return esc_url_raw( $fonts_url );

	}

}

// functions
include get_parent_theme_file_path() . '/inc/tgm/tgm-init.php';
include get_parent_theme_file_path() . '/inc/importer/init.php';
include get_parent_theme_file_path() . '/inc/logic.php';
include get_parent_theme_file_path() . '/inc/display.php';
include get_parent_theme_file_path() . '/inc/theme-options.php';
include get_parent_theme_file_path() . '/inc/woocommerce.php';
include get_parent_theme_file_path() . '/inc/welcome.php';
include get_parent_theme_file_path() . '/inc/customizer-upsell/init.php';