<?php

/**
 * Otzi functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Otzi Lite
 */

if (! function_exists('otzi_lite_setup')) :
	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme hook, which
	 * runs before the init hook. The init hook is too late for some features, such
	 * as indicating support for post thumbnails.
	 */
	function otzi_lite_setup()
	{
		/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Otzi, use a find and replace
	 * to change 'otzi-lite' to the name of your theme in all the template files.
	 */
		load_theme_textdomain('otzi-lite', get_template_directory() . '/languages');

		// Add default posts and comments RSS feed links to head.
		add_theme_support('automatic-feed-links');

		// Add tyles the visual editor to resemble the theme style.
		add_editor_style(array('css/editor-style.css', otzi_lite_fonts_url()));

		/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * litevide it for us.
	 */
		add_theme_support('title-tag');

		/**
		 * Setup Custom Logo Support for theme
		 * Supported from WordPress version 4.5 onwards
		 * More Info: https://make.wordpress.org/core/2016/03/10/custom-logo/
		 */
		add_theme_support('custom-logo', array(
			'height'      => 135,
			'width'       => 300,
			'flex-height' => true,
			'flex-width' => true,
			'header-text' => array('site-title', 'site-description'),
		));

		/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
	 */
		add_theme_support('post-thumbnails');

		// Archive Post Thumbnail 3:2
		set_post_thumbnail_size(300, 200, true);

		// Single post - Content Width, Slider Content Width Layout, Ration 3:2
		add_image_size('otzi-featured-image', 807, 600, true);

		// Slider Full Width Layout, Ratio 21:9
		add_image_size('otzi-slider', 1680, 720, true);

		// Featured Image Full Width, Ratio 21:9
		add_image_size('otzi-featured-image-full', 1153, 494, true);

		// Featured Content, Ration 3:2
		add_image_size('otzi-featured-content', 559, 373, true);

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(array(
			'primary' => esc_html__('Primary Menu', 'otzi-lite'),
			'social'  => esc_html__('Social Menu', 'otzi-lite'),
		));

		/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
		add_theme_support('html5', array(
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		));

		/*
	 * Enable support for Post Formats.
	 * See https://developer.wordpress.org/themes/functionality/post-formats/
	 */
		add_theme_support('post-formats', array(
			'aside',
			'image',
			'video',
			'quote',
			'link',
		));

		// Set up the WordPress core custom background feature.
		add_theme_support('custom-background', apply_filters('otzi_lite_custom_background_args', array(
			'default-color' => 'ffffff',
			'default-image' => '',
		)));
	}
endif; // otzi_lite_setup
add_action('after_setup_theme', 'otzi_lite_setup');

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function otzi_lite_content_width()
{
	$GLOBALS['content_width'] = apply_filters('otzi_lite_content_width', 727);
}
add_action('after_setup_theme', 'otzi_lite_content_width', 0);

/**
 * Enqueue scripts and styles.
 */
