Advertisement
elvishp2006

pagination.php

May 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2. namespace Unilever\ATH\Theme;
  3.  
  4. if ( ! function_exists( 'add_action' ) ) {
  5.     exit(0);
  6. }
  7.  
  8. class Pagination
  9. {
  10.     /**
  11.      * args
  12.      *
  13.      * @since 0.0.1
  14.      * @var array
  15.      */
  16.     private $args = array();
  17.  
  18.     /**
  19.      * WP_Query wordpress
  20.      *
  21.      * @since 0.0.1
  22.      * @var object
  23.      */
  24.     private $wp_query;
  25.  
  26.     public function __construct( $args = array(), $attrs = array() )
  27.     {
  28.         $defaults = array(
  29.             'template'        => '',
  30.             'class_container' => '',
  31.             'class_button'    => 'btn-s',
  32.             'class_title'     => 'btn-s-text',
  33.             'title'           => __( 'Show More', App::TEXTDOMAIN ),
  34.             'paged'           => 1,
  35.             'lazyload'        => 3,
  36.             'show_always'     => false,
  37.             'query'           => null,
  38.             'wait'            => __( 'Wait...', App::TEXTDOMAIN ),
  39.         );
  40.  
  41.         $this->initialize( wp_parse_args( $args, $defaults ), $attrs );
  42.     }
  43.  
  44.     public function initialize( $args, $attrs = array() )
  45.     {
  46.         $this->args = $args;
  47.         $this->set_query();
  48.  
  49.         if ( ! $this->is_pages() )
  50.             return;
  51.  
  52.         $this->render( $attrs );
  53.     }
  54.  
  55.     public function set_query()
  56.     {
  57.         global $wp_query;
  58.  
  59.         if ( is_null( $this->args['query'] ) ) {
  60.             $this->args['query'] = $wp_query;
  61.         }
  62.  
  63.         $this->wp_query = $this->args['query'];
  64.     }
  65.  
  66.     public function is_pages()
  67.     {
  68.         return ( $this->wp_query->max_num_pages > 1 || $this->args['show_always'] );
  69.     }
  70.  
  71.     public function render( $attrs = array() )
  72.     {
  73.         $defaults = array(
  74.             'data-query'         => json_encode( $this->wp_query->query ),
  75.             'data-max'           => $this->wp_query->max_num_pages,
  76.             'data-paged'         => $this->args['paged'],
  77.             'data-template'      => $this->args['template'],
  78.             'data-load-more'     => $this->args['lazyload'],
  79.             'class'              => $this->args['class_container'],
  80.             'style'              => ( $this->wp_query->max_num_pages <= 1 ) ? 'display:none' : '',
  81.             'data-label-wait'    => $this->args['wait'],
  82.             'data-label-trigger' => $this->args['title'],
  83.         );
  84.  
  85.         $attrs = wp_parse_args( $attrs, $defaults );
  86.  
  87.         ?>
  88.         <div data-component="paginator" <?php echo $this->get_attr_html( $attrs ); ?>>
  89.             <a class="<?php echo esc_attr( $this->args['class_button'] ); ?>" data-action="next-page" data-element="trigger" href="javascript:void(0);">
  90.                 <span class="<?php echo esc_attr( $this->args['class_title'] ); ?>"><?php echo esc_html( $this->args['title'] ); ?></span>
  91.             </a>
  92.         </div>
  93.         <?php
  94.     }
  95.  
  96.     public function get_attr_html( $attrs )
  97.     {
  98.         $fields = array();
  99.  
  100.         foreach ( $attrs as $attr => $value ) {
  101.             $fields[] = "{$attr}='{$value}'";
  102.         }
  103.  
  104.         return implode( ' ', $fields );
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement