<?php
/***
 * Template Tags
 *
 * This file contains several template functions which are used to print out specific HTML markup
 * in the theme. You can override these template functions within your child theme.
 *
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
// Display Site Title
add_action( 'pmthemes_site_title', 'pmthemes_display_site_title' );
function pmthemes_display_site_title() { ?>
	<a href="<?php echo esc_url(home_url('/')); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
		<h1 class="site-title"><?php bloginfo('name'); ?></h1>
	</a>
	<?php }

// Display Custom Header
if ( ! function_exists( 'pmthemes_display_custom_header' ) ):
	function pmthemes_display_custom_header() {
	if( get_header_image() ) : ?>
			<style type="text/css">#header-wrap{background: url(<?php echo get_header_image(); ?>);}</style>
	<?php endif; }
endif;

// Display Postmeta Data
if ( ! function_exists( 'pmthemes_display_postmeta' ) ):
	function pmthemes_display_postmeta() { ?>
	<?php 
	$theme_options = pmthemes_theme_options();
	if ( isset($theme_options['show_post_dates']) and $theme_options['show_post_dates'] == true ) : ?>
    <div class="postmeta">
		<span class="meta-date">
		<?php printf(__('<a href="%1$s" title="%2$s" rel="bookmark"><time datetime="%3$s">%4$s</time></a>', 'pmthemes'), 
				esc_url( get_permalink() ),
				esc_attr( get_the_time() ),
				esc_attr( get_the_date( 'c' ) ),
				esc_html( get_the_date() )
			);
		?>
		</span>
    </div>
	<?php endif;
    if ( isset($theme_options['show_post_author']) and $theme_options['show_post_author'] == true ) : ?>
		<span class="meta-author">
		<?php printf(__('<a href="%1$s" title="%2$s" rel="author">%3$s</a>', 'pmthemes'), 
				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
				esc_attr( sprintf( __( 'View all posts by %s', 'pmthemes' ), get_the_author() ) ),
				get_the_author()
			);
		?>
		</span>
	<?php endif;
    if ( isset($theme_options['show_post_category']) and $theme_options['show_post_category'] == true ) : ?>
		<span class="meta-category">
			<?php printf(__('%1$s', 'pmthemes'), get_the_category_list(', ')); ?>
		</span>
 	<?php endif; ?>
	<?php
		edit_post_link(__( 'Edit Post', 'pmthemes' ));
	}
endif;

// Display Post Thumbnail on Archive Pages
function pmthemes_display_thumbnail_index() {
    $theme_options = pmthemes_theme_options();
    if ( isset($theme_options['post_thumbnails_index']) and $theme_options['post_thumbnails_index'] == true ) : ?>
        <a href="<?php esc_url(the_permalink()) ?>" rel="bookmark">
            <?php
            if ( $theme_options['pmth_arc_img_size'] == 'pmth-img-large' ) {
                $featimg = 'featured_image_posts';
            } else {
                $featimg = 'featured_image';
            }
            the_post_thumbnail($featimg, array('class' => ''. $theme_options['pmth_arc_align_img'] .' grow'));
            ?>
        </a>
    <?php endif;
}

// Display Post Thumbnail on single posts
function pmthemes_display_thumbnail_single() {
	$theme_options = pmthemes_theme_options();
	if ( isset($theme_options['post_thumbnails_single']) and $theme_options['post_thumbnails_single'] == true ) :
		the_post_thumbnail('featured_image_posts', array('class' => 'aligncenter'));
	endif;
}

// Display Postinfo Data and Comment Info on Archive Pages
if ( ! function_exists( 'pmthemes_display_postinfo' ) ):
function pmthemes_display_postinfo() {
  	$theme_options = pmthemes_theme_options();
    $tags = get_the_tag_list('<span class="meta-tags"> ',', ','</span>');
    $comments = $theme_options['hide_post_comment'];
	if (( $comments == false ) or ( ! empty ( $tags ) )) :
        echo '<div class="postinfo clearfix">';
            if ( $comments == false ) :
                echo '<span class="meta-comments">';
                    comments_popup_link( __('Leave a comment', 'pmthemes'),__('One comment','pmthemes'),__('% comments','pmthemes') );
                echo '</span>';
            endif;
            if ( ! empty ( $tags ) ) :
                echo $tags;
            endif;
        echo '</div>';
    endif;
}
endif;

// Display Related Posts
function pmthemes_related_posts() {
	$theme_options = pmthemes_theme_options();
	if ( isset($theme_options['related_posts_single']) and $theme_options['related_posts_single'] == true ) :
			get_template_part('inc/related');
	endif;
}

// Display Related Posts
function pmthemes_excerpt_text() {
    $theme_options = pmthemes_theme_options();
    $excerpt_text = $theme_options['excerpt_text'];
    if ( isset($excerpt_text) and $excerpt_text !== '' ) :
            printf(__('<a href="%1$s" class="more-link">%2$s</a>', 'pmthemes'),
                esc_url( get_permalink() ),
                $excerpt_text
        );
    endif;
}

// Display Content Pagination
if ( ! function_exists( 'pmthemes_display_pagination' ) ):
	function pmthemes_display_pagination() { 
		global $wp_query;
		$big = 999999999; // need an unlikely integer
		 $paginate_links = paginate_links( array(
				'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
				'format' => '?paged=%#%',				
				'current' => max( 1, get_query_var( 'paged' ) ),
				'total' => $wp_query->max_num_pages,
				'next_text' => '&raquo;',
				'prev_text' => '&laquo',
				'add_args' => false
			) );
		// Display the pagination if more than one page is found
		if ( $paginate_links ) : ?>
			<div class="post-pagination clearfix">
				<?php echo $paginate_links; ?>
			</div>
		<?php
		endif;
	}
endif;

// Display Footer Text
add_action( 'pmthemes_footer_text', 'pmthemes_display_footer_text' );
function pmthemes_display_footer_text() { 
	$theme_options = pmthemes_theme_options();
    $theme_def = pmthemes_default_options();
	if ( isset( $theme_options['footer_text'] ) and $theme_options['footer_text'] <> $theme_def['footer_text'] ) :
		echo do_shortcode(wp_kses_post($theme_options['footer_text']));	
		else: ?>
	<?php endif;
	// Display Credit Link only if activated
	if ( isset($theme_options['credit_link']) and $theme_options['credit_link'] == true ) :
		echo ' <span class="credit-link">';
		echo 'Theme by <a href="'. PMTHEMES_SITE_URL .'" target="_blank" title="'. PMTHEMES_SITE_NAME .' WordPress Theme">'. PMTHEMES_SITE_NAME .'</a>';
		echo '</span>';	
	endif;
}

// Header Code
add_action( 'wp_head','pmthemes_headercode', 100 );
function pmthemes_headercode(){
	$theme_options = pmthemes_theme_options();
    $theme_def = pmthemes_default_options();
	if ( isset( $theme_options['header_code'] ) and $theme_options['header_code'] <> $theme_def['track_code'] ) :
		echo do_shortcode(($theme_options['header_code']));	
	endif; 
}

// Footer Analytics
add_action( 'wp_footer','pmthemes_analytics', 100 );
function pmthemes_analytics(){
	$theme_options = pmthemes_theme_options();
    $theme_def = pmthemes_default_options();
	if ( isset( $theme_options['track_code'] ) and $theme_options['track_code'] <> $theme_def['track_code'] ) :
		echo do_shortcode(($theme_options['track_code']));	
	endif; 
}

// Scroll to Top
add_action( 'pmthemes_scroll_top', 'pmthemes_display_scroll_top' );
function pmthemes_display_scroll_top() {
    $theme_options = pmthemes_theme_options();
    $theme_def = pmthemes_default_options();
    if ( isset( $theme_options['link_to_top'] ) and $theme_options['link_to_top'] == $theme_def['link_to_top'] ) :
        echo '<a href="#" class="scrollToTop"></a>';
    endif;
}

// Social Icons
function pmthemes_display_social_icons() {
    echo '<div id="header-social-icons" class="social-icons-wrap clearfix">';
	// Check if there is a social_icons menu
	if( has_nav_menu( 'social' ) ) :
		// Display Social Icons Menu
		wp_nav_menu( array(
			'theme_location' => 'social',
			'container' => false,
			'menu_class' => 'social-icons-menu',
			'echo' => true,
			'fallback_cb' => '',
			'before' => '',
			'after' => '',
			'link_before' => '<span class="screen-reader-text">',
			'link_after' => '</span>',
			'depth' => 1
			)
		);
	else: ?>
		<p class="social-icons-hint">
			<?php _e('Please go to WP-Admin-> Appearance-> Menus and create a new custom menu with custom links to all your social networks. Then click on "Manage Locations" tab and assign your created menu to the "Social Icons" theme location.', 'pmthemes'); ?>
		</p>
	<?php endif;
    echo '</div>';
}

//Add the Open Graph in the Language Attributes
$theme_options = pmthemes_theme_options();
if ( isset($theme_options['fb_meta_graph']) and $theme_options['fb_meta_graph'] == true ) :
function add_opengraph_doctype( $output ) {
		return $output . ' prefix="og: http://ogp.me/ns#"';
	}
add_filter('language_attributes', 'add_opengraph_doctype');
//Lets add Open Graph Meta Info
function insert_fb_in_head() {
	global $post;
	if( !is_object($post) || get_class($post) != 'WP_Post' ) {
		return '';
	}
		echo "<!-- Open Graph protocol meta data -->\n";
		$theme_options = pmthemes_theme_options();
		echo '<meta property="fb:app_id" content="' . $theme_options['fb_app_id'] . '"/>' . "\n";
		if ( is_front_page() ) {
			$pmnsogp_url = home_url();
		} else {
			$pmnsogp_url = 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
		}
		echo '<meta property="og:url" content="' . esc_url( user_trailingslashit( trailingslashit( apply_filters( 'pmnsogp_url', $pmnsogp_url ) ) ) ) . '"/>' . "\n";
		if ( is_home() || is_front_page() ) {
			$pmnsogp_title = get_bloginfo( 'name' );
		} else {
			$pmnsogp_title = get_the_title();
		}
		echo '<meta property="og:title" content="' . esc_attr( apply_filters( 'pmnsogp_title', $pmnsogp_title ) ) . '"/>' . "\n";
		echo '<meta property="og:site_name" content="' . get_bloginfo( 'name' ) . '"/>' . "\n";
		if ( is_singular() ) {
			if ( has_excerpt( $post->ID ) ) {
				$pmnsogp_description = strip_tags( get_the_excerpt() );
			} else {
				$pmnsogp_description = str_replace( "\r\n", ' ' , substr( strip_tags( strip_shortcodes( $post->post_content ) ), 0, 234 ) );
			}
		} else {
			$pmnsogp_description = get_bloginfo( 'description' );
		}
		echo '<meta property="og:description" content="' . esc_attr( apply_filters( 'pmnsogp_description', $pmnsogp_description ) ) . '"/>' . "\n";
		if ( is_single() ) {
			$pmnsogp_type = 'article';
		} else {
			$pmnsogp_type = 'website';
		}
		echo '<meta property="og:type" content="' . esc_attr( apply_filters( 'pmnspogp_type', $pmnsogp_type ) ) . '"/>' . "\n";
		if(!has_post_thumbnail( $post->ID )) {
		$default_image= $theme_options['default_fb_image'];
		echo '<meta property="og:image" content="' . $default_image . '"/>' . "\n";
		}
		else{
		$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
		echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>' . "\n";
		}
		
		echo '<meta property="og:locale" content="' . strtolower( esc_attr( get_locale() ) ) . '"/>' . "\n";
		echo "<!-- Open Graph protocol meta data -->\n";
	}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
endif;

add_action( 'pmthemes_nav_extra', 'pmthemes_nav_sticky' );
function pmthemes_nav_sticky() {
    if ( has_nav_menu( 'sticky' ) ) : ?>
        <div class="stickynav">
            <nav id="site-navigation" class="main-navigation ducksoy" role="navigation">
                <?php wp_nav_menu(
                    array(
                        'theme_location' => 'sticky',
                        'container' => false,
                        'menu_class' => 'nav-menu',
                        'echo' => true,
                        'fallback_cb' => 'pmthemes_default_menu'
                    )
                );
                ?>
                <?php $theme_options = pmthemes_theme_options();
                if ( isset($theme_options['search_icon']) and $theme_options['search_icon'] == true ) : ?>
                    <div class="search-main-menu search-top">
                        <span class="search-icon"></span>
                        <div class="search-form-top">
                            <?php get_search_form(); ?>
                        </div>
                    </div>
                <?php endif; ?>
            </nav>
        </div>
    <?php else: ?>
        <p class="social-icons-hint">
            <?php _e('Sticky menu not yet set up. Please go to WP-Admin-> Appearance-> Menus and create a new custom menu. Then click on "Manage Locations" tab and assign your created menu to the "Sticky Navigation" theme location. Disable this by going to Appearance-> Customize-> Theme Settings-> Header Content.', 'pmthemes'); ?>
        </p>
    <?php endif;
}
/*** Disable the emoji's */
$theme_options = pmthemes_theme_options();
if ( isset($theme_options['wp_emoji']) and $theme_options['wp_emoji'] == true ) :
    function disable_emojis() {
        remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
        remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
        remove_action( 'wp_print_styles', 'print_emoji_styles' );
        remove_action( 'admin_print_styles', 'print_emoji_styles' );
        remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
        remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
        remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
        add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
    }
    add_action( 'init', 'disable_emojis' );
    /**
     * Filter function used to remove the tinymce emoji plugin.
     *
     * @param    array  $plugins
     * @return   array             Difference betwen the two arrays
     */
    function disable_emojis_tinymce( $plugins ) {
        if ( is_array( $plugins ) ) {
            return array_diff( $plugins, array( 'wpemoji' ) );
        } else {
            return array();
        }
    }
endif;
?>