<?php
# Define
define('AULIETTALITE_LIBS_URI', get_template_directory_uri() . '/libs/');
define('AULIETTALITE_CORE_PATH', get_template_directory() . '/core/');
define('AULIETTALITE_CORE_URI', get_template_directory_uri() . '/core/');
define('AULIETTALITE_CORE_CLASSES', AULIETTALITE_CORE_PATH . 'classes/');
define('AULIETTALITE_CORE_FUNCTIONS', AULIETTALITE_CORE_PATH . 'functions/');

# Set Content Width
if ( ! isset( $content_width ) ) { $content_width = 1530; }

# After setup theme
add_action('after_setup_theme', 'auliettalite_setup');
function auliettalite_setup()
{
    load_theme_textdomain('auliettalite', get_template_directory().'/languages');
    $defaults = array(
     'height'      => 155,
     'width'       => 600,
     'flex-height' => true,
     'flex-width'  => true
    );
    add_theme_support( 'custom-logo', $defaults );
	add_theme_support('automatic-feed-links');
	add_theme_support('post-thumbnails');
    add_theme_support('title-tag');
    add_theme_support('align-wide');
	register_nav_menus(array(
        'primary' => esc_html__('Main Menu', 'auliettalite'),
    ));
    register_nav_menus(array('footer-menu' => esc_html__('Footer Menu', 'auliettalite')));
    add_theme_support( 'custom-background' );
}


# Google Fonts
add_action( 'wp_enqueue_scripts', 'auliettalite_enqueue_googlefonts' );
function auliettalite_enqueue_googlefonts()
{
    $fonts_url = '';
    $EB_Garamond = _x( 'on', 'EB Garamond: on or off', 'auliettalite' );
    $Quicksand = _x( 'on', 'Quicksand: on or off', 'auliettalite' );
    if( 'off' != $EB_Garamond || 'off' != $Quicksand )
    {
        $font_families = array();
        if ( 'off' !== $Quicksand ) $font_families[] = 'Quicksand:400,400i,600';
        if ( 'off' !== $EB_Garamond ) $font_families[] = 'EB Garamond:500i';
        $query_args = array('family' => urlencode(implode('|', $font_families)), 'subset' => urlencode('latin,latin-ext'));
        $fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
    }

    wp_enqueue_style('auliettalite-googlefonts', esc_url_raw($fonts_url), array(), null);
}

add_action( 'enqueue_block_editor_assets', 'auliettalite_enqueue_googlefonts' );



# Enqueue Scripts
add_action( 'wp_enqueue_scripts', 'auliettalite_load_scripts' );
function auliettalite_load_scripts()
{
    # CSS
    wp_enqueue_style('bootstrap', AULIETTALITE_LIBS_URI . 'bootstrap/bootstrap.min.css');
    wp_enqueue_style('azura-font-awesome', AULIETTALITE_LIBS_URI . 'font-awesome/css/all.min.css');
    wp_enqueue_style('chosen', AULIETTALITE_LIBS_URI . 'chosen/chosen.css');
    wp_enqueue_style('auliettalite-style', get_template_directory_uri() . '/style.css');
    wp_enqueue_style('auliettalite-theme-style', get_template_directory_uri() . '/assets/css/theme.css', '', rand(0,9999) );

    # JS
    wp_enqueue_script('jquery');
	wp_enqueue_script('fitvids', AULIETTALITE_LIBS_URI . 'fitvids/fitvids.js', array(), false, true);
    wp_enqueue_script('chosen', AULIETTALITE_LIBS_URI . 'chosen/chosen.js', array(), false, true);
    wp_enqueue_script('imagesloaded');
    wp_enqueue_script('auliettalite-scripts', get_template_directory_uri() . '/assets/js/theme-scripts.js', array(), false, true);
    
    if ( is_singular() && get_option('thread_comments') ) {
        wp_enqueue_script('comment-reply');
    }
}

# Register Sidebar
add_action( 'widgets_init', 'auliettalite_widgets_init' );
function auliettalite_widgets_init() {
    register_sidebar(array(
		'name'            => esc_html__('Sidebar', 'auliettalite'),
		'id'              => 'sidebar',
		'before_widget'   => '<div id="%1$s" class="widget %2$s">',
		'after_widget'    => '</div>',
		'before_title'    => '<h3 class="widget-title">',
		'after_title'     => '</h3>'
	));
}

# Check file exists
function auliettalite_require_file( $path ) {
    if ( file_exists($path) ) {
        require $path;
    }
}

# Require file
auliettalite_require_file( get_template_directory() . '/core/init.php' );


