<?php

if (!class_exists('Breeza_Blog')) {

    class Breeza_Blog
    {

        public function get_archive_meta() {
            $archive_author = breeza()->get_theme_opt( 'archive_author', true );
            $archive_date = breeza()->get_theme_opt( 'archive_date', true );
            if($archive_author || $archive_category || $archive_date) : ?>
                <div class="pxl-item--meta">
                    <?php if($archive_date) : ?>
                        <span class="pxl-item--date"><?php echo get_the_date(); ?></span>
                    <?php endif; ?>
                    <?php if($archive_author) : ?>
                        <span class="pxl-item--author"><?php echo esc_html__('by', 'breeza'); ?>&nbsp;<?php the_author_posts_link(); ?></span>
                    <?php endif; ?>
                </div>
            <?php endif; 
        }

        public function get_excerpt(){
            $archive_excerpt_length = breeza()->get_theme_opt('archive_excerpt_length', '50');
            $breeza_the_excerpt = get_the_excerpt();
            if(!empty($breeza_the_excerpt)) {
                echo wp_trim_words( $breeza_the_excerpt, $archive_excerpt_length, $more = null );
            } else {
                echo wp_kses_post($this->get_excerpt_more( $archive_excerpt_length ));
            }
        }

        public function get_excerpt_more( $post = null ) {
            $archive_excerpt_length = breeza()->get_theme_opt('archive_excerpt_length', '50');
            $post = get_post( $post );

            if ( empty( $post ) || 0 >= $archive_excerpt_length ) {
                return '';
            }

            if ( post_password_required( $post ) ) {
                return esc_html__( 'Post password required.', 'breeza' );
            }

            $content = apply_filters( 'the_content', strip_shortcodes( $post->post_content ) );
            $content = str_replace( ']]>', ']]&gt;', $content );

            $excerpt_more = apply_filters( 'breeza_excerpt_more', '&hellip;' );
            $excerpt      = wp_trim_words( $content, $archive_excerpt_length, $excerpt_more );

            return $excerpt;
        }

        public function get_post_metas(){            
            $post_author = breeza()->get_theme_opt( 'post_author', true );
            $post_date = breeza()->get_theme_opt( 'post_date', true );
            $post_comment = breeza()->get_theme_opt( 'post_comment', true );
            $post_view = breeza()->get_theme_opt( 'post_view', true );
            $postID = get_the_ID();
            $author = get_user_by('ID', get_post_field('post_author', $postID));
            if($post_author || $post_date || $post_comment) : ?>
                <div class="pxl-item--meta">
                    <?php if($post_author) : ?>                   
                        <span class="pxl-item--author">
                            <?php 
                            $avatar_url = get_avatar_url($author->ID);
                            $author_name = $author->display_name; 
                            if ($author || !empty($user_position)) {                                
                                echo '<img src="' . esc_url($avatar_url) . '" alt="' . esc_attr($author_name) . '" />';                                
                            } ?> 
                            <?php echo esc_html__('By', 'breeza'); ?>&nbsp;<?php the_author_posts_link(); ?></span>
                        <?php endif; ?>
                        <?php if($post_date) : ?>
                            <span class="pxl-item--date">
                                <i class="fas fa-calendar-alt"></i>
                                <?php echo get_the_date(); ?></span>
                            <?php endif; ?>      
                            <?php if($post_comment) : ?>
                                <span class="pxl-item--comments">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="18" height="19" viewBox="0 0 18 19" fill="none">
                                        <path d="M17.991 2.70625C17.991 1.71625 17.19 0.90625 16.2 0.90625H1.8C0.81 0.90625 0 1.71625 0 2.70625V13.5062C0 14.4962 0.81 15.3062 1.8 15.3062H14.4L18 18.9062L17.991 2.70625ZM13.5 11.7062H4.5C4.005 11.7062 3.6 11.3012 3.6 10.8062C3.6 10.3112 4.005 9.90625 4.5 9.90625H13.5C13.995 9.90625 14.4 10.3112 14.4 10.8062C14.4 11.3012 13.995 11.7062 13.5 11.7062ZM13.5 9.00625H4.5C4.005 9.00625 3.6 8.60125 3.6 8.10625C3.6 7.61125 4.005 7.20625 4.5 7.20625H13.5C13.995 7.20625 14.4 7.61125 14.4 8.10625C14.4 8.60125 13.995 9.00625 13.5 9.00625ZM13.5 6.30625H4.5C4.005 6.30625 3.6 5.90125 3.6 5.40625C3.6 4.91125 4.005 4.50625 4.5 4.50625H13.5C13.995 4.50625 14.4 4.91125 14.4 5.40625C14.4 5.90125 13.995 6.30625 13.5 6.30625Z" fill="#2CB770"/>
                                    </svg>
                                    <?php echo get_comments_number(); ?></span>
                                <?php endif; ?>   
                                <?php if($post_view) : ?>
                                    <span class="pxl-item--comments">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="21" height="15" viewBox="0 0 21 15" fill="none">
                                          <path d="M20.9396 7.62275C20.909 7.55363 20.1679 5.9095 18.5202 4.26187C16.3249 2.0665 13.552 0.90625 10.5 0.90625C7.44799 0.90625 4.67512 2.0665 2.47974 4.26187C0.832117 5.9095 0.0874916 7.55625 0.0603666 7.62275C0.0205656 7.71227 0 7.80915 0 7.90712C0 8.0051 0.0205656 8.10198 0.0603666 8.1915C0.0909916 8.26062 0.832117 9.90388 2.47974 11.5515C4.67512 13.746 7.44799 14.9062 10.5 14.9062C13.552 14.9062 16.3249 13.746 18.5202 11.5515C20.1679 9.90388 20.909 8.26062 20.9396 8.1915C20.9794 8.10198 21 8.0051 21 7.90712C21 7.80915 20.9794 7.71227 20.9396 7.62275ZM10.5 11.4062C9.80776 11.4062 9.13107 11.201 8.5555 10.8164C7.97992 10.4318 7.53132 9.88518 7.26641 9.24564C7.00151 8.6061 6.9322 7.90237 7.06724 7.22343C7.20229 6.5445 7.53563 5.92086 8.02512 5.43138C8.5146 4.94189 9.13824 4.60855 9.81718 4.4735C10.4961 4.33845 11.1998 4.40776 11.8394 4.67267C12.4789 4.93758 13.0256 5.38618 13.4101 5.96175C13.7947 6.53733 14 7.21402 14 7.90625C14 8.83451 13.6312 9.72475 12.9749 10.3811C12.3185 11.0375 11.4283 11.4062 10.5 11.4062Z" fill="#2CB770"/>
                                      </svg>
                                      <?php
                                      $postid = get_the_id();
                                      $countkey = 'post_views_count';
                                      $postviews = get_post_meta($postid, $countkey, true);
                                      echo esc_attr($postviews); ?>
                                  </span>
                              <?php endif; ?>                              
                          </div>
                      <?php endif; 
                  }

                  public function breeza_set_post_views( $postID ) {
                    $countKey = 'post_views_count';
                    $count    = get_post_meta( $postID, $countKey, true );
                    if ( $count == '' ) {
                        $count = 0;
                        delete_post_meta( $postID, $countKey );
                        add_post_meta( $postID, $countKey, '0' );
                    } else {
                        $count ++;
                        update_post_meta( $postID, $countKey, $count );
                    }
                }

                public function get_tagged_in(){
                    $tags_list = get_the_tag_list( '<label class="label">'.esc_attr__('Tags:', 'breeza'). '</label>', ' ' );
                    if ( $tags_list )
                    {
                        echo '<div class="pxl--tags">';
                        printf('%2$s', '', $tags_list);
                        echo '</div>';
                    }
                }

                public function get_socials_share() {
                    $img_url = '';
                    if (has_post_thumbnail(get_the_ID()) && wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false)) {
                        $img_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false);
                    }
                    $social_facebook = breeza()->get_theme_opt( 'social_facebook', true );
                    $social_twitter = breeza()->get_theme_opt( 'social_twitter', true );
                    $social_pinterest = breeza()->get_theme_opt( 'social_pinterest', true );
                    $social_linkedin = breeza()->get_theme_opt( 'social_linkedin', true );
                    $social_stumbleupon = breeza()->get_theme_opt( 'social_stumbleupon', true );
                    ?>
                    <div class="pxl--social">
                        <h8>Share:</h8>
                        <?php if($social_facebook) : ?>
                            <a class="fb-social" title="<?php echo esc_attr__('Facebook', 'breeza'); ?>" target="_blank" href="https://www.facebook.com/sharer.php?u=<?php echo urlencode(get_the_permalink()); ?>">
                                <span><?php echo esc_attr__('Facebook', 'breeza'); ?></span>
                            </a>
                        <?php endif; ?>
                        <?php if($social_twitter) : ?>
                            <a class="tw-social" title="<?php echo esc_attr__('Twitter', 'breeza'); ?>" target="_blank" href="https://twitter.com/intent/tweet?url=<?php echo urlencode(get_the_permalink()); ?>&text=<?php echo urlencode(get_the_title()); ?>">
                                <span><?php echo esc_attr__('Twitter', 'breeza'); ?></span>
                            </a>
                        <?php endif; ?>
                        <?php if($social_pinterest) : ?>
                            <a class="pin-social" title="<?php echo esc_attr__('Pinterest', 'breeza'); ?>" target="_blank" href="https://www.pinterest.com/pin/create/button/?url=<?php echo urlencode(get_the_permalink()); ?>&media=<?php echo esc_url($img_url[0]); ?>&description=<?php echo urlencode(get_the_title()); ?>">
                                <span><?php echo esc_attr__('Pinterest', 'breeza'); ?></span>
                            </a>
                        <?php endif; ?>
                        <?php if($social_linkedin) : ?>
                            <a class="lin-social" title="<?php echo esc_attr__('LinkedIn', 'breeza'); ?>" target="_blank" href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo urlencode(get_the_permalink()); ?>&title=<?php echo urlencode(get_the_title()); ?>">
                                <span><?php echo esc_attr__('LinkedIn', 'breeza'); ?></span>
                            </a>
                        <?php endif; ?>
                        <?php if($social_stumbleupon) : ?>
                            <a class="lin-social" title="<?php echo esc_attr__('Stumbleupon', 'breeza'); ?>" target="_blank" href="https://www.stumbleupon.com/submit?url=<?php echo urlencode(get_the_permalink()); ?>&title=<?php echo urlencode(get_the_title()); ?>">
                                <span><?php echo esc_attr__('Stumbleupon', 'breeza'); ?></span>
                            </a>
                        <?php endif; ?>
                    </div>
                    <?php
                }

                public function get_socials_share_icon() {
                    $img_url = '';
                    if (has_post_thumbnail(get_the_ID()) && wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false)) {
                        $img_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false);
                    }
                    $social_facebook = breeza()->get_theme_opt( 'social_facebook', true );
                    $social_twitter = breeza()->get_theme_opt( 'social_twitter', true );
                    $social_pinterest = breeza()->get_theme_opt( 'social_pinterest', true );
                    $social_linkedin = breeza()->get_theme_opt( 'social_linkedin', true );
                    ?>
                    <div class="pxl-social">
                        <?php if($social_facebook) : ?>
                            <a class="fb-social" title="<?php echo esc_attr__('Facebook', 'breeza'); ?>" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(get_the_permalink()); ?>">
                                <i class="caseicon-facebook"></i>
                            </a>
                        <?php endif; ?>
                        <?php if($social_twitter) : ?>
                            <a class="tw-social" title="<?php echo esc_attr__('Twitter', 'breeza'); ?>" target="_blank" href="https://twitter.com/intent/tweet?url=<?php echo urlencode(get_the_permalink()); ?>&text=<?php echo urlencode(get_the_title()); ?>">
                                <i class="caseicon-twitter"></i>
                            </a>
                        <?php endif; ?>
                        <?php if($social_pinterest) : ?>
                            <a class="pin-social" title="<?php echo esc_attr__('Pinterest', 'breeza'); ?>" target="_blank" href="https://www.pinterest.com/pin/create/button/?url=<?php echo urlencode(get_the_permalink()); ?>&media=<?php echo esc_url($img_url[0]); ?>&description=<?php echo urlencode(get_the_title()); ?>">
                                <i class="caseicon-pinterest"></i>
                            </a>
                        <?php endif; ?>
                        <?php if($social_linkedin) : ?>
                            <a class="lin-social" title="<?php echo esc_attr__('LinkedIn', 'breeza'); ?>" target="_blank" href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo urlencode(get_the_permalink()); ?>&title=<?php echo urlencode(get_the_title()); ?>">
                                <i class="caseicon-linkedin"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                    <?php
                }

                public function get_socials_share_portfolio() { 
                    $img_url = '';
                    if (has_post_thumbnail(get_the_ID()) && wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false)) {
                        $img_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false);
                    }
                    ?>
                    <div class="pxl--social">
                        <a class="fb-social" title="<?php echo esc_attr__('Facebook', 'breeza'); ?>" target="_blank" href="http://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>"><i class="caseicon-facebook"></i></a>
                        <a class="tw-social" title="<?php echo esc_attr__('Twitter', 'breeza'); ?>" target="_blank" href="https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>%20"><i class="caseicon-twitter"></i></a>
                        <a class="pin-social" title="<?php echo esc_attr__('Pinterest', 'breeza'); ?>" target="_blank" href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php echo esc_url($img_url[0]); ?>&description=<?php the_title(); ?>%20"><i class="caseicon-pinterest"></i></a>
                        <a class="lin-social" title="<?php echo esc_attr__('LinkedIn', 'breeza'); ?>" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>%20"><i class="caseicon-linkedin"></i></a>
                    </div>
                    <?php
                }

                public function get_post_title(){
                    $post_title_on = breeza()->get_theme_opt('post_title_on','0');
                    if( $post_title_on == '0' ) return;
                    ?>
                    <h2 class="post-title">
                        <a href="<?php echo esc_url( get_permalink()); ?>" title="<?php the_title_attribute(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
                    <?php 
                }

                public function get_post_nav() {
                    global $post;
                    $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
                    $next     = get_adjacent_post( false, '', false );

                    if ( ! $next && ! $previous )
                        return;
                    ?>
                    <?php
                    $next_post = get_next_post();
                    $previous_post = get_previous_post();

                    if( !empty($next_post) || !empty($previous_post) ) { 
                        ?>
                        <div class="pxl-post--navigation">
                            <div class="pxl--items">
                                <div class="pxl--item pxl--item-prev">
                                    <?php if ( is_a( $previous_post , 'WP_Post' ) && get_the_title( $previous_post->ID ) != '') { 
                                        $prev_img_id = get_post_thumbnail_id($previous_post->ID);
                                        $prev_img_url = wp_get_attachment_image_src($prev_img_id, 'breeza-thumb-xs');
                                        ?>
                                        <a class="pxl--label" href="<?php echo esc_url(get_permalink( $previous_post->ID )); ?>"><i class="caseicon-double-chevron-left"></i><span><?php echo esc_html__('Previous Post', 'breeza'); ?></span></a>
                                        <div class="pxl--holder">
                                            <?php if(!empty($prev_img_id)) : ?>
                                                <div class="pxl--img">
                                                    <a  href="<?php echo esc_url(get_permalink( $previous_post->ID )); ?>"><img src="<?php echo wp_kses_post($prev_img_url[0]); ?>" /></a>
                                                </div>
                                            <?php endif; ?>
                                            <div class="pxl--meta">
                                                <a  href="<?php echo esc_url(get_permalink( $previous_post->ID )); ?>"><?php echo get_the_title( $previous_post->ID ); ?></a>
                                            </div>
                                        </div>
                                    <?php } ?>
                                </div>
                                <div class="pxl--item pxl--item-next">
                                    <?php if ( is_a( $next_post , 'WP_Post' ) && get_the_title( $next_post->ID ) != '') {
                                        $next_img_id = get_post_thumbnail_id($next_post->ID);
                                        $next_img_url = wp_get_attachment_image_src($next_img_id, 'breeza-thumb-xs');
                                        ?>
                                        <a class="pxl--label" href="<?php echo esc_url(get_permalink( $next_post->ID )); ?>"><span><?php echo esc_html__('Next Post', 'breeza'); ?></span><i class="caseicon-double-chevron-right"></i></a>
                                        <div class="pxl--holder">
                                            <div class="pxl--meta">
                                                <a href="<?php echo esc_url(get_permalink( $next_post->ID )); ?>"><?php echo get_the_title( $next_post->ID ); ?></a>
                                            </div>
                                            <?php if(!empty($next_img_id)) : ?>
                                                <div class="pxl--img">
                                                    <a href="<?php echo esc_url(get_permalink( $next_post->ID )); ?>"><img src="<?php echo wp_kses_post($next_img_url[0]); ?>" /></a>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                    <?php } ?>
                                </div>
                            </div><!-- .nav-links -->
                        </div>
                    <?php }
                }

                public function     get_post_author_info() { ?>
                    <div class="pxl-post--author-info pxl-item--flexnw">            
                        <div class="pxl-post--author-image pxl-mr-28"><?php echo get_avatar( get_the_author_meta( 'ID' ), 133 ); ?></div>
                        <div class="pxl-post--author-meta">
                            <?php breeza_get_user_name(); ?>
                            <div class="pxl-post--author-description"><?php the_author_meta( 'description' ); ?></div>
                            <?php breeza_get_user_social(); ?>
                        </div>
                    </div>
                <?php }

                public function get_related_post(){
                    $post_related_on = breeza()->get_theme_opt( 'post_related_on', false );

                    if($post_related_on) {
                        global $post;
                        $current_id = $post->ID;
                        $posttags = get_the_category($post->ID);
                        if (empty($posttags)) return;

                        $tags = array();

                        foreach ($posttags as $tag) {

                            $tags[] = $tag->term_id;
                        }
                        $post_number = '6';
                        $query_similar = new WP_Query(array('posts_per_page' => $post_number, 'post_type' => 'post', 'post_status' => 'publish', 'category__in' => $tags));
                        if (count($query_similar->posts) > 1) {
                            wp_enqueue_script( 'swiper' );
                            wp_enqueue_script( 'pxl-swiper' );
                            $opts = [
                                'slide_direction'               => 'horizontal',
                                'slide_percolumn'               => '1', 
                                'slide_mode'                    => 'slide', 
                                'slides_to_show'                => 3, 
                                'slides_to_show_lg'             => 3, 
                                'slides_to_show_md'             => 2, 
                                'slides_to_show_sm'             => 2, 
                                'slides_to_show_xs'             => 1, 
                                'slides_to_scroll'              => 1, 
                                'slides_gutter'                 => 30, 
                                'arrow'                         => false,
                                'dots'                          => true,
                                'dots_style'                    => 'bullets'
                            ];
                            $data_settings = wp_json_encode($opts);
                            $dir           = is_rtl() ? 'rtl' : 'ltr';
                            ?>
                            <div class="pxl-related-post">
                                <h4 class="widget-title"><?php echo esc_html__('Related Posts', 'breeza'); ?></h4>
                                <div class="class" data-settings="<?php echo esc_attr($data_settings) ?>" data-rtl="<?php echo esc_attr($dir) ?>">
                                    <div class="pxl-related-post-inner pxl-swiper-wrapper swiper-wrapper">
                                        <?php foreach ($query_similar->posts as $post):
                                            $thumbnail_url = '';
                                            if (has_post_thumbnail(get_the_ID()) && wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), false)) :
                                                $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'pxl-blog-small', false);
                                        endif;
                                        if ($post->ID !== $current_id) : ?>
                                            <div class="pxl-swiper-slide swiper-slide grid-item">
                                                <div class="pxl-grid-item-inner">
                                                    <?php if (has_post_thumbnail()) { ?>
                                                        <div class="item-featured">
                                                            <a href="<?php the_permalink(); ?>"><img src="<?php echo esc_url($thumbnail_url[0]); ?>" /></a>
                                                        </div>
                                                    <?php } ?>
                                                    <h3 class="item-title">
                                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                    </h3>
                                                </div>
                                            </div>
                                        <?php endif;
                                    endforeach; ?>
                                </div>
                            </div>
                        </div>
                    <?php }
                }

                wp_reset_postdata();
            }
        }
    }
