<?php 
 
if(!function_exists('hubsteel_get_post_grid')){
    function hubsteel_get_post_grid($posts = [], $settings = []){ 
        if (empty($posts) || !is_array($posts) || empty($settings) || !is_array($settings)) {
            return false;
        }
        switch ($settings['layout']) {

            case 'service-1':
                hubsteel_get_service_grid_layout1($posts, $settings);
                break;

            case 'portfolio-1':
                hubsteel_get_portfolio_grid_layout1($posts, $settings);
                break;

            case 'portfolio-2':
                hubsteel_get_portfolio_grid_layout2($posts, $settings);
                break;

            case 'portfolio-3':
                hubsteel_get_portfolio_grid_layout3($posts, $settings);
                break;

            default:
                return false;
                break;
        }
    }
}

// Start Post Grid
//--------------------------------------------------
function hubsteel_get_post_grid_layout1($posts = [], $settings = []){ 
    extract($settings);
    
    $images_size = !empty($img_size) ? $img_size : '762x781';

    if (is_array($posts)):
        foreach ($posts as $key => $post):
            $item_class = "pxl-grid-item col-xl-{$col_xl} col-lg-{$col_lg} col-md-{$col_md} col-sm-{$col_sm} col-{$col_xs}";
            if(isset($grid_masonry) && !empty($grid_masonry[$key]) && (count($grid_masonry) > 1)) {
                if($grid_masonry[$key]['col_xl_m'] == 'col-66') {
                    $col_xl_m = '66-pxl';
                } else {
                    $col_xl_m = 12 / $grid_masonry[$key]['col_xl_m'];
                }
                if($grid_masonry[$key]['col_lg_m'] == 'col-66') {
                    $col_lg_m = '66-pxl';
                } else {
                    $col_lg_m = 12 / $grid_masonry[$key]['col_lg_m'];
                }
                $col_md_m = 12 / $grid_masonry[$key]['col_md_m'];
                $col_sm_m = 12 / $grid_masonry[$key]['col_sm_m'];
                $col_xs_m = 12 / $grid_masonry[$key]['col_xs_m'];
                $item_class = "pxl-grid-item col-xl-{$col_xl_m} col-lg-{$col_lg_m} col-md-{$col_md_m} col-sm-{$col_sm_m} col-{$col_xs_m}";
                
                $img_size_m = $grid_masonry[$key]['img_size_m'];
                if(!empty($img_size_m)) {
                    $images_size = $img_size_m;
                }
            } elseif (!empty($img_size)) {
                $images_size = $img_size;
            }

            if(!empty($tax))
                $filter_class = pxl_get_term_of_post_to_class($post->ID, array_unique($tax));
            else 
                $filter_class = ''; 
            $tags = get_the_tags($post->ID);
            $author = get_user_by('id', $post->post_author);
            $author_avatar = get_avatar( $post->post_author, 60, '', $author->display_name, array( 'class' => '' ) );
            $user_position = get_user_meta(get_the_author_meta( 'ID' ), 'user_position', true);
            $post_video_link = get_post_meta($post->ID, 'post_video_link', true); ?>
            <div class="<?php echo esc_attr($item_class . ' ' . $filter_class); ?>">
                <div class="pxl-post--inner <?php echo esc_attr($pxl_animate); ?>" data-wow-duration="1.2s">
                    <?php if (has_post_thumbnail($post->ID) && wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), false)):
                        $img_id = get_post_thumbnail_id($post->ID);
                        $img          = pxl_get_image_by_size( array(
                            'attach_id'  => $img_id,
                            'thumb_size' => $images_size
                        ) );
                        $thumbnail    = $img['thumbnail'];
                        ?>
                        <div class="pxl-post--featured">
                            <a class="pxl-img-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>"><?php echo wp_kses_post($thumbnail); ?></a>
                            <?php if(!empty($post_video_link)) : ?>
                                <div class="pxl-item-video">
                                    <a class="pxl-btn-video pxl-action-popup" href="<?php echo esc_url($post_video_link); ?>">
                                        <i class="caseicon-play1"></i>
                                    </a>
                                </div>
                            <?php endif; ?>
                            <?php if ($show_tags == 'true' && ($tags = get_the_tags($post->ID))) : ?>
                                <div class="pxl-item--tags pxl-post-list">
                                    <?php foreach ($tags as $tag): ?>
                                        <a href="<?php echo esc_url(get_tag_link($tag->term_id)); ?>">
                                            <?php echo esc_html($tag->name); ?>
                                        </a>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                    <div class="pxl-item-content">
                        <h3 class="pxl-post--title title-hover-line"><a href="<?php echo esc_url(get_permalink( $post->ID )); ?>"><?php echo pxl_print_html(get_the_title($post->ID)); ?></a></h3>
                        <div class="pxl-post--meta">
                            <?php if ($show_date == 'true') : ?>
                                <div class="pxl-inner-date">
                                    <div class="pxl-item--date ">
                                        <?php echo get_the_date('M j, Y'); ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                            <span>by</span>
                            <?php if($show_author == 'true'): ?>
                                <div class="pxl-post--author pxl-flex">
                                    <div class="pxl-author-meta">
                                        <a href="<?php echo esc_url(get_author_posts_url($post->post_author, $author->user_nicename)); ?>"><?php echo esc_html($author->display_name); ?></a>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        <?php
        endforeach;
    endif;
}

// End Post Grid
//--------------------------------------------------

