<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
add_action('wp_enqueue_scripts', 'pmthemes_enqueue_scripts');
function pmthemes_enqueue_scripts(){
    // Get Theme Options from Database
    $theme_options = pmthemes_theme_options();
    // Register and Enqueue Stylesheet
    wp_enqueue_style('pmthemes-stylesheet', get_stylesheet_uri());
    // Register Dashicons
    wp_enqueue_style( 'dashicons' );
    // Register and Enqueue FlexSlider JS and CSS if necessary
    if ((isset($theme_options['slider_activated_blog']) and $theme_options['slider_activated_blog'] == true)
        || (isset($theme_options['slider_activated_front_page']) and $theme_options['slider_activated_front_page'] == true)) :
        // FlexSlider CSS
        wp_enqueue_style('pmthemes-flexslider', PMTHEMES_URI . '/css/flexslider.css');
        // FlexSlider JS
        wp_enqueue_script('pmthemes-jquery-flexslider', PMTHEMES_URI . '/js/jquery.flexslider-min.js', array('jquery'), false, true);
        // Register and enqueue slider.js
        wp_enqueue_script('pmthemes-jquery-frontpage_slider', PMTHEMES_URI . '/js/slider.js', array('pmthemes-jquery-flexslider'), false, true);
    endif;
    // Register and enqueue navigation.js
    wp_enqueue_script('pmthemes-jquery-navigation', PMTHEMES_URI . '/js/navigation.js', array('jquery'), false, true);
    // Combine Menu Script
    if ((isset($theme_options['pmthemes_merge_menus']) and $theme_options['pmthemes_merge_menus'] == true)) :
        wp_enqueue_script('pmthemes-merge-menus', PMTHEMES_URI . '/js/merge-menus.js', array('jquery'), false, true);
    endif;
    // Register and Enqueue Google Font
    if ((isset($theme_options['activate_google_fonts']) and $theme_options['activate_google_fonts'] == true)
        and ($theme_options['available_fonts'] <> "default") ) :
        wp_enqueue_style('pmthemes-default-fonts', pmthemes_fonts_url(), array(), null);
    endif;
    // Register Lightbox CSS
    wp_enqueue_script('fancybox', get_template_directory_uri() . '/inc/lightbox/js/jquery.fancybox.min.js', array('jquery'), false, true);
    wp_enqueue_script('lightbox', get_template_directory_uri() . '/inc/lightbox/js/lightbox.js', array('fancybox'), false, true);
    wp_enqueue_style('lightbox-style', get_template_directory_uri() . '/inc/lightbox/css/jquery.fancybox.min.css');

}

