<?php

if (!class_exists('Solarva_Blog')) {

    class Solarva_Blog
    {
        
        public function get_archive_metas() {
            $archive_author = solarva()->get_theme_opt( 'archive_author', true );
            $archive_date = solarva()->get_theme_opt( 'archive_date', true );
            $archive_category = solarva()->get_theme_opt( 'archive_category', true );
            $post_id = get_the_ID();
            if($archive_author || $archive_category || $archive_date) : ?>
                <div class="pxl-item--metas">
                    <?php if($archive_author) : ?>
                        <?php $this->get_author_info(); ?>
                    <?php endif; ?>
                    <?php if($archive_category && !empty(get_the_category($post_id))) : ?>
                        <div class="pxl-item--category">
                            <?php the_terms( $post_id, 'category', '', ', ' ); ?>
                        </div>
                    <?php endif; ?>
                    <?php if($archive_date) : ?>
                        <div class="pxl-item--date">
                            <?php echo get_the_date('M d'); ?>,<?php echo get_the_date('Y'); ?>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; 
        }

        public function get_post_metas(){
            $post_author = solarva()->get_theme_opt( 'post_author', true );
            $post_date = solarva()->get_theme_opt( 'post_date', true );
            $post_comment = solarva()->get_theme_opt( 'post_comment', true );
            $count_post_view = $this->solarva_set_post_views(get_the_ID());
            if($post_author || $post_date || $post_comment) : ?>
                <div class="pxl-post--meta">
                    <div class="pxl-meta--inner">
                        <?php if($post_author) : ?>
                            <div class="pxl-post--author pxl-dflex-center-v pxl-post--item">
                                <span class="pxl-author--avatar">
                                    <?php echo get_avatar( get_the_author_meta('ID'), 280 ); ?>
                                </span>
                                <span class="pxl-author--name">
                                    <span class="pxl-text--highlight"><?php echo esc_html__('By ', 'solarva'); ?></span><?php the_author_posts_link(); ?>
                                </span>
                            </div>
                        <?php endif; ?>
                        <?php if($post_date) : ?>
                            <div class="pxl-post--date pxl-dflex-center-v pxl-post--item">
                                <svg width="14" height="18" viewBox="0 0 14 18" fill="none" xmlns="http://www.w3.org/2000/svg">
                                    <path d="M0 15.5V7H14V15.5C14 16.3438 13.3125 17 12.5 17H1.5C0.65625 17 0 16.3438 0 15.5ZM10 9.375V10.625C10 10.8438 10.1562 11 10.375 11H11.625C11.8125 11 12 10.8438 12 10.625V9.375C12 9.1875 11.8125 9 11.625 9H10.375C10.1562 9 10 9.1875 10 9.375ZM10 13.375V14.625C10 14.8438 10.1562 15 10.375 15H11.625C11.8125 15 12 14.8438 12 14.625V13.375C12 13.1875 11.8125 13 11.625 13H10.375C10.1562 13 10 13.1875 10 13.375ZM6 9.375V10.625C6 10.8438 6.15625 11 6.375 11H7.625C7.8125 11 8 10.8438 8 10.625V9.375C8 9.1875 7.8125 9 7.625 9H6.375C6.15625 9 6 9.1875 6 9.375ZM6 13.375V14.625C6 14.8438 6.15625 15 6.375 15H7.625C7.8125 15 8 14.8438 8 14.625V13.375C8 13.1875 7.8125 13 7.625 13H6.375C6.15625 13 6 13.1875 6 13.375ZM2 9.375V10.625C2 10.8438 2.15625 11 2.375 11H3.625C3.8125 11 4 10.8438 4 10.625V9.375C4 9.1875 3.8125 9 3.625 9H2.375C2.15625 9 2 9.1875 2 9.375ZM2 13.375V14.625C2 14.8438 2.15625 15 2.375 15H3.625C3.8125 15 4 14.8438 4 14.625V13.375C4 13.1875 3.8125 13 3.625 13H2.375C2.15625 13 2 13.1875 2 13.375ZM12.5 3C13.3125 3 14 3.6875 14 4.5V6H0V4.5C0 3.6875 0.65625 3 1.5 3H3V1.5C3 1.25 3.21875 1 3.5 1H4.5C4.75 1 5 1.25 5 1.5V3H9V1.5C9 1.25 9.21875 1 9.5 1H10.5C10.75 1 11 1.25 11 1.5V3H12.5Z" fill="#4AAB3D"/>
                                </svg>
                                <span class="pxl-date--text">
                                    <?php echo get_the_date('F d,Y');?>
                                </span>
                            </div>
                        <?php endif; ?>
                        <?php if($post_comment) : ?>
                            <div class="pxl-post--comment pxl-dflex-center-v pxl-post--item">
                                <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="#4AAB3D"/>
                                </svg>
                                <a href="<?php the_permalink(); ?>#comments">
                                    <?php echo comments_number(esc_html__('0', 'solarva'),esc_html__('1', 'solarva'),esc_html__('%', 'solarva')); ?>
                                </a>
                            </div>
                        <?php endif; ?>
                        <div class="pxl-post--view pxl-dflex-center-v pxl-post--item">
                            <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="#4AAB3D"/>
                            </svg>
                            <span class="pxl-count-view">
                                <?php echo esc_attr($count_post_view); ?>
                            </span>
                        </div>
                    </div>
                </div>
            <?php endif; 
        }

        public function get_excerpt(){
            $archive_excerpt_length = solarva()->get_theme_opt('archive_excerpt_length', '50');
            $solarva_the_excerpt = get_the_excerpt();
            if(!empty($solarva_the_excerpt)) {
                echo wp_trim_words( $solarva_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 = solarva()->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.', 'solarva' );
            }

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

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

            return $excerpt;
        }

        public function solarva_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 );
            }
            return $count;
        }

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

        public function get_socials_share() {
            $img_url = '';
            if (has_post_thumbnail(get_the_ID())) {
                $img_data = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), false);
                $img_url = ($img_data) ? $img_data[0] : '';
            }
    
            $social_facebook = solarva()->get_theme_opt('social_facebook', true);
            $social_twitter = solarva()->get_theme_opt('social_twitter', true);
            $social_pinterest = solarva()->get_theme_opt('social_pinterest', true);
            $social_linkedin = solarva()->get_theme_opt('social_linkedin', true);
            $social_instagram = solarva()->get_theme_opt('social_instagram', true);
            $social_youtube = solarva()->get_theme_opt('social_youtube', true);
    
            $permalink = get_permalink();
            $title = get_the_title();
            $link_social_facebook = 'http://www.facebook.com/sharer/sharer.php?u=' . esc_url($permalink);
            $link_social_instagram = 'http://www.instagram.com/sharer/sharer.php?u=' . esc_url($permalink);
            $link_social_linkedin = 'http://www.linkedin.com/shareArticle?mini=true&url=' . esc_url($permalink) . '&title=' . esc_attr($title) . '%20';
            $link_social_pinterest = 'http://pinterest.com/pin/create/button/?url=' . esc_url($permalink) . '&media=' . esc_url($img_url) . '&description=' . esc_attr($title) . '%20';
            $link_social_youtube = 'http://youtube.com/pin/create/button/?url=' . esc_url($permalink) . '&media=' . esc_url($img_url) . '&description=' . esc_attr($title) . '%20';
            $link_social_twitter = 'https://twitter.com/intent/tweet?url=' . esc_url($permalink) . '&text=' . esc_attr($title) . '%20';
            $classes = "btn pxl-hover-split-bg";
            
            ?>
            <div class="pxl-socials">
                <h5 class="pxl-item--title"><?php echo esc_html__('Share:', 'solarva'); ?></h5>
                <div class="pxl-social-group">
                    <?php if ($social_facebook) : ?>
                        <a class="<?php echo esc_attr($classes); ?>" target="_blank" href="<?php echo esc_url($link_social_facebook); ?>">
                            <span class="pxl-btn--text">
                                <?php echo esc_html__('Facebook', 'solarva'); ?>
                            </span>
                        </a>
                    <?php endif; ?>
                    <?php if ($social_instagram) : ?>
                        <a class="<?php echo esc_attr($classes); ?>" target="_blank" href="<?php echo esc_url($link_social_instagram); ?>">
                            <span class="pxl-btn--text">
                                <?php echo esc_html__('Instagram', 'solarva'); ?>
                            </span>
                        </a>
                    <?php endif; ?>
                    <?php if ($social_linkedin) : ?>
                        <a class="<?php echo esc_attr($classes); ?>" target="_blank" href="<?php echo esc_url($link_social_linkedin); ?>">
                            <span class="pxl-btn--text">
                                <?php echo esc_html__('Linked In', 'solarva'); ?>
                            </span>
                        </a>
                    <?php endif; ?>
                    <?php if ($social_pinterest) : ?>
                        <a class="<?php echo esc_attr($classes); ?>" target="_blank" href="<?php echo esc_url($link_social_pinterest); ?>">
                            <span class="pxl-btn--text">
                                <?php echo esc_html__('Pinterest', 'solarva'); ?>
                            </span>
                        </a>
                    <?php endif; ?>
                    <?php if ($social_youtube) : ?>
                        <a class="<?php echo esc_attr($classes); ?>" target="_blank" href="<?php echo esc_url($link_social_youtube); ?>">
                            <span class="pxl-btn--text">
                                <?php echo esc_html__('Youtube', 'solarva'); ?>
                            </span>
                        </a>
                    <?php endif; ?>
                    <?php if ($social_twitter) : ?>
                        <a class="<?php echo esc_attr($classes); ?>" target="_blank" href="<?php echo esc_url($link_social_twitter); ?>">
                            <span class="pxl-btn--text">
                                <?php echo esc_html__('Twitter', 'solarva'); ?>
                            </span>
                        </a>
                    <?php endif; ?>
                </div>
            </div>
            <?php
        }
        

        public function get_author_info() {
            if(!is_single()) : ?>
                <div class="pxl-post--author">
                    <div class="pxl-author--avatar">
                        <?php echo get_avatar( get_the_author_meta( 'ID' ), 280 ); ?>
                    </div>
                    <div class="pxl-author-meta">
                        <?php solarva_get_user_name(); ?>
                        <?php solarva_get_user_position(); ?>
                    </div>
                </div>
            <?php else : ?>
                <?php 
                    $author_desc = get_user_meta(get_the_author_meta( 'ID' ), 'description', true);
                ?>
                <div class="pxl-post--author">
                    <div class="pxl-author--avatar">
                        <?php echo get_avatar( get_the_author_meta( 'ID' ), 280 ); ?>
                    </div>
                    <div class="pxl-author-meta">
                        <?php solarva_get_user_name(); ?>
                        <div class="pxl-author--desc">
                            <?php echo esc_attr($author_desc); ?>
                        </div>
                        <?php solarva_get_user_social(); ?>
                    </div>
                </div>
            <?php endif;
        }

    }
}
