<?php
/**
 * Youplay functions and definitions
 *
 * @package Youplay
 */

add_action( 'after_setup_theme', 'yp_setup' );
if ( ! function_exists( 'yp_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 yp_setup() {

    /*
     * Make theme available for translation.
     * Translations can be filed in the /languages/ directory.
     * If you're building a theme based on Youplay, use a find and replace
     * to change 'youplay' to the name of your theme in all the template files
     */
    load_theme_textdomain( 'youplay', 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 http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
     */
    // This theme uses wp_nav_menu() in one location.
    register_nav_menus( array(
        'primary'       => esc_html__( 'Primary Menu', 'youplay' ),
        'primary-right' => esc_html__( 'Primary Right Menu', 'youplay' ),
    ) );

    /*
     * 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 WooCommerce
     */
    add_theme_support( 'woocommerce' );

    // Add default image sizes
    add_theme_support('post-thumbnails');
    add_image_size('500x375', 500, 375, true);
    add_image_size('200x200', 200, 200, true);
    add_image_size('90x90', 90, 90, true);
    add_image_size('1400x600', 1400, 600, true);
    add_image_size('1440x900', 1440, 900, true);

    // Register the three useful image sizes for use in Add Media modal
    add_filter( 'image_size_names_choose', 'yp_custom_sizes' );
    function yp_custom_sizes( $sizes ) {
        return array_merge( $sizes, array(
            '500x375'   => __( 'Carousel Thumbnail (500x375)', 'youplay' ),
            '200x200'   => __( 'User Avatar (200x200)', 'youplay' ),
            '90x90'     => __( 'User Small Avatar (90x90)', 'youplay' ),
            '1400x600'  => __( '1400x600', 'youplay' ),
            '1440x900'  => __( '1440x900', 'youplay' ),
        ) );
    }
}
endif; // yp_setup

/**
 * Register widget area.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
 */
add_action( 'widgets_init', 'yp_widgets_init' );
if ( ! function_exists( 'yp_widgets_init' ) ) :
function yp_widgets_init() {

    register_sidebar( array(
        'name'          => esc_html__( 'Sidebar', 'youplay' ),
        'id'            => 'sidebar-1',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="side-block widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title block-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'WooCommerce Sidebar', 'youplay' ),
        'id'            => 'woocommerce_sidebar',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="side-block widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title block-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'BuddyPress Sidebar', 'youplay' ),
        'id'            => 'buddypress_sidebar',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="side-block widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title block-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'bbPress Sidebar', 'youplay' ),
        'id'            => 'bbpress_sidebar',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="side-block widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title block-title">',
        'after_title'   => '</h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_html__( 'Matches Sidebar', 'youplay' ),
        'id'            => 'matches_sidebar',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="side-block widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title block-title">',
        'after_title'   => '</h4>',
    ) );
}
endif;

/**
 * Change default params when if right sidebar
 */
add_filter( 'dynamic_sidebar_params', 'yp_dynamic_sidebar_params' );
if ( ! function_exists( 'yp_dynamic_sidebar_params' ) ) :
function yp_dynamic_sidebar_params( $params ) {
    $post_type = get_post_type();
    $layout = '';

    switch($post_type) {
        case 'post':
            $layout = yp_opts('single_post_layout', true);
            break;
        case 'page':
            $layout = yp_opts('single_page_layout', true);
            break;
        default:
            if(function_exists('is_bbpress') && is_bbpress()) {
                $layout = yp_opts('press_layout', true);
            } else if(function_exists('is_woocommerce') && is_woocommerce()) {
                $layout = yp_opts('single_product_layout', true);
            }
            break;
    }

    if (strpos($layout, 'cont-side') !== false) {
        $params[0]['before_widget'] = str_replace('side-block', 'side-block right-side', $params[0]['before_widget']);
    }

    return $params;
}
endif;


/**
 * Enqueue scripts and styles.
 */
add_action( 'wp_enqueue_scripts', 'yp_scripts' );
if ( ! function_exists( 'yp_scripts' ) ) :
function yp_scripts() {
    wp_enqueue_style( 'youplay', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/plugins/bootstrap/css/bootstrap.min.css' );
    wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/assets/plugins/fontawesome/css/font-awesome.min.css' );
    wp_enqueue_style( 'magnific-popup', get_template_directory_uri() . '/assets/plugins/magnific-popup/magnific-popup.css' );
    wp_enqueue_style( 'owl.carousel', get_template_directory_uri() . '/assets/plugins/owl.carousel/owl.carousel.css' );
    wp_enqueue_style( 'social-likes', get_template_directory_uri() . '/assets/plugins/social-likes/social-likes_flat.css' );

    // light style
    if(yp_opts('theme_style') == 'light') {
        wp_enqueue_style( 'youplay-style2', get_template_directory_uri() . '/assets/css/youplay-light.min.css' );
        wp_enqueue_style( 'youplay-buddypress2', get_template_directory_uri() . '/assets/css/youplay-buddypress-light.css' );
        wp_enqueue_style( 'youplay-bbpress2', get_template_directory_uri() . '/assets/css/youplay-bbpress-light.css' );
        wp_enqueue_style( 'youplay-woocommerce2', get_template_directory_uri() . '/assets/css/youplay-woocommerce-light.css' );
    } else {
        // dark style
        if(yp_opts('theme_style') == 'dark') {
            wp_enqueue_style( 'youplay-style1', get_template_directory_uri() . '/assets/css/youplay.min.css' );
        }

        // custom style
        else {
            wp_enqueue_style( 'youplay-style2', get_template_directory_uri() . '/assets/css/youplay-custom.min.css' );
        }

        wp_enqueue_style( 'youplay-buddypress1', get_template_directory_uri() . '/assets/css/youplay-buddypress.css' );
        wp_enqueue_style( 'youplay-bbpress1', get_template_directory_uri() . '/assets/css/youplay-bbpress.css' );
        wp_enqueue_style( 'youplay-woocommerce1', get_template_directory_uri() . '/assets/css/youplay-woocommerce.css' );
    }
    wp_enqueue_style( 'youplay-style3', get_template_directory_uri() . '/assets/css/wp-youplay.css' );

    // deregister bbPress styles
    wp_deregister_style( 'bbp-default' );

    // rtl
    if(yp_opts('general_rtl')) {
        wp_enqueue_style( 'youplay-rtl', get_template_directory_uri() . '/assets/css/youplay-rtl.css' );
    }

    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/assets/plugins/bootstrap/js/bootstrap.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'isotope', get_template_directory_uri() . '/assets/plugins/isotope/isotope.pkgd.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'jquery.coundown', get_template_directory_uri() . '/assets/plugins/jquery.coundown/jquery.countdown.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'magnific-popup', get_template_directory_uri() . '/assets/plugins/magnific-popup/jquery.magnific-popup.min.js', '', '', true );
    wp_enqueue_script( 'owl.carousel', get_template_directory_uri() . '/assets/plugins/owl.carousel/owl.carousel.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'skrollr', get_template_directory_uri() . '/assets/plugins/skrollr/skrollr.min.js', '', '', true );
    wp_enqueue_script( 'social-likes', get_template_directory_uri() . '/assets/plugins/social-likes/social-likes.min.js', '', '', true );
    wp_enqueue_script( 'hexagon-progress', get_template_directory_uri() . '/assets/plugins/jquery.hexagonprogress/jquery.hexagonprogress.min.js', '', '', true );

    if(yp_opts('general_smoothscroll')) {
        wp_enqueue_script( 'smoothscroll', get_template_directory_uri() . '/assets/plugins/smoothscroll/smoothscroll.js', '', '', true );
    }

    wp_enqueue_script( 'youplay-js', get_template_directory_uri() . '/assets/js/youplay.min.js', array('jquery', 'bootstrap', 'isotope', 'jquery.coundown', 'magnific-popup', 'owl.carousel', 'skrollr', 'social-likes', 'hexagon-progress'), '', true );
    wp_enqueue_script( 'youplay-wp-js', get_template_directory_uri() . '/assets/js/youplay-wp.js', array('jquery', 'youplay-js'), '', true );
    wp_enqueue_script( 'youplay-cf7-js', get_template_directory_uri() . '/assets/js/youplay-cf7.js', array('jquery', 'youplay-js'), '', true );

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


/**
 * Add custom typography styles
 */
add_action('wp_head', 'yp_wp_head', 100);
if ( ! function_exists( 'yp_wp_head' ) ) :
function yp_wp_head() {
    require get_template_directory() . '/inc/head_styles.php';
}
endif;


/**
 * Return Option from OptionTree
 */
if ( ! function_exists( 'yp_opts' ) ) :
function yp_opts($opt_name = null, $use_meta_box = null, $postId = null){
    if($opt_name == null) {
        return null;
    }
    $value = null;

    // try get value from meta box
    if($use_meta_box) {
        if($postId == null) {
            $postId = get_the_ID();
        }
        $value = get_post_meta($postId, $opt_name, true);
    }

    // get value from options
    if($value == null || $value == 'default') {
        $value = ot_get_option($opt_name, null);
    }

    // get std value
    if($value == null && function_exists( 'ot_settings_id' )) {
        $std = get_option( ot_settings_id(), array() );
        $std = $std['settings'];

        foreach($std as $v) {
            if($v['id'] == $opt_name) {
                $value = $v['std'];
            }
        }
    }

    // change 'on' to 1 and 'off' to 0
    if($value == 'on') {
        $value = 1;
    } else if($value == 'off') {
        $value = 0;
    }

    return $value;
}
endif;
if ( ! function_exists( 'yp_opts_e' ) ) :
function yp_opts_e($opt_name = null, $use_meta_box = null, $postId = null){
    echo yp_opts($opt_name, $use_meta_box, $postId);
}
endif;


/**
 * Admin References
 */
require get_template_directory() . '/admin/youplay_admin.php';

/**
 * Theme Options (Option Tree)
 */
add_filter( 'ot_theme_mode', '__return_true' );
require get_template_directory() . '/inc/lib/OptionTree/ot-loader.php';
require get_template_directory() . '/inc/theme-options.php';

/**
 * Theme Metaboxes (Option Tree)
 */
require get_template_directory() . '/inc/theme-metaboxes.php';

/**
 * TGMA
 */
require get_template_directory() . '/inc/plugins-activation.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

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

/**
 * Menu walker
 */
require get_template_directory() . '/inc/menu-walker.php';

/**
 * Comments walker
 */
require get_template_directory() . '/inc/comments-walker.php';

/**
 * Post Like
 */
require get_template_directory() . '/inc/lib/post-like/post-like.php';




/**
 * Shortcodes
 */
require get_template_directory() . '/shortcodes/_all.php';


/**
 * Widgets
 */
require get_template_directory() . '/widgets/recent-posts.php';


/**
 * Custom WooCommerce functions
 */
require get_template_directory() . '/woocommerce/functions.php';


/**
 * Custom BuddyPress functions
 */
require get_template_directory() . '/buddypress/functions.php';


/**
 * Custom bbPress functions
 */
require get_template_directory() . '/bbpress/functions.php';


/**
 * WP Clan Wars
 */
require get_template_directory() . '/inc/lib/WPClanWars/wp-clanwars.php';


/**
 * Demo Importer
 */
if(is_admin()){
    require get_template_directory() .'/inc/lib/radium-one-click-demo-install/init.php';

    function load_radium_one_click_demo_install(){
        new Radium_Theme_Demo_Data_Importer;
    }
    add_action( 'after_setup_theme', 'load_radium_one_click_demo_install', 2 );
}