/**
  * Get pagination.
  * @api
  * @param array   $prefs
  * @return array mixed
  */
 public static function get_pagination($prefs)
 {
     global $wp_query;
     global $paged;
     global $wp_rewrite;
     $args = array();
     $args['total'] = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
     if ($wp_rewrite->using_permalinks()) {
         $url = explode('?', get_pagenum_link(0));
         if (isset($url[1])) {
             parse_str($url[1], $query);
             $args['add_args'] = $query;
         }
         $args['format'] = $wp_rewrite->pagination_base . '/%#%';
         $args['base'] = trailingslashit($url[0]) . '%_%';
     } else {
         $big = 999999999;
         $args['base'] = str_replace($big, '%#%', esc_url(get_pagenum_link($big)));
     }
     $args['type'] = 'array';
     $args['current'] = max(1, get_query_var('paged'));
     $args['mid_size'] = max(9 - $args['current'], 3);
     if (is_int($prefs)) {
         $args['mid_size'] = $prefs - 2;
     } else {
         $args = array_merge($args, $prefs);
     }
     $data = array();
     $data['current'] = $args['current'];
     $data['total'] = $args['total'];
     $data['pages'] = Pagination::paginate_links($args);
     if ($data['total'] <= count($data['pages'])) {
         // decrement current so that it matches up with the 0 based index used by the pages array
         $current = $data['current'] - 1;
     } else {
         // $data['current'] can't be used b/c there are more than 10 pages and we are condensing with dots
         foreach ($data['pages'] as $key => $page) {
             if (!empty($page['current'])) {
                 $current = $key;
                 break;
             }
         }
     }
     // set next and prev using pages array generated by paginate links
     if (isset($current) && isset($data['pages'][$current + 1])) {
         $data['next'] = array('link' => user_trailingslashit($data['pages'][$current + 1]['link']), 'class' => 'page-numbers next');
         if (Pagination::is_search_query($data['next']['link'])) {
             $data['next']['link'] = untrailingslashit($data['next']['link']);
         }
     }
     if (isset($current) && isset($data['pages'][$current - 1])) {
         $data['prev'] = array('link' => user_trailingslashit($data['pages'][$current - 1]['link']), 'class' => 'page-numbers prev');
         if (Pagination::is_search_query($data['prev']['link'])) {
             $data['prev']['link'] = untrailingslashit($data['prev']['link']);
         }
     }
     if ($paged < 2) {
         $data['prev'] = '';
     }
     if ($data['total'] === (double) 0) {
         $data['next'] = '';
     }
     return $data;
 }
Example #2
0
 protected function init($prefs = array(), $wp_query = null)
 {
     if (!$wp_query) {
         global $wp_query;
     }
     // use the current page from the provided query if available; else fall back to the global
     $paged = isset($wp_query->query_vars['paged']) ? $wp_query->query_vars['paged'] : get_query_var('paged');
     global $wp_rewrite;
     $args = array();
     // calculate the total number of pages based on found posts and posts per page
     $ppp = 10;
     if (isset($wp_query->query_vars['posts_per_page'])) {
         $ppp = $wp_query->query_vars['posts_per_page'];
     }
     $args['total'] = ceil($wp_query->found_posts / $ppp);
     if ($wp_rewrite->using_permalinks()) {
         $url = explode('?', get_pagenum_link(0));
         if (isset($url[1])) {
             parse_str($url[1], $query);
             $args['add_args'] = $query;
         }
         $args['format'] = $wp_rewrite->pagination_base . '/%#%';
         $args['base'] = trailingslashit($url[0]) . '%_%';
     } else {
         $big = 999999999;
         $args['base'] = str_replace($big, '%#%', esc_url(get_pagenum_link($big)));
     }
     $args['type'] = 'array';
     $args['current'] = max(1, $paged);
     $args['mid_size'] = max(9 - $args['current'], 3);
     if (is_int($prefs)) {
         $args['mid_size'] = $prefs - 2;
     } else {
         $args = array_merge($args, $prefs);
     }
     $this->current = $args['current'];
     $this->total = $args['total'];
     $this->pages = Pagination::paginate_links($args);
     if ($this->total <= count($this->pages)) {
         // decrement current so that it matches up with the 0 based index used by the pages array
         $current = $this->current - 1;
     } else {
         // $data['current'] can't be used b/c there are more than 10 pages and we are condensing with dots
         foreach ($this->pages as $key => $page) {
             if (!empty($page['current'])) {
                 $current = $key;
                 break;
             }
         }
     }
     // set next and prev using pages array generated by paginate links
     if (isset($current) && isset($this->pages[$current + 1])) {
         $this->next = array('link' => user_trailingslashit($this->pages[$current + 1]['link']), 'class' => 'page-numbers next');
         if (Pagination::is_search_query($this->next['link'])) {
             $this->next['link'] = untrailingslashit($this->next['link']);
         }
     }
     if (isset($current) && isset($this->pages[$current - 1])) {
         $this->prev = array('link' => user_trailingslashit($this->pages[$current - 1]['link']), 'class' => 'page-numbers prev');
         if (Pagination::is_search_query($this->prev['link'])) {
             $this->prev['link'] = untrailingslashit($this->prev['link']);
         }
     }
     if ($paged < 2) {
         $this->prev = '';
     }
     if ($this->total === (double) 0) {
         $this->next = '';
     }
 }