# Comment Layout
function auliettalite_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="header-comment-body">
                <h4 class="author-name"><?php echo get_comment_author_link(); ?></h4>
                <div class="date-comment">
                    <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
                    <?php printf( esc_html__('%1$s at %2$s', 'auliettalite'), get_comment_date(),  get_comment_time() ); ?></a>
                </div>
                <div class="reply">
                    <?php edit_comment_link( esc_html__( '(Edit)', 'auliettalite' ), '  ', '' );?>
                    <?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.', 'auliettalite' ); ?></em>
                <br />
            <?php endif; ?>
            <div class="comment-text"><?php comment_text(); ?></div>
        </div>  
    <?php if ( 'div' != $args['style'] ) : ?>
    </div>
    <?php endif; ?>
<?php
}

#Pagination
function auliettalite_pagination() {
    if ( get_the_posts_pagination() ) : ?>
    <div class="auliettalite-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
    endif;
}

/**
 * Count Post Visits
 */
add_action( 'wp_head', 'auliettalite_count_post_visits' );
function auliettalite_count_post_visits()
{
    if( is_single() )
    {
        global $post;
        $views = get_post_meta( $post->ID, 'auliettalite_post_viewed', true );
        if ( $views == '' ) {
            update_post_meta( $post->ID, 'auliettalite_post_viewed', '1' ); 
        } else {
            $views_no = intval( $views );
            update_post_meta( $post->ID, 'auliettalite_post_viewed', ++$views_no );
        }
    }
}

// Get Post View
function auliettalite_get_post_view(){
    global $post;
    $view = get_post_meta( $post->ID, 'auliettalite_post_viewed',true);
    $view = (int)$view;
    if ($view > 1 ) {
        $view = $view . ' ' . esc_html__( 'views', 'auliettalite' );
    } elseif ( $view == 1) {
         $view = $view . ' ' . esc_html__( 'views', 'auliettalite' );
    } else {
        $view = '0'. ' ' . esc_html__( 'views', 'auliettalite' );
    }

    return $view;
}


/* Convert hexdec color string to rgb(a) string */ 
function auliettalite_hex2rgb($color) {
 
    $default = 'rgb(0,0,0)';
 
    //Return default if no color provided
    if(empty($color))
          return $default; 
 
    //Sanitize $color if "#" is provided 
        if ($color[0] == '#' ) {
            $color = substr( $color, 1 );
        }
 
        //Check if color has 6 or 3 characters and get values
        if (strlen($color) == 6) {
                $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
        } elseif ( strlen( $color ) == 3 ) {
                $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
        } else {
                return $default;
        }
 
        //Convert hexadec to rgb
        $rgb =  array_map('hexdec', $hex);
 
        $output = implode(",",$rgb);
 
        //Return rgb(a) color string
        return $output;
}


// Banner Page
function auliettalite_page_banner( $title = null, $image = null)
{    
    ?>
    <?php if ($title || $image) { ?>
    <div class="banner-page">
        <?php if ($image) { ?>
        <div class="bg-banner-image" style="background-image: url(<?php echo esc_url( $image ); ?>)"></div>
        <?php } ?>
        <?php if ($title) { ?>
            <div class="inner-banner">
                <div class="container">
                    <h1 class="entry-title"><?php echo wp_kses_post( $title ); ?></h1>
                </div>
             </div>
        <?php } ?>
    </div>
    <?php } ?>
    <?php
}


# Include the TGM_Plugin_Activation class
add_action('tgmpa_register', 'auliettalite_register_required_plugins');
function auliettalite_register_required_plugins()
{
	$plugins = array(
        array(
            'name'   => esc_html__('Kirki', 'auliettalite'),
            'slug'   => 'kirki',
            'require' => true,
        ),
        array(
            'name'   => esc_html__('Contact Form 7', 'auliettalite'),
            'slug'   => 'contact-form-7'
        ),
        array(
			'name'   => esc_html__('MailChimp for WordPress', 'auliettalite'),
			'slug'   => 'mailchimp-for-wp'
		),
        array(
            'name'   => esc_html__('Smash Balloon Instagram Feed', 'auliettalite'),
            'slug'   => 'instagram-feed'
        ),
	);

	$config = array(
		'id'           => 'auliettalite',
		'default_path' => '',
		'menu'         => 'tgmpa-install-plugins',
		'parent_slug'  => 'themes.php',
		'capability'   => 'edit_theme_options',
		'has_notices'  => true,
		'dismissable'  => true,
		'dismiss_msg'  => '',
		'is_automatic' => true,
		'message'      => ''
	);
	tgmpa($plugins, $config);
}