// Start Portfolio Grid
//--------------------------------------------------
function hubsteel_get_portfolio_grid_layout1($posts = [], $settings = []){ 
    extract($settings);
    
    $images_size = !empty($img_size) ? $img_size : '582x694';

    if (is_array($posts)):
        foreach ($posts as $key => $post):
            $item_class = "pxl-grid-item col-xl-{$col_xl} col-lg-{$col_lg} col-md-{$col_md} col-sm-{$col_sm} col-{$col_xs}";
            if(isset($grid_masonry) && !empty($grid_masonry[$key]) && (count($grid_masonry) > 1)) {
                if($grid_masonry[$key]['col_xl_m'] == 'col-66') {
                    $col_xl_m = '66-pxl';
                } else {
                    $col_xl_m = 12 / $grid_masonry[$key]['col_xl_m'];
                }
                if($grid_masonry[$key]['col_lg_m'] == 'col-66') {
                    $col_lg_m = '66-pxl';
                } else {
                    $col_lg_m = 12 / $grid_masonry[$key]['col_lg_m'];
                }
                $col_md_m = 12 / $grid_masonry[$key]['col_md_m'];
                $col_sm_m = 12 / $grid_masonry[$key]['col_sm_m'];
                $col_xs_m = 12 / $grid_masonry[$key]['col_xs_m'];
                $item_class = "pxl-grid-item col-xl-{$col_xl_m} col-lg-{$col_lg_m} col-md-{$col_md_m} col-sm-{$col_sm_m} col-{$col_xs_m}";
                
                $img_size_m = $grid_masonry[$key]['img_size_m'];
                if(!empty($img_size_m)) {
                    $images_size = $img_size_m;
                }
            } elseif (!empty($img_size)) {
                $images_size = $img_size;
            }

            if(!empty($tax))
                $filter_class = pxl_get_term_of_post_to_class($post->ID, array_unique($tax));
            else 
                $filter_class = '';

            $img_id = get_post_thumbnail_id($post->ID);
            if (has_post_thumbnail($post->ID) && wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), false)): 
                if($img_id) {
                    $img = pxl_get_image_by_size( array(
                        'attach_id'  => $img_id,
                        'thumb_size' => $images_size,
                        'class' => 'no-lazyload',
                    ));
                    $thumbnail = $img['thumbnail'];
                } else {
                    $thumbnail = get_the_post_thumbnail($post->ID, $images_size);
                }
                $portfolio_excerpt = get_post_meta($post->ID, 'portfolio_excerpt', true); ?>
                <div class="<?php echo esc_attr($item_class . ' ' . $filter_class); ?>">
                    <div class="pxl-post--inner <?php echo esc_attr($pxl_animate); ?>" data-wow-duration="1.2s">
                        <div class="pxl-post--featured hover-imge-effect3">
                            <a class="pxl-featured-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                <?php echo wp_kses_post($thumbnail); ?>
                                <span class="pxl-post--overlay"></span>
                            </a>
                        </div>
                        <div class="pxl-post--holder">
                            <div class="holder-content">
                                <?php if($show_category == 'true'): ?>
                                    <div class="pxl-category">
                                        <div class="pxl-post--category link-none">
                                            <?php the_terms( $post->ID, 'portfolio-category', '', ' ' ); ?>
                                        </div>
                                    </div>
                                <?php endif; ?>
                                <div class="pxl-title">
                                    <h5 class="pxl-post--title">
                                        <a class="pxl-holder-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                            <?php echo esc_attr(get_the_title($post->ID)); ?>
                                        </a>
                                    </h5>
                                </div>
                                <?php if($show_excerpt == 'true' && !empty($portfolio_excerpt)): ?>
                                    <div class="pxl-item--content">
                                        <?php echo wp_trim_words( $portfolio_excerpt, $num_words, $more = null ); ?>
                                    </div>
                                <?php endif; ?>
                                <?php if($show_button == 'true'): ?>
                                    <div class="pxl-post--icon">
                                        <a class="pxl-item-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                            <svg height="512" viewBox="0 0 24 24" width="512" xmlns="http://www.w3.org/2000/svg"><path d="m16.0039 9.414-8.60699 8.607-1.414-1.414 8.60599-8.607h-7.58499v-2h10.99999v11h-2z" fill="rgb(0,0,0)"/></svg>
                                        </a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach;
    endif;
}

