<?php

/**
 * IZI THEME functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage IZI_THEME
 * @since 1.0.0
 */

/*********************************************************************************************
 * FEATURES FOR SECURITY
 *********************************************************************************************/
defined('ABSPATH') || exit;

/*********************************************************************************************
 * SETS UP THEME DEFAULTS AND REGISTERS SUPPORT FOR VARIOUS WORDPRESS FEATURES.
 *********************************************************************************************/
if (!function_exists('izi_theme_support')) :

	function izi_theme_support()
	{

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

		/*
		* Add custom image size used in Cover Template.
		* 
		* @link https://developer.wordpress.org/reference/functions/set_post_thumbnail_size/
		* add_image_size('main-full', 1078, 516, false);
		*/
		add_image_size('cat-thumb', 648, 364, array('center', 'center')); // 648 pixels wide and 364px height crop by the center
		// add_image_size('single-thumb', 825, '', array('center', 'center')); // 634 pixels wide (and unlimited height) crop by the center

		/*
		* 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
		* provide it for us.
		*/
		add_theme_support('title-tag');

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

		/*
		* Make theme available for translation.
		* Translations can be filed in the /languages/ directory.
		*/
		//load_theme_textdomain( 'izitheme' );

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

		// Add support for Block Styles.
		add_theme_support('wp-block-styles');

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

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

		// This theme uses wp_nav_menu() in two locations.
		register_nav_menus(
			array(
				'menu-1' => __('Primary', 'izitheme'),
				'footer' => __('Footer Menu', 'izitheme'),
				'prefooter1' => __('Pre Footer Menu 1', 'izitheme'),
			)
		);
	}
endif;
add_action('after_setup_theme', 'izi_theme_support');

/*********************************************************************************************
 * INCLUDE REQUIRED FILES.
 *********************************************************************************************/

/**
 * Register and Enqueue Styles on Front
 */
function izi_register_styles()
{
	// Bootstrap CSS
	wp_register_style('bootstrap-css', get_template_directory_uri() . '/assets/css/bootstrap.min.css', false, '5.2');
	wp_enqueue_style('bootstrap-css');

	wp_enqueue_style('my-fontawesome', get_template_directory_uri() . '/assets/css/all.min.css', false, '2', 'all');

	// Libraries CSS
	wp_enqueue_style('izi-style', get_stylesheet_uri(), array(), '1');
}
add_action('wp_enqueue_scripts', 'izi_register_styles');

/**
 * Register and Enqueue Scripts on Front
 */
