PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.2.6
Gallery : FooGallery v3.2.6
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / class-foogallery-paging.php
foogallery / includes Last commit date
abilities 4 weeks ago admin 4 weeks ago compatibility 4 weeks ago extensions 4 weeks ago foopluginbase 4 weeks ago public 4 weeks ago thumbs 4 weeks ago asset-manifest.php 4 weeks ago class-attachment-filters.php 4 weeks ago class-command-palette.php 4 weeks ago class-foogallery-animated-gif-support.php 4 weeks ago class-foogallery-attachment-custom-class.php 4 weeks ago class-foogallery-attachment-filename.php 4 weeks ago class-foogallery-attachment-type.php 4 weeks ago class-foogallery-attachment.php 4 weeks ago class-foogallery-cache.php 4 weeks ago class-foogallery-common-fields.php 4 weeks ago class-foogallery-crop-position.php 4 weeks ago class-foogallery-datasource-media_library.php 4 weeks ago class-foogallery-debug.php 4 weeks ago class-foogallery-delayed-runtime-loader.php 4 weeks ago class-foogallery-extensions-compatibility.php 4 weeks ago class-foogallery-force-https.php 4 weeks ago class-foogallery-lazyload.php 4 weeks ago class-foogallery-license-constant-handler.php 4 weeks ago class-foogallery-lightbox.php 4 weeks ago class-foogallery-paging.php 4 weeks ago class-foogallery-password-protect.php 4 weeks ago class-foogallery-sitemaps.php 4 weeks ago class-foogallery-widget.php 4 weeks ago class-foogallery.php 4 weeks ago class-gallery-advanced-settings.php 4 weeks ago class-il8n.php 4 weeks ago class-override-thumbnail.php 4 weeks ago class-posttypes.php 4 weeks ago class-previews.php 4 weeks ago class-retina.php 4 weeks ago class-thumbnail-dimensions.php 4 weeks ago class-thumbnails.php 4 weeks ago class-version-check.php 4 weeks ago constants.php 4 weeks ago functions.php 4 weeks ago gallery-management-functions.php 4 weeks ago includes.php 4 weeks ago index.php 4 weeks ago render-functions.php 4 weeks ago
class-foogallery-paging.php
498 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Class used to handle paging for gallery templates
9 */
10 if ( ! class_exists( 'FooGallery_Paging' ) ) {
11
12 class FooGallery_Paging {
13
14 function __construct() {
15 add_action( 'plugins_loaded', array( $this, 'load_feature' ) );
16 }
17
18 function load_feature() {
19 // Register paging field definitions in every request context so
20 // non-admin consumers, including abilities, can resolve and save
21 // layout settings against the same schema as wp-admin.
22 add_filter( 'foogallery_override_gallery_template_fields', array( $this, 'add_paging_fields' ), 10, 2 );
23
24 add_action( 'foogallery_located_template', array( $this, 'determine_paging' ), 10, 1 );
25
26 //add the paging options to the gallery container
27 add_filter( 'foogallery_build_container_data_options', array( $this, 'add_paging_data_options' ), 20, 3 );
28
29 //limit the number of attachments returned when rendering a gallery if paging is enabled
30 add_filter( 'foogallery_gallery_attachments_override_for_rendering', array( $this, 'attachments_override' ), 10, 3 );
31
32 //output pagination placeholders
33 add_action( 'foogallery_loaded_template_before', array( $this, 'output_pagination_placeholders_before' ), 20, 1 );
34 add_action( 'foogallery_loaded_template_after', array( $this, 'output_pagination_placeholders_after' ), 10, 1 );
35
36 //output a script block with the rest of the attachments as json
37 add_action( 'foogallery_loaded_template_after', array( $this, 'output_paging_script_block' ), 90, 1 );
38
39 add_filter( 'foogallery_attachment_html_item_classes', array( $this, 'hide_item_for_html_output' ), 10, 3 );
40
41 add_filter( 'foogallery_attachment_get_posts_args', array( $this, 'add_paging_query_args' ), 10, 3 );
42 }
43
44 /**
45 * Add paging query args to the attachment query
46 *
47 * @param $args
48 *
49 * @return mixed
50 */
51 function add_paging_query_args( $args ) {
52 global $current_foogallery_arguments;
53 global $current_foogallery;
54
55 if ( isset( $current_foogallery_arguments['page'] ) ) {
56 $args['page'] = $args['paged'] = intval( $current_foogallery_arguments['page'] );
57 }
58
59 if ( isset( $current_foogallery_arguments['posts_per_page'] ) ) {
60 $args['posts_per_page'] = intval( $current_foogallery_arguments['posts_per_page'] );
61 }
62
63 $posts_per_page = isset( $args['posts_per_page'] ) ? intval( $args['posts_per_page'] ) : 0;
64
65 //check if no posts per page is set
66 if ( isset( $current_foogallery_arguments['page'] ) && $posts_per_page <= 0 ) {
67 $this->determine_paging( $current_foogallery );
68 if ( foogallery_current_gallery_has_cached_value('paging' ) ) {
69 $paging_options = foogallery_current_gallery_get_cached_value( 'paging' );
70 $page_size = intval( $paging_options['size'] );
71 $args['posts_per_page'] = $page_size;
72 }
73 }
74
75 return $args;
76 }
77
78 function hide_item_for_html_output( $classes, $foogallery_attachment, $args ) {
79 if ( isset( $foogallery_attachment->class ) ) {
80 $classes[] = $foogallery_attachment->class;
81 }
82 return $classes;
83 }
84
85 /**
86 * Determine if the gallery supports paging and set a cached value for all paging options on the gallery for later use
87 *
88 * @param $foogallery
89 */
90 function determine_paging( $foogallery ) {
91 if ( foogallery_current_gallery_check_template_has_supported_feature( 'paging_support') ) {
92
93 //check if we have arguments from the shortcode and override the saved settings
94 $paging = foogallery_gallery_template_setting( 'paging_type', '' );
95
96 if ( '' !== $paging ) {
97 $paging_position = foogallery_gallery_template_setting( 'paging_position', 'both' );
98 $paging_theme = foogallery_gallery_template_setting( 'paging_theme', '' );
99 $paging_size = intval( foogallery_gallery_template_setting( 'paging_size', 20 ) );
100 $paging_scroll = foogallery_gallery_template_setting( 'paging_scroll', 'true' ) === 'true';
101 $paging_output = foogallery_gallery_template_setting( 'paging_output', '' );
102
103 //force bottom position for infinite and loadMore paging
104 if ( 'infinite' === $paging || 'loadMore' === $paging ) {
105 $paging_position = 'bottom';
106 }
107
108 $paging_options = array(
109 'type' => $paging,
110 'size' => $paging_size,
111 'position' => $paging_position,
112 'scrollToTop' => $paging_scroll,
113 'output' => $paging_output
114 );
115
116 if ( !empty( $paging_theme ) ) {
117 $paging_options['theme'] = $paging_theme;
118 }
119
120 if ( 'pagination' === $paging ) {
121 $paging_options['limit'] = intval( foogallery_gallery_template_setting( 'paging_limit', 5 ) );
122 $paging_options['showFirstLast'] = foogallery_gallery_template_setting( 'paging_showFirstLast', 'true' ) === 'true';
123 $paging_options['showPrevNext'] = foogallery_gallery_template_setting( 'paging_showPrevNext', 'true' ) === 'true';
124 $paging_options['showPrevNextMore'] = foogallery_gallery_template_setting( 'paging_showPrevNextMore', 'true' ) === 'true';
125 }
126
127 //cache the paging options on the gallery to be used later
128 foogallery_current_gallery_set_cached_value( 'paging', $paging_options );
129 }
130 }
131 }
132
133 /**
134 * Renders the top pagination placeholder
135 *
136 * @param $foogallery FooGallery
137 */
138 function output_pagination_placeholders_before( $foogallery ) {
139 $this->output_pagination_placeholder( $foogallery, 'top' );
140 }
141
142 /**
143 * Renders the bottom pagination placeholder
144 *
145 * @param $foogallery FooGallery
146 */
147 function output_pagination_placeholders_after( $foogallery ) {
148 $this->output_pagination_placeholder( $foogallery, 'bottom' );
149 }
150
151 /**
152 * render the pagination placeholder
153 *
154 * @param $foogallery FooGallery
155 * @param $position
156 */
157 function output_pagination_placeholder( $foogallery, $position ) {
158 if ( foogallery_current_gallery_has_cached_value('paging' ) ) {
159 $paging_options = foogallery_current_gallery_get_cached_value( 'paging' );
160
161 //check to see if the page size is less than the number of items
162 if ( $foogallery->attachment_count() > intval( $paging_options['size'] ) ) {
163
164 $paging_position = $paging_options['position'];
165 if ( $position === $paging_position || 'both' === $paging_position ) {
166
167 $paging_type = $paging_options['type'];
168
169 $paging_types_that_require_placeholders = apply_filters( 'foogallery_pagination_types_require_placeholders', array( 'dots' ) );
170
171 if ( in_array( $paging_type, $paging_types_that_require_placeholders ) ) {
172 $paging_type = apply_filters( 'foogallery_pagination_format_type_for_placeholder', $paging_type );
173
174 echo '<nav id="' . esc_attr( $foogallery->container_id() ) . '_paging-' . esc_attr( $position ) . '" class="fg-paging-container fg-ph-' . esc_attr( $paging_type ) . '"></nav>';
175 }
176 }
177 }
178 }
179 }
180
181 /**
182 * Add paging fields to the gallery template
183 *
184 * @uses "foogallery_override_gallery_template_fields"
185 * @param $fields
186 * @param $template
187 *
188 * @return array
189 */
190 function add_paging_fields( $fields, $template ) {
191 $paging_support = false;
192 if ( $template && is_array( $template ) && array_key_exists( 'paging_support', $template ) ) {
193 $paging_support = $template['paging_support'];
194 }
195
196 if ( $paging_support ) {
197 $fields[] = array(
198 'id' => 'paging_type',
199 'title' => __( 'Paging Type', 'foogallery' ),
200 'desc' => __( 'Add paging to a large gallery.', 'foogallery' ),
201 'section' => __( 'Paging', 'foogallery' ),
202 'section_order' => 5,
203 'type' => 'radio',
204 'default' => '',
205 'choices' => apply_filters( 'foogallery_gallery_template_paging_type_choices', array(
206 '' => __( 'None', 'foogallery' ),
207 'dots' => __( 'Dots', 'foogallery' )
208 ) ),
209 'row_data'=> array(
210 'data-foogallery-change-selector' => 'input',
211 'data-foogallery-preview' => 'shortcode',
212 'data-foogallery-value-selector' => 'input:checked',
213 'data-foogallery-persist' => true
214 )
215 );
216
217 $fields[] = array(
218 'id' => 'paging_size',
219 'title' => __( 'Page Size', 'foogallery' ),
220 'desc' => __( 'The size of your pages.', 'foogallery' ),
221 'section' => __( 'Paging', 'foogallery' ),
222 'type' => 'number',
223 'class' => 'small-text',
224 'default' => 20,
225 'step' => '1',
226 'min' => '0',
227 'row_data'=> array(
228 'data-foogallery-change-selector' => 'input',
229 'data-foogallery-preview' => 'shortcode',
230 'data-foogallery-hidden' => true,
231 'data-foogallery-show-when-field' => 'paging_type',
232 'data-foogallery-show-when-field-operator' => 'regex',
233 'data-foogallery-show-when-field-value' => 'dots|pagination|infinite|loadMore',
234 'data-foogallery-persist' => true
235 )
236 );
237
238 $fields[] = array(
239 'id' => 'paging_position',
240 'title' => __( 'Position', 'foogallery' ),
241 'desc' => __( 'The position of the paging for either dots or pagination.', 'foogallery' ),
242 'section' => __( 'Paging', 'foogallery' ),
243 'type' => 'radio',
244 'default' => 'bottom',
245 'choices' => apply_filters( 'foogallery_gallery_template_paging_position_choices', array(
246 '' => __( 'None', 'foogallery' ),
247 'top' => __( 'Top', 'foogallery' ),
248 'bottom' => __( 'Bottom', 'foogallery' ),
249 'both' => __( 'Both', 'foogallery' )
250 ) ),
251 'row_data'=> array(
252 'data-foogallery-hidden' => true,
253 'data-foogallery-show-when-field-operator' => 'regex',
254 'data-foogallery-show-when-field' => 'paging_type',
255 'data-foogallery-show-when-field-value' => 'dots|pagination',
256 'data-foogallery-change-selector' => 'input',
257 'data-foogallery-preview' => 'shortcode',
258 'data-foogallery-persist' => true
259 )
260 );
261
262 $fields[] = array(
263 'id' => 'paging_theme',
264 'title' => __( 'Theme', 'foogallery' ),
265 'desc' => __( 'The theme used for pagination. Default uses the theme of the gallery.', 'foogallery' ),
266 'section' => __( 'Paging', 'foogallery' ),
267 'type' => 'radio',
268 'default' => '',
269 'choices' => apply_filters( 'foogallery_gallery_template_paging_theme_choices', array(
270 '' => __( 'Default', 'foogallery' ),
271 'fg-light' => __( 'Light', 'foogallery' ),
272 'fg-dark' => __( 'Dark', 'foogallery' ),
273 'fg-custom' => __( 'Custom', 'foogallery' ),
274 ) ),
275 'row_data'=> array(
276 'data-foogallery-change-selector' => 'input',
277 'data-foogallery-preview' => 'shortcode',
278 'data-foogallery-hidden' => true,
279 'data-foogallery-show-when-field' => 'paging_type',
280 'data-foogallery-show-when-field-operator' => 'regex',
281 'data-foogallery-show-when-field-value' => 'dots|pagination|loadMore',
282 'data-foogallery-persist' => true
283 )
284 );
285
286 $fields[] = array(
287 'id' => 'paging_scroll',
288 'title' => __( 'Scroll To Top', 'foogallery' ),
289 'desc' => __( 'Whether or not it should scroll to the top of the gallery when paging is changed.', 'foogallery' ),
290 'section' => __( 'Paging', 'foogallery' ),
291 'type' => 'radio',
292 'default' => 'false',
293 'choices' => array(
294 'true' => __( 'Yes', 'foogallery' ),
295 'false' => __( 'No', 'foogallery' ),
296 ),
297 'row_data'=> array(
298 'data-foogallery-hidden' => true,
299 'data-foogallery-show-when-field-operator' => 'regex',
300 'data-foogallery-show-when-field' => 'paging_type',
301 'data-foogallery-show-when-field-value' => 'dots|pagination',
302 'data-foogallery-change-selector' => 'input',
303 'data-foogallery-preview' => 'shortcode',
304 'data-foogallery-persist' => true
305 )
306 );
307
308 $fields[] = array(
309 'id' => 'paging_limit',
310 'title' => __( 'Paging Limit', 'foogallery' ),
311 'desc' => __( 'The maximum number of page links to display for the gallery.', 'foogallery' ),
312 'section' => __( 'Paging', 'foogallery' ),
313 'type' => 'number',
314 'class' => 'small-text',
315 'default' => 5,
316 'step' => '1',
317 'min' => '0',
318 'row_data'=> array(
319 'data-foogallery-hidden' => true,
320 'data-foogallery-show-when-field-operator' => '===',
321 'data-foogallery-show-when-field' => 'paging_type',
322 'data-foogallery-show-when-field-value' => 'pagination',
323 'data-foogallery-change-selector' => 'input',
324 'data-foogallery-preview' => 'shortcode',
325 'data-foogallery-persist' => true
326 )
327 );
328
329 $fields[] = array(
330 'id' => 'paging_showFirstLast',
331 'title' => __( 'First &amp; Last Buttons', 'foogallery' ),
332 'desc' => __( 'Whether or not to show the first &amp; last buttons for numbered pagination.', 'foogallery' ),
333 'section' => __( 'Paging', 'foogallery' ),
334 'type' => 'radio',
335 'default' => 'true',
336 'choices' => array(
337 'true' => __( 'Show', 'foogallery' ),
338 'false' => __( 'Hide', 'foogallery' ),
339 ),
340 'row_data'=> array(
341 'data-foogallery-hidden' => true,
342 'data-foogallery-show-when-field-operator' => '===',
343 'data-foogallery-show-when-field' => 'paging_type',
344 'data-foogallery-show-when-field-value' => 'pagination',
345 'data-foogallery-change-selector' => 'input',
346 'data-foogallery-preview' => 'shortcode',
347 'data-foogallery-persist' => true
348 )
349 );
350
351 $fields[] = array(
352 'id' => 'paging_showPrevNext',
353 'title' => __( 'Prev &amp; Next Buttons', 'foogallery' ),
354 'desc' => __( 'Whether or not to show the previous &amp; next buttons for numbered pagination.', 'foogallery' ),
355 'section' => __( 'Paging', 'foogallery' ),
356 'type' => 'radio',
357 'default' => 'true',
358 'choices' => array(
359 'true' => __( 'Show', 'foogallery' ),
360 'false' => __( 'Hide', 'foogallery' ),
361 ),
362 'row_data'=> array(
363 'data-foogallery-hidden' => true,
364 'data-foogallery-show-when-field-operator' => '===',
365 'data-foogallery-show-when-field' => 'paging_type',
366 'data-foogallery-show-when-field-value' => 'pagination',
367 'data-foogallery-change-selector' => 'input',
368 'data-foogallery-preview' => 'shortcode',
369 'data-foogallery-persist' => true
370 )
371 );
372
373 $fields[] = array(
374 'id' => 'paging_showPrevNextMore',
375 'title' => __( 'More Buttons', 'foogallery' ),
376 'desc' => __( 'Whether or not to show the previous &amp; next more buttons for numbered pagination.', 'foogallery' ),
377 'section' => __( 'Paging', 'foogallery' ),
378 'type' => 'radio',
379 'default' => 'true',
380 'choices' => array(
381 'true' => __( 'Show', 'foogallery' ),
382 'false' => __( 'Hide', 'foogallery' ),
383 ),
384 'row_data'=> array(
385 'data-foogallery-hidden' => true,
386 'data-foogallery-show-when-field-operator' => '===',
387 'data-foogallery-show-when-field' => 'paging_type',
388 'data-foogallery-show-when-field-value' => 'pagination',
389 'data-foogallery-change-selector' => 'input',
390 'data-foogallery-preview' => 'shortcode',
391 'data-foogallery-persist' => true
392 )
393 );
394
395 $fields[] = array(
396 'id' => 'paging_output',
397 'title' => __( 'Paging Output', 'foogallery' ),
398 'desc' => __( 'How the paging items are output. We recommend that very large galleries output as JSON.', 'foogallery' ),
399 'section' => __( 'Paging', 'foogallery' ),
400 'type' => 'radio',
401 'default' => '',
402 'choices' => array(
403 '' => __( 'Fastest (JSON)', 'foogallery' ),
404 'html' => __( 'Legacy (HTML)', 'foogallery' )
405 ),
406 'row_data'=> array(
407 'data-foogallery-change-selector' => 'input',
408 'data-foogallery-preview' => 'shortcode',
409 'data-foogallery-value-selector' => 'input:checked',
410 'data-foogallery-hidden' => true,
411 'data-foogallery-show-when-field' => 'paging_type',
412 'data-foogallery-show-when-field-operator' => '!==',
413 'data-foogallery-show-when-field-value' => '',
414 'data-foogallery-persist' => true
415 )
416 );
417 }
418
419 return $fields;
420 }
421
422 /**
423 * Add the required paging data options if needed
424 *
425 * @param $attributes array
426 * @param $gallery FooGallery
427 *
428 * @return array
429 */
430 function add_paging_data_options($options, $gallery, $attributes) {
431 if ( foogallery_current_gallery_has_cached_value('paging' ) ) {
432 $options['paging'] = foogallery_current_gallery_get_cached_value( 'paging' );
433 }
434 return $options;
435 }
436
437 /**
438 * Override the attachments returned for rendering a paginated gallery to the first page only
439 * The rest of the items will be added to the script block below the gallery as json items
440 * This is only when paging_output is JSON. When it's HTML then the all items are rendered
441 *
442 * @param bool $override
443 * @param FooGallery $gallery
444 * @return bool|array
445 */
446 function attachments_override( $override, $gallery ) {
447
448 if ( foogallery_current_gallery_has_cached_value('paging' ) ) {
449
450 $paging_options = foogallery_current_gallery_get_cached_value( 'paging' );
451 $page_size = intval( $paging_options['size'] );
452 $output = $paging_options['output'];
453
454 if ( $page_size > 0 ) {
455 $attachments = $gallery->attachments();
456
457 if ( $output === 'html' ) {
458 $index = 0;
459 foreach ( $attachments as &$attachment ) {
460 $index++;
461 if ( $index > $page_size ) {
462 $attachment->class = 'fg-hidden';
463 }
464 }
465 return $attachments;
466
467 } else {
468 //return the first page of attachments
469 return array_splice( $attachments, 0, $page_size );
470 }
471 }
472 }
473
474 return $override;
475 }
476
477 /**
478 * Output a script block with all the gallery attachments as json
479 *
480 * @param FooGallery $gallery
481 */
482 function output_paging_script_block( $gallery ) {
483 if ( foogallery_current_gallery_has_cached_value('paging' ) ) {
484 $paging_options = foogallery_current_gallery_get_cached_value( 'paging' );
485 $page_size = intval( $paging_options['size'] );
486 $output = $paging_options['output'];
487
488 if ( $page_size > 0 && $output === '' ) {
489 //get the attachments that are not on the first page
490 $attachments = array_slice( $gallery->attachments(), $page_size );
491 foogallery_render_script_block_for_json_items( $gallery, $attachments );
492 foogallery_render_noscript_block_for_json_items( $gallery, $attachments );
493 }
494 }
495 }
496 }
497 }
498