1: <?php
2: /**
3: * Pagination option
4: *
5: * @package EPL
6: * @subpackage Classes/Pagination
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 2.1
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_Pagination_Call Class
19: *
20: * @since 3.5.3 Check for class
21: */
22: if ( ! class_exists( 'EPL_Pagination_Call' ) ) :
23: /**
24: * EPL_Pagination_Call Class
25: *
26: * @since 2.1
27: * @since 3.4.0 Moved class to separate file.
28: */
29: class EPL_Pagination_Call {
30:
31: /**
32: * Arguments
33: *
34: * @var array
35: * @since 2.1
36: */
37: protected $args;
38:
39: /**
40: * Get things started
41: *
42: * @param array $args Array of arguments.
43: * @since 2.1
44: */
45: public function __construct( $args ) {
46: $this->args = $args;
47: }
48:
49: /**
50: * Get the key
51: *
52: * @param string $key Key name.
53: *
54: * @return mixed
55: * @since 2.1
56: */
57: public function __get( $key ) {
58: return $this->args[ $key ];
59: }
60:
61: /**
62: * Get Pagination arguments
63: *
64: * @since 2.1
65: */
66: public function get_pagination_args() {
67: global $numpages;
68:
69: $query = $this->query;
70:
71: switch ( $this->type ) {
72: case 'multipart':
73: // Multipart page.
74: $posts_per_page = 1;
75: $paged = max( 1, absint( get_query_var( 'page' ) ) );
76: $total_pages = max( 1, $numpages );
77: break;
78: case 'users':
79: // WP_User_Query.
80: $posts_per_page = $query->query_vars['number'];
81: $paged = max( 1, floor( $query->query_vars['offset'] / $posts_per_page ) + 1 );
82: $total_pages = max( 1, ceil( $query->total_users / $posts_per_page ) );
83: break;
84: default:
85: // WP_Query.
86: $posts_per_page = intval( $query->get( 'posts_per_page' ) );
87: $paged = max( 1, absint( $query->get( 'paged' ) ) );
88: $total_pages = max( 1, absint( $query->max_num_pages ) );
89: break;
90: }
91:
92: return array( $posts_per_page, $paged, $total_pages );
93: }
94:
95: /**
96: * Get the single pagination
97: *
98: * @param string $page page id.
99: * @param string $raw_text text content.
100: * @param array $attr attributes of page.
101: * @param string $format format of single page.
102: * @param string $tag attributes of single page tag.
103: *
104: * @return string
105: * @since 2.1
106: */
107: public function get_single( $page, $raw_text, $attr, $format = '%PAGE_NUMBER%', $tag = 'a' ) {
108: if ( empty( $raw_text ) ) {
109: return '';
110: }
111:
112: $text = str_replace( $format, number_format_i18n( $page ), $raw_text );
113: $text = apply_filters( 'epl_pagination_single_content_text', $text, $raw_text );
114:
115: $attr['href'] = $this->get_url( $page );
116:
117: list( $posts_per_page, $paged, $total_pages ) = $this->get_pagination_args();
118: $tag = apply_filters( 'epl_pagination_single_tag', $tag, $page, $paged );
119:
120: return apply_filters( 'epl_pagination_single', epl_pagination_html( $tag, $attr, $text ), $page, $paged, $total_pages, $posts_per_page );
121: }
122:
123: /**
124: * Outputting content of dot sigle elements.
125: *
126: * @since 2.3.1
127: * @param string $tag tag of single dot.
128: * @param string $content content of single dot.
129: * @param array $attributes attributes of single dot tag.
130: * @return string
131: */
132: public function get_single_dot( $tag = 'span', $content = '...', array $attributes = array() ) {
133: $tag = apply_filters( 'epl_pagination_single_dot_tag', $tag );
134: $content = apply_filters( 'epl_pagination_single_dot_content', $content );
135: $attributes = apply_filters( 'epl_pagination_single_dot_attributes', $attributes );
136:
137: $output = '<' . $tag;
138: if ( is_array( $attributes ) && count( $attributes ) ) {
139: foreach ( $attributes as $key => $value ) {
140: if ( ! empty( $key ) && ! empty( $value ) ) {
141: $output .= ' ' . $key . '="' . $value . '"';
142: }
143: }
144: }
145: // Tag is self closed.
146: if ( in_array( $tag, array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta' ), true ) ) {
147: $output .= ' />';
148: } else {
149: // Tag is not self closed.
150: $output .= '>' . $content . '</' . $tag . '>';
151: }
152:
153: return apply_filters( 'epl_pagination_single_dot', $output );
154: }
155:
156: /**
157: * Get url
158: *
159: * @param string $page Page number.
160: *
161: * @return string
162: *
163: * @since 2.1
164: * @since 3.5.1 Fixed shortcode pagination when permalinks are plain.
165: * @since 3.5.3 Fixed sorting not working for pagination on shortcode.
166: */
167: public function get_url( $page ) {
168: $link = ( 'multipart' === $this->type ) ? epl_get_multipage_link( $page ) : get_pagenum_link( $page );
169:
170: if ( $this->query->get( 'is_epl_shortcode' ) &&
171: in_array( $this->query->get( 'epl_shortcode_name' ), epl_get_shortcode_list(), true ) ) {
172: $permalink_structure = get_option( 'permalink_structure' );
173:
174: if ( empty( $permalink_structure ) ) {
175: $link = epl_add_or_update_params( $link, 'paged', $page );
176: }
177: $link = epl_add_or_update_params( $link, 'pagination_id', $this->query->get( 'instance_id' ) );
178: $link = epl_add_or_update_params( $link, 'instance_id', $this->query->get( 'instance_id' ) );
179: }
180:
181: return $link;
182: }
183: }
184: endif; // End class_exists check.
185: