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

200 lines
6.6 KiB
PHP

<?php
get_header();
?>
<?php
global $wpdb;
$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'])) {
$page = $_GET['page'];
}
$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'
];
?>
<main class="main-content container">
<div class="home-wrapper">
<div class="title">blog</div>
<div class="content-section__body">
<div class="sub-title d-flex flex-row justify-content-between align-items-end">
<div class="line"></div>
<div class="social-share">
<?php echo do_shortcode('[Sassy_Social_Share]') ?>
</div>
</div>
</div>
<div class="post-list-filter row no-gutter mt-4">
<div class="col-md-12 d-flex justify-content-between">
<div class="post-list-filter__post-type d-inline-block">
<h2>
<?php if (!empty($filterMap[$post_filter])): ?>
<?php echo $filterMap[$post_filter] ?>
<?php endif; ?>
</h2>
</div>
<div class="post-list-filter__dropdown d-inline-block dropdown">
<span style="font-size: 30px" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-sliders-h" style="cursor: pointer"></i>
</span>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="/?post_filter=recent">Recent</a>
<a class="dropdown-item" href="/?post_filter=trending">Trending</a>
<a class="dropdown-item" href="/?post_filter=hottest_stories">Hottest Stories</a>
<a class="dropdown-item" href="/?post_filter=newest">Newest</a>
<a class="dropdown-item" href="/?post_filter=oldest">Oldest</a>
</div>
</div>
</div>
</div>
<div class="dynamic-content">
<?php
switch ($current_country) {
case 'SG':
get_template_part('template-parts/home_template/default');
break;
case 'US':
get_template_part('template-parts/home_template/default');
break;
default:
get_template_part('template-parts/home_template/default');
}
?>
</div>
<div class="pagination-block text-center">
<?php for ($i = 1; $i <= $total_page; $i++) { ?>
<a href="/?post_filter=<?= $post_filter ?>&page=<?= $i ?>"class="btn btn-page btn-default <?php echo $i === intval($page) ? 'active' : '' ?>" data-page="<?php echo $i ?>"><?php echo $i ?></a>
<?php } ?>
</div>
</div>
</main>
<?php
get_footer();
?>