Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/brizy-pro   php

Repository URL to install this package:

Version: 2.3.7 

/ placeholders / post-loop-pagination.php

<?php

use BrizyPlaceholders\ContentPlaceholder;
use BrizyPlaceholders\ContextInterface;

class BrizyPro_Content_Placeholders_PostLoopPagination extends Brizy_Content_Placeholders_Abstract
{

    /**
     * BrizyPro_Content_Placeholders_PostLoopPagination constructor.
     * @throws Exception
     */
    public function __construct()
    {
        $this->placeholder = 'brizy_dc_post_loop_pagination';
        $this->label = 'Post loop pagination';
        $this->setDisplay(self::DISPLAY_BLOCK);
        $this->setGroup(null);
    }

    /**
     * @param ContextInterface $context
     * @param ContentPlaceholder $contentPlaceholder
     * @return false|mixed|string
     */
    public function getValue(ContextInterface $context, ContentPlaceholder $contentPlaceholder)
    {

        global $wp_rewrite;
        $old_pagination_base = $wp_rewrite->pagination_base;
        $wp_rewrite->pagination_base = BrizyPro_Content_Placeholders_PostLoop::getPaginationKey();

        // URL base depends on permalink settings.
        $pagenum_link = html_entity_decode(get_pagenum_link());
        $url_parts = explode('?', $pagenum_link);
        $pagenum_link = trailingslashit($url_parts[0]) . '%_%';
        $format = $wp_rewrite->using_index_permalinks() && !strpos($pagenum_link, 'index.php') ? 'index.php/' : '';
        $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit(BrizyPro_Content_Placeholders_PostLoop::getPaginationKey() . '/%#%', 'paged') : '?' . BrizyPro_Content_Placeholders_PostLoop::getPaginationKey() . '=%#%';

        $attributes = $contentPlaceholder->getAttributes();

        $queryVars = wp_parse_args(isset($attributes['query']) ? $attributes['query'] : '');
        $paginationContext = array();

        $paginationContext['totalCount'] = $this->getPostCount($attributes);
        $attributes['count'] = isset($attributes['count']) ? (int)$attributes['count'] : (isset($queryVars['posts_per_page']) ? $queryVars['posts_per_page'] : '3');
        $paginationContext['pages'] = ceil($paginationContext['totalCount'] / $attributes['count']);
        $paginationContext['page'] = $this->getPageVar();
        $paginationContext['pagination'] = paginate_links(array(
            'prev_next' => false,
            'type' => 'list',
            'format' => $format,
            'current' => $this->getPageVar(),
            'total' => $paginationContext['pages']
        ));
        $wp_rewrite->pagination_base = $old_pagination_base;

        // that is bad.. but there is no easy way to return ids instead of content
        if (isset($attributes['content_type']) && $attributes['content_type'] === 'json') {
            return json_encode([
                'itemsPerPage' => $attributes['count'],
                'totalCount' => $paginationContext['totalCount']
            ]);
        }

        return Brizy_TwigEngine::instance(BRIZY_PRO_PLUGIN_PATH . "/content/views/")->render('pagination.html.twig', $paginationContext);
    }


    /**
     * @param $attributes
     *
     * @return int
     */
    private function getPostCount($attributes)
    {

        $query = null;
        $queryVars = [];

        if (isset($attributes['query']) && !empty($attributes['query'])) {
            $wpParseArgs = wp_parse_args($attributes['query']);

            $params = [
                'fields' => 'ids',
                'posts_per_page' => isset($attributes['count']) ? $attributes['count'] : 3,
                'orderby' => isset($attributes['orderby']) ? $attributes['orderby'] : 'none',
                'order' => isset($attributes['order']) ? $attributes['order'] : 'ASC',
                'post_type' => isset($attributes['post_type']) ? $attributes['post_type'] : array_keys(
                    get_post_types(['public' => true])
                ),
            ];

            $queryVars = array_merge($params, $wpParseArgs);
        } else {
            global $wp_query;
            $queryVars = $wp_query->query_vars;
            $queryVars['orderby'] = isset($attributes['orderby']) ? $attributes['orderby'] : (isset($queryVars['orderby']) ? $queryVars['orderby'] : null);
            $queryVars['order'] = isset($attributes['order']) ? $attributes['order'] : (isset($queryVars['order']) ? $queryVars['order'] : null);
            $queryVars['posts_per_page'] = isset($attributes['count']) ? (int)$attributes['count'] : (isset($queryVars['posts_per_page']) ? $queryVars['posts_per_page'] : null);
            $queryVars['post_type'] = isset($attributes['post_type']) ? $attributes['post_type'] : (isset($queryVars['post_type']) ? $queryVars['post_type'] : null);
            $queryVars['post__not_in'] = array_merge((array)$queryVars['post__not_in']?:[],isset($attributes['post__not_in']) ? explode(',',$attributes['post__not_in']) : []);
            $queryVars['post__in'] = array_merge((array)$queryVars['post__in']?:[],isset($attributes['post__in']) ? explode(',',$attributes['post__in']) : []);

        }

        if (isset($attributes['offset']) && $attributes['offset'] > 0) {
            $ids = $this->getOffsetPostIds($queryVars, $attributes['offset']);
            $queryVars['post__not_in'] = array_merge(isset($queryVars['post__not_in']) ? $queryVars['post__not_in'] : [], $ids);
        }

        $queryVars = apply_filters('brizy_post_loop_args', $queryVars);

        $query = new WP_Query($queryVars);
        $count = $query->found_posts;
        wp_reset_postdata();

        return $count;
    }

    /**
     * @return int|mixed
     */
    private function getPageVar()
    {
        if ($paged = get_query_var(BrizyPro_Content_Placeholders_PostLoop::getPaginationKey())) {
            return (int)$paged;
        }

        return 1;
    }

    private function getOffsetPostIds($params, $offset)
    {

        $params['posts_per_page'] = $offset;
        $params['paged'] = 1;
        $params['fields'] = 'ids';

        $query = new WP_Query($params);
        $ids = $query->posts;
        unset($query);

        return $ids;
    }

}