<?php

/**
 * Theme functions and definitions.
 *
 * @link https://codex.wordpress.org/Functions_File_Explained
 *
 * @package Trade_Line
 */

if (! function_exists('trade_line_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 trade_line_setup()
	{
		/*
		 * Make theme available for translation.
		 */
		load_theme_textdomain('trade-line-pro', get_template_directory() . '/languages');

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

		/*
		 * Let WordPress manage the document title.
		 */
		add_theme_support('title-tag');

		/*
		 * Enable support for Post Thumbnails on posts and pages.
		 */
		add_theme_support('post-thumbnails');
		add_image_size('trade-line-thumb', 360, 270);

		// This theme uses wp_nav_menu() in four location.
		register_nav_menus(array(
			'primary'  => esc_html__('Primary Menu', 'trade-line-pro'),
			'footer'   => esc_html__('Footer Menu', 'trade-line-pro'),
			'social'   => esc_html__('Social Menu', 'trade-line-pro'),
			'notfound' => esc_html__('404 Menu', 'trade-line-pro'),
		));

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

		/*
		 * Enable support for 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('trade_line_custom_background_args', array(
			'default-color' => 'dfdfd0',
			'default-image' => get_template_directory_uri() . '/images/body-bg.png',
		)));

		/*
		 * Enable support for selective refresh of widgets in Customizer.
		 */
		add_theme_support('customize-selective-refresh-widgets');

		// Editor style.
		$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
		add_editor_style('css/editor-style' . $min . '.css');

		/*
		 * Enable support for custom logo.
		 */
		add_theme_support('custom-logo');

		// Load default block styles.
		add_theme_support('wp-block-styles');

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

		// Enable support for footer widgets.
		add_theme_support('footer-widgets', 4);

		// Add WooCommerce Support.
		add_theme_support('woocommerce');
		add_theme_support('wc-product-gallery-lightbox');

		// Load Supports.
		require_once get_template_directory() . '/inc/support.php';

		global $trade_line_default_options;
		$trade_line_default_options = trade_line_get_default_theme_options();
	}
endif;

add_action('after_setup_theme', 'trade_line_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 trade_line_content_width()
{
	$GLOBALS['content_width'] = apply_filters('trade_line_content_width', 640);
}
add_action('after_setup_theme', 'trade_line_content_width', 0);

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function trade_line_widgets_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Primary Sidebar', 'trade-line-pro'),
		'id'            => 'sidebar-1',
		'description'   => esc_html__('Add widgets here to appear in your Primary Sidebar.', 'trade-line-pro'),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'name'          => esc_html__('Secondary Sidebar', 'trade-line-pro'),
		'id'            => 'sidebar-2',
		'description'   => esc_html__('Add widgets here to appear in your Secondary Sidebar.', 'trade-line-pro'),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'name'          => esc_html__('Front Page Widget Area', 'trade-line-pro'),
		'id'            => 'sidebar-front-page-widget-area',
		'description'   => esc_html__('Add widgets here to appear in your Front Page.', 'trade-line-pro'),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title"><span>',
		'after_title'   => '</span></h2>',
	));
	register_sidebar(array(
		'name'          => sprintf(__('Extra Sidebar %d', 'trade-line-pro'), 1),
		'id'            => 'extra-sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'name'          => sprintf(__('Extra Sidebar %d', 'trade-line-pro'), 2),
		'id'            => 'extra-sidebar-2',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'name'          => sprintf(__('Extra Sidebar %d', 'trade-line-pro'), 3),
		'id'            => 'extra-sidebar-3',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'name'          => sprintf(__('Extra Sidebar %d', 'trade-line-pro'), 4),
		'id'            => 'extra-sidebar-4',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'trade_line_widgets_init');

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

	$theme_version = wp_get_theme()->get('Version');

	$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';

	wp_enqueue_style('font-awesome', get_template_directory_uri() . '/third-party/font-awesome/css/all' . $min . '.css', '', '6.7.2');

	$fonts_url = trade_line_fonts_url();
	if (! empty($fonts_url)) {
		wp_enqueue_style('trade-line-google-fonts', $fonts_url, array(), null);
	}

	if ('disabled' !== trade_line_get_option('featured_clients_status')) {
		wp_enqueue_style('jquery-slick', get_template_directory_uri() . '/third-party/slick/slick' . $min . '.css', '', '1.5.9');
	}

	wp_enqueue_style('jquery-sidr', get_template_directory_uri() . '/third-party/sidr/css/jquery.sidr.dark' . $min . '.css', '', '2.2.1');

	wp_enqueue_style('trade-line-style', get_stylesheet_uri(), array(), $theme_version);

	if (has_header_image()) {
		$custom_css = '#masthead{ background-image: url("' . esc_url(get_header_image()) . '"); background-repeat: no-repeat; background-position: center center; }';
		wp_add_inline_style('trade-line-style', $custom_css);
	}

	wp_enqueue_script('trade-line-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix' . $min . '.js', array(), '20130115', true);

	wp_enqueue_script('jquery-cycle2', get_template_directory_uri() . '/third-party/cycle2/js/jquery.cycle2' . $min . '.js', array('jquery'), '2.1.6', true);

	$transition_effect = trade_line_get_option('featured_slider_transition_effect');

	if (in_array($transition_effect, array('tileBlind', 'tileSlide'))) {
		wp_enqueue_script('jquery-cycle2-tile', get_template_directory_uri() . '/third-party/cycle2/js/jquery.cycle2.tile' . $min . '.js', array('jquery'), '2.1.6', true);
	}

	if (in_array($transition_effect, array('flipHorz', 'flipVert'))) {
		wp_enqueue_script('jquery-cycle2-flip', get_template_directory_uri() . '/third-party/cycle2/js/jquery.cycle2.flip' . $min . '.js', array('jquery'), '2.1.6', true);
	}

	if (in_array($transition_effect, array('shuffle'))) {
		wp_enqueue_script('jquery-cycle2-shuffle', get_template_directory_uri() . '/third-party/cycle2/js/jquery.cycle2.shuffle' . $min . '.js', array('jquery'), '2.1.6', true);
	}

	if ('disabled' !== trade_line_get_option('featured_clients_status')) {
		wp_enqueue_script('jquery-slick', get_template_directory_uri() . '/third-party/slick/slick' . $min . '.js', array('jquery'), '1.5.9', true);
	}

	wp_enqueue_script('jquery-sidr', get_template_directory_uri() . '/third-party/sidr/js/jquery.sidr' . $min . '.js', array('jquery'), '2.2.1', true);

	wp_enqueue_script('trade-line-custom', get_template_directory_uri() . '/js/custom' . $min . '.js', array('jquery'), '2.2.0', true);
	$custom_args = array(
		'featured_clients_status' => ('disabled' !== trade_line_get_option('featured_clients_status')) ? 1 : 0,
	);
	wp_localize_script('trade-line-custom', 'Trade_line_Custom_Options', $custom_args);

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

if (! function_exists('trade_line_blocks_support')) :
	/**
	 * Create add default blocks support
	 */
	function trade_line_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', 'trade-line-pro'),
					'shortName' => esc_html__('S', 'trade-line-pro'),
					'size'      => 14,
					'slug'      => 'small',
				),
				array(
					'name'      => esc_html__('Normal', 'trade-line-pro'),
					'shortName' => esc_html__('M', 'trade-line-pro'),
					'size'      => 18,
					'slug'      => 'normal',
				),
				array(
					'name'      => esc_html__('Large', 'trade-line-pro'),
					'shortName' => esc_html__('L', 'trade-line-pro'),
					'size'      => 42,
					'slug'      => 'large',
				),
				array(
					'name'      => esc_html__('Huge', 'trade-line-pro'),
					'shortName' => esc_html__('XL', 'trade-line-pro'),
					'size'      => 54,
					'slug'      => 'huge',
				),
			)
		);

		// Add support for custom color scheme.
		add_theme_support('editor-color-palette', array(
			array(
				'name'  => esc_html__('White', 'trade-line-pro'),
				'slug'  => 'white',
				'color' => '#ffffff',
			),
			array(
				'name'  => esc_html__('Black', 'trade-line-pro'),
				'slug'  => 'black',
				'color' => '#111111',
			),
			array(
				'name'  => esc_html__('Gray', 'trade-line-pro'),
				'slug'  => 'gray',
				'color' => '#f4f4f4',
			),
			array(
				'name'  => esc_html__('Blue', 'trade-line-pro'),
				'slug'  => 'blue',
				'color' => '#21759b',
			),
			array(
				'name'  => esc_html__('Green', 'trade-line-pro'),
				'slug'  => 'green',
				'color' => '#2ba48c',
			),
		));
	}
	add_action('after_setup_theme', 'trade_line_blocks_support', 20);
endif; //trade_line_blocks_support

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

if (! function_exists('trade_line_block_editor_styles')) :
	/**
	 * Enqueue editor styles for Blocks
	 */
	function trade_line_block_editor_styles()
	{
		// Block styles.
		wp_enqueue_style('trade-line-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('trade-line-fonts', trade_line_fonts_url(), array(), null);
	}
	add_action('enqueue_block_editor_assets', 'trade_line_block_editor_styles');
endif; //trade_line_block_editor_styles

/**
 * Load init.
 */
require_once get_template_directory() . '/inc/init.php';

/**
 * Load theme updater functions.
 */
function trade_line_theme_updater()
{
	if (is_admin()) {
		require_once get_template_directory() . '/updater/theme-updater.php';
	}
}
add_action('after_setup_theme', 'trade_line_theme_updater');


// Load Demo Import Class
if (class_exists('CatchThemesDemoImportPlugin')) :
	require(get_template_directory() . '/inc/support/ctdi/demo-import.php');
endif;
