/**
  * we have to render the block first in the buffer, to avoid two queries.
  * - IF there are posts, we already have the buffer with the block's content and we don't have to make another query
  * - IF there are no posts, we ignore the buffer and we can also remove the wraps in the child classes of this class
  */
 protected function render_posts_to_buffer()
 {
     // get the global category top posts grid style setting
     $td_grid_style = td_util::get_option('tds_category_td_grid_style');
     $limit = td_api_category_top_posts_style::_helper_get_posts_shown_in_the_loop();
     $block_name = td_api_category_top_posts_style::get_key(get_class($this), 'td_block_name');
     // overwrite the $td_grid_style if the setting for this category was changed
     $td_grid_style_per_category_setting = td_util::get_category_option(td_global::$current_category_obj->cat_ID, 'tdc_category_td_grid_style');
     if ($td_grid_style_per_category_setting != '') {
         $td_grid_style = $td_grid_style_per_category_setting;
     }
     // we have to have a default grid, there seems to be a problem with the grid styles
     if (empty($td_grid_style)) {
         $td_grid_style = 'td-grid-style-1';
     }
     //parameters to filter to for big grid
     $atts_for_big_grid = array('limit' => $limit, 'category_id' => td_global::$current_category_obj->cat_ID, 'sort' => get_query_var('filter_by'), 'td_grid_style' => $td_grid_style);
     //show the big grid
     $block_instance = td_global_blocks::get_instance($block_name);
     $this->rendered_block_buffer = $block_instance->render($atts_for_big_grid);
     $this->rendered_posts_count = $block_instance->td_query->post_count;
     if ($this->rendered_posts_count > 0) {
         td_global::$custom_no_posts_message = false;
     }
     // use class_name($this) to get the id :)
 }