function izi_register_scripts()
{
	wp_enqueue_script('bootstrap-js', get_template_directory_uri() . '/assets/js/bootstrap.bundle.min.js', '', '1.0.0', true);
	wp_enqueue_script('app-js', get_template_directory_uri() . '/assets/js/app.js', '', '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'izi_register_scripts');


/*********************************************************************************************
 * SIDEBAR
 *********************************************************************************************/
/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function izi_widgets_init()
{
	register_sidebar(
		array(
			'name'          => __('Lateral', 'izitheme'),
			'id'            => 'sidebar-1',
			'description'   => __('Ajouter des widgets aux pages articles', 'izitheme'),
			'before_widget' => '<div id="%1$s" class="widget-sidebar widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h5 class="sidebar-title">',
			'after_title'   => '</h5>',
		)
	);
}
add_action('widgets_init', 'izi_widgets_init');

/*********************************************************************************************
 * CUSTOM SETTINGS
 *********************************************************************************************/

/**
 * Remove the Admin Toolbar on Frontend
 */
//add_filter('show_admin_bar', '__return_false');

/**
 * Disable emoji's
 */
function disable_emojis()
{
	remove_action('wp_head', 'print_emoji_detection_script', 7);
	remove_action('admin_print_scripts', 'print_emoji_detection_script');
	remove_action('wp_print_styles', 'print_emoji_styles');
	remove_action('admin_print_styles', 'print_emoji_styles');
	remove_filter('the_content_feed', 'wp_staticize_emoji');
	remove_filter('comment_text_rss', 'wp_staticize_emoji');
	remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
}
add_action('init', 'disable_emojis');

/**
 * Disable error login message
 */
function disable_login_errors()
{
	return 'L\'un de vos champs est erroné !';
}
add_filter('login_errors', 'disable_login_errors');

/**
 * Change login logo link & message
 */
function my_login_logo_url()
{
	return home_url();
}
add_filter('login_headerurl', 'my_login_logo_url');

function my_login_logo_url_title()
{
	return 'All for console gamers';
}
add_filter('login_headertitle', 'my_login_logo_url_title');

/**
 * Disable Gutenberg for spécific pages
 */
function disable_gutenberg_spe($is_enabled) {
 	global $post;
 	if ($post->ID == 15) return false; // disable on this post ID

 	return $is_enabled;
}
add_filter('use_block_editor_for_post_type', 'disable_gutenberg_spe', 10, 2);


/*
* Custom H1 hide in TinyMCE
*/
function noh1_tinymce($init)
{
	$init['block_formats'] = 'Paragraph=p;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6;Preformatted=pre';
	return $init;
}
add_filter('tiny_mce_before_init', 'noh1_tinymce');

/**
 * Custom excerpt lenght
 */
function custom_excerpt_length($length)
{
	if (is_home()) {
		return 18;
	} else {
		return 55;
	}
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);

/**
 * Custom uppercase title
 */
function uppercase_title($title)
{
	if (get_post_type() == 'produits' && is_home()) {
		$title = strtoupper($title);
	}
	return $title;
}
add_filter('the_title', 'uppercase_title', 10, 1);

/*********************************************************************************************
 * PAGINATION SETTINGS
 *********************************************************************************************/
// Fonction permettant d'appeler la pagination numérique avec une WP_query pour un CPT
function pagination_bar( $query_wp ) 
{
    global $wp_query;
	
	$pages = $query_wp->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if ($pages > 1)
    {
        $page_current = max(1, get_query_var('paged'));
        echo paginate_links(array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
			'current' => $page_current,
            'total' => $pages,
			'prev_next' => true,
        ));
    }
}

/*********************************************************************************************
 * REGISTER CUSTOM NAVIGATION WALKER
 *********************************************************************************************/
/**
 * @link https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker
 */
function register_navwalker()
{
	require_once get_template_directory() . '/classes/class-wp-bootstrap-navwalker.php';
}
add_action('after_setup_theme', 'register_navwalker');


/*********************************************************************************************
 * ADD LOGIN/LOGOUT LINK TO AN EXISTING MENU
 *********************************************************************************************/
function login_logout_link_in_menu($items, $args)
{
	if ($args->theme_location == 'footer' && !is_user_logged_in()) {
		$loginoutlink = wp_loginout('wp-admin', false);
		$items .= '<li class="menu-item">' . $loginoutlink . '</li>';
		return $items;
	} else if ($args->theme_location == 'footer' && is_user_logged_in()) {
		$loginoutlink = wp_loginout('index.php', false);
		$adminlink = '<a href="' . admin_url() . '">admin</a>';
		$items .= '<li class="menu-item">' . $adminlink . '</li>';
		$items .= '<li class="menu-item">' . $loginoutlink . '</li>';
		return $items;
	}
	return $items;
}
add_filter('wp_nav_menu_items', 'login_logout_link_in_menu', 10, 2);

/*********************************************************************************************
 * OPTIMIZATION LOAD CONTACT FORM 7 
 *********************************************************************************************/
function remove_cf7_css()
{
	if (function_exists('wpcf7_enqueue_styles')) {
		if (!is_page(array('contact'))) {
			wp_deregister_style('contact-form-7');
			wp_deregister_style('contact-form-7-style');
		}
	}
}
add_action('wp_print_styles', 'remove_cf7_css', 100);

function remove_cf7_js()
{
	if (function_exists('wpcf7_enqueue_scripts')) {
		if (!is_page(array('contact'))) {
			wp_deregister_script('contact-form-7');
		}
	}
}
add_action('wp_print_scripts', 'remove_cf7_js', 100);

/*********************************************************************************************
 * OPTIMIZATION GUTENGERG BLOCKS CSS - LOAD ONLY COMMON.CSS
 *********************************************************************************************/
add_filter('should_load_separate_core_block_assets', '__return_true');

/*********************************************************************************************
 * CONTACT FORM 7
 *********************************************************************************************/
// Disable <p> add between each input in form ContactForm 7
add_filter('wpcf7_autop_or_not', '__return_false');

/*********************************************************************************************
 * INCLUDE CUSTOM POST TYPE
 *********************************************************************************************/
require get_template_directory() . '/inc/cpt-series.php';

/*********************************************************************************************
 * INCLUDE YOUTUBE METABOX
 *********************************************************************************************/
require get_template_directory() . '/inc/cf-url.php';

/*********************************************************************************************
 * CUSTOM LOGIN PAGE
 *********************************************************************************************/
function my_custom_login()
{
	echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('stylesheet_directory') . '/assets/css/login.css" />';
}
add_action('login_head', 'my_custom_login');
