<?php
class BestPosts
{
public static $cache_key = 'grid___cache_best_post_';
private $posts = [];
public function __construct()
{
if (!is_singular() && !is_archive()) {
add_filter('grid__best_posts_array', [$this, 'fixed_posts'], 10, 1);
add_action('grid__best_posts', [$this, "block"], 10, 2);
}
add_action('updated_postmeta', [$this, 'clear_cache'], 20, 3);
add_filter('grid__best_posts_title', function ($title, $offset) {
if (0 < $offset) {
return __return_empty_string();
}
return $title;
}, 10, 2);
}
public function clear_cache($meta_id, $object_id, $meta_key)
{
if ('in_best' == $meta_key) {
delete_transient(self::$cache_key . 0);
delete_transient(self::$cache_key . 5);
}
}
public function block($item, $offset)
{
if (0 < $offset) {
$this->posts = array_slice($this->get_posts($offset), 0, -1);
} else {
$this->posts = apply_filters('grid__best_posts_array', $this->get_posts($offset));
}
echo $this->block_html($offset);
}
public function block_html($offset)
{
$transient_key = self::$cache_key . $offset;
$cached = get_transient($transient_key);
if ($cached !== false) {
return $cached;
}
$title = apply_filters('grid__best_posts_title', 'Топ лучших статей', $offset);
$res = '';
$res .= "<div class='grid__best-posts-wrapper'>";
$res .= "<div class='grid__best-posts-wrap'>";
$res .= "<div class='grid__best-posts'>";
if (!empty($title)) {
$res .= " <div class='grid__best-posts-title'>";
$res .= apply_filters('grid__best_posts_title', 'Топ лучших статей', $offset);
$res .= "</div>";
}
$res .= "<div class='grid__best-posts-items'>";
$i = $offset;
foreach ($this->posts as $post):
$i++;
$link = get_the_permalink($post);
$res .= "<div class='grid__best-posts-item'>";
$res .= "<div class='grid__best-posts-item-number'>";
$res .= $i;
$res .= "</div>";
$res .= "<a href='{$link}' class='grid__best-posts-item-title'>";
$res .= get_the_title($post);
$res .= "</a>";
$res .= "</div>";
endforeach;
$res .= "</div>";
$res .= "</div>";
$res .= "</div>";
$res .= "</div>";
set_transient($transient_key, $res, 24 * HOUR_IN_SECONDS);
return $res;
}
public function fixed_posts($posts)
{
$date = new DateTime(date('Y-m-d') . " 23:59:00");
$timestamp = $date->getTimestamp();
$args = [
'post_type' => ['post', 'advert_post', 'hub'],
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'meta_value_num',
'category__not_in' => [6],
'post_status' => 'publish',
'meta_query' => [
'best' => [
'key' => 'in_best',
'value' => $timestamp,
'compare' => '>=',
'type' => 'NUMERIC'
],
]
];
$best_posts = get_posts($args);
$best_posts_ids = array_map(function ($elem) {
return $elem->ID;
}, $best_posts);
$posts = array_filter($posts, function ($elem) use ($best_posts_ids) {
if (in_array($elem->ID, $best_posts_ids)) {
return false;
}
return true;
});
$merge = array_merge($best_posts, $posts);
return array_slice($merge, 0, 5);
}
public function get_posts($offset = 0)
{
$args = [
'offset' => $offset,
'post_type' => ['post', 'advert_post'],
'posts_per_page' => 5,
'order' => 'DESC',
'category__not_in' => [6],
'post__not_in' => [ 609503 ],
'post_status' => 'publish',
'orderby' => 'meta_value_num',
'meta_key' => apply_filters('pvc__mata-key',''),
'date_query' => [
array(
'after' => '7 days ago',
'before' => '1 days ago',
'inclusive' => true,
)
]
];
$posts = get_posts(apply_filters("grid__best_posts_args", $args, 'in_date'));
if (5 <= count($posts)) {
return $posts;
}
unset($args['date_query']);
$posts = get_posts(apply_filters("grid__best_posts_args", $args, 'out_date'));
return $posts;
}
}