function otzi_lite_scripts()
{

	wp_enqueue_style('otzi-lite-style', get_stylesheet_uri(), array(), '1.0.0');

	wp_enqueue_style('otzi-lite-fonts', otzi_lite_fonts_url(), array(), '1.0.0');

	// To avoid double loading, we don't prefix custom styles or scripts.

	// Font Awesome
	wp_enqueue_style('font-awesome', trailingslashit(esc_url(get_template_directory_uri())) . 'css/font-awesome/css/all.min.css', array(), '6.7.2', 'all');


	wp_enqueue_script('otzi-lite-navigation', trailingslashit(esc_url(get_template_directory_uri())) . 'js/navigation.js', array('jquery'), '20170624', true);

	wp_localize_script('otzi-lite-navigation', 'screenReaderText', array(
		'expand'   => __('expand child menu', 'otzi-lite'),
		'collapse' => __('collapse child menu', 'otzi-lite'),
	));

	wp_enqueue_script('otzi-lite-helpers', trailingslashit(esc_url(get_template_directory_uri())) . 'js/helpers.js', array('jquery'), '1.0.0', true);

	wp_enqueue_script('otzi-lite-skip-link-focus-fix', trailingslashit(esc_url(get_template_directory_uri())) . 'js/skip-link-focus-fix.js', array(), '1.0.0', true);

	// We localize only few lines from helpers.js
	wp_localize_script('otzi-lite-helpers', 'placeholder', array(
		'author'   => esc_html__('Name', 'otzi-lite'),
		'email'    => esc_html__('Email', 'otzi-lite'),
		'url'      => esc_html__('URL', 'otzi-lite'),
		'comment'  => esc_html__('Comment', 'otzi-lite')
	));

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

	/**
	 * Loads up Scroll Up script
	 */
	$disable_scrollup = otzi_lite_get_option('disable_scrollup');

	if (!$disable_scrollup) {
		wp_enqueue_script('otzi-lite-scrollup', trailingslashit(esc_url(get_template_directory_uri())) . 'js/scrollup.js', array('jquery'), '20160109	', true);
	}

	/**
	 * Loads up Cycle JS
	 */
	$featured_slider_option  = otzi_lite_get_option('featured_slider_option');
	$featured_content_slider = otzi_lite_get_option('featured_content_slider');
	$transition_effect       = otzi_lite_get_option('featured_slider_transition_effect');

	/**
	 * Loads up Cycle JS
	 */
	if ('disabled' != $featured_slider_option || $featured_content_slider) {
		wp_enqueue_script('jquery-cycle2', trailingslashit(esc_url(get_template_directory_uri())) . 'js/jquery.cycle/jquery.cycle2.min.js', array('jquery'), '2.1.5', true);

		/**
		 * Condition checks for additional slider transition plugins
		 */
		// Scroll Vertical transition plugin addition.
		if ('scrollVert' == $transition_effect) {
			wp_enqueue_script('jquery-cycle2-scrollVert', trailingslashit(esc_url(get_template_directory_uri())) . 'js/jquery.cycle/jquery.cycle2.scrollVert.min.js', array('jquery-cycle2'), '20140128', true);
		} elseif ('flipHorz' == $transition_effect || 'flipVert' == $transition_effect) {
			// Flip transition plugin addition.
			wp_enqueue_script('jquery-cycle2-flip', trailingslashit(esc_url(get_template_directory_uri())) . 'js/jquery.cycle/jquery.cycle2.flip.min.js', array('jquery-cycle2'), '20140128', true);
		} elseif ('tileSlide' == $transition_effect || 'tileBlind' == $transition_effect) {
			// Shuffle transition plugin addition.
			wp_enqueue_script('jquery-cycle2-tile', trailingslashit(esc_url(get_template_directory_uri())) . 'js/jquery.cycle/jquery.cycle2.tile.min.js', array('jquery-cycle2'), '20140128', true);
		}
	}
}
add_action('wp_enqueue_scripts', 'otzi_lite_scripts');

/**
 * Register Google fonts.
 *
 */
function otzi_lite_fonts_url()
{
	$fonts_url = '';

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

	/* Translators: If there are characters in your language that are not
	* supported by Playfair Display, translate this to 'off'. Do not translate
	* into your own language.
	*/
	$playfair_display = _x('on', 'Playfair Display font: on or off', 'otzi-lite');

	/* Translators: If there are characters in your language that are not
	* supported by Montserrat, translate this to 'off'. Do not translate
	* into your own language.
	*/
	$rubik = _x('on', 'Rubik font: on or off', 'otzi-lite');

	if ('off' !== $montserrat || 'off' !== $playfair_display || 'off' !== $rubik) {
		$font_families = array();

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

		if ('off' !== $playfair_display) {
			$font_families[] = 'Playfair Display:300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,900,900italic';
		}

		if ('off' !== $rubik) {
			$font_families[] = 'Rubik:300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,900,900italic';
		}

		$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');
	}
	// Load google font locally.
	require_once get_theme_file_path('inc/wptt-webfont-loader.php');

	return esc_url_raw(wptt_get_webfont_url($fonts_url));
}


/**
 * Main navigation description.
 *
 * @author Bill Erickson
 *
 * @link http://www.billerickson.net/code/add-description-to-wordpress-menu-items/
 *
 */
function otzi_lite_main_nav_description($item_output, $item, $depth, $args)
{

	if ('primary' == $args->theme_location && ! $depth && $item->description)
		$item_output = str_replace('</a>', '<span class="menu-description">' . $item->description . '</span></a>', $item_output);

	return $item_output;
}
add_filter('walker_nav_menu_start_el', 'otzi_lite_main_nav_description', 10, 4);

