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