<?php
/**
 * Vite Nera functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Vite_Nera
 */

if (!defined('VITE_NERA_VERSION')) {
    // Replace the version number of the theme on each release.
    define('VITE_NERA_VERSION', vite_nera_get_version());
}

if (!function_exists('vite_nera_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 vite_nera_setup()
    {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on Vite Nera, use a find and replace
         * to change 'vite-nera' to the name of your theme in all the template files.
         */
        load_theme_textdomain('vite-nera', 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.
         * 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');

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

        set_post_thumbnail_size(770);

        add_image_size('vite-nera-small', 330);
        add_image_size('vite-nera-large', 1170);
        add_image_size('vite-nera-x-large', 1920);

        // This theme uses wp_nav_menu() in one location.
        register_nav_menus(
            array(
                'menu-1' => esc_html__('Primary', 'vite-nera'),
                'menu-2' => esc_html__('Footer', 'vite-nera'),
                'menu-3' => esc_html__('Header Left', 'vite-nera'),
                'menu-4' => esc_html__('Header Right', 'vite-nera'),
            )
        );

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

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

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

        /**
         * Add support for core custom logo.
         *
         * @link https://codex.wordpress.org/Theme_Logo
         */
        add_theme_support(
            'custom-logo',
            array(
                'height' => 60,
                'width' => 60,
                'flex-width' => true,
                'flex-height' => true,
            )
        );

        add_theme_support('responsive-embeds');
        add_theme_support('align-wide');
        add_theme_support('editor-styles');
        add_editor_style('editor-style.css');

        add_theme_support('editor-color-palette', array(
            array(
                'name' => esc_html__('Color 1', 'vite-nera'),
                'slug' => 'color-1',
                'color' => '#c36739',
            ),
            array(
                'name' => esc_html__('Color 2', 'vite-nera'),
                'slug' => 'color-2',
                'color' => '#333533',
            ),
            array(
                'name' => esc_html__('Color 3', 'vite-nera'),
                'slug' => 'color-3',
                'color' => '#555555',
            ),
            array(
                'name' => esc_html__('Color 4', 'vite-nera'),
                'slug' => 'color-4',
                'color' => '#f3f5f6',
            ),
            array(
                'name' => esc_html__('Color 5', 'vite-nera'),
                'slug' => 'color-5',
                'color' => '#fff',
            ),
            array(
                'name' => esc_html__('Color 6', 'vite-nera'),
                'slug' => 'color-6',
                'color' => '#e8ebed',
            ),
            array(
                'name' => esc_html__('Color 7', 'vite-nera'),
                'slug' => 'color-7',
                'color' => '#f4f5f7',
            ),
            array(
                'name' => esc_html__('Color 8', 'vite-nera'),
                'slug' => 'color-8',
                'color' => '#dbdee1',
            ),
        ));

        vite_nera_regiter_block_styles();

    }
endif;
add_action('after_setup_theme', 'vite_nera_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 vite_nera_content_width()
{
    // This variable is intended to be overruled from themes.
    // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
    $GLOBALS['content_width'] = apply_filters('vite_nera_content_width', 770);
}

add_action('after_setup_theme', 'vite_nera_content_width', 0);

function vite_nera_adjust_content_width()
{
    global $content_width;

    if (is_page_template('template-wide-page.php') || is_page_template('template-canvas-page.php')) {
        $content_width = 1170;
    }

}

add_action('template_redirect', 'vite_nera_adjust_content_width');

function vite_nera_regiter_block_styles()
{
    if (function_exists('register_block_style')) {
        register_block_style('getwid/advanced-heading', array(
            'label' => esc_html_x('Highlighted', 'block style name', 'vite-nera'),
            'name' => 'highlight'
        ));

        register_block_style('getwid/person', array(
            'label' => esc_html_x('Horizontal', 'block style name', 'vite-nera'),
            'name' => 'horizontal'
        ));

        register_block_style('getwid/image-box', array(
            'label' => esc_html_x('With border', 'block style name', 'vite-nera'),
            'name' => 'with-border'
        ));

        register_block_style('getwid/image-box', array(
            'label' => esc_html_x('With background', 'block style name', 'vite-nera'),
            'name' => 'with-background'
        ));

        register_block_style('getwid/media-text-slider', array(
            'label' => esc_html_x('Half content slider', 'block style name', 'vite-nera'),
            'name' => 'half-content-slider'
        ));

        register_block_style('getwid/testimonial', array(
            'label' => esc_html_x('Testimonial without border', 'block style name', 'vite-nera'),
            'name' => 'testimonial-without-border'
        ));
    }
}

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function vite_nera_widgets_init()
{
    register_sidebar(
        array(
            'name' => esc_html__('Header', 'vite-nera'),
            'id' => 'header',
            'description' => esc_html__('Add widgets here.', 'vite-nera'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        )
    );

    register_sidebar(
        array(
            'name' => esc_html__('Footer 1', 'vite-nera'),
            'id' => 'footer-1',
            'description' => esc_html__('Add widgets here.', 'vite-nera'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        )
    );

    register_sidebar(
        array(
            'name' => esc_html__('Footer 2', 'vite-nera'),
            'id' => 'footer-2',
            'description' => esc_html__('Add widgets here.', 'vite-nera'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        )
    );

    register_sidebar(
        array(
            'name' => esc_html__('Footer 3', 'vite-nera'),
            'id' => 'footer-3',
            'description' => esc_html__('Add widgets here.', 'vite-nera'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        )
    );

    register_sidebar(
        array(
            'name' => esc_html__('Footer 4', 'vite-nera'),
            'id' => 'footer-4',
            'description' => esc_html__('Add widgets here.', 'vite-nera'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        )
    );
}

add_action('widgets_init', 'vite_nera_widgets_init');

/**
 * Enqueue scripts and styles.
 */
function vite_nera_scripts()
{
    wp_enqueue_style('font-awesome-free', get_template_directory_uri() . '/assets/fontawesome-free/css/all.min.css', array(), '5.9.0');

    wp_enqueue_style('linearicons-free', get_template_directory_uri() . '/assets/linearicons/style.css', array(), '1.0.0');

    wp_enqueue_style('vite-nera-fonts', vite_nera_fonts_url(), array(), null);

    wp_enqueue_style('vite-nera-style', get_stylesheet_uri(), array(), VITE_NERA_VERSION);

    wp_enqueue_script('vite-nera-functions', get_template_directory_uri() . '/js/functions.js', array('jquery'), VITE_NERA_VERSION, true);

    wp_enqueue_script('vite-nera-navigation', get_template_directory_uri() . '/js/navigation.js', array(), VITE_NERA_VERSION, true);

    wp_enqueue_script('vite-nera-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), VITE_NERA_VERSION, true);

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

add_action('wp_enqueue_scripts', 'vite_nera_scripts');

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Include LinearIcons file.
 */
require get_template_directory() . '/inc/lnr-icons.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';

/**
 * TGMPA
 */
require get_template_directory() . '/inc/tgmpa-init.php';

/**
 * Demo data import
 */
require get_template_directory() . '/inc/demo-import.php';

/**
 * Load WooCommerce compatibility file.
 */
if (class_exists('WooCommerce')) {
    require get_template_directory() . '/inc/woocommerce.php';
}

if ( ! function_exists( 'vite_nera_theme_updater' ) ) :
    /**
     * Theme Updater
     * Easy Digital Downloads.
     *
     * Action is used so that child themes can easily disable.
     */
    function vite_nera_theme_updater() {
        if ( current_user_can( 'update_themes' ) ) {
            require get_template_directory() . '/inc/updater/theme-updater.php';
        }
    }
endif;

add_action( 'after_setup_theme', 'vite_nera_theme_updater' );

function vite_nera_fonts_url()
{
    $url = 'https://fonts.googleapis.com/css2?';
    $fonts = [];

    $font1 = esc_html_x('on', 'Amiri font: on or off', 'vite-nera');
    if ('off' !== $font1) {
        $fonts[] = 'family=Amiri:ital,wght@0,400;0,700;1,400;1,700';
    }

    $font2 = esc_html_x('on', 'Lato font: on or off', 'vite-nera');
    if ('off' !== $font2) {
        $fonts[] = 'family=Lato:ital,wght@0,400;0,700;1,400;1,700';
    }

    $font3 = esc_html_x('on', 'Dancing Script font: on or off', 'vite-nera');
    if ('off' !== $font3) {
        $fonts[] = 'family=Dancing+Script';
    }

    if (!$fonts) {
        return null;
    }

    $url .= implode('&amp;', $fonts);
    $url .= '&amp;display=swap';

    return esc_url_raw($url);
}

function vite_nera_get_version()
{
    $theme_info = wp_get_theme(get_template());

    return $theme_info->get('Version');
}

function vite_nera_load_fonts_to_editor() {
	wp_enqueue_style('vite-nera-fonts', vite_nera_fonts_url(), array(), null);
}

add_action('enqueue_block_editor_assets', 'vite_nera_load_fonts_to_editor');