if (! function_exists('otzi_lite_blocks_support')) :
	/**
	 * Create add default blocks support
	 */
	function otzi_lite_blocks_support()
	{
		// Add support for Block Styles.
		add_theme_support('wp-block-styles');

		// Add support for full and wide align images.
		add_theme_support('align-wide');

		// Add support for editor styles.
		add_theme_support('editor-styles');

		// Add support for responsive embeds.
		add_theme_support('responsive-embeds');

		// Add custom editor font sizes.
		add_theme_support(
			'editor-font-sizes',
			array(
				array(
					'name'      => esc_html__('Small', 'otzi-lite'),
					'shortName' => esc_html__('S', 'otzi-lite'),
					'size'      => 14,
					'slug'      => 'small',
				),
				array(
					'name'      => esc_html__('Normal', 'otzi-lite'),
					'shortName' => esc_html__('M', 'otzi-lite'),
					'size'      => 18,
					'slug'      => 'normal',
				),
				array(
					'name'      => esc_html__('Large', 'otzi-lite'),
					'shortName' => esc_html__('L', 'otzi-lite'),
					'size'      => 42,
					'slug'      => 'large',
				),
				array(
					'name'      => esc_html__('Huge', 'otzi-lite'),
					'shortName' => esc_html__('XL', 'otzi-lite'),
					'size'      => 54,
					'slug'      => 'huge',
				),
			)
		);

		// Add support for custom color scheme.
		add_theme_support('editor-color-palette', array(
			array(
				'name'  => esc_html__('White', 'otzi-lite'),
				'slug'  => 'white',
				'color' => '#ffffff',
			),
			array(
				'name'  => esc_html__('Black', 'otzi-lite'),
				'slug'  => 'black',
				'color' => '#111111',
			),
			array(
				'name'  => esc_html__('Light Black', 'otzi-lite'),
				'slug'  => 'light-black',
				'color' => '#242424',
			),
			array(
				'name'  => esc_html__('Gray', 'otzi-lite'),
				'slug'  => 'gray',
				'color' => '#86828e',
			),
			array(
				'name'  => esc_html__('Blue', 'otzi-lite'),
				'slug'  => 'blue',
				'color' => '#21759b',
			),
			array(
				'name'  => esc_html__('Orange', 'otzi-lite'),
				'slug'  => 'orange',
				'color' => '#ffc300',
			),
		));
	}
	add_action('after_setup_theme', 'otzi_lite_blocks_support', 20);
endif; //otzi_blocks_support

if (! function_exists('otzi_lite_add_blocks_style')) :
	/**
	 * Add Blocks Style
	 */
	function otzi_lite_add_blocks_style()
	{
		// Theme block stylesheet.
		wp_enqueue_style('otzi-lite-block-style', get_theme_file_uri('/css/blocks.css'), array('otzi-lite-style'), date('Ymd-Gis', filemtime(get_template_directory() . '/css/blocks.css')));
	}
	add_action('wp_enqueue_scripts', 'otzi_lite_add_blocks_style');
endif; //otzi_add_blocks_style

if (! function_exists('otzi_lite_block_editor_styles')) :
	/**
	 * Enqueue editor styles for Blocks
	 */
	function otzi_lite_block_editor_styles()
	{
		// Block styles.
		wp_enqueue_style('otzi-lite-block-editor-style', get_theme_file_uri('/css/editor-blocks.css'), null, date('Ymd-Gis', filemtime(get_template_directory() . '/css/editor-blocks.css')));

		// Add custom fonts.
		wp_enqueue_style('otzi-lite-fonts', otzi_lite_fonts_url(), array(), null);
	}
	add_action('enqueue_block_editor_assets', 'otzi_lite_block_editor_styles');
endif; //otzi_block_editor_styles

/**
 * Include Default Options for Otzi
 */
require trailingslashit(get_template_directory()) . 'inc/default-options.php';

/**
 * Include Main Structure file
 */
require trailingslashit(get_template_directory()) . 'inc/structure.php';

/**
 * Custom template tags for this theme.
 */
require trailingslashit(get_template_directory()) . 'inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require trailingslashit(get_template_directory()) . 'inc/extras.php';

/**
 * Include breadcrumb
 */
require trailingslashit(get_template_directory()) . 'inc/breadcrumb.php';

/**
 * Customizer additions.
 */
require trailingslashit(get_template_directory()) . 'inc/customizer-includes/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require trailingslashit(get_template_directory()) . 'inc/jetpack.php';

/**
 * Include slider
 */
require trailingslashit(get_template_directory()) . 'inc/featured-slider.php';

/**
 * Include Featured Content
 */
require trailingslashit(get_template_directory()) . 'inc/featured-content.php';

/**
 * Include Widgets
 */
require trailingslashit(get_template_directory()) . 'inc/widgets/widgets.php';