function hubsteel_get_portfolio_grid_layout2($posts = [], $settings = []){ 
    extract($settings);
    
    $images_size = !empty($img_size) ? $img_size : '1012x694';

    if (is_array($posts)):
        foreach ($posts as $key => $post):
            $item_class = "pxl-grid-item col-xl-{$col_xl} col-lg-{$col_lg} col-md-{$col_md} col-sm-{$col_sm} col-{$col_xs}";
            if(isset($grid_masonry) && !empty($grid_masonry[$key]) && (count($grid_masonry) > 1)) {
                if($grid_masonry[$key]['col_xl_m'] == 'col-66') {
                    $col_xl_m = '66-pxl';
                } else {
                    $col_xl_m = 12 / $grid_masonry[$key]['col_xl_m'];
                }
                if($grid_masonry[$key]['col_lg_m'] == 'col-66') {
                    $col_lg_m = '66-pxl';
                } else {
                    $col_lg_m = 12 / $grid_masonry[$key]['col_lg_m'];
                }
                $col_md_m = 12 / $grid_masonry[$key]['col_md_m'];
                $col_sm_m = 12 / $grid_masonry[$key]['col_sm_m'];
                $col_xs_m = 12 / $grid_masonry[$key]['col_xs_m'];
                $item_class = "pxl-grid-item col-xl-{$col_xl_m} col-lg-{$col_lg_m} col-md-{$col_md_m} col-sm-{$col_sm_m} col-{$col_xs_m}";
                
                $img_size_m = $grid_masonry[$key]['img_size_m'];
                if(!empty($img_size_m)) {
                    $images_size = $img_size_m;
                }
            } elseif (!empty($img_size)) {
                $images_size = $img_size;
            }

            if(!empty($tax))
                $filter_class = pxl_get_term_of_post_to_class($post->ID, array_unique($tax));
            else 
                $filter_class = '';

            $img_id = get_post_thumbnail_id($post->ID);
            if (has_post_thumbnail($post->ID) && wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), false)): 
                if($img_id) {
                    $img = pxl_get_image_by_size( array(
                        'attach_id'  => $img_id,
                        'thumb_size' => $images_size,
                        'class' => 'no-lazyload',
                    ));
                    $thumbnail = $img['thumbnail'];
                } else {
                    $thumbnail = get_the_post_thumbnail($post->ID, $images_size);
                }
                $portfolio_excerpt = get_post_meta($post->ID, 'portfolio_excerpt', true); ?>
                <div class="<?php echo esc_attr($item_class . ' ' . $filter_class); ?>">
                    <div class="pxl-post--inner <?php echo esc_attr($pxl_animate); ?>" data-wow-duration="1.2s">
                        <div class="pxl-post--featured hover-imge-effect3">
                            <a class="pxl-featured-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                <?php echo wp_kses_post($thumbnail); ?>
                            </a>
                        </div>
                        <div class="pxl-post--holder">
                            <div class="holder-content">
                                <div class="pxl-content-top">
                                    <div class="pxl-item-meta">
                                        <?php if($show_category == 'true'): ?>
                                            <div class="pxl-category">
                                                <div class="pxl-post--category link-none">
                                                    <?php the_terms( $post->ID, 'portfolio-category', '', ' ' ); ?>
                                                </div>
                                            </div>
                                        <?php endif; ?>
                                        <div class="pxl-title">
                                            <h5 class="pxl-post--title">
                                                <a class="pxl-holder-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                                    <?php echo esc_attr(get_the_title($post->ID)); ?>
                                                </a>
                                            </h5>
                                        </div>
                                    </div>
                                    <?php if($show_button == 'true'): ?>
                                        <div class="pxl-post--icon">
                                            <a class="pxl-item-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                                <svg height="512" viewBox="0 0 24 24" width="512" xmlns="http://www.w3.org/2000/svg"><path d="m16.0039 9.414-8.60699 8.607-1.414-1.414 8.60599-8.607h-7.58499v-2h10.99999v11h-2z" fill="rgb(0,0,0)"/></svg>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <?php if($show_excerpt == 'true' && !empty($portfolio_excerpt)): ?>
                                    <div class="pxl-item--content">
                                        <?php echo wp_trim_words( $portfolio_excerpt, $num_words, $more = null ); ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach;
    endif;
}

function hubsteel_get_portfolio_grid_layout3($posts = [], $settings = []){ 
    extract($settings);
    
    $images_size = !empty($img_size) ? $img_size : '1200x596';

    if (is_array($posts)):
        foreach ($posts as $key => $post):
            $item_class = "pxl-grid-item col-xl-{$col_xl} col-lg-{$col_lg} col-md-{$col_md} col-sm-{$col_sm} col-{$col_xs}";
            if(isset($grid_masonry) && !empty($grid_masonry[$key]) && (count($grid_masonry) > 1)) {
                if($grid_masonry[$key]['col_xl_m'] == 'col-66') {
                    $col_xl_m = '66-pxl';
                } else {
                    $col_xl_m = 12 / $grid_masonry[$key]['col_xl_m'];
                }
                if($grid_masonry[$key]['col_lg_m'] == 'col-66') {
                    $col_lg_m = '66-pxl';
                } else {
                    $col_lg_m = 12 / $grid_masonry[$key]['col_lg_m'];
                }
                $col_md_m = 12 / $grid_masonry[$key]['col_md_m'];
                $col_sm_m = 12 / $grid_masonry[$key]['col_sm_m'];
                $col_xs_m = 12 / $grid_masonry[$key]['col_xs_m'];
                $item_class = "pxl-grid-item col-xl-{$col_xl_m} col-lg-{$col_lg_m} col-md-{$col_md_m} col-sm-{$col_sm_m} col-{$col_xs_m}";
                
                $img_size_m = $grid_masonry[$key]['img_size_m'];
                if(!empty($img_size_m)) {
                    $images_size = $img_size_m;
                }
            } elseif (!empty($img_size)) {
                $images_size = $img_size;
            }

            if(!empty($tax))
                $filter_class = pxl_get_term_of_post_to_class($post->ID, array_unique($tax));
            else 
                $filter_class = '';

            $img_id = get_post_thumbnail_id($post->ID);
            if (has_post_thumbnail($post->ID) && wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), false)): 
                if($img_id) {
                    $img = pxl_get_image_by_size( array(
                        'attach_id'  => $img_id,
                        'thumb_size' => $images_size,
                        'class' => 'no-lazyload',
                    ));
                    $thumbnail = $img['thumbnail'];
                } else {
                    $thumbnail = get_the_post_thumbnail($post->ID, $images_size);
                }
                $client = get_post_meta($post->ID, 'client', true);
                $tags = get_post_meta($post->ID, 'tags', true);
                $portfolio_excerpt = get_post_meta($post->ID, 'portfolio_excerpt', true); ?>
                <div class="<?php echo esc_attr($item_class . ' ' . $filter_class); ?>">
                    <div class="pxl-post--inner <?php echo esc_attr($pxl_animate); ?>" data-wow-duration="1.2s">
                        <div class="pxl-post--featured hover-imge-effect3">
                            <a class="pxl-featured-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                <?php echo wp_kses_post($thumbnail); ?>
                            </a>
                        </div>
                        <div class="pxl-post--holder">
                            <?php if($show_info == 'true'): ?>
                                <div class="pxl-content-list">
                                    <div class="pxl-item-list client">
                                        <span class="pxl-item-name pxl-mr-6"><?php echo esc_html__('Client: ','hubsteel'); ?></span>
                                        <?php echo pxl_print_html($client); ?>
                                    </div>
                                    <div class="pxl-item-list date">
                                        <span class="pxl-item-name pxl-mr-6"><?php echo esc_html__('Start Day: ','hubsteel'); ?></span>
                                        <?php echo get_the_date('d/m/Y', $post->ID); ?>       
                                    </span>
                                    </div>
                                    <div class="pxl-item-list tags">
                                        <span class="pxl-item-name"><?php echo esc_html__('Tags: ','hubsteel'); ?></span>
                                        <?php echo pxl_print_html($tags); ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                            <div class="holder-content">
                                <h6 class="pxl-post--title">
                                    <a class="pxl-holder-link" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                        <?php echo esc_attr(get_the_title($post->ID)); ?>
                                    </a>
                                </h6>
                                <?php if($show_excerpt == 'true' && !empty($portfolio_excerpt)): ?>
                                    <div class="pxl-item--content">
                                        <?php echo wp_trim_words( $portfolio_excerpt, $num_words, $more = null ); ?>
                                    </div>
                                <?php endif; ?>
                                <?php if($show_button == 'true'): ?>
                                    <div class="pxl-post--btn">
                                        <a class="pxl-item-btn" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                            <span class="pxl-btn-title pxl-mr-44"><?php if(!empty($button_text)) {
                                                echo esc_attr($button_text);
                                            } else {
                                                echo esc_html__('Read more', 'hubsteel');
                                            } ?></span>
                                            <span class="pxl-btn-icon">
                                                <svg height="512" viewBox="0 0 24 24" width="512" xmlns="http://www.w3.org/2000/svg"><path d="m16.0039 9.414-8.60699 8.607-1.414-1.414 8.60599-8.607h-7.58499v-2h10.99999v11h-2z" fill="rgb(0,0,0)"/></svg>
                                            </span>
                                        </a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach;
    endif;
}

