<?php
if ( ! function_exists( 'kayla_require_file' ) ) {
    /**
     * Check and require file if file exists
     */
    function kayla_require_file( $path ) {
        if ( file_exists($path) ) {
            require $path;
        }
    }
}

/**
 * Setup theme
 */
if ( ! function_exists( 'kayla_setup_theme' ) ) {
    function kayla_setup_theme() {
        if ( ! isset( $content_width ) ) { $content_width = 1296; }

        /**
         * Theme text domain
         */
        load_theme_textdomain( 'kayla', get_template_directory() . '/languages' );

    	/**
         * Theme support
         */
        add_theme_support( 'title-tag' );
        add_theme_support( 'woocommerce' );
        add_theme_support( 'wc-product-gallery-zoom' );
        add_theme_support( 'wc-product-gallery-lightbox' );
        add_theme_support( 'wc-product-gallery-slider' );
    	add_theme_support( 'automatic-feed-links' );
    	add_theme_support( 'post-thumbnails' );
        add_theme_support( 'responsive-embeds' );
        add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
        add_theme_support( 'custom-background', array( 'default-color' => 'f5f5f5' ) );
        add_theme_support( 'custom-logo', array(
            'height'      => 80,
            'width'       => 350,
            'flex-height' => true,
            'flex-width'  => true
        ) );

        /**
         * Theme navigation menus
         */
        register_nav_menus(
            array(
                'primary' => __( 'Primary Menu', 'kayla' )
            )
        );
    }
}

add_action( 'after_setup_theme', 'kayla_setup_theme' );

// Disables the block editor from managing widgets. renamed from wp_use_widgets_block_editor
add_filter( 'use_widgets_block_editor', '__return_false' );

if ( ! function_exists( 'kayla_has_social_links' ) ) {
    /**
     * Check social links
     */
    function kayla_has_social_links() {
        $facebook   = get_theme_mod( 'kayla_facebook_url' );
        $twitter    = get_theme_mod( 'kayla_twitter_url' );
        $instagram  = get_theme_mod( 'kayla_instagram_url' );
        $pinterest  = get_theme_mod( 'kayla_pinterest_url' );
        $youtube    = get_theme_mod( 'kayla_youtube_url' );
        $tiktok     = get_theme_mod( 'kayla_tiktok_url' );
        $linkedin   = get_theme_mod( 'kayla_linkedin_url' );
        $soundcloud = get_theme_mod( 'kayla_soundcloud_url' );
        $vimeo      = get_theme_mod( 'kayla_vimeo_url' );
        
        if ( $facebook || $twitter || $instagram || $pinterest || $youtube || $soundcloud || $vimeo || $tiktok || $linkedin ) {
            return true;
        }
        return false;
    }
}

// Add specific CSS class by filter.

add_filter( 'body_class', function( $classes ) {
	if ( is_active_sidebar('sidebar') && !is_page() ) {
	   return array_merge( $classes, array( 'has-sidebar' ) );
	}
    return $classes;
} );

if ( ! function_exists( 'kayla_enqueue_scripts' ) ) {
    /**
     *  Enqueue Styles / Scripts
     */
    function kayla_enqueue_scripts() {
        $fonts_url  = '';
        $Lora       = _x( 'on', 'Lora font: on or off', 'kayla' );
        $Jost = _x( 'on', 'Jost: on or off', 'kayla' );
    
        if ( 'off' !== $Jost || 'off' !== $Lora ) {
            $font_families = array();
            if ( 'off' !== $Lora ) {
                $font_families[] = 'Lora:400,400i';
            }

            if ( 'off' !== $Jost ) {
                $font_families[] = 'Jost:400,400i,700,700i';
            }

            $query_args = array(
                'family' => urlencode( implode( '|', $font_families ) ),
                'subset' => urlencode( 'latin,latin-ext' ),
                'display' => 'swap'
            );

            $fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
        }

        wp_enqueue_style( 'kayla-google-fonts', esc_url_raw( $fonts_url ), array(), null );

        // CSS
        wp_enqueue_style( 'select2', get_template_directory_uri() . '/libs/select2/css/select2.min.css' );
        wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/libs/font-awesome/css/all.min.css', '', rand(0,9999) );
        wp_enqueue_style( 'kayla-style', get_template_directory_uri() . '/style.css', '', rand(0,9999) );
        wp_enqueue_style( 'kayla-theme', get_template_directory_uri() . '/assets/css/theme.css', '', rand(0,9999) );
    
        // JS
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'select2', get_template_directory_uri() . '/libs/select2/js/select2.min.js' );
        wp_enqueue_script( 'kayla-scripts', get_template_directory_uri() . '/assets/js/scripts.js', array(), false, rand(0,9999) );

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

add_action( 'wp_enqueue_scripts', 'kayla_enqueue_scripts' );

function kayla_customizer_control_scripts() {
    wp_enqueue_script( 'kayla-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-controls' ), time() );
    wp_enqueue_style( 'kayla-customizer', get_template_directory_uri() . '/assets/css/customizer.css' );
}