function td_modify_main_query_for_category_page($query)
{
    //checking for category page and main query
    if (!is_admin() and is_category() and $query->is_main_query()) {
        // get the category object - with or without permalinks
        if (empty($query->query_vars['cat'])) {
            td_global::$current_category_obj = get_category_by_path(get_query_var('category_name'), false);
            // when we have permalinks, we have to get the category object like this.
        } else {
            td_global::$current_category_obj = get_category($query->query_vars['cat']);
        }
        // we are on a category page with an ID that doesn't exists - wp will show a 404 and we do nothing
        if (is_null(td_global::$current_category_obj)) {
            return;
        }
        //get the number of page where on
        $paged = get_query_var('paged');
        //get the `filter_by` URL($_GET) variable
        $filter_by = get_query_var('filter_by');
        //get the limit of posts on the category page
        $limit = get_option('posts_per_page');
        //echo $filter_by;
        switch ($filter_by) {
            case 'featured':
                //get the category object
                $query->set('category_name', td_global::$current_category_obj->slug);
                $query->set('cat', get_cat_ID(TD_FEATURED_CAT));
                //add the fetured cat
                break;
            case 'popular':
                $query->set('meta_key', td_page_views::$post_view_counter_key);
                $query->set('orderby', 'meta_value_num');
                $query->set('order', 'DESC');
                break;
            case 'popular7':
                $query->set('meta_key', td_page_views::$post_view_counter_7_day_total);
                $query->set('orderby', 'meta_value_num');
                $query->set('order', 'DESC');
                break;
            case 'review_high':
                $query->set('meta_key', td_review::$td_review_key);
                $query->set('orderby', 'meta_value_num');
                $query->set('order', 'DESC');
                break;
            case 'random_posts':
                $query->set('orderby', 'rand');
                break;
        }
        //end switch
        // how many posts are we showing in the big grid for this category
        $offset = td_api_category_top_posts_style::_helper_get_posts_shown_in_the_loop();
        // offset + custom pagination - if we have offset, WordPress overwrites the pagination and works with offset + limit
        if (empty($query->is_feed)) {
            if (!empty($offset) and $paged > 1) {
                $query->set('offset', $offset + ($paged - 1) * $limit);
            } else {
                $query->set('offset', $offset);
            }
        }
        //print_r($query);
    }
    //end if main query
}
 /**
  * WARNING: this function also runs in the page-pagebuilder-latest.php in a FAKE LOOP - this means that wordpress functions
  * like is_category DO NOT WORK AS EXPECTED when you use for example a category filter for the loop, is_category returns true
  */
 static function get_pagination()
 {
     global $wp_query;
     if (td_global::$current_template == '404') {
         return;
     }
     // if we have infinite pagination, we will render it there
     if (self::render_infinite_pagination() === true) {
         return;
     }
     /**
      * use normal pagination
      */
     $pagenavi_options = self::pagenavi_init();
     $request = $wp_query->request;
     $posts_per_page = intval(get_query_var('posts_per_page'));
     $paged = intval(get_query_var('paged'));
     $numposts = $wp_query->found_posts;
     $max_page = $wp_query->max_num_pages;
     // hack for category pages - pagination
     // we also have to check for page-pagebuilder-latest.php template because we are running there in a FAKE loop and if the category
     // filter is active for that loop, WordPress believes that we are on a category
     if (!is_admin() and td_global::$current_template != 'page-homepage-loop' and is_category()) {
         $numposts = $wp_query->found_posts - td_api_category_top_posts_style::_helper_get_posts_shown_in_the_loop();
         // fix the pagination, we have x less posts because the rest are in the top posts loop
         $max_page = ceil($numposts / $posts_per_page);
     }
     if (empty($paged) || $paged == 0) {
         $paged = 1;
     }
     $pages_to_show = intval($pagenavi_options['num_pages']);
     $larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
     $larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
     $pages_to_show_minus_1 = $pages_to_show - 1;
     $half_page_start = floor($pages_to_show_minus_1 / 2);
     $half_page_end = ceil($pages_to_show_minus_1 / 2);
     $start_page = $paged - $half_page_start;
     if ($start_page <= 0) {
         $start_page = 1;
     }
     $end_page = $paged + $half_page_end;
     if ($end_page - $start_page != $pages_to_show_minus_1) {
         $end_page = $start_page + $pages_to_show_minus_1;
     }
     if ($end_page > $max_page) {
         $start_page = $max_page - $pages_to_show_minus_1;
         $end_page = $max_page;
     }
     if ($start_page <= 0) {
         $start_page = 1;
     }
     $larger_per_page = $larger_page_to_show * $larger_page_multiple;
     $larger_start_page_start = self::td_round_number($start_page, 10) + $larger_page_multiple - $larger_per_page;
     $larger_start_page_end = self::td_round_number($start_page, 10) + $larger_page_multiple;
     $larger_end_page_start = self::td_round_number($end_page, 10) + $larger_page_multiple;
     $larger_end_page_end = self::td_round_number($end_page, 10) + $larger_per_page;
     if ($larger_start_page_end - $larger_page_multiple == $start_page) {
         $larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
         $larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
     }
     if ($larger_start_page_start <= 0) {
         $larger_start_page_start = $larger_page_multiple;
     }
     if ($larger_start_page_end > $max_page) {
         $larger_start_page_end = $max_page;
     }
     if ($larger_end_page_end > $max_page) {
         $larger_end_page_end = $max_page;
     }
     if ($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
         $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
         $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
         echo '<div class="page-nav td-pb-padding-side">';
         previous_posts_link($pagenavi_options['prev_text']);
         if ($start_page >= 2 && $pages_to_show < $max_page) {
             $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
             echo '<a href="' . esc_url(get_pagenum_link()) . '" class="first" title="' . $first_page_text . '">' . $first_page_text . '</a>';
             if (!empty($pagenavi_options['dotleft_text']) && $start_page > 2) {
                 echo '<span class="extend">' . $pagenavi_options['dotleft_text'] . '</span>';
             }
         }
         //            if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
         //                for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
         //                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
         //                    echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
         //                }
         //	            echo '<span class="extend">'.$pagenavi_options['dotleft_text'].'</span>';
         //            }
         for ($i = $start_page; $i <= $end_page; $i++) {
             if ($i == $paged) {
                 $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
                 echo '<span class="current">' . $current_page_text . '</span>';
             } else {
                 $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                 echo '<a href="' . esc_url(get_pagenum_link($i)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
             }
         }
         //            if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
         //	            echo '<span class="extend">'.$pagenavi_options['dotright_text'].'</span>';
         //                for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
         //                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
         //                    echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
         //                }
         //            }
         if ($end_page < $max_page) {
             if (!empty($pagenavi_options['dotright_text']) && $end_page + 1 < $max_page) {
                 echo '<span class="extend">' . $pagenavi_options['dotright_text'] . '</span>';
             }
             $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
             echo '<a href="' . esc_url(get_pagenum_link($max_page)) . '" class="last" title="' . $last_page_text . '">' . $last_page_text . '</a>';
         }
         next_posts_link($pagenavi_options['next_text'], $max_page);
         if (!empty($pages_text)) {
             echo '<span class="pages">' . $pages_text . '</span>';
         }
         echo '</div>';
     }
 }