// End Portfolio Grid
//--------------------------------------------------

// Start Service Grid
//--------------------------------------------------
function hubsteel_get_service_grid_layout1($posts = [], $settings = []){ 
    extract($settings);

    if (is_array($posts)):
        foreach ($posts as $key => $post):
            $item_class = "pxl-grid-item col-xl-{$col_xl} col-lg-{$col_lg} col-md-{$col_md} col-sm-{$col_sm} col-{$col_xs}";
            if(isset($grid_masonry) && !empty($grid_masonry[$key]) && (count($grid_masonry) > 1)) {
                $col_xl_m = 12 / $grid_masonry[$key]['col_xl_m'];
                $col_lg_m = 12 / $grid_masonry[$key]['col_lg_m'];
                $col_md_m = 12 / $grid_masonry[$key]['col_md_m'];
                $col_sm_m = 12 / $grid_masonry[$key]['col_sm_m'];
                $col_xs_m = 12 / $grid_masonry[$key]['col_xs_m'];
                $item_class = "pxl-grid-item col-xl-{$col_xl_m} col-lg-{$col_lg_m} col-md-{$col_md_m} col-sm-{$col_sm_m} col-{$col_xs_m}";   
            }

            if(!empty($tax))
                $filter_class = pxl_get_term_of_post_to_class($post->ID, array_unique($tax));
            else 
                $filter_class = '';

            $service_excerpt = get_post_meta($post->ID, 'service_excerpt', true);
            $service_external_link = get_post_meta($post->ID, 'service_external_link', true);
            $service_icon_type = get_post_meta($post->ID, 'service_icon_type', true);
            $service_icon_font = get_post_meta($post->ID, 'service_icon_font', true);
            $service_icon_img = get_post_meta($post->ID, 'service_icon_img', true);
            $gradient_color = hubsteel()->get_opt( 'gradient_color' );
            ?>
            <div class="<?php echo esc_attr($item_class . ' ' . $filter_class); ?>">
                <div class="pxl-item--inner pxl-not-active <?php echo esc_attr($pxl_animate); ?>" data-wow-duration="1.2s">
                    <?php if($service_icon_type == 'icon' && !empty($service_icon_font)) : ?>
                        <div class="pxl-item--icon">
                            <div class="pxl-inner-icon">
                                <div class="pxl-icon-clip">
                                    <i class="<?php echo esc_attr($service_icon_font); ?> pxl-icon1"></i>
                                    <span class="pxl-icon2">
                                        <i class="<?php echo esc_attr($service_icon_font); ?>"></i>
                                    </span>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                    <?php if($service_icon_type == 'image' && !empty($service_icon_img)) : 
                        $icon_img = pxl_get_image_by_size( array(
                            'attach_id'  => $service_icon_img['id'],
                            'thumb_size' => 'full',
                        ));
                        $icon_thumbnail = $icon_img['thumbnail'];
                        ?>
                        <div class="pxl-item--icon">
                            <span class="pxl-icon">
                                <?php echo wp_kses_post($icon_thumbnail); ?>
                            </span>
                        </div>
                    <?php endif; ?>
                    <div class="pxl-inner-content">
                        <div class="pxl-item-meta">
                            <div class="pxl-item--holder">
                                <h5 class="pxl-item--title">
                                    <a href="<?php if(!empty($service_external_link)) { echo esc_url($service_external_link); } else { echo esc_url(get_permalink( $post->ID )); } ?>"><?php echo pxl_print_html(get_the_title($post->ID)); ?></a>
                                </h5>
                                <?php if($show_excerpt == 'true' && !empty($service_excerpt)): ?>
                                    <div class="pxl-item--content">
                                        <?php echo wp_trim_words( $service_excerpt, $num_words, $more = null ); ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php
        endforeach;
    endif;
}
// End Service Grid
//-------------------------------------------------

