<?php
/**
 * trainify functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */

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

        /*
         * Add post format support
         */
        add_theme_support(
            'post-formats',
            array(
                'aside',
                'gallery',
                'link',
                'image',
                'quote',
                'status',
                'video',
                'audio',
                'chat'
            )
        );

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

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

        // Add theme support for Custom Logo.
        add_theme_support(
            'custom-logo',
            array(
                'width' => 300,
                'height' => 45,
                'flex-width' => true,
                'flex-height' => true,
            )
        );

        $header_image_args = array(
            'default-image' => '',
            'flex-height' => true,
            'flex-width' => true,
            'uploads' => true,
            'random-default' => false,
            'header-text' => true,
            'video' => true,
        );
        add_theme_support('custom-header', $header_image_args);

        // Set up the WordPress core custom background feature.
        add_theme_support(
            'custom-background',
            apply_filters(
                'trainify_custom_background_args',
                array(
                    'default-color' => '#fff',
                    'default-image' => '',
                    'default-repeat' => 'no-repeat',
                    'default-position-x' => 'center',
                    'default-position-y' => 'center',
                    'default-size' => 'cover',
                    'default-attachment' => 'fixed',
                )
            )
        );

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

        // Declare WooCommerce support
        $woocommerce_settings = apply_filters(
            'trainify_woocommerce_args',
            array(
                'single_image_width' => 555,
                'thumbnail_image_width' => 262,
                'gallery_thumbnail_image_width' => 160,
                'product_grid' => array(
                    'default_columns' => 3,
                    'default_rows' => 4,
                    'min_columns' => 1,
                    'max_columns' => 6,
                    'min_rows' => 1,
                ),
            )
        );
        add_theme_support('woocommerce', $woocommerce_settings);

        if (class_exists('WooCommerce', false)) {
            add_theme_support('wc-product-gallery-zoom');
            add_theme_support('wc-product-gallery-lightbox');
            add_theme_support('wc-product-gallery-slider');
        }

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

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

        add_image_size('trainify-thumb-300x200', 300, 200, true);
        add_image_size('trainify-thumb-300x300', 300, 300, true);
        add_image_size('trainify-thumb-400x600', 400, 600, true);
        add_image_size('trainify-thumb-500x365', 500, 365, true);
        add_image_size('trainify-thumb-600x600', 600, 600, true);
        add_image_size('trainify-thumb-800x600', 800, 600, true);
        add_image_size('trainify-thumb-1200x500', 1200, 500, true);

        // This theme uses wp_nav_menu() in one location.
        register_nav_menus(array(
            'trainify_top_menu' => esc_html__('Top Menu', 'trainify'),
            'trainify_primary_menu' => esc_html__('Primary Menu', 'trainify'),
            'trainify_footer_menu' => esc_html__('Footer Menu', 'trainify')
        ));

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

        add_editor_style(trainify_fonts_url());

        add_editor_style(get_template_directory_uri() . '/assets/admin/css/editor-style' . $min . '.css');

        $remove_src_set = get_theme_mod('trainify_remove_src_set', 1);

        if ($remove_src_set) {
            add_filter('wp_calculate_image_srcset', '__return_false');
        }

    }

endif;

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

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

/*-----------------------------------------------------------------------------------------------------------------------*/
/**
 * Set the theme version
 *
 * @global int $trainify_version
 * @since 1.0.0
 */
function trainify_theme_version()
{
    $trainify_theme_info = wp_get_theme();
    $GLOBALS['trainify_version'] = $trainify_theme_info->get('Version');
}

add_action('after_setup_theme', 'trainify_theme_version', 10);

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function trainify_pingback_header()
{
    if (is_singular() && pings_open()) {
        echo '<link rel="pingback" href="', esc_url(get_bloginfo('pingback_url')), '">';
    }
}

add_action('wp_head', 'trainify_pingback_header');

/*-----------------------------------------------------------------------------------------------------------------------*/
/**
 * Enqueue scripts and styles for only admin
 *
 * @since 1.0.0
 */
function trainify_admin_scripts($hook)
{

    global $trainify_version;

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

    wp_enqueue_style('trainify-admin-style', get_template_directory_uri() . '/assets/admin/css/tff-admin-style' . $min . '.css', array(), esc_attr($trainify_version));

    if ('widgets.php' != $hook && 'customize.php' != $hook && 'edit.php' != $hook && 'post.php' != $hook && 'post-new.php' != $hook) {
        return;
    }

    wp_enqueue_script('jquery-ui-button');

    wp_enqueue_script('trainify-admin-script', get_template_directory_uri() . '/assets/admin/js/admin-script' . $min . '.js', array('jquery'), esc_attr($trainify_version), true);

}

add_action('admin_enqueue_scripts', 'trainify_admin_scripts');

/*-----------------------------------------------------------------------------------------------------------------------*/
/**
 * Enqueue scripts and styles.
 *
 * @since 1.0.0
 */
function trainify_enqueue_scripts()
{

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

    global $trainify_version;

    wp_enqueue_style('trainify-fonts', trainify_fonts_url(), array(), null);

    wp_enqueue_style('font-awesome', trainify_framework_assets_url('library/font-awesome/css/font-awesome.min.css'), array(), '4.7.0');

    wp_enqueue_style('lightslider-style', get_template_directory_uri() . '/assets/vendor/lightslider/css/lightslider.min.css', array(), '1.1.6');

    wp_enqueue_style('magnific-popup-style', get_template_directory_uri() . '/assets/vendor/magnific-popup/css/magnific-popup.min.css', array(), '1.1.0');

    wp_enqueue_style('trainify-main', get_template_directory_uri() . '/assets/css/trainify-style' . $min . '.css', array(), esc_attr($trainify_version));
    wp_style_add_data('trainify-main', 'rtl', 'replace');

    wp_enqueue_style('trainify-style', get_stylesheet_uri(), array(), esc_attr($trainify_version));

    wp_enqueue_script('trainify-navigation', get_template_directory_uri() . '/assets/js/navigation-script' . $min . '.js', array(), esc_attr($trainify_version), true);

    $menu_sticky_option = get_theme_mod('trainify_menu_sticky_option', 'show');
    if ($menu_sticky_option == 'show') {
        wp_enqueue_script('jquery-sticky', get_template_directory_uri() . '/assets/vendor/sticky/jquery.sticky.js', array('jquery'), '20150416', true);

        wp_enqueue_script('tff-sticky-menu-setting', get_template_directory_uri() . '/assets/vendor/sticky/sticky-setting.js', array('jquery-sticky'), '20150309', true);
    }

    wp_enqueue_script('trainify-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix-script' . $min . '.js', array(), esc_attr($trainify_version), true);

    wp_enqueue_script('lightslider', get_template_directory_uri() . '/assets/vendor/lightslider/js/lightslider.min.js', array('jquery'), '1.1.6', true);

    wp_enqueue_script('jquery-ui-tabs');

    wp_enqueue_script('magnific-popup-js', get_template_directory_uri() . '/assets/vendor/magnific-popup/js/jquery.magnific-popup.min.js', array('jquery'), '1.1.0', true);

    wp_enqueue_script('trainify-custom-script', get_template_directory_uri() . '/assets/js/main-script' . $min . '.js', array('jquery'), esc_attr($trainify_version), true);

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

add_action('wp_enqueue_scripts', 'trainify_enqueue_scripts', 20);