// Load comment-reply.js if comment form is loaded and threaded comments activated
add_action( 'comment_form_before', 'pmthemes_enqueue_comment_reply' );
function pmthemes_enqueue_comment_reply() {
	if( get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
/*
* Retrieve Font URL to register default Google Fonts
*/
function pmthemes_fonts_url() {
		// Set Default Fonts
		$font_families = array('Raleway:400,700', 'Bitter'); 
		// Set Google Font Query Args
		$query_args = array(
			'family' => urlencode( implode( '|', $font_families ) ),
			'subset' => urlencode( 'latin,latin-ext' ),
		);
		// Create Fonts URL
		$fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
		return apply_filters( 'pmthemes_google_fonts_url', $fonts_url );
}
// Setup Function: Registers support for various WordPress features
add_action( 'after_setup_theme', 'pmthemes_setup' );
function pmthemes_setup() {
	// Set Content Width
	global $content_width;
	if ( ! isset( $content_width ) )
		$content_width = 860;
	// init Localization
	load_theme_textdomain('pmthemes', PMTHEMES_DIR . '/languages' );
	// Add Theme Support
    add_theme_support('align-wide');
	add_theme_support('post-thumbnails');
	add_theme_support('automatic-feed-links');
	add_theme_support('title-tag');
    
	// Editor Styles
	add_theme_support( 'editor-styles' );
	add_editor_style( 'css/editor-style.css' );

	// Add Custom Background
	add_theme_support('custom-background', array(
		'default-color' => 'e5e5e5',
		'default-attachment' => 'fixed',
    ));
	// Add Custom Header
	add_theme_support('custom-header', array(
		'header-text' => false,
		'width'	=> 1340,
		'height' => 250,
	    'default-repeat'         => '',
	    'default-position-x'     => '',
		'flex-width' => true,
		'flex-height' => true));
	// Enable support for WooCommerce
    if ( class_exists( 'WooCommerce' ) ):
	    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' );
    endif;
	// Enable support for Widget Plugin
	add_theme_support( 'pmthemes' );
	// Register Navigation Menus
	register_nav_menu( 'primary', __('Main Navigation', 'pmthemes') );
	register_nav_menu( 'secondary', __('Top Navigation', 'pmthemes') );
	register_nav_menu( 'sticky', __('Sticky Navigation', 'pmthemes') );
	register_nav_menu( 'footer', __('Footer Navigation', 'pmthemes') );
	// Register Social Icons Menu
	register_nav_menu( 'social', __('Social Icons', 'pmthemes') );
	// Register Add On Widgets
	register_widget('Pmthemes_Popular_Posts_Widget');
	register_widget('Pmthemes_Recent_Comments_Widget');
	register_widget('Pmthemes_Recent_Posts_Widget');
	register_widget('Pmthemes_Social_Icons_Widget');
	register_widget('Pmthemes_Tabbed_Content_Widget');

    if ( ! function_exists( 'wp_body_open' ) ) {
        function wp_body_open() {
            do_action( 'wp_body_open' );
        }
    }
}

// Add custom Image Sizes
add_action( 'after_setup_theme', 'pmthemes_add_image_sizes' );
function pmthemes_add_image_sizes() {
	// Add Custom Header Image Size
	add_image_size( 'custom_header_image', 1340, 250, true);
	// Add Featured Image Size
	add_image_size( 'featured_image', 220, 220, true);
	add_image_size( 'featured_image_posts', 800, 500, true);
	// Add Slider Image Size
	add_image_size( 'slider_image', 800, 400, true);
	// Add Frontpage Thumbnail Sizes
	add_image_size( 'category_posts_wide_thumb', 600, 240, true);
	add_image_size( 'category_posts_small_thumb', 150, 150, true);
	// Add Widget Post Thumbnail Size
	add_image_size( 'widget_post_thumb', 75, 75, true);
}

// Register Sidebars
add_action( 'widgets_init', 'pmthemes_register_sidebars' );
function pmthemes_register_sidebars() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'pmthemes' ),
        'id' => 'sidebar',
        'description' => __( 'Appears on posts and pages except front page and fullwidth template.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s clearfix">',
        'after_widget' => '</aside>',
        'before_title' => '<div class="widget-header"><h3 class="widgettitle">',
        'after_title' => '</h3></div>',
    ));
    register_sidebar( array(
        'name' => esc_html__( 'Sidebar Sub Column Left', 'pmthemes' ),
        'id' => 'sidebar2',
        'description' => esc_html__( 'Appears on left side under main sidebar.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s clearfix">',
        'after_widget' => '</aside>',
        'before_title' => '<div class="widget-header"><h4 class="widgettitle">',
        'after_title' => '</h4></div>',
    ));
    register_sidebar( array(
        'name' => esc_html__( 'Sidebar Sub Column Right', 'pmthemes' ),
        'id' => 'sidebar3',
        'description' => esc_html__( 'Appears on right side under main sidebar.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s clearfix">',
        'after_widget' => '</aside>',
        'before_title' => '<div class="widget-header"><h4 class="widgettitle">',
        'after_title' => '</h4></div>',
    ));
    register_sidebar( array(
        'name' => __( 'Header Content', 'pmthemes' ),
        'id' => 'head-content',
        'description' => __( 'Place your own content on the right side header. Ad block, search form, text, etc.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s clearfix">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widgettitle"><span>',
        'after_title' => '</span></h3>',
    ));
    register_sidebar( array(
        'name' => __( 'Magazine Front Page', 'pmthemes' ),
        'id' => 'frontpage-magazine',
        'description' => __( 'Appears on Magazine Front Page page template only. You can use the Home Posts widgets here.', 'pmthemes' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<div class="widget-header"><h3 class="widgettitle">',
        'after_title' => '</h3></div>',
    ));
    register_sidebar( array(
        'name' => __( 'Footer Widget 1', 'pmthemes' ),
        'id' => 'footer-widgets-1',
        'description' => __( 'Appears on footer widgets.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widgettitle"><span>',
        'after_title' => '</span></h3>'
    ));
    register_sidebar( array(
        'name' => __( 'Footer Widget 2', 'pmthemes' ),
        'id' => 'footer-widgets-2',
        'description' => __( 'Appears on footer widgets.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widgettitle"><span>',
        'after_title' => '</span></h3>'
    ));
    register_sidebar( array(
        'name' => __( 'Footer Widget 3', 'pmthemes' ),
        'id' => 'footer-widgets-3',
        'description' => __( 'Appears on footer widgets.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widgettitle"><span>',
        'after_title' => '</span></h3>'
    ));
    register_sidebar( array(
        'name' => __( 'Footer Widget 4', 'pmthemes' ),
        'id' => 'footer-widgets-4',
        'description' => __( 'Appears on footer widgets.', 'pmthemes' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widgettitle"><span>',
        'after_title' => '</span></h3>'
    ));
}

// Add title tag for older WordPress versions
if ( ! function_exists( '_wp_render_title_tag' ) ) :
	add_action( 'wp_head', 'pmthemes_wp_title' );
	function pmthemes_wp_title() { ?>	
		<title><?php wp_title( '|', true, 'right' ); ?></title>
<?php } endif;
// Add Default Menu Fallback Function
function pmthemes_default_menu() {
	echo '<ul id="nav-menu" class="nav-menu">'. wp_list_pages('title_li=&echo=0') .'</ul>';
}
// Get Featured Posts
function pmthemes_get_featured_content() {	return apply_filters( 'pmthemes_get_featured_content', false );
}
// Change Excerpt Length
add_filter('excerpt_length', 'pmthemes_excerpt_length');
function pmthemes_excerpt_length($length) {
    return 77;
}
// Slideshow Excerpt Length
function pmthemes_slideshow_excerpt_length($length) {
    return 15;
}
// Frontpage Category Excerpt Length
function pmthemes_frontpage_category_excerpt_length($length) {
    return 19;
}
// Post Boxed Excerpt Length
function pmthemes_postsbox_excerpt_length($length) {
    return 12;
}
// Post Boxed First Post Excerpt Length
function pmthemes_postsbox_first_excerpt_length($length) {
    return 45;
}
function pmthemes_related_posts_excerpt($length) {
$theme_options = pmthemes_theme_options();
    return $theme_options['related_excerpt_length'];
}
function pmthemes_archive_excerpts($length) {
    $theme_options = pmthemes_theme_options();
    return $theme_options['arch_excerpt_length'];
}

add_filter('excerpt_more', 'pmthemes_arch_excerpt_more');
// Remove Excerpt more if there's no excerpt
function pmthemes_arch_excerpt_more( $more ) {
    $theme_options = pmthemes_theme_options();
     if ($theme_options['arch_excerpt_length'] == 0 ) {
         return '';
     }
}

// Add Custom Body Classess
add_filter( 'body_class', 'pmthemes_body_classes' );
function pmthemes_body_classes( $classes ) {
	$theme_options = pmthemes_theme_options();
	if ( isset($theme_options['layout']) and $theme_options['layout'] == 'left-sidebar' ) :
		$classes[] = 'rlayout';
	endif;
    if( is_archive('template-parts/content-fullpost.php') ):
        $classes[] = 'fullpost';
    endif;
    if ( isset($theme_options['theme_style']) and $theme_options['theme_style'] == 'wide-head' ) :
        $classes[] = 'wide-header';
    endif;
    if ( isset($theme_options['nav_on_header']) and $theme_options['nav_on_header'] == 'main-menu-header' ) :
        $classes[] = 'navhdr';
    endif;
	if ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd2' ) :
		$classes[] = 'wd2';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd3' ) :
		$classes[] = 'wd3';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd4' ) :
		$classes[] = 'wd4';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd5' ) :
		$classes[] = 'wd5';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd6' ) :
		$classes[] = 'wd6';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd7' ) :
		$classes[] = 'wd7';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd8' ) :
		$classes[] = 'wd8';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd9' ) :
		$classes[] = 'wd9';
	elseif ( isset($theme_options['theme_width']) and $theme_options['theme_width'] == 'wd10' ) :
		$classes[] = 'wd10';
	endif;

    if ( ! function_exists( 'pmthemes_child_body_classes' ) ) :
        if ( isset($theme_options['topnav_down']) and $theme_options['topnav_down'] == false ) :
            $classes[] = 'natty';
        endif;
    endif;
    
    return $classes;
}

// Custom Template for comments and pingbacks.
if ( ! function_exists( 'pmthemes_list_comments' ) ):
function pmthemes_list_comments($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;
	if( $comment->comment_type == 'pingback' or $comment->comment_type == 'trackback' ) : ?>
		<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
			<p><?php _e( 'Pingback:', 'pmthemes' ); ?> <?php comment_author_link(); ?>
			<?php edit_comment_link( __( '(Edit)', 'pmthemes' ), '<span class="edit-link">', '</span>' ); ?>
			</p>
	<?php else : ?>
		<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
			<div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
				<div class="comment-author vcard">
					<?php echo get_avatar( $comment, 56 ); ?>
					<?php printf(__('<span class="fn">%s</span>', 'pmthemes'), get_comment_author_link()) ?>
				</div>
		<?php if ($comment->comment_approved == '0') : ?>
				<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'pmthemes' ); ?></p>
		<?php endif; ?>
				<div class="comment-meta commentmetadata">
					<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><?php printf(__('%1$s at %2$s', 'pmthemes'), get_comment_date(),  get_comment_time()) ?></a>
					<?php edit_comment_link(__('(Edit)', 'pmthemes'),'  ','') ?>
				</div>
				<div class="comment-content"><?php comment_text(); ?></div>
				<div class="reply">
					<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
				</div>
			</div>
	<?php endif; }
endif; 
if ( isset($theme_options['fb_meta_graph']) and $theme_options['fb_meta_graph'] == true ) :
add_opengraph_doctype(); 
insert_fb_in_head();
endif;
?>