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

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

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

		add_theme_support( "responsive-embeds" );
		
		add_theme_support( 'post-thumbnails' );
		
		register_nav_menus( array(
			'primary'     => esc_html__( 'Primary', 'the-wonders' ),
		) );

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );
		
		// woocommerce support
		
		add_theme_support( 'woocommerce' );
		
		// Woocommerce Gallery Support
		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-lightbox' );
		add_theme_support( 'wc-product-gallery-slider' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support( 'custom-logo', array(
			'height'      => 39,
			'width'       => 210,
			'flex-height' => true,
			'flex-width' => true,
			'header-text' => array( 'site-title', 'site-description' ),
			
		) );
		
		/**
		 * Custom background support.
		 */
		add_theme_support( 'custom-background', apply_filters( 'the_wonders_custom_background_args', array(
			'default-color' => 'f3f8fe',
			'default-image' => '',
		) ) );
		
		/**
		* Set default content width.
		*/
		if ( ! isset( $content_width ) ) {
			$content_width = 800;
		}
		
	}
endif;
add_action( 'after_setup_theme', 'the_wonders_setup' );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */

function the_wonders_widgets_init() {
	$sidebars = apply_filters( 'the_wonders_sidebars_data', array(
	    'sidebar-main'            => esc_html__( 'Sidebar Widget Area', 'the-wonders' ),
		'footer-sidebar-one'      => esc_html__( 'Footer Sidebar One', 'the-wonders' ),
		'footer-sidebar-two'      => esc_html__( 'Footer Sidebar Two', 'the-wonders' ),
		'footer-sidebar-three'    => esc_html__( 'Footer Sidebar Three', 'the-wonders' ),
		'footer-sidebar-four'     => esc_html__( 'Footer Sidebar Four', 'the-wonders' ),
	) );

	foreach ( $sidebars as $id => $name ) :
	
		register_sidebar( array(
			'id'            => $id,
			'name'          => $name,
			'description'   => $name,
			'before_widget' => '<aside id="%1$s" class="widget text_widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h4 class="widget-title">',
			'after_title'   => '</h4>',
		) );

	endforeach;

}
add_action( 'widgets_init', 'the_wonders_widgets_init');

/**
 * Enqueue scripts and styles.
 */
function the_wonders_scripts() {

	// Include the file.
	require_once get_theme_file_path( 'inc/wptt-webfont-loader.php' );

	// Load the webfont.
	wp_enqueue_style(
		'fredoka',
		wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Fredoka:wght@300;400;500;600;700&display=swap' ),
		array(),
		'1.0'
	);

	/**
	 * Styles.
	 */
	wp_enqueue_style('bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.css'); 	 
	// Fontawesome.
	wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/assets/css/font-awesome/css/font-awesome.min.css' );
	// Theme styles.
	wp_enqueue_style( 'the-wonders-style', get_stylesheet_uri() );
	wp_style_add_data('the-wonders-style', 'rtl', 'replace');
	wp_enqueue_style('the-wonders-theme-default', get_template_directory_uri() . '/assets/css/theme-default.css');
	wp_enqueue_style('bootstrap-smartmenus-css', get_template_directory_uri() . '/assets/css/bootstrap-smartmenus.css');

	/**
	 * Scripts.
	 */
	wp_enqueue_script('bootstrap-js', get_template_directory_uri() . '/assets/js/bootstrap.js', array('jquery'));	 
	// Theme JavaScript.
	wp_enqueue_script('smartmenus-js', get_template_directory_uri() . '/assets/js/smartmenus/jquery.smartmenus.js');
	wp_enqueue_script( 'the-wonders-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );
	wp_enqueue_script('the-wonders-custom-js', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'));
	wp_enqueue_script('bootstrap-smartmenus-js', get_template_directory_uri() . '/assets/js/smartmenus/bootstrap-smartmenus.js');
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	$css = '';

	if ( get_header_image() ) :

		$css .=  '
			.navbar{
				background-image: url('.esc_url(get_header_image()).');
				-webkit-background-size: cover !important;
				-moz-background-size: cover !important;
				-o-background-size: cover !important;
				background-size: cover !important;
			}';

	endif;

	wp_add_inline_style( 'the-wonders-style', $css );

}
add_action( 'wp_enqueue_scripts', 'the_wonders_scripts' );

function the_wonders_logo_resizer() {

    $theme_logo_size_css = '';
    $logo_resizer = get_theme_mod('the_wonders_logo_resizer');

	$theme_logo_size_css = '
		.custom-logo{
			height: '.esc_attr($logo_resizer).'px !important;
			width: '.esc_attr($logo_resizer).'px !important;
		}
	';
    wp_add_inline_style( 'the-wonders-style',$theme_logo_size_css );

}
add_action( 'wp_enqueue_scripts', 'the_wonders_logo_resizer' );


function the_wonders_customize_register($wp_customize){
    $wp_customize->add_setting('the_wonders_logo_resizer',array(
        'default' => '',
        'sanitize_callback' => 'sanitize_text_field'
    ));
    $wp_customize->add_control('the_wonders_logo_resizer',array(
        'label' => esc_html__('Logo Sizer','the-wonders'),
        'section' => 'title_tagline',
        'setting' => 'the_wonders_logo_resizer',
        'type'  => 'text'
    ));
}
add_action('customize_register', 'the_wonders_customize_register');

/**
 * Define constants
 */
// Root path/URI.
define( 'THE_WONDERS_PARENT_DIR', get_template_directory() );
define( 'THE_WONDERS_PARENT_URI', get_template_directory_uri() );

// Include path/URI.
define( 'THE_WONDERS_PARENT_INC_DIR', THE_WONDERS_PARENT_DIR . '/inc' );
define( 'THE_WONDERS_PARENT_INC_URI', THE_WONDERS_PARENT_URI . '/inc' );

/**
 * Implement the Custom Header feature.
 */
require THE_WONDERS_PARENT_INC_DIR . '/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require THE_WONDERS_PARENT_INC_DIR . '/template-tags.php';

/**
 * Bootstrap class navwalker.
 */

require THE_WONDERS_PARENT_INC_DIR . '/class-bootstrap-navwalker.php';