PluginProbe
WP Ultimate Post Grid / 4.0.1
WP Ultimate Post Grid v4.0.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-pagination-pages.php

class-wpupg-pagination-pages.php in WP Ultimate Post Grid 4.0.1, at includes/public/class-wpupg-pagination-pages.php

360 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle output for the pages pagination.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.0.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Handle output for the pages pagination.
14 *
15 * @since 3.0.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <[email protected]>
19 */
20 class WPUPG_Pagination_Pages {
21
22 /**
23 * Register actions and filters.
24 *
25 * @since 3.0.0
26 */
27 public static function init() {
28 add_filter( 'wpupg_pagination_defaults', array( __CLASS__, 'defaults' ), 9 );
29 add_filter( 'wpupg_pagination_sanitize_options', array( __CLASS__, 'sanitize' ), 9, 2 );
30 add_filter( 'wpupg_javascript_args_pagination', array( __CLASS__, 'javascript_args' ), 9, 2 );
31 add_filter( 'wpupg_query_post_args', array( __CLASS__, 'query_args' ), 10, 3 );
32 add_filter( 'wpupg_grid_ids', array( __CLASS__, 'ids' ), 9, 3 );
33 add_filter( 'wpupg_output_item_classes', array( __CLASS__, 'classes' ), 9, 4 );
34 add_filter( 'wpupg_output_pagination', array( __CLASS__, 'output' ), 9, 3 );
35 }
36
37 /**
38 * Default options for this filter.
39 *
40 * @since 3.0.0
41 * @param mixed $defaults Current defaults to filter.
42 */
43 public static function defaults( $defaults ) {
44 $defaults['pages'] = array(
45 'posts_per_page' => 20,
46 'adaptive_pages' => true,
47 'max_buttons' => 0,
48 'style' => array(
49 'font_size' => '14',
50 'background_color' => '#2E5077',
51 'background_active_color' => '#1C3148',
52 'background_hover_color' => '#1C3148',
53 'text_color' => '#FFFFFF',
54 'text_active_color' => '#FFFFFF',
55 'text_hover_color' => '#FFFFFF',
56 'border_color' => '#1C3148',
57 'border_active_color' => '#1C3148',
58 'border_hover_color' => '#1C3148',
59 'border_width' => '1',
60 'border_radius' => '0',
61 'margin_vertical' => '5',
62 'margin_horizontal' => '5',
63 'padding_vertical' => '5',
64 'padding_horizontal' => '10',
65 'alignment' => 'left',
66 ),
67 );
68
69 return $defaults;
70 }
71
72 /**
73 * Sanitize pagination options when saving.
74 *
75 * @since 3.0.0
76 * @param mixed $sanitized_options Current sanitized options.
77 * @param mixed $pagination Pagination to sanitize.
78 */
79 public static function sanitize( $sanitized_pagination, $pagination ) {
80 if ( isset( $pagination['pages'] ) ) {
81 $options = $pagination['pages'];
82 $sanitized_options = array();
83
84 // Number fields.
85 if ( isset( $options['posts_per_page'] ) ) { $sanitized_options['posts_per_page'] = intval( $options['posts_per_page'] ); }
86 if ( isset( $options['max_buttons'] ) ) { $sanitized_options['max_buttons'] = intval( $options['max_buttons'] ); }
87
88 // Boolean fields.
89 if ( isset( $options['adaptive_pages'] ) ) { $sanitized_options['adaptive_pages'] = $options['adaptive_pages'] ? true : false; }
90
91 // Style.
92 if ( isset( $options['style'] ) ) {
93 $sanitized_style = array();
94 $style = $options['style'];
95
96 // Text fields.
97 if ( isset( $style['background_active_color'] ) ) { $sanitized_style['background_active_color'] = sanitize_text_field( $style['background_active_color'] ); }
98 if ( isset( $style['background_color'] ) ) { $sanitized_style['background_color'] = sanitize_text_field( $style['background_color'] ); }
99 if ( isset( $style['background_hover_color'] ) ) { $sanitized_style['background_hover_color'] = sanitize_text_field( $style['background_hover_color'] ); }
100 if ( isset( $style['border_active_color'] ) ) { $sanitized_style['border_active_color'] = sanitize_text_field( $style['border_active_color'] ); }
101 if ( isset( $style['border_color'] ) ) { $sanitized_style['border_color'] = sanitize_text_field( $style['border_color'] ); }
102 if ( isset( $style['border_hover_color'] ) ) { $sanitized_style['border_hover_color'] = sanitize_text_field( $style['border_hover_color'] ); }
103 if ( isset( $style['text_active_color'] ) ) { $sanitized_style['text_active_color'] = sanitize_text_field( $style['text_active_color'] ); }
104 if ( isset( $style['text_color'] ) ) { $sanitized_style['text_color'] = sanitize_text_field( $style['text_color'] ); }
105 if ( isset( $style['text_hover_color'] ) ) { $sanitized_style['text_hover_color'] = sanitize_text_field( $style['text_hover_color'] ); }
106
107 // Number fields.
108 if ( isset( $style['border_width'] ) ) { $sanitized_style['border_width'] = intval( $style['border_width'] ); }
109 if ( isset( $style['border_radius'] ) ) { $sanitized_style['border_radius'] = intval( $style['border_radius'] ); }
110 if ( isset( $style['font_size'] ) ) { $sanitized_style['font_size'] = intval( $style['font_size'] ); }
111 if ( isset( $style['margin_horizontal'] ) ) { $sanitized_style['margin_horizontal'] = intval( $style['margin_horizontal'] ); }
112 if ( isset( $style['margin_vertical'] ) ) { $sanitized_style['margin_vertical'] = intval( $style['margin_vertical'] ); }
113 if ( isset( $style['padding_horizontal'] ) ) { $sanitized_style['padding_horizontal'] = intval( $style['padding_horizontal'] ); }
114 if ( isset( $style['padding_vertical'] ) ) { $sanitized_style['padding_vertical'] = intval( $style['padding_vertical'] ); }
115
116 // Limited options fields.
117 $field_options = array( 'left', 'center', 'right' );
118 if ( isset( $style['alignment'] ) && in_array( $style['alignment'], $field_options, true ) ) {
119 $sanitized_style['alignment'] = $style['alignment'];
120 }
121
122 $sanitized_options['style'] = $sanitized_style;
123 }
124
125 // Combine with defaults.
126 $pagination_defaults = WPUPG_Pagination::get_defaults( 'pages' );
127 $sanitized_options = array_replace_recursive( $pagination_defaults, $sanitized_options );
128
129 $sanitized_pagination['pages'] = $sanitized_options;
130 }
131
132 return $sanitized_pagination;
133 }
134
135 /**
136 * JavaScript arguments for this type of pagination.
137 *
138 * @since 3.0.0
139 * @param mixed $args Current JavaScript arguments.
140 * @param mixed $grid Grid to output.
141 */
142 public static function javascript_args( $args, $grid ) {
143 if ( 'pages' === $grid->pagination_type() ) {
144 $options = $grid->pagination( 'pages' );
145
146 $args = array(
147 'posts_per_page' => $options['posts_per_page'],
148 'max_buttons' => $options['max_buttons'],
149 'adaptive_pages' => $options['adaptive_pages'],
150 'load_on_filter' => $options['adaptive_pages'], // Load on filter when using adaptive pages.
151 );
152 }
153
154 return $args;
155 }
156
157 /**
158 * Filter the grid query args.
159 *
160 * @since 3.0.0
161 * @param mixed $args Current arguments.
162 * @param mixed $grid Current grid.
163 * @param mixed $grid_args Optional arguments.
164 */
165 public static function query_args( $args, $grid, $grid_args ) {
166 if ( 'pages' === $grid->pagination_type() ) {
167 $options = $grid->pagination( 'pages' );
168
169 // Don't paginate when loading all on filter, using adaptive pages.
170 if ( $options['adaptive_pages'] && isset( $grid_args['type'] ) && 'load_all' === $grid_args['type'] ) {
171 return $args;
172 }
173
174 // Don't exclude loaded items unless using random ordering. Otherwise we don't always get the same items for the same page.
175 if ( 'rand' !== $grid->grid_order_by() ) {
176 unset( $args['post__not_in'] );
177 }
178
179 // Only filter by page, basically.
180 unset( $args['tax_query'] );
181 unset( $args['s'] );
182 }
183
184 return $args;
185 }
186
187 /**
188 * Filter the IDs that are displayed in this grid page.
189 *
190 * @since 3.0.0
191 * @param mixed $ids Current list of IDs.
192 * @param mixed $grid Current grid.
193 * @param mixed $grid_args Optional arguments.
194 */
195 public static function ids( $ids, $grid, $grid_args ) {
196 if ( 'pages' === $grid->pagination_type() ) {
197 $options = $grid->pagination( 'pages' );
198
199 // Don't paginate when loading all on filter, using adaptive pages.
200 if ( $options['adaptive_pages'] && isset( $grid_args['type'] ) && 'load_all' === $grid_args['type'] ) {
201 return $ids;
202 }
203
204 $posts_per_page = $options['posts_per_page'];
205
206 // Get page from args (or default to 0).
207 $page = isset( $grid_args['page'] ) ? $grid_args['page'] : 0;
208
209 // Always use first page if random ordering.
210 if ( 'rand' === $grid->grid_order_by() ) {
211 $page = 0;
212 }
213
214 if ( -1 !== $page ) {
215 $ids = array_slice( $ids, $page * $posts_per_page, $posts_per_page );
216 }
217 }
218
219 return $ids;
220 }
221
222 /**
223 * Filter grid output classes.
224 *
225 * @since 3.0.0
226 * @param array $classes Current item classes.
227 * @param mixed $grid Grid the item is in.
228 * @param mixed $item Item getting output.
229 * @param mixed $args Optional arguments.
230 */
231 public static function classes( $classes, $grid, $item, $args = array() ) {
232 if ( 'pages' === $grid->pagination_type() ) {
233 $page = isset( $args['page'] ) ? $args['page'] : 0;
234
235 $classes[] = 'wpupg-page-' . $page;
236 }
237
238 return $classes;
239 }
240
241 /**
242 * Output grid pagination.
243 *
244 * @since 3.0.0
245 * @param array $output Current pagination output.
246 * @param mixed $grid Grid to output the pagination for.
247 * @param mixed $args Optional arguments.
248 */
249 public static function output( $output, $grid, $args = array() ) {
250 if ( 'pages' === $grid->pagination_type() ) {
251 $options = $grid->pagination( 'pages' );
252 $nbr_posts = count( $grid->all_ids( $args ) );
253
254 if ( $options['posts_per_page'] < $nbr_posts ) {
255 $output = '';
256 $output .= self::style( $grid, $args, $options );
257 $output .= self::html( $grid, $args, $options );
258 }
259 }
260
261 return $output;
262 }
263
264 /**
265 * Output HTML for the pagination.
266 *
267 * @since 3.0.0
268 * @param mixed $grid Grid to output.
269 * @param mixed $args Optional arguments.
270 * @param mixed $options Pagination options.
271 */
272 public static function html( $grid, $args, $options ) {
273 $output = '';
274
275 $nbr_posts = count( $grid->all_ids( $args ) );
276 $nbr_pages = 0 < $options['posts_per_page'] ? ceil( $nbr_posts / floatval( $options['posts_per_page'] ) ) : 1;
277
278 for ( $page = 0; $page < $nbr_pages; $page++ ) {
279 $active = $page == 0 ? ' active' : '';
280 $output .= '<div class="wpupg-pagination-term wpupg-page-' . $page . $active . '" data-page="' . $page . '" role="button" tabindex="0">' . ( $page + 1 ) . '</div>';
281 }
282
283 return $output;
284 }
285
286 /**
287 * Output styling for the pagination.
288 *
289 * @since 3.0.0
290 * @param mixed $grid Grid to output.
291 * @param mixed $args Optional arguments.
292 * @param mixed $options Pagination options.
293 */
294 public static function style( $grid, $args, $options ) {
295 $style = $options['style'];
296
297 $output = '<style>';
298
299 // Pagination styling.
300 $pagination_selector = '#wpupg-grid-' . $grid->slug_or_id() . '-pagination';
301 $output .= $pagination_selector . ' {';
302 $output .= 'text-align: ' . $style['alignment'] . ';';
303 $output .= 'margin: 0 -' . $style['margin_horizontal'] . 'px;';
304 $output .= '}';
305
306 // Default Item styling.
307 $item_selector = $pagination_selector . ' .wpupg-pagination-term';
308
309 $output .= $item_selector . ' {';
310
311 if ( $style['border_width'] ) {
312 $output .= 'border: ' . $style['border_width'] . 'px solid ' . $style['border_color'] . ';';
313 }
314 if ( $style['border_radius'] ) {
315 $output .= 'border-radius: ' . $style['border_radius'] . 'px;';
316 }
317 $output .= 'background-color: ' . $style['background_color'] . ';';
318 $output .= 'color: ' . $style['text_color'] . ';';
319
320 $output .= 'font-size: ' . $style['font_size'] . 'px;';
321 $output .= 'margin: ' . $style['margin_vertical'] . 'px ' . $style['margin_horizontal'] . 'px;';
322 $output .= 'padding: ' . $style['padding_vertical'] . 'px ' . $style['padding_horizontal'] . 'px;';
323 $output .= '}';
324
325 // Active Item styling.
326 $output .= $item_selector . '.active {';
327 $output .= 'border-color: ' . $style['border_active_color'] . ';';
328 $output .= 'background-color: ' . $style['background_active_color'] . ';';
329 $output .= 'color: ' . $style['text_active_color'] . ';';
330 $output .= '}';
331
332 // Focus Item styling.
333 $hover_styles = '';
334 $hover_styles .= 'outline: none;';
335 $hover_styles .= 'border-color: ' . $style['border_hover_color'] . ';';
336 $hover_styles .= 'background-color: ' . $style['background_hover_color'] . ';';
337 $hover_styles .= 'color: ' . $style['text_hover_color'] . ';';
338
339 $output .= $item_selector . ':focus {' . $hover_styles . '}';
340
341 // Hover Item, only with mouse pointer.
342 $output .= '@media (hover: hover) and (pointer: fine) {';
343 $output .= $item_selector . ':hover {' . $hover_styles . '}';
344 $output .= '}';
345
346 // Gap Item styling.
347 $gap_margin = max( array( 5 * $style['margin_horizontal'], 20 ) ); // At least 20px.
348
349 $output .= $item_selector . '.wpupg-pagination-button-gap {';
350 $output .= 'margin-left: ' . $gap_margin . 'px;';
351 $output .= '}';
352
353 $output .= '</style>';
354
355 return $output;
356 }
357 }
358
359 WPUPG_Pagination_Pages::init();
360