// Start Product Grid
//--------------------------------------------------
// End Product Grid
//--------------------------------------------------

add_action( 'wp_ajax_hubsteel_load_more_post_grid', 'hubsteel_load_more_post_grid' );
add_action( 'wp_ajax_nopriv_hubsteel_load_more_post_grid', 'hubsteel_load_more_post_grid' );
function hubsteel_load_more_post_grid(){
    try{
        if(!isset($_POST['settings'])){
            throw new Exception(__('Something went wrong while requesting. Please try again!', 'hubsteel'));
        }

        $settings = isset($_POST['settings']) ? $_POST['settings'] : null;

        $source = isset($settings['source']) ? $settings['source'] : '';
        $term_slug = isset($settings['term_slug']) ? $settings['term_slug'] : '';
        if( !empty($term_slug) && $term_slug !='*'){
            $term_slug = str_replace('.', '', $term_slug);
            $source = [$term_slug.'|'.$settings['tax'][0]]; 
        }
        if( isset($_POST['handler_click']) && sanitize_text_field(wp_unslash( $_POST[ 'handler_click' ] )) == 'filter'){
            set_query_var('paged', 1);
            $settings['paged'] = 1;
        }elseif( isset($_POST['handler_click']) && sanitize_text_field(wp_unslash( $_POST[ 'handler_click' ] )) == 'select_orderby'){
            set_query_var('paged', 1);
            $settings['paged'] = 1;
        }else{
            set_query_var('paged', (int)$settings['paged']);
        }

        extract(pxl_get_posts_of_grid($settings['post_type'], [
            'source'      => $source,
            'orderby'     => isset($settings['orderby'])?$settings['orderby']:'date',
            'order'       => isset($settings['order']) ? ($settings['orderby'] == 'title' ? 'asc' : sanitize_text_field($settings['order']) ) : 'desc',
            'limit'       => isset($settings['limit'])?$settings['limit']:'6',
            'post_ids'    => isset($settings['post_ids'])?$settings['post_ids']: [],
            'post_not_in' => isset($settings['post_not_in'])?$settings['post_not_in']: [],
        ],
        $settings['tax']
    ));

        ob_start();
        if( isset($settings['wg_type']) && $settings['wg_type'] == 'post-list'){
            hubsteel_get_post_list($posts, $settings);
        }else{
            hubsteel_get_post_grid($posts, $settings);
        }
        $html = ob_get_clean();

        $pagin_html = '';
        if( isset($settings['pagination_type']) && $settings['pagination_type'] == 'pagination' ){ 
            ob_start();
            hubsteel()->page->get_pagination( $query,  true );
            $pagin_html = ob_get_clean();
        }

        $result_count = '';
        if( isset($settings['show_toolbar']) && $settings['show_toolbar'] == 'show' ){ 
            ob_start();
            if( (int)$settings['paged'] == 0){
                $limit_start = 1;
                $limit_end = ( (int)$settings['limit'] >= $total ) ? $total : (int)$settings['limit'];
            }else{
                $limit_start = (((int)$settings['paged'] - 1 ) * (int)$settings['limit']) + 1;
                $limit_end = (int)$settings['paged'] * (int)$settings['limit'];
                $limit_end = ( $limit_end >= $total ) ? $total : $limit_end;
            }
            if( isset($settings['pagination_type']) && $settings['pagination_type'] == 'loadmore' ){ 
                printf(
                    '<span class="result-count">%1$s %2$s %3$s %4$s %5$s</span>',
                    esc_html__('Showing','hubsteel'),
                    '1-'.$limit_end,
                    esc_html__('of','hubsteel'),
                    $total,
                    esc_html__('results','hubsteel')
                );
            }else{
                printf(
                    '<span class="result-count">%1$s %2$s %3$s %4$s %5$s</span>',
                    esc_html__('Showing','hubsteel'),
                    $limit_start.'-'.$limit_end,
                    esc_html__('of','hubsteel'),
                    $total,
                    esc_html__('results','hubsteel')
                );
            }

            $result_count = ob_get_clean();
        }

        wp_send_json(
            array(
                'status' => true,
                'message' => esc_attr__('Load Successfully!', 'hubsteel'),
                'data' => array(
                    'html' => $html,
                    'pagin_html' => $pagin_html,
                    'paged' => $settings['paged'],
                    'posts' => $posts,
                    'max' => $max,
                    'result_count' => $result_count,
                ),
            )
        );
    }
    catch (Exception $e){
        wp_send_json(array('status' => false, 'message' => $e->getMessage()));
    }
    die;
}