add_action( 'customize_controls_enqueue_scripts', 'kayla_customizer_control_scripts', 99 );

if ( ! function_exists( 'kayla_widgets_init' ) && function_exists( 'register_sidebar' ) ) {
    /**
     * Register Sidebars
     */
    function kayla_widgets_init() {
        register_sidebar(array(
    		'name'            => __( 'Sidebar', 'kayla' ),
    		'id'              => 'sidebar',
    		'before_widget'   => '<div id="%1$s" class="widget kayla-box %2$s">',
    		'after_widget'    => '</div>',
    		'before_title'    => '<h2 class="widget-title">',
    		'after_title'     => '</h2>'
    	));
        register_sidebar(array(
    		'name'            => __( 'Instagram Footer', 'kayla' ),
    		'id'              => 'footer-sidebar',
    		'before_widget'   => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'    => '</div>',
    		'before_title'    => '<h2 class="widget-title"><span>',
    		'after_title'     => '</span></h2>',
            'description'     => __( 'Use the "Instagram" widget here. IMPORTANT: For best result set number of photos to 6.', 'kayla' ),
    	));
    }
}

add_action( 'widgets_init', 'kayla_widgets_init' );

if ( ! function_exists( 'kayla_custom_comment' ) ) {
    /**
     * Custom comment html
     */
    function kayla_custom_comment($comment, $args, $depth) {
    	extract($args, EXTR_SKIP);
    	if ( 'div' == $args['style'] ) {
    		$tag = 'div';
    		$add_below = 'comment';
    	} else {
    		$tag = 'li';
    		$add_below = 'div-comment';
    	} ?>
    	<<?php echo esc_attr($tag); ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
    	<?php if ( 'div' != $args['style'] ) : ?>
    	<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    	<?php endif; ?>
    		<div class="comment-author">
    		<?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
    		</div>
    		<div class="comment-content">
    			<div class="comment-content-header">
                    <?php printf( __( '<h4 class="author-name">%s</h4>', 'kayla' ), get_comment_author_link() ); ?>
        			<span class="date-comment">
        				<a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
        				<?php printf( __('%1$s at %2$s', 'kayla'), get_comment_date(),  get_comment_time() ); ?></a>
        			</span>
        			<div class="reply">
        				<?php edit_comment_link( esc_html__( 'Edit', 'kayla' ), '  ', '' );?>
        				<?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
        			</div>
                </div>
    			<?php if ( $comment->comment_approved == '0' ) : ?>
    				<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'kayla' ); ?></em>
    				<br />
    			<?php endif; ?>
    			<div class="comment-text"><?php comment_text(); ?></div>
    		</div>	
    	<?php if ( 'div' != $args['style'] ) : ?>
    	</div>
    	<?php endif; ?>
    <?php
    }
}

if ( ! function_exists( 'kayla_has_site_social_media_links' ) ) {
    /**
     * Check social media links
     */
    function kayla_has_site_social_media_links() { 
        $facebook   = get_theme_mod('kayla_facebook');
        $twitter    = get_theme_mod('kayla_twitter');
        $instagram  = get_theme_mod('kayla_instagram');
        $pinterest  = get_theme_mod('kayla_pinterest');
        $bloglovin  = get_theme_mod('kayla_bloglovin');
        $tumblr     = get_theme_mod('kayla_tumblr');
        $youtube    = get_theme_mod('kayla_youtube');
        $dribbble   = get_theme_mod('kayla_dribbble');
        $soundcloud = get_theme_mod('kayla_soundcloud');
        $vimeo      = get_theme_mod('kayla_vimeo');
        $linkedin   = get_theme_mod('kayla_linkedin');
        
        if ( !$facebook && !$twitter && !$instagram && !$pinterest && !$bloglovin && !$tumblr && !$youtube && !$dribbble && !$soundcloud && !$vimeo && !$linkedin ) {
            return false;
        }

        return true;
    }
}

if ( ! function_exists( 'kayla_site_pagination' ) ) {
    /**
     * Site navigation
     */
    function kayla_site_pagination() {
        if ( get_the_posts_pagination() ) { ?>
        <div class="kayla-pagination"><?php
            $args = array(
                'prev_text' => '<span class="fa fa-angle-left"></span>',
                'next_text' => '<span class="fa fa-angle-right"></span>'
            );
            the_posts_pagination($args);
        ?>
        </div>
        <?php
        }
    }
}

/**
 * Theme widgets
 */
kayla_require_file( get_template_directory() . '/core/widgets/about-me.php' );
kayla_require_file( get_template_directory() . '/core/widgets/latest-posts.php' );
kayla_require_file( get_template_directory() . '/core/widgets/social-media-links.php' );

/**
 * Require theme hooks
 */
kayla_require_file( get_template_directory() . '/core/theme-hooks.php' );

/**
 * Theme customizer
 */
kayla_require_file( get_template_directory() . '/core/theme-customizer.php' );
