1: <?php
2: /**
3: * Pagination option
4: *
5: * @package EPL
6: * @subpackage Functions/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: * Pagination function
19: *
20: * @param array $args Arguments.
21: *
22: * @return mixed|string|void
23: * @since 2.1
24: * @since 3.5 Added accessibility labels to select elements.
25: */
26: function epl_fancy_pagination( $args = array() ) {
27: if ( ! is_array( $args ) ) {
28: $argv = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
29:
30: $args = array();
31: foreach ( array( 'before', 'after', 'options' ) as $i => $key ) {
32: $args[ $key ] = isset( $argv[ $i ] ) ? $argv[ $i ] : '';
33: }
34: }
35:
36: $args = wp_parse_args(
37: $args,
38: array(
39: 'before' => '',
40: 'after' => '',
41: 'options' => array(),
42: 'query' => $GLOBALS['wp_query'],
43: 'type' => 'posts',
44: 'echo' => true,
45: )
46: );
47:
48: foreach ( $args as $args_key => $args_value ) {
49: ${$args_key} = $args_value;
50: }
51:
52: $options = array(
53: 'pages_text' => __( 'Page %CURRENT_PAGE% of %TOTAL_PAGES%', 'easy-property-listings' ),
54: 'current_text' => '%PAGE_NUMBER%',
55: 'page_text' => '%PAGE_NUMBER%',
56: 'first_text' => __( '&laquo; First', 'easy-property-listings' ),
57: 'last_text' => __( 'Last &raquo;', 'easy-property-listings' ),
58: 'prev_text' => __( '&laquo;', 'easy-property-listings' ),
59: 'next_text' => __( '&raquo;', 'easy-property-listings' ),
60: 'dotleft_text' => __( '...', 'easy-property-listings' ),
61: 'dotright_text' => __( '...', 'easy-property-listings' ),
62: 'num_pages' => 5,
63: 'num_larger_page_numbers' => 3,
64: 'larger_page_numbers_multiple' => 10,
65: 'always_show' => false,
66: 'use_pagenavi_css' => true,
67: 'style' => 1,
68: );
69: $options = apply_filters( 'epl_pagination_options', $options );
70: $instance = new EPL_Pagination_Call( $args );
71:
72: list( $posts_per_page, $paged, $total_pages ) = $instance->get_pagination_args();
73:
74: if ( 1 === $total_pages && ! $options['always_show'] ) {
75: return;
76: }
77:
78: $pages_to_show = absint( $options['num_pages'] );
79: $larger_page_to_show = absint( $options['num_larger_page_numbers'] );
80: $larger_page_multiple = absint( $options['larger_page_numbers_multiple'] );
81: $pages_to_show_minus_1 = $pages_to_show - 1;
82: $half_page_start = floor( $pages_to_show_minus_1 / 2 );
83: $half_page_end = ceil( $pages_to_show_minus_1 / 2 );
84: $start_page = $paged - $half_page_start;
85:
86: if ( $start_page <= 0 ) {
87: $start_page = 1;
88: }
89:
90: $end_page = $paged + $half_page_end;
91:
92: if ( ( $end_page - $start_page ) !== $pages_to_show_minus_1 ) {
93: $end_page = $start_page + $pages_to_show_minus_1;
94: }
95:
96: if ( $end_page > $total_pages ) {
97: $start_page = $total_pages - $pages_to_show_minus_1;
98: $end_page = $total_pages;
99: }
100:
101: if ( $start_page < 1 ) {
102: $start_page = 1;
103: }
104:
105: $out = '';
106: switch ( intval( $options['style'] ) ) {
107: // Normal.
108: case 1:
109: // Text.
110: if ( ! empty( $options['pages_text'] ) ) {
111: $pages_text = str_replace(
112: array( '%CURRENT_PAGE%', '%TOTAL_PAGES%' ),
113: array( number_format_i18n( $paged ), number_format_i18n( $total_pages ) ),
114: $options['pages_text']
115: );
116: $out .= "<span class='pages'>$pages_text</span>";
117: }
118:
119: $out = apply_filters( 'epl_pagination_before_page_numbers', $out, $start_page, $end_page );
120: if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
121: // First.
122: $first_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $total_pages ), $options['first_text'] );
123: $out .= $instance->get_single(
124: 1,
125: $first_text,
126: array(
127: 'class' => 'first',
128: ),
129: '%TOTAL_PAGES%'
130: );
131: }
132:
133: // Previous.
134: if ( $paged > 1 && ! empty( $options['prev_text'] ) ) {
135: $out .= $instance->get_single(
136: $paged - 1,
137: $options['prev_text'],
138: array(
139: 'class' => 'previouspostslink',
140: 'rel' => 'prev',
141: )
142: );
143: }
144:
145: if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
146: if ( ! empty( $options['dotleft_text'] ) ) {
147: $out .= $instance->get_single_dot( 'span', $options['dotleft_text'], array( 'class' => 'extend' ) );
148: }
149: }
150:
151: // Smaller pages.
152: $larger_pages_array = array();
153: if ( $larger_page_multiple ) {
154: for ( $i = $larger_page_multiple; $i <= $total_pages; $i += $larger_page_multiple ) {
155: $larger_pages_array[] = $i;
156: }
157: }
158:
159: $larger_page_start = 0;
160: foreach ( $larger_pages_array as $larger_page ) {
161: if ( $larger_page < ( $start_page - $half_page_start ) && $larger_page_start < $larger_page_to_show ) {
162: $out .= $instance->get_single(
163: $larger_page,
164: $options['page_text'],
165: array(
166: 'class' => 'smaller page',
167: )
168: );
169: ++$larger_page_start;
170: }
171: }
172:
173: if ( $larger_page_start ) {
174: $out .= $instance->get_single_dot( 'span', $options['dotleft_text'], array( 'class' => 'extend' ) );
175: }
176:
177: // Page numbers.
178: $timeline = 'smaller';
179: foreach ( range( $start_page, $end_page ) as $i ) {
180: if ( $i == $paged && ! empty( $options['current_text'] ) ) { //phpcs:ignore
181: $out .= $instance->get_single( $i, $options['current_text'], array( 'class' => 'current' ), '%PAGE_NUMBER%', 'span' );
182: $timeline = 'larger';
183: } else {
184: $out .= $instance->get_single(
185: $i,
186: $options['page_text'],
187: array(
188: 'class' => "page $timeline",
189: )
190: );
191: }
192: }
193:
194: // Large pages.
195: $larger_page_end = 0;
196: $larger_page_out = '';
197: foreach ( $larger_pages_array as $larger_page ) {
198: if ( $larger_page > ( $end_page + $half_page_end ) && $larger_page_end < $larger_page_to_show ) {
199: $larger_page_out .= $instance->get_single(
200: $larger_page,
201: $options['page_text'],
202: array(
203: 'class' => 'larger page',
204: )
205: );
206: ++$larger_page_end;
207: }
208: }
209:
210: if ( $larger_page_out ) {
211: $out .= $instance->get_single_dot( 'span', $options['dotright_text'], array( 'class' => 'extend' ) );
212: }
213: $out .= $larger_page_out;
214:
215: if ( $end_page < $total_pages && ! empty( $options['dotright_text'] ) ) {
216: $out .= $instance->get_single_dot( 'span', $options['dotright_text'], array( 'class' => 'extend' ) );
217: }
218:
219: // Next.
220: if ( $paged < $total_pages && ! empty( $options['next_text'] ) ) {
221: $out .= $instance->get_single(
222: $paged + 1,
223: $options['next_text'],
224: array(
225: 'class' => 'nextpostslink',
226: 'rel' => 'next',
227: )
228: );
229: }
230:
231: if ( $end_page < $total_pages ) {
232: // Last.
233: $out .= $instance->get_single(
234: $total_pages,
235: $options['last_text'],
236: array(
237: 'class' => 'last',
238: ),
239: '%TOTAL_PAGES%'
240: );
241: }
242: $out = apply_filters( 'epl_pagination_after_page_numbers', $out, $start_page, $end_page );
243: break;
244:
245: // Dropdown.
246: case 2:
247: $out .= '<form action="" method="get">' . "\n";
248: $out .= '<select aria-label="' . esc_attr__( 'Pagination', 'easy-property-listings' ) . '" size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">' . "\n";
249:
250: foreach ( range( 1, $total_pages ) as $i ) {
251: $page_num = $i;
252: if ( 1 === $page_num ) {
253: $page_num = 0;
254: }
255:
256: if ( $i == $paged ) { //phpcs:ignore
257: $current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
258: $out .= '<option value="' . esc_url( $instance->get_url( $page_num ) ) . '" selected="selected" class="current">' . $current_page_text . "</option>\n";
259: } else {
260: $page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
261: $out .= '<option value="' . esc_url( $instance->get_url( $page_num ) ) . '">' . $page_text . "</option>\n";
262: }
263: }
264:
265: $out .= "</select>\n";
266: $out .= "</form>\n";
267: break;
268: }
269: $out = $before . "<div class='epl-pagination'>\n$out\n</div>" . $after;
270:
271: $out = apply_filters( 'epl_pagination_html', $out );
272:
273: if ( ! $echo ) {
274: return $out;
275: }
276:
277: echo $out; //phpcs:ignore
278: }
279:
280: if ( ! function_exists( 'epl_pagination_html' ) ) :
281:
282: /**
283: * Pagination HTML
284: *
285: * @param string $tag Wrapper tag.
286: * @since 2.1
287: */
288: function epl_pagination_html( $tag ) {
289: static $self_closing_tags = array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta' );
290:
291: $args = func_get_args();
292:
293: $tag = array_shift( $args );
294:
295: if ( is_array( $args[0] ) ) {
296: $closing = $tag;
297: $attributes = array_shift( $args );
298: foreach ( $attributes as $key => $value ) {
299: if ( false === $value ) {
300: continue;
301: }
302:
303: if ( true === $value ) {
304: $value = $key;
305: }
306:
307: $tag .= ' ' . $key . '="' . esc_attr( $value ) . '"';
308: }
309: } else {
310: list( $closing ) = explode( ' ', $tag, 2 );
311: }
312:
313: if ( in_array( $closing, $self_closing_tags, true ) ) {
314: return "<{$tag} />";
315: }
316:
317: $content = implode( '', $args );
318:
319: return "<{$tag}>{$content}</{$closing}>";
320: }
321: endif;
322:
323: if ( ! function_exists( 'epl_get_multipage_link' ) ) :
324:
325: /**
326: * Pagination Multipage link
327: *
328: * @param int $page Page number.
329: * @since 2.1
330: */
331: function epl_get_multipage_link( $page = 1 ) {
332: global $post, $wp_rewrite;
333:
334: if ( 1 === $page ) {
335: $url = get_permalink();
336: } else {
337: $opt_permalink_str = get_option( 'permalink_structure' );
338: if ( empty( $opt_permalink_str ) || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) {
339: $url = add_query_arg( 'page', $page, get_permalink() );
340: } elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) === $post->ID ) {
341: $url = trailingslashit( get_permalink() ) . user_trailingslashit( $wp_rewrite->pagination_base . "/$page", 'single_paged' );
342: } else {
343: $url = trailingslashit( get_permalink() ) . user_trailingslashit( $page, 'single_paged' );
344: }
345: }
346:
347: return esc_url( $url );
348: }
349: endif;
350:
351: /**
352: * Get next page URL for EPL archives / shortcodes
353: *
354: * @param WP_Query $query WP Query object.
355: * @return string
356: * @since 3.3.3
357: * @since 3.5.1 Fixed shortcode pagination when permalinks are plain.
358: * @since 3.5.3 Fixed sorting not working for pagination on shortcode.
359: * @since 3.5.21 Tweak for epl-search-builder ajax pagination.
360: */
361: function epl_get_next_page_link( $query ) {
362: $link = next_posts( $query->max_num_pages, false );
363:
364: if ( ($query->get( 'is_epl_shortcode' ) &&
365: in_array( $query->get( 'epl_shortcode_name' ), epl_get_shortcode_list(), true ) ) || $query->is_epl_search ) {
366:
367: $permalink_structure = get_option( 'permalink_structure' );
368:
369: if ( empty( $permalink_structure ) || $query->is_epl_search ) {
370:
371: $page = $query->get( 'paged' );
372:
373: if ( ! $page ) {
374: $page = 1;
375: }
376:
377: ++$page;
378:
379: $link = epl_add_or_update_params( $link, 'paged', $page );
380: }
381:
382: $link = epl_add_or_update_params( $link, 'pagination_id', $query->get( 'instance_id' ) );
383: $link = epl_add_or_update_params( $link, 'instance_id', $query->get( 'instance_id' ) );
384: }
385: return apply_filters( 'epl_get_next_page_link', $link );
386: }
387:
388: /**
389: * Next page Link
390: *
391: * @param WP_Query $query WP Query object.
392: * @param string $label Pagination 'next' label.
393: * @return string
394: * @since 3.3.3
395: */
396: function epl_next_post_link( $query, $label = null ) {
397:
398: $paged = $query->get( 'paged' );
399: $nextpage = intval( $paged ) + 1;
400:
401: if ( $nextpage <= $query->max_num_pages ) {
402:
403: if ( null === $label ) {
404: $label = __( 'Next Page &raquo;', 'easy-property-listings' );
405: }
406:
407: $attr = apply_filters( 'epl_next_posts_link_attributes', '' );
408: return '<a href="' . epl_get_next_page_link( $query ) . "\" $attr>" . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
409: }
410: }
411:
412: /**
413: * Get Prev page URL for EPL archives / shortcodes
414: *
415: * @param WP_Query $query WP Query object.
416: * @return string
417: * @since 3.3.3
418: * @since 3.5.1 Fixed shortcode pagination when permalinks are plain.
419: * @since 3.5.3 Fixed sorting not working for pagination on shortcode.
420: * @since 3.5.21 Tweak for epl-search-builder ajax pagination.
421: */
422: function epl_get_prev_page_link( $query ) {
423:
424: $link = previous_posts( false );
425:
426: if ( ($query->get( 'is_epl_shortcode' ) &&
427: in_array( $query->get( 'epl_shortcode_name' ), epl_get_shortcode_list(), true ) ) || $query->is_epl_search ) {
428: $permalink_structure = get_option( 'permalink_structure' );
429:
430: if ( empty( $permalink_structure ) || $query->is_epl_search ) {
431:
432: $page = $query->get( 'paged' );
433:
434: if ( ! $page ) {
435: $page = 1;
436: }
437:
438: --$page;
439:
440: $link = epl_add_or_update_params( $link, 'paged', $page );
441: }
442: $link = epl_add_or_update_params( $link, 'pagination_id', $query->get( 'instance_id' ) );
443: $link = epl_add_or_update_params( $link, 'instance_id', $query->get( 'instance_id' ) );
444: }
445: return apply_filters( 'epl_get_prev_page_link', $link );
446: }
447:
448: /**
449: * Prev page Link
450: *
451: * @param WP_Query $query WP Query object.
452: * @param string $label Pagination 'previous' label.
453: * @return string
454: *
455: * @since 3.3.3
456: * @since 3.5.21 Tweak for epl-search-builder ajax pagination.
457: */
458: function epl_prev_post_link( $query, $label = null ) {
459:
460: $paged = $query->get( 'paged' );
461:
462: if ( $paged > 1 ) {
463:
464: if ( null === $label ) {
465: $label = __( '&laquo; Previous Page', 'easy-property-listings' );
466: }
467:
468: $attr = apply_filters( 'epl_prev_posts_link_attributes', '' );
469: return '<a href="' . epl_get_prev_page_link( $query ) . "\" $attr>" . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
470: }
471: }
472:
473: /**
474: * WordPress Default Pagination
475: *
476: * @param WP_Query $query WP Query object.
477: *
478: * @since 2.1
479: * @since 3.3.3 Revised.
480: * @since 3.4.44 Fix: Warning for wp_kses_post when empty link.
481: * @since 3.5.11 Fix: Pagination not using return return value in write context.
482: */
483: function epl_wp_default_pagination( $query = array() ) {
484: if ( empty( $query ) ) {
485: ?>
486: <div class="epl-paginate-default-wrapper epl-clearfix">
487: <div class="alignleft"><?php previous_posts_link( __( '&laquo; Previous Page', 'easy-property-listings' ) ); ?></div>
488: <div class="alignright"><?php next_posts_link( __( 'Next Page &raquo;', 'easy-property-listings' ) ); ?></div>
489: </div>
490: <?php
491: } else {
492: $query_open = $query['query'];
493: ?>
494: <div class="epl-paginate-default-wrapper epl-clearfix">
495: <div class="alignleft">
496: <?php
497: $prev_post_link = epl_prev_post_link( $query_open );
498: if ( ! empty( $prev_post_link ) ) {
499: echo wp_kses_post( epl_prev_post_link( $query_open ) );
500: }
501: ?>
502: </div>
503: <div class="alignright">
504: <?php
505: $next_post_link = epl_next_post_link( $query_open );
506: if ( ! empty( $next_post_link ) ) {
507: echo wp_kses_post( epl_next_post_link( $query_open ) );
508: }
509: ?>
510: </div>
511: </div>
512: <?php
513: }
514: }
515: