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

$portfolio_one_themeObject 		= wp_get_theme();
$portfolio_one_ver      		= $portfolio_one_themeObject -> get( 'Version' );
define( 'PORTFOLIO_ONE_VER',$portfolio_one_ver);



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

		/*
		 * Responsive embedded content
		 *
		 * @link https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#responsive-embedded-content
		 */
		add_theme_support( 'responsive-embeds' );


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

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(
			array(
				'menu-1' => esc_html__( 'Primary Menu', 'portfolio-one' ),
			)
		);

		/*
		 * 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(
				'portfolio_one_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'      => 250,
				'width'       => 250,
				'flex-width'  => true,
				'flex-height' => true,
			)
		);

		add_theme_support ('align-wide');

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



/**
 * Register Google Fonts
 */
function portfolio_one_fonts_url() {

	$fonts_url 		= '';
	$jost 			= esc_html_x( 'on', 'Jost font: on or off', 'portfolio-one' );
	$poppins 		= esc_html_x( 'on', 'Poppins font: on or off', 'portfolio-one' );
	$rubik 			= esc_html_x( 'on', 'Rubik font: on or off', 'portfolio-one' );

	$font_families 	= array();

	if ( 'off' !== $jost ) {
		$font_families[] = 'Jost:ital,wght@0,400;0,500;1,600&display=swap';
	}
	if ( 'off' !== $poppins ) {
		$font_families[] = 'Poppins:wght@0,400;0,500;1,600;2,700;3,800&display=swap';
	}
	if ( 'off' !== $rubik ) {
		$font_families[] = 'Rubik:wght@300;400;500;600&display=swap';
	}

	if ( in_array( 'on', array( $jost, $poppins ) ) ) {
		$query_args = array(
			'family' => urlencode( implode( '|', $font_families ) ),
			'subset' => urlencode( 'latin,latin-ext' ),
		);
		$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
	}

	return esc_url_raw( $fonts_url );
}


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

	register_sidebar( array(
		'name'          => esc_html__( 'Right Sidebar', 'portfolio-one' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', 'portfolio-one' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h4 class="widget-title"><span class="title-bg">',
		'after_title'   => '</span></h4>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Left Sidebar', 'portfolio-one' ),
		'id'            => 'portfolio_one_lite_left_sidebar',
		'description'   => esc_html__( 'Add widgets here.', 'portfolio-one' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h4 class="widget-title"><span class="title-bg">',
		'after_title'   => '</span></h4>',
	) );


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


/**
 * Enqueue scripts and styles.
 */
add_action( 'wp_enqueue_scripts', 'portfolio_one_scripts' );
function portfolio_one_scripts() {

	$default 									= portfolio_one_customizer_defaults();
	$portfolio_one_post_sidebar_sticky_enable 	= get_theme_mod('portfolio_one_post_sidebar_sticky_enable', $default['portfolio_one_post_sidebar_sticky_enable']);
	$portfolio_one_blog_sidebar_sticky_enable 	= get_theme_mod('portfolio_one_blog_sidebar_sticky_enable', $default['portfolio_one_blog_sidebar_sticky_enable']);
	$cww_dark_mode_enable						= get_theme_mod('cww_dark_mode_enable',$default['cww_dark_mode_enable']);
	$cww_adv_fawesome_enable 					= get_theme_mod('cww_adv_fawesome_enable', $default['cww_adv_fawesome_enable']);

	if( $cww_adv_fawesome_enable == true ){
    	wp_enqueue_style( 'font-awesome-icons', get_theme_file_uri('/assets/icons/font-awesome/css/all.min.css'), array(), PORTFOLIO_ONE_VER );
	}
	
	wp_enqueue_style( 'portfolio-one-fonts', portfolio_one_fonts_url(), array(), null );
	wp_enqueue_style( 'portfolio-one-style-vars', get_theme_file_uri('/assets/css/cww-vars.css'), array(), PORTFOLIO_ONE_VER );
	wp_register_style( 'portfolio-one-blog-archive', get_theme_file_uri( '/assets/css/blog-archive.css' ), array(), PORTFOLIO_ONE_VER );
	wp_register_style( 'portfolio-one-single-blog', get_theme_file_uri( '/assets/css/single-blog.css' ), array(), PORTFOLIO_ONE_VER );
	wp_register_style( 'portfolio-one-404', get_theme_file_uri( '/assets/css/404.css' ), array(), PORTFOLIO_ONE_VER );
	wp_enqueue_style( 'portfolio-one-keyboard', get_theme_file_uri( '/assets/css/keyboard.css' ), array(), PORTFOLIO_ONE_VER );


	wp_enqueue_style( 'portfolio-one-style', get_stylesheet_uri(), array(), PORTFOLIO_ONE_VER );
	wp_style_add_data( 'portfolio-one-style', 'rtl', 'replace' );
	wp_enqueue_style( 'portfolio-one-responsive', get_theme_file_uri( '/assets/css/responsive.css' ), array(), PORTFOLIO_ONE_VER );

	wp_enqueue_script( 'portfolio-one-navigation', get_theme_file_uri('/assets/js/navigation.js'), array(), PORTFOLIO_ONE_VER, true );
	
	if( $cww_dark_mode_enable == true ) {
		wp_enqueue_script( 'portfolio-one-darkmode', get_theme_file_uri('/assets/js/darkmode.js'), array('jquery'), PORTFOLIO_ONE_VER, false );
		wp_enqueue_style( 'portfolio-one-darkmode', get_theme_file_uri( '/assets/css/darkmode.css' ), array(), PORTFOLIO_ONE_VER );
	}
	
	
	// sticky option
	if( $portfolio_one_post_sidebar_sticky_enable == true || $portfolio_one_blog_sidebar_sticky_enable == true ){
		wp_enqueue_script( 'theia-sticky-sidebar', get_template_directory_uri() . '/assets/js/theia-sticky-sidebar.js', array('jquery'), PORTFOLIO_ONE_VER, true );
	}

	//localizing customizer option
	wp_register_script( 'portfolio-one-scripts', get_template_directory_uri() . '/assets/js/portfolio-one.js', array('jquery'), PORTFOLIO_ONE_VER, true );
	$localize_options =  array(
      	'stickySidebar'		=> esc_html($portfolio_one_post_sidebar_sticky_enable),
		'stickySidebarBlog'	=> esc_html($portfolio_one_blog_sidebar_sticky_enable),
        
        );
    wp_localize_script( 'portfolio-one-scripts', 'cwwLocalizeScript', $localize_options  );
    wp_enqueue_script( 'portfolio-one-scripts' );
}

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

/**
* Admin scripts
*/
function portfolio_one_admin_scripts(){
	wp_enqueue_style( 'font-awesome-icons', get_theme_file_uri('/assets/icons/font-awesome/css/all.min.css'), array(), PORTFOLIO_ONE_VER );
	wp_enqueue_style( 'wp-color-picker');
    wp_enqueue_script( 'wp-color-picker');

	wp_enqueue_script( 'portfolio-one-admin', get_theme_file_uri('/assets/js/cww-admin.js'), array(), PORTFOLIO_ONE_VER, true );

}
add_action('admin_enqueue_scripts','portfolio_one_admin_scripts');

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.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/customizer-sanitize.php';
require get_template_directory() . '/inc/customizer/customizer-defaults.php';
require get_template_directory() . '/inc/customizer/controllers/controller-main.php';
require get_template_directory() . '/inc/customizer/customizer.php';
require get_template_directory() . '/inc/customizer/customizer-functions.php';




require get_template_directory() . '/inc/hooks/header-hooks.php';
require get_template_directory() . '/inc/hooks/homepage-hooks.php';
require get_template_directory() . '/inc/hooks/footer-hooks.php';

require get_template_directory() . '/inc/dynamic-styling.php';

/**
 * SVG icons
 */
require get_template_directory() . '/inc/svg-icons/svg-icons.php';


if ( is_admin() ) {
require get_template_directory() . '/inc/welcome/welcome-config.php';
}

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
	require get_template_directory() . '/inc/jetpack.php';
}