<?php

/**
 * functions.php
 *
 * @author    Denis Franchi
 * @package   Willer
 */

/* TABLE OF CONTENT
 1 - Setup Theme
 2 - Widget area
 3 - Include javascript files
 4 - Include css files
 5 - Add additional templates
 6 - Custom numer of category widget
 7 - Custom Excerpt
 8 - Willer Support Page
 9 - Google Font
11 - Url Admin Upload / Copyright
12 - Breadcrumbs
13 - Include Plugin
14 - Demo Import
15 - Function Body Open
16 - Admin settings page direct
*/

/*  1 Setup Theme
------------------------------------------------------------*/

if (!function_exists('willer_setup')) :
	function willer_setup()
	{

		// Add Language translation
		load_theme_textdomain('willer', get_template_directory() . '/languages');

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

		// Add title-tag
		add_theme_support('title-tag');

		// Support full image
		add_theme_support('align-wide');

		//Add Post Thumbnails on posts and pages.
		add_theme_support('post-thumbnails');

		// Add support for Responsive Video
		add_theme_support("responsive-embeds");
		// Support Block Style
		add_theme_support('wp-block-styles');

		// Support Custom Palette
		add_theme_support('editor-color-palette', array(
			array(
				'name'  => esc_attr__('willer', 'willer'),
				'slug'  => 'willer',
				'color' => '#f3ba57',
			),
			array(
				'name'  => esc_attr__('red', 'willer'),
				'slug'  => 'red',
				'color' => '#ff1100',
			),
			array(
				'name'  => esc_attr__('green', 'willer'),
				'slug'  => 'green',
				'color' => '#00ff0a',
			),
		));

		// create custom size images

		add_image_size('willer_big', 1600, 960, true);
		add_image_size('willer_single_nws', 1200, 960, true);
		add_image_size('willer_quad', 290, 250, true);
		add_image_size('willer_quad_nw', 800, 600, true);
		add_image_size('willer_single', 800, 500, true);
		add_image_size('willer_gallery', 330, 330, true);
		add_image_size('willer_brands', 520, 520, true);
		add_image_size('willer_team', 600, 800, true);
		add_image_size('willer_contact_parallax', 1800, 600, true);

		//Add Excerpt in Pages
		add_post_type_support('page', 'excerpt');

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(array(
			'primary' => esc_html__('Primary Menu', 'willer'),
		));

		/*
		 * 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',
		));

		// Set up the WordPress core custom background feature.
		add_theme_support('custom-background', apply_filters('willer_custom_background_args', array(
			'default-color' => '000000',
			'default-image' => '',
		)));

		// add support for Video
		add_theme_support('custom-header', array(
			'video' => true,
			'video-active-callback' => true,
		));

		function willer_header_video_settings($settings)
		{
			$settings['width'] = 1960;
			$settings['height'] = 960;
			return $settings;
		}
		add_filter('header_video_settings', 'willer_header_video_settings');

		// Add theme support for selective refresh for widgets.
		add_theme_support('customize-selective-refresh-widgets');

		//Add support for core custom logo.
		add_theme_support('custom-logo', array(
			'height'      => 250,
			'width'       => 250,
			'flex-width'  => true,
			'flex-height' => true,
		));
	}
endif;
add_action('after_setup_theme', 'willer_setup');

function willer_content_width()
{
	$GLOBALS['content_width'] = apply_filters('willer_content_width', 640);
}
add_action('after_setup_theme', 'willer_content_width', 0);


// Default Header Image
$willer_header_info = array(
	'width'         => 2600,
	'height'        => 750,
	'flex-width'    => true,
	'flex-height'   => true,
	'default-image' => get_template_directory_uri() . '/images/willer-default.jpg',
);
add_theme_support('custom-header', $willer_header_info);

$willer_header_images = array(
	'city' => array(
		'url'           => get_template_directory_uri() . '/images/willer-default.jpg',
		'thumbnail_url' => get_template_directory_uri() . '/images/willer-default.jpg',
		'description'   => __('Man', 'willer'),
	),
	'man' => array(
		'url'           => get_template_directory_uri() . '/images/willer-header-image.jpg',
		'thumbnail_url' => get_template_directory_uri() . '/images/willer-header-image.jpg',
		'description'   => __('City', 'willer'),
	),
);
register_default_headers($willer_header_images);

/*  2 Widget area
------------------------------------------------------------*/

// Widget area Sidebar default

function willer_widgets_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Sidebar', 'willer'),
		'id'            => 'sidebar-1',
		'description'   => esc_html__('Add blocks here.', 'willer'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'willer_widgets_init');

//  Widget area Footer 1

function willer_widgets_footer_1_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Blocks Footer 1', 'willer'),
		'id'            => 'footer-1',
		'description'   => esc_html__('Add blocks here.', 'willer'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'willer_widgets_footer_1_init');

//  Widget area Footer 2

function willer_widgets_footer_2_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Blocks Footer 2', 'willer'),
		'id'            => 'footer-2',
		'description'   => esc_html__('Add blocks here.', 'willer'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'willer_widgets_footer_2_init');

//  Widget area Footer 3

function willer_widgets_footer_3_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Blocks Footer 3', 'willer'),
		'id'            => 'footer-3',
		'description'   => esc_html__('Add blocks here.', 'willer'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'willer_widgets_footer_3_init');

//  Widget area Footer 4

function willer_widgets_footer_4_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Blocks Footer 4', 'willer'),
		'id'            => 'footer-4',
		'description'   => esc_html__('Add blocks here.', 'willer'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'willer_widgets_footer_4_init');

//  Widget BubiBlock

function willer_widgets_block_init()
{
	register_sidebar(array(
		'name'          => esc_html__('BubiBlock Slider', 'willer'),
		'id'            => 'bubiblock-widget',
		'description'   => esc_html__('Add block slider here.', 'willer'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'willer_widgets_block_init');


/*  3 Include javascript files
------------------------------------------------------------*/

function willer_scripts()
{

	// Willer script
	wp_enqueue_script('willer-custom-script', get_template_directory_uri() . '/js/willer-custom-script.js', array('jquery'), null, true);
	wp_enqueue_script('willer-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true);
	wp_enqueue_script('willer-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true);
	// Fixed Menu effect
	wp_enqueue_script('willer-float.menu', get_template_directory_uri() . '/js/float-panel.min.js', array(), '2016.10.28.', true);
	// Bootstrap
	wp_enqueue_script('willer-popper-js', get_template_directory_uri() . '/js/popper.min.js', array('jquery'), '1.14.3', true);
	wp_enqueue_script('willer-bootstrap-js', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '4.1.3', true);
	// AOS Animate
	wp_enqueue_script('willer-aos-js', get_template_directory_uri() . '/js/aos.min.js', array(), '2.3.1', false);
	// OWL Carousel
	wp_enqueue_script('willer-owl-carousel-js', get_template_directory_uri() . '/js/owl.carousel.min.js', array(), '2.3.4', true);
	// Add Script OWL Carousel Brands in Home Willer
	if (is_page_template(array('home-willer.php'))) {
		wp_enqueue_script('willer_scripts_carousel_brands-js', get_template_directory_uri() . '/js/willer-brands.js', array(), null, true);
	}
	// Add Comments
	if (is_singular() && comments_open() && get_option('thread_comments')) {
		wp_enqueue_script('comment-reply');
	}
}
add_action('wp_enqueue_scripts', 'willer_scripts');


/* 4 Include css files
-------------------------------------------------------- */

if (!function_exists('willer_styles')) {

	function willer_styles()
	{
		// Willer defoult style
		wp_enqueue_style('willer-style', get_stylesheet_uri());
		// Bootstrap
		wp_enqueue_style('willer-bootstrap-css', get_template_directory_uri() . '/css/bootstrap.min.css');
		// Font Awesome
		wp_enqueue_style('willer-font-awesome', get_template_directory_uri() . '/css/all.min.css');
		// AOS Animate
		wp_enqueue_style('willer-aos-css', get_template_directory_uri() . '/css/aos.min.css');
		// Animate CSS
		wp_enqueue_style('willer-animate-css', get_template_directory_uri() . '/css/animate.min.css');
		// OWL Carousel
		wp_enqueue_style('willer-owl-carousel-css', get_template_directory_uri() . '/css/owl.carousel.min.css');
	}
}

add_action('wp_enqueue_scripts', 'willer_styles');


/*  5 Add additional templates
-------------------------------------------------------- */

// Custom Header
require get_template_directory() . '/inc/custom-header.php';

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

// Functions which enhance the theme by hooking into WordPress
require get_template_directory() . '/inc/template-functions.php';

// Customizer additions
require get_template_directory() . '/inc/customizer.php';

// Custom Controll Css
require get_template_directory() . '/inc/custom-css.php';

// Load Jetpack compatibility file
if (defined('JETPACK__VERSION')) {
	require get_template_directory() . '/inc/jetpack.php';
}

// Class Custom Menu.
if (!file_exists(get_template_directory() . '/assets/class-wp-bootstrap-navwalker.php')) {
	// file does not exist... return an error.
	return new WP_Error('class-wp-bootstrap-navwalker-missing', esc_html__('It appears the class-wp-bootstrap-navwalker.php file may be missing.', 'willer'));
} else {
	// file exists... require it.
	require_once get_template_directory() . '/assets/class-wp-bootstrap-navwalker.php';
}

// Register Custom Comment Walker
if (!file_exists(get_template_directory() . '/assets/class-wp-bootstrap-comment-walker.php')) {
	// file does not exist... return an error.
	return new WP_Error('class-wp-bootstrap-comment-walker-missing', esc_html__('It appears the class-wp-bootstrap-comment-walker.php file may be missing.', 'willer'));
} else {
	// file exists... require it.
	require_once get_template_directory() . '/assets/class-wp-bootstrap-comment-walker.php';
}

/* 6 Custom numer of category widget
------------------------------------------------------------*/

function willer_add_span_cat_count($links)
{

	$links = str_replace('</a> (', '</a> <span class="willer-cat-count">(', $links);

	$links = str_replace(')', ')</span>', $links);
	return $links;
}
add_filter('wp_list_categories', 'willer_add_span_cat_count');


/* 7 Custom Excerpt
------------------------------------------------------------*/

function willer_excerpt_length($length)
{
	$willer_excpert = (esc_attr(get_theme_mod('willer_excerpt_lenght', 10)));
	return $willer_excpert;
}
add_filter('excerpt_length', 'willer_excerpt_length');


function willer_excerpt_more($more)
{
	return '...';
}
add_filter('excerpt_more', 'willer_excerpt_more');

/* 8 Willer Support Page
 ------------------------------------------------------------*/

add_action('admin_menu', 'willer_page_create');

function willer_page_create()
{
	add_theme_page('Willer', 'WILLER', 'edit_theme_options', 'willer_page', 'willer_page_display', 1);
}

function willer_page_display()
{
	require get_template_directory() . '/willer-admin/willer-support.php';
}

//Include Admin Style

function willer_load_admin_style($hook)
{
	if ($hook == 'appearance_page_willer_page') {
		wp_enqueue_style('willer_admin_css', get_template_directory_uri() . '/willer-admin/css/willer-admin-style.css', false, '1.0.0');
		wp_enqueue_script('willer_admin_script', get_template_directory_uri() . '/willer-admin/js/willer-admin-script.js', false, '1.0.0');
		wp_enqueue_style('willer-font-awesome-admin', get_template_directory_uri() . '/css/all.min.css');
	}
}
add_action('admin_enqueue_scripts', 'willer_load_admin_style');


/* 9 Google Font
-------------------------------------------------------- */

// Google Font

function willer_custom_add_google_fonts()
{
	wp_enqueue_style('willer-custom-google-fonts', 'https://fonts.googleapis.com/css?family=Aldrich:200,300,400,500,600,700,800,900|Carme:200,300,400,500,600,700,800,900|Covered+By+Your+Grace:200,300,400,500,600,700,800,900|Dancing+Script:200,300,400,500,600,700,800,900|Inconsolata:200,300,400,500,600,700,800,900|Indie+Flower:200,300,400,500,600,700,800,900|Lato:200,300,400,500,600,700,800,900|Montserrat:200,300,400,500,600,700,800,900|Nanum+Gothic+Coding:200,300,400,500,600,700,800,900|Rajdhani:200,300,400,500,600,700,800,900|Roboto:200,300,400,500,600,700,800,900|Sarala:200,300,400,500,600,700,800,900|Text+Me+One:200,300,400,500,600,700,800,900|Titillium+Web:200,300,400,700,900|Ubuntu:200,300,400,500,600,700,800,900|Amatic+SC|Courier+Prime|Kalam|Orbitron|Permanent+Marker&display=swap', false);
}
add_action('wp_enqueue_scripts', 'willer_custom_add_google_fonts');


/* 11 Url Admin Upload / Franchi Design
	========================================================================== */

if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly.
}

define('willer_url_promotion', 'https://denisfranchi.dev'); // Author site
define('franchi_design_url', 'https://denisfranchi.dev'); // Author site
define('willer_url_documentation_theme', 'https://denisfranchi.dev'); // Author site
define('willer_url_support_theme', 'https://wordpress.org/support/theme/willer/'); // Support Theme
define('willer_review_theme', 'https://wordpress.org/support/theme/willer/reviews/'); // Review Theme
define('willer_forum_theme', 'https://wordpress.org/support/theme/willer/'); // Support forum
define('willer_bubiblock_slider', 'https://wordpress.org/plugins/bubiblock-slider/'); // BubiBlock Slider




/* 12 Breadcrumbs
========================================================================== */

function willer_custom_breadcrumbs()
{

	$sep = ' > ';

	if (!is_front_page()) {

		// Start the breadcrumb with a link to your homepage
		echo '<div id="breadcrumbs" class="willer-breadcrumbs">';
		echo '<a href="';
		echo esc_url(home_url());
		echo '">';
		bloginfo('name');
		echo '</a>' . $sep;

		// Check if the current page is a category, an archive or a single page. If so show the category or archive name.
		if (is_category() || is_single()) {
			the_category($sep);
		} elseif (is_archive() || is_single()) {
			if (is_day()) {
				printf(('%s'), get_the_date());
			} elseif (is_month()) {
				printf(('%s'), get_the_date(_x('F Y', 'monthly archives date format', 'willer')));
			} elseif (is_year()) {
				printf(('%s'), get_the_date(_x('Y', 'yearly archives date format', 'willer')));
			} else {
				_e('Blog Archives', 'willer');
			}
		}

		// If the current page is a single post, show its title with the separator
		if (is_single()) {
			echo $sep;
			the_title();
		}

		// If the current page is a static page, show its title.
		if (is_page()) {
			echo the_title();
		}

		echo '</div>';
	}
}

/* 13 Include Plugin
-------------------------------------------------------- */


require_once get_template_directory() . '/wiler-functionality-plugin/class-tgm-plugin-activation.php';


add_action('tgmpa_register', 'willer_register_required_plugins');


function willer_register_required_plugins()
{

	$plugins = array(

		// On Click Demo Import
		array(
			'name'      => __('One Click Demo Import', 'willer'),
			'slug'      => 'one-click-demo-import',
			'required'  => false,
		),
		// BubiBlock Slider
		array(
			'name'      => 'BubiBlock Slider',
			'slug'      => 'bubiblock-slider',
			'required'  => false,
		),

	);

	$config = array(
		'id'           => 'willer',                 // Unique ID for hashing notices for multiple instances of TGMPA.
		'default_path' => '',                      // Default absolute path to bundled plugins.
		'menu'         => 'tgmpa-install-plugins', // Menu slug.
		'has_notices'  => true,                    // Show admin notices or not.
		'dismissable'  => true,                    // If false, a user cannot dismiss the nag message.
		'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
		'is_automatic' => false,                   // Automatically activate plugins after installation or not.
		'message'      => '',                      // Message to output right before the plugins table.

		'strings'      => array(
			'page_title'                      => __('Install Required Plugins', 'willer'),
			'menu_title'                      => __('Install Plugins', 'willer'),
			/* translators: %s: search term */
			'installing'                      => __('Installing Plugin: %s', 'willer'),
			/* translators: %s: search term */
			'updating'                        => __('Updating Plugin: %s', 'willer'),
			'oops'                            => __('Something went wrong with the plugin API.', 'willer'),
			/* translators: %1: search term */
			'notice_can_install_required'     => _n_noop(

				'This theme requires the following plugin: %1$s.',
				'This theme requires the following plugins: %1$s.',
				'willer'
			),
			/* translators: %1: search term */
			'notice_can_install_recommended'  => _n_noop(
				'This theme recommends the following plugin: %1$s.',
				'This theme recommends the following plugins: %1$s.',
				'willer'
			),
			/* translators: %1: search term */
			'notice_ask_to_update'            => _n_noop(
				'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
				'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
				'willer'
			),
			/* translators: %1: search term */
			'notice_ask_to_update_maybe'      => _n_noop(
				'There is an update available for: %1$s.',
				'There are updates available for the following plugins: %1$s.',
				'willer'
			),
			/* translators: %1: search term */
			'notice_can_activate_required'    => _n_noop(
				'The following required plugin is currently inactive: %1$s.',
				'The following required plugins are currently inactive: %1$s.',
				'willer'
			),
			/* translators: %1: search term */
			'notice_can_activate_recommended' => _n_noop(
				'The following recommended plugin is currently inactive: %1$s.',
				'The following recommended plugins are currently inactive: %1$s.',
				'willer'
			),
			'install_link'                    => _n_noop(
				'Begin installing plugin',
				'Begin installing plugins',
				'willer'
			),
			'update_link' 					  => _n_noop(
				'Begin updating plugin',
				'Begin updating plugins',
				'willer'
			),
			'activate_link'                   => _n_noop(
				'Begin activating plugin',
				'Begin activating plugins',
				'willer'
			),
			'return'                          => __('Return to Required Plugins Installer', 'willer'),
			'plugin_activated'                => __('Plugin activated successfully.', 'willer'),
			'activated_successfully'          => __('The following plugin was activated successfully:', 'willer'),
			/* translators: %1: search term */
			'plugin_already_active'           => __('No action taken. Plugin %1$s was already active.', 'willer'),
			/* translators: %s: search term */
			'plugin_needs_higher_version'     => __('Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'willer'),
			/* translators: %1: search term */
			'complete'                        => __('All plugins installed and activated successfully. %1$s', 'willer'),
			'dismiss'                         => __('Dismiss this notice', 'willer'),
			'notice_cannot_install_activate'  => __('There are one or more required or recommended plugins to install, update or activate.', 'willer'),
			'contact_admin'                   => __('Please contact the administrator of this site for help.', 'willer'),

			'nag_type'                        => '', // Determines admin notice type - can only be one of the typical WP notice classes, such as 'updated', 'update-nag', 'notice-warning', 'notice-info' or 'error'. Some of which may not work as expected in older WP versions.
		),
	);

	tgmpa($plugins, $config);
}

/* 14 Demo Import
========================================================================== */

// Menu and Page
function willer_after_import_setup()
{
	// Assign menus to their locations.
	$primary = get_term_by('name', 'Primary', 'nav_menu');

	set_theme_mod(
		'nav_menu_locations',
		array(
			'primary' => $primary->term_id,
		)
	);

	// Assign front page and posts page (blog page).
	$front_page_id = get_page_by_title('Home');
	$blog_page_id  = get_page_by_title('Blog');
}
add_action('pt-ocdi/after_import', 'willer_after_import_setup');



// Custom Title Plugin
function willer_plugin_page_setup($default_settings)
{
	$default_settings['parent_slug'] = 'themes.php';
	$default_settings['page_title']  = esc_html__('One Click Demo Import', 'willer');
	$default_settings['menu_title']  = esc_html__('WILLER Import Demo', 'willer');
	$default_settings['capability']  = 'import';
	$default_settings['menu_slug']   = 'one-click-demo-import';

	return $default_settings;
}
add_filter('pt-ocdi/plugin_page_setup', 'willer_plugin_page_setup');

// No Banner 
add_filter('pt-ocdi/disable_pt_branding', '__return_true');

/* 15 Function Body Open
========================================================================== */

if (!function_exists('wp_body_open')) {
	function wp_body_open()
	{
		do_action('wp_body_open');
	}
}

/* 16 Admin settings page direct
========================================================================== */

if (is_admin() && isset($_GET['activated']) && $pagenow == "themes.php")
	wp_redirect('themes.php?page=willer_page');