if(!function_exists('hubsteel_get_post_list')){
    function hubsteel_get_post_list($posts = [], $settings = []){ 
        if (empty($posts) || !is_array($posts) || empty($settings) || !is_array($settings)) {
            return;
        }
        extract($settings);

        switch ($settings['layout']) {
            case 'post-list-1':
            hubsteel_get_post_list_layout1($posts, $settings);
            break;

            default:
            return false;
            break;
        }
    }
}
function hubsteel_get_post_list_layout1($posts = [], $settings = []){
    extract($settings); 
    foreach ($posts as $key => $post):

        if (has_post_thumbnail($post->ID) && wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), false)){
            $img_id = get_post_thumbnail_id($post->ID);
            if($img_id){
                $img = pxl_get_image_by_size( array(
                    'attach_id'  => $img_id,
                    'thumb_size' => $img_size,
                    'class' => 'no-lazyload',
                ));
                $thumbnail = $img['thumbnail'];
            }else{  
                $thumbnail = get_the_post_thumbnail($post->ID, $img_size);
            }
        }else{
            $thumbnail = '';
        }
        $tags = get_the_tags($post->ID);
        $author = get_user_by('id', $post->post_author);
        $readmore_text = !empty($readmore_text) ? $readmore_text : esc_html__('Continue Reading', 'hubsteel');
        $date_format = get_option('date_format');

        $data_settings = '';
        $animate_cls = '';
        if ( !empty( $item_animation ) ) {
            $animate_cls = ' pxl-animate pxl-invisible animated-'.$item_animation_duration;
            $data_animation =  json_encode([
                'animation'      => $item_animation,
                'animation_delay' => (float)$item_animation_delay
            ]);
            $data_settings = 'data-settings="'.esc_attr($data_animation).'"';
        }

        
        $flag = false;
        $post_format = get_post_format($post->ID) == false ? 'format-standard' : 'format-'.get_post_format($post->ID);
        ?>
        <div class="<?php echo esc_attr('list-item w-100 '. $post_format); ?> <?php echo esc_attr($animate_cls) ?>" <?php pxl_print_html($data_settings); ?>>
            <div class="grid-item-inner item-inner-wrap row  <?php echo esc_attr($post_format) ?>">
                <?php
                if (has_post_format('quote', $post->ID)){
                    $quote_text = get_post_meta( $post->ID, 'featured-quote-text', true );
                    $quote_cite = get_post_meta( $post->ID, 'featured-quote-cite', true );
                    ?>
                    <div class="col-12">
                        <div class="quote-wrap">
                            <div class="pxl-hl-image"><img src="<?php echo esc_url(get_template_directory_uri().'/assets/img/fm-qt.png'); ?>" /></div>
                            <div class="quote-inner-wrap">

                                <div class="link-inner ">
                                    <div class="link-icon">
                                       <span>“</span>
                                   </div>
                                   <div class="content-right">
                                    <div class="item-post-metas ">
                                        <div class="meta-inner  align-items-center">
                                            <?php if($show_date == 'true') : ?>
                                                <span class="post-date">
                                                    <?php echo get_the_date('d M', $post->ID); ?>
                                                </span>
                                            <?php endif; ?>
                                            <?php if( $show_category == 'true' ) : ?>
                                                <span class="meta-item post-category  d-flex">
                                                    <?php the_terms( $post->ID, 'category', '', ', ', '' ); ?>
                                                </span>   
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <a class="quote-text" href="<?php echo esc_url( get_permalink($post->ID)); ?>"><?php echo esc_html($quote_text);?></a>
                                </div>
                            </div>
                            <div class="quote-footer ">
                                <div class="quote-cite "><?php echo esc_html($quote_cite);?></div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php
            } elseif (has_post_format('link', $post->ID)){
                $link_url = get_post_meta( $post->ID , 'featured-link-url', true );
                $link_text = get_post_meta( $post->ID , 'featured-link-text', true );
                ?>
                <div class="col-12">
                    <div class="link-wrap">
                        <div class="pxl-hl-image"><img src="<?php echo esc_url(get_template_directory_uri().'/assets/img/fm-qt.png'); ?>" /></div>
                        <div class="link-inner-wrap">
                            <div class="link-inner ">
                                <div class="link-icon">
                                    <a href="<?php echo esc_url( $link_url); ?>">
                                        <svg version="1.1" id="Glyph" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
                                        viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
                                        <path d="M192.5,240.5c20.7-21,56-23,79,0h0.2c6.4,6.4,11,14.2,13.8,22.6c6.7-1.1,12.6-4,17.1-8.5l22.1-21.9
                                        c-5-9.6-11.4-18.4-19-26.2c-42-41.1-106.9-40-147.2,0l-80,80c-40.6,40.9-40.6,106.3,0,147.2c40.9,40.6,106.3,40.6,147.2,0l75.4-75.4
                                        c-22,3.6-43.1,1.6-62.7-5.3l-46.7,46.6c-21.1,21.3-57.9,21.3-79.2,0c-21.8-21.8-21.8-57.3,0-79C113.9,318.9,197.8,235.1,192.5,240.5
                                        L192.5,240.5z"/>
                                        <path d="M319.5,271.5c-21,21.3-56.3,22.7-79,0c-0.2,0-0.2,0-0.2,0c-6.4-6.4-11-14.2-13.8-22.6c-6.7,1.1-12.6,4-17.1,8.5l-22.1,21.9
                                        c5,9.6,11.4,18.4,19,26.2c42,41.1,106.9,40,147.2,0l80-80c40.6-40.9,40.6-106.3,0-147.2c-40.9-40.6-106.3-40.6-147.2,0L211,153.8
                                        c22-3.6,43.1-1.6,62.7,5.3l46.7-46.6c21.1-21.3,57.9-21.3,79.2,0c21.8,21.8,21.8,57.3,0,79C398.1,193.1,314.2,276.9,319.5,271.5
                                        L319.5,271.5z"/>
                                    </svg>
                                </a>
                            </div>
                            <div class="content-right">
                                <div class="item-post-metas ">
                                    <div class="meta-inner  align-items-center">
                                        <?php if($show_date == 'true') : ?>
                                            <span class="post-date">
                                                <?php echo get_the_date('d M', $post->ID); ?>
                                            </span>
                                        <?php endif; ?>
                                        <?php if( $show_category == 'true' ) : ?>
                                            <span class="meta-item post-category  d-flex">
                                                <?php the_terms( $post->ID, 'category', '', ', ', '' ); ?>
                                            </span>   
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <h3 class="link-title"><a href="<?php echo esc_url( $link_url); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_title($post->ID); ?></a></h3>
                            </div>
                        </div>
                        <div class="link-footer">
                            <a class="link-text" target="_blank" href="<?php echo esc_url( $link_url); ?>"><?php echo esc_html($link_text);?></a>
                        </div>
                    </div>
                </div>
            </div>
            <?php  
        }else{
            if ( !empty( $thumbnail )){
                $flag = true;
                $featured_video = get_post_meta( $post->ID, 'featured-video-url', true );
                $audio_url = get_post_meta( $post->ID, 'featured-audio-url', true ); 
                ?>
                <div class="item-featured col-lg-5">
                    <div class="post-image <?php echo esc_attr('scale-hover') ?>">
                        <?php echo wp_kses_post($thumbnail); ?>       
                        <?php if (has_post_format('audio', $post->ID)) {  
                            $audio = get_post_meta( $post->ID, 'featured-audio-url', true );
                            ?>  
                            <a class="btn-volumn" href="<?php echo esc_url($audio); ?>" target="_blank"><i class="fas fa-volume"></i></a>
                        <?php } ?>

                        <?php if (has_post_format('video', $post->ID)) {  
                            $video = get_post_meta( $post->ID, 'featured-video-url', true );
                            ?>  
                            <a class="video-play-button pxl-action-popup" href="<?php echo esc_url($video); ?>">
                                <i class="caseicon-play1"></i>
                            </a>

                        <?php } ?>
                        <?php
                        if($show_date == 'true') : ?>
                            <div class="post-date">
                                <span class="day"><?php echo get_the_date('d', $post->ID); ?></span>
                                <span class="month"><?php echo get_the_date('M', $post->ID); ?></span>
                            </div>
                        <?php endif; ?>
                    </div> 
                </div>
                <?php
            }else{
                if (has_post_format('video', $post->ID)){
                    $flag = true;
                    global $wp_embed;
                    $featured_video = get_post_meta( $post->ID, 'featured-video-url', true );
                    if (!empty($featured_video)) {
                        echo '<div class="item-featured col-lg-5">';
                        echo '<div class="feature-video">';
                        echo do_shortcode($wp_embed->autoembed($featured_video));
                        echo '</div>';
                        echo '</div>';
                    }
                }elseif(has_post_format('audio', $post->ID)){

                    $flag = true;
                    global $wp_embed;
                    $audio_url = get_post_meta( $post->ID, 'featured-audio-url', true );
                    if (!empty($audio_url)) {
                        echo '<div class="item-featured col-lg-5">';
                        echo '<div class="feature-audio">';
                        echo do_shortcode($wp_embed->autoembed($audio_url));
                        echo '</div>';
                        echo '</div>';
                    }
                }
            }
            ?>
            <?php $col_cls = ($flag = true) ? 'col-lg-7' : 'col'; ?>
            <div class="wrap-item-content <?php echo esc_attr($col_cls) ?>">
                <div class="item-content">
                    <?php
                    if ($show_author == 'true' || $show_tags == 'true' || $show_comment == 'true' ){
                        ?>
                        <div class="item-post-metas">
                            <ul class="meta-inner d-flex-wrap align-items-center">
                                <?php if( $show_author == 'true' ) : ?>
                                    <li class="pxl-item--author pxl-post-list pxl-mr-27">
                                        <span class="pxl-icon pxl-mr-10">
                                            <svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="256" height="256">
                                                <title>user</title>
                                                <style>
                                                    .user-svg { fill: #000000 } 
                                                </style>
                                                <path class="user-svg" d="m230.9 212c-15.2-26.3-38.7-45.2-66.1-54.2 27.9-16.5 41.2-49.7 32.6-80.9-8.6-31.3-37-52.9-69.4-52.9-32.4 0-60.8 21.6-69.4 52.9-8.6 31.2 4.7 64.4 32.6 80.9-27.4 9-50.9 27.9-66.1 54.2-1.1 1.8-1.5 4.1-1 6.1 0.6 2.1 1.9 3.9 3.8 5 1.9 1.1 4.1 1.4 6.2 0.8 2-0.6 3.8-2 4.8-3.9 18.9-32.6 52.2-52 89.1-52 36.9 0 70.2 19.4 89.1 52 1 1.9 2.8 3.3 4.8 3.9 2.1 0.6 4.3 0.3 6.2-0.8 1.9-1.1 3.2-2.9 3.8-5 0.5-2 0.1-4.3-1-6.1zm-158.9-116c0-22.6 13.6-43.1 34.6-51.7 20.9-8.7 45-3.9 61 12.1 16 16 20.8 40.1 12.1 61-8.6 21-29.1 34.6-51.7 34.6-7.4 0-14.6-1.5-21.4-4.3-6.8-2.8-13-6.9-18.2-12.1-5.2-5.2-9.3-11.4-12.1-18.2-2.8-6.8-4.3-14-4.3-21.4z"/>
                                            </svg>
                                        </span>
                                        <span class="pxl-name">
                                            <?php esc_html_e('By','hubsteel')?> <a href="<?php echo esc_url(get_author_posts_url($post->post_author, $author->user_nicename)); ?>"><?php echo esc_html($author->display_name); ?></a>
                                        </span>
                                    </li>
                                <?php endif; ?>

                                <?php if ($show_tags == 'true' && ($tags = get_the_tags($post->ID))) : ?>
                                    <li class="pxl-item--tags pxl-post-list pxl-mr-26">
                                        <span class="pxl-icon pxl-mr-10">
                                            <svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="256" height="256">
                                                <title>tag-chevron</title>
                                                <style>
                                                    .tags-svg { fill: #000000 } 
                                                </style>
                                                <path class="tags-svg" d="m246.7 123.6l-45.7-68.5q-1.1-1.6-2.6-2.9-1.4-1.4-3.1-2.3-1.8-0.9-3.7-1.4-1.9-0.5-3.9-0.5h-155.7c-1.4 0-2.9 0.4-4.1 1.1-1.3 0.8-2.3 1.8-3 3.1-0.6 1.3-1 2.7-0.9 4.2 0.1 1.4 0.5 2.8 1.3 4l45.1 67.6-45 67.6c-0.8 1.2-1.3 2.6-1.3 4-0.1 1.4 0.2 2.9 0.9 4.2 0.7 1.2 1.7 2.3 2.9 3 1.2 0.8 2.7 1.2 4.1 1.2h155.7q2 0 3.9-0.5 1.9-0.5 3.7-1.4 1.7-0.9 3.1-2.3 1.5-1.3 2.6-2.9l45.6-68.5q0.7-0.9 1-2.1 0.4-1.1 0.4-2.3 0-1.2-0.3-2.3-0.4-1.2-1-2.1zm-59 68.4h-140.7l39.7-59.6q0.7-0.9 1-2.1 0.4-1.1 0.4-2.3 0-1.2-0.4-2.3-0.3-1.2-1-2.1l-39.7-59.6h140.7l42.7 64z"/>
                                            </svg>
                                        </span>
                                        <?php foreach ($tags as $tag): ?>
                                            <a class="pxl-name" href="<?php echo esc_url(get_tag_link($tag->term_id)); ?>">
                                                <?php echo esc_html($tag->name); ?>
                                            </a>
                                        <?php endforeach; ?>
                                    </li>
                                <?php endif; ?>

                                <?php if($show_comment == 'true') : ?>
                                    <li class="post-comments">
                                        <a class="meta-item post-comment-count" href="<?php echo get_comments_link($post->ID); ?>#comments">
                                            <span class="pxl-icon pxl-mr-10">
                                                <svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="256" height="256">
                                                    <title>chat-teardrop</title>
                                                    <style>
                                                        .chat-svg { fill: #000000 } 
                                                    </style>
                                                    <path class="chat-svg" d="m132 24c-13.1 0-26.1 2.6-38.3 7.6-12.1 5.1-23.1 12.4-32.4 21.7-9.3 9.3-16.6 20.3-21.7 32.4-5 12.2-7.6 25.2-7.6 38.3v84c0 2.1 0.4 4.2 1.2 6.1 0.8 2 2 3.7 3.5 5.2 1.5 1.5 3.2 2.7 5.2 3.5 1.9 0.8 4 1.2 6.1 1.2h84c26.5 0 52-10.5 70.7-29.3 18.8-18.7 29.3-44.2 29.3-70.7 0-26.5-10.5-52-29.3-70.7-18.7-18.8-44.2-29.3-70.7-29.3zm0 184h-84v-84c0-34 20.5-64.6 51.9-77.6 31.3-13 67.5-5.8 91.5 18.2 24 24 31.2 60.2 18.2 91.5-13 31.4-43.6 51.9-77.6 51.9z"/>
                                                </svg>
                                            </span>
                                            <span class="pxl-name">
                                                <?php
                                                    echo comments_number(
                                                        '<span class="cmt-count">0</span> '.esc_html__('Comments', 'hubsteel'),
                                                        '<span class="cmt-count">1</span> '.esc_html__('Comment', 'hubsteel'),
                                                        '<span class="cmt-count">%</span> '.esc_html__('Comments', 'hubsteel'),
                                                        $post->ID
                                                    ); 
                                                ?>
                                            </span>
                                        </a>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </div>
                    <?php
                }
                ?>
                <h3 class="item-title"><a href="<?php echo esc_url(get_permalink( $post->ID )); ?>"><?php echo esc_html(get_the_title($post->ID)); ?></a></h3>
                <?php if($show_excerpt == 'true'): ?>
                    <div class="item-excerpt">
                        <?php
                        if(!empty($post->post_excerpt)){
                            echo wp_trim_words( $post->post_excerpt, $num_words, null );
                        } else{
                            $content = strip_shortcodes( $post->post_content );
                            $content = apply_filters( 'the_content', $content );
                            $content = str_replace(']]>', ']]&gt;', $content);
                            echo wp_trim_words( $content, $num_words, null );
                        }
                        ?>
                    </div>
                <?php endif; ?>
                <?php 
                if($show_readmore == 'true' || $post_share == 'true') : ?>
                    <div class="blog-post-footer  align-items-center justify-content-between">
                        <?php if( $show_readmore == 'true'): ?>
                            <div class="post-readmore ">
                                <a class="btn pxl-icon-active btn-outline pxl-icon--right" href="<?php echo esc_url( get_permalink($post->ID)); ?>">
                                    <span class="pxl-btn-icon pxl-ml-6">
                                        <svg xmlns="http://www.w3.org/2000/svg" fill="#000" height="512" viewBox="0 0 24 24" width="512"><path d="m16.0039 9.414-8.60699 8.607-1.414-1.414 8.60599-8.607h-7.58499v-2h10.99999v11h-2z" fill="rgb(0,0,0)"></path></svg>
                                    </span>
                                    <span class="pxl--btn-text"><?php echo hubsteel_html($readmore_text); ?></span>
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
    ?>
</div>
</div>
<?php
endforeach; 
}