Files
DESKTOP-GBA0BK8\Admin 7c8c8b1c76 first commit
2023-04-08 12:19:53 -04:00

268 lines
8.7 KiB
PHP

<?php
namespace WPC\Widgets;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use WP_Query;
if (!defined('ABSPATH')) exit; // Exit if accessed directly
class CustomPostGrid extends Widget_Base {
public function get_name(){
return 'custom-post-grid';
}
public function get_title(){
return 'Custom Post Grid';
}
public function get_icon(){
return 'fa fa-th';
}
public function get_categories(){
return ['basic'];
}
protected function _register_controls(){
$this->start_controls_section(
'section_content',
[
'label' => 'Settings',
]
);
$this->end_controls_section();
}
protected function render(){
$settings = $this->get_settings_for_display();
$location = get_user_location();
$current_country = $location['country_code'];
// default settings
$post_filter = 'recent';
$posts_per_page = 6;
$page = 1;
if (!empty($_GET['post_filter'])) {
$post_filter = $_GET['post_filter'];
}
if (!empty($_GET['page_no'])) {
$page = $_GET['page_no'];
}
$args = [
'posts_per_page' => $posts_per_page,
'post_type' => 'post',
'post_status' => array('publish'),
'paged' => $page,
];
if ($post_filter == 'recent') {
$args = array_merge($args, [
'ignore_sticky_posts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
]);
} else if ($post_filter == 'newest') {
$args = array_merge($args, [
'ignore_sticky_posts' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
]);
} else if ($post_filter == 'trending') {
$args = array_merge($args, [
'ignore_sticky_posts' => 1,
'orderby' => 'ID',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'is_trending',
'value' => '1',
'compare' => '=='
)
)
]);
} else if ($post_filter == 'hottest_stories') {
$args = array_merge($args, [
'ignore_sticky_posts' => 1,
'meta_key' => 'float_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
]);
} else if ($post_filter == 'oldest') {
$args = array_merge($args, [
'ignore_sticky_posts' => 1,
'order' => 'ASC'
]);
}
// Insert query by country
if (isset($args['meta_query'])) {
$metaQuery = $args['meta_query'];
$metaQuery = array_merge($metaQuery, [
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'location',
'value' => $current_country,
'compare' => 'LIKE',
),
array(
'key'=> 'location',
'compare' => 'NOT EXISTS'
),
array(
'key'=> 'location',
'value' => '',
'compare' => '='
)
)
]);
$args['meta_query'] = $metaQuery;
} else {
$args = array_merge($args, [
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'location',
'value' => $current_country,
'compare' => 'LIKE',
),
array(
'key'=> 'location',
'compare' => 'NOT EXISTS'
),
array(
'key'=> 'location',
'value' => '',
'compare' => '='
)
)
]);
}
$count_query_args = $args;
$count_query_args['posts_per_page'] = -1;
$post_query = new WP_Query( $args );
$count_query = new WP_Query($count_query_args);
$posts = $post_query->posts;
$count = $count_query->post_count;
if ($count === 0 || $count < $posts_per_page) {
$total_page = 0;
} else {
$total_page = intval($count) / intval($posts_per_page);
$total_page = $total_page / intval($total_page) > 0 ? intval($total_page) + 1 : intval($total_page);
}
$filterMap = [
'recent' => 'Recent',
'trending' => 'Trending',
'hottest_stories' => 'Hottest Stories',
'newest' => 'Newest',
'oldest' => 'Oldest'
];
// Filter block
$html =
'<div class="filter-wrapper">
<h2>' . $filterMap[$post_filter] . '</h2>
<div class="dropdown">
<button onclick="customDropdown()" class="dropbtn"><i id="icon" class="filter-icon fas fa-sliders-h"></i></button>
<div id="myDropdown" class="dropdown-content">
<a href="/?post_filter=recent">Recent</a>
<a href="/?post_filter=trending">Trending</a>
<a href="/?post_filter=hottest_stories">Hottest Stories</a>
<a href="/?post_filter=newest">Newest</a>
<a href="/?post_filter=oldest">Oldest</a>
</div>
</div>
</div>';
// End filter block
// Post grid block
$html .= '<div class="custom-post-grid-block">';
foreach ($posts as $post) {
// Get co-authors
$authors = [];
$co_authors = get_coauthors($post->ID);
foreach($co_authors as $co_author) {
if(get_class($co_author) !== 'WP_User') {
array_push($authors, $co_author->display_name);
continue;
}
array_push($authors, $co_author->data->display_name);
}
if (empty($authors)) {
$post_author = get_the_author_meta( 'display_name' , get_post_field('post_author') );
array_push($authors, $post_author);
}
$authors = join(', ', $authors);
$post_content = strip_tags( $post->post_content );
$post_content = strip_shortcodes( $post_content );
$post_content = str_replace( array("\n", "\r", "\t"), ' ', $post_content );
$post_content = trim( $post_content );
$post_content = mb_substr( $post_content, 0, 300, 'utf8' );
$comment_count = '';
if ($post->comment_count) {
$comment_count = '<i class="icon-bubble" aria-hidden="true"></i>' . $post->comment_count;
}
$html .=
'<div class="post-wrapper">
<div>
<a href="' . get_permalink($post->ID) . '">
<div class="thumbnail" style="background-image: url(' . get_the_post_thumbnail_url($post->ID) . ');"></div>
</a>
</div>
<div class="content-wrapper">
<h2 class="title">
<a class="text-truncate-2-line" href="' . get_permalink($post->ID) . '" title="'. $post->post_title .'">
' . $post->post_title . '
</a>
</h2>
<div class="meta text-truncate-1-line" title="' . $authors . ' - ' . get_the_date( 'F j, Y', $post->ID ) . '">
' . $authors . ' - ' . get_the_date( 'F j, Y', $post->ID ) . ' ' . $comment_count .'
</div>
<p class="post-content text-truncate-2-line">' . $post_content . '</p>
<a class="read-more" href="' . get_permalink($post->ID) .'">Read More ></a>
</div>
</div>';
}
$html .= '</div>';
// End post grid block
// Pagination block
$html .= '<div class="custom-pagination">';
$html .= '<ul>';
for ($i = 1; $i <= $total_page; $i++) {
$html .= '<a href="/?post_filter=' . $post_filter . '&page_no=' . $i . '" class="' . ($i === intval($page) ? 'is-active' : '') . '" data-page="'. $i . '"><li>' . $i . '</li></a>';
}
$html .= '</ul>';
$html .= '</div>';
// End pagination block
echo $html;
}
}