/var/www/hkosl.com/aga/wp-content/plugins/foogallery/includes/class-foogallery-paging.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
 * Class used to handle paging for gallery templates
 */
if ( ! class_exists'FooGallery_Paging' ) ) {

    class 
FooGallery_Paging {

        function 
__construct() {
            if ( 
is_admin() ) {
                
//add extra fields to the templates that support paging
                
add_filter'foogallery_override_gallery_template_fields', array( $this'add_paging_fields' ), 10);

                
//build up any preview arguments
                
add_filter'foogallery_preview_arguments', array( $this'preview_arguments' ), 10);
            }

            
//adds the paging property to a FooGallery
            
add_action'foogallery_located_template', array( $this'determine_paging' ), 10);

            
//add the paging attributes to the gallery container
            
add_filter'foogallery_build_container_data_options', array( $this'add_paging_options' ), 20);

            
//limit the number of attachments returned when rendering a gallery
            
add_filter'foogallery_gallery_attachments_override_for_rendering', array( $this'attachments_override' ), 10);

            
//output a script block with the rest of the attachments as json
            
add_action'foogallery_loaded_template', array( $this'output_paging_script_block' ) );
        }

        
/**
         * Add paging fields to the gallery template
         *
         * @uses "foogallery_override_gallery_template_fields"
         * @param $fields
         * @param $template
         *
         * @return array
         */
        
function add_paging_fields$fields$template ) {
            if ( 
$template && array_key_exists'paging_support'$template ) && true === $template['paging_support'] ) {
                
$fields[] = array(
                    
'id'      => 'paging_type',
                    
'title'   => __'Paging Type''foogallery' ),
                    
'desc'    => __'Add paging to a large gallery.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'spacer'  => '<span class="spacer"></span>',
                    
'type'    => 'radio',
                    
'default' => '',
                    
'choices' => apply_filters'foogallery_gallery_template_paging_type_choices', array(
                        
''  => __'None''foogallery' ),
                        
'dots'   => __'Dots''foogallery' )
                    ) ),
                    
'row_data'=> array(
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode',
                        
'data-foogallery-value-selector' => 'input:checked',
                    )
                );

                
$fields[] = array(
                    
'id'      => 'paging_size',
                    
'title'   => __'Page Size''foogallery' ),
                    
'desc'    => __'The size of your pages.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'type'    => 'number',
                    
'class'   => 'small-text',
                    
'default' => 20,
                    
'step'    => '1',
                    
'min'     => '0',
                    
'row_data'=> array(
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode',
                        
'data-foogallery-hidden'                   => true,
                        
'data-foogallery-show-when-field'          => 'paging_type',
                        
'data-foogallery-show-when-field-operator' => '!==',
                        
'data-foogallery-show-when-field-value'    => '',
                    )
                );

                
$fields[] = array(
                    
'id'      => 'paging_position',
                    
'title'   => __'Position''foogallery' ),
                    
'desc'    => __'The position of the paging for either dots or pagination.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'spacer'  => '<span class="spacer"></span>',
                    
'type'    => 'radio',
                    
'default' => 'both',
                    
'choices' => apply_filters'foogallery_gallery_template_paging_position_choices', array(
                        
''  => __'None''foogallery' ),
                        
'top'   => __'Top''foogallery' ),
                        
'bottom'   => __'Bottom''foogallery' ),
                        
'both'   => __'Both''foogallery' )
                    ) ),
                    
'row_data'=> array(
                        
'data-foogallery-hidden' => true,
                        
'data-foogallery-show-when-field-operator' => 'regex',
                        
'data-foogallery-show-when-field' => 'paging_type',
                        
'data-foogallery-show-when-field-value' => 'dots|pagination',
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode'
                    
)
                );

                
$fields[] = array(
                    
'id'      => 'paging_theme',
                    
'title'   => __'Theme''foogallery' ),
                    
'desc'    => __'The theme used for paging.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'spacer'  => '<span class="spacer"></span>',
                    
'type'    => 'radio',
                    
'default' => 'fg-light',
                    
'choices' => apply_filters'foogallery_gallery_template_paging_theme_choices', array(
                        
'fg-light'  => __'Light''foogallery' ),
                        
'fg-dark'   => __'Dark''foogallery' ),
                    ) ),
                    
'row_data'=> array(
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode',
                        
'data-foogallery-hidden'                   => true,
                        
'data-foogallery-show-when-field'          => 'paging_type',
                        
'data-foogallery-show-when-field-operator' => '!==',
                        
'data-foogallery-show-when-field-value'    => '',
                    )
                );

                
$fields[] = array(
                    
'id'      => 'paging_scroll',
                    
'title'   => __'Scroll To Top''foogallery' ),
                    
'desc'    => __'Whether or not it should scroll to the top of the gallery when paging is changed.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'type'    => 'radio',
                    
'spacer'  => '<span class="spacer"></span>',
                    
'default' => 'true',
                    
'choices' => array(
                        
'true'  => __'Yes''foogallery' ),
                        
'false'  => __'No''foogallery' ),
                    ),
                    
'row_data'=> array(
                        
'data-foogallery-hidden' => true,
                        
'data-foogallery-show-when-field-operator' => 'regex',
                        
'data-foogallery-show-when-field' => 'paging_type',
                        
'data-foogallery-show-when-field-value' => 'dots|pagination',
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode'
                    
)
                );

                
$fields[] = array(
                    
'id'      => 'paging_limit',
                    
'title'   => __'Paging Limit''foogallery' ),
                    
'desc'    => __'The maximum number of page links to display for the gallery.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'type'    => 'number',
                    
'class'   => 'small-text',
                    
'default' => 5,
                    
'step'    => '1',
                    
'min'     => '0',
                    
'row_data'=> array(
                        
'data-foogallery-hidden' => true,
                        
'data-foogallery-show-when-field' => 'paging_type',
                        
'data-foogallery-show-when-field-value' => 'pagination',
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode'
                    
)
                );

                
$fields[] = array(
                    
'id'      => 'paging_showFirstLast',
                    
'title'   => __'First &amp; Last Buttons''foogallery' ),
                    
'desc'    => __'Whether or not to show the first &amp; last buttons for pagination.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'type'    => 'radio',
                    
'spacer'  => '<span class="spacer"></span>',
                    
'default' => 'true',
                    
'choices' => array(
                        
'true'  => __'Show''foogallery' ),
                        
'false'  => __'Hide''foogallery' ),
                    ),
                    
'row_data'=> array(
                        
'data-foogallery-hidden' => true,
                        
'data-foogallery-show-when-field' => 'paging_type',
                        
'data-foogallery-show-when-field-value' => 'pagination',
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode'
                    
)
                );

                
$fields[] = array(
                    
'id'      => 'paging_showPrevNext',
                    
'title'   => __'Prev &amp; Next Buttons''foogallery' ),
                    
'desc'    => __'Whether or not to show the previous &amp; next buttons for pagination.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'type'    => 'radio',
                    
'spacer'  => '<span class="spacer"></span>',
                    
'default' => 'true',
                    
'choices' => array(
                        
'true'  => __'Show''foogallery' ),
                        
'false'  => __'Hide''foogallery' ),
                    ),
                    
'row_data'=> array(
                        
'data-foogallery-hidden' => true,
                        
'data-foogallery-show-when-field' => 'paging_type',
                        
'data-foogallery-show-when-field-value' => 'pagination',
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode'
                    
)
                );

                
$fields[] = array(
                    
'id'      => 'paging_showPrevNextMore',
                    
'title'   => __'More Buttons''foogallery' ),
                    
'desc'    => __'Whether or not to show the previous &amp; next more buttons for pagination.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'type'    => 'radio',
                    
'spacer'  => '<span class="spacer"></span>',
                    
'default' => 'true',
                    
'choices' => array(
                        
'true'  => __'Show''foogallery' ),
                        
'false'  => __'Hide''foogallery' ),
                    ),
                    
'row_data'=> array(
                        
'data-foogallery-hidden' => true,
                        
'data-foogallery-show-when-field' => 'paging_type',
                        
'data-foogallery-show-when-field-value' => 'pagination',
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode'
                    
)
                );

                
$fields[] = array(
                    
'id'      => 'paging_output',
                    
'title'   => __'Paging Output''foogallery' ),
                    
'desc'    => __'How the paging items are output. We recommend that very large galleries output as JSON.''foogallery' ),
                    
'section' => __'Paging''foogallery' ),
                    
'spacer'  => '<span class="spacer"></span>',
                    
'type'    => 'radio',
                    
'default' => 'html',
                    
'choices' => apply_filters'foogallery_gallery_template_paging_output_choices', array(
                        
''  => __'JSON''foogallery' ),
                        
'html'   => __'HTML''foogallery' )
                    ) ),
                    
'row_data'=> array(
                        
'data-foogallery-change-selector' => 'input',
                        
'data-foogallery-preview' => 'shortcode',
                        
'data-foogallery-value-selector' => 'input:checked',
                        
'data-foogallery-hidden'                   => true,
                        
'data-foogallery-show-when-field'          => 'paging_type',
                        
'data-foogallery-show-when-field-operator' => '!==',
                        
'data-foogallery-show-when-field-value'    => '',
                    )
                );
            }

            return 
$fields;
        }

        
/**
         * Determine if the gallery has paging enabled
         *
         * @param $foogallery FooGallery
         */
        
function determine_paging$foogallery ) {
            
$template_data foogallery_get_gallery_template$foogallery->gallery_template );

            
//check the template supports paging
            
$paging $template_data && array_key_exists'paging_support'$template_data ) && true === $template_data['paging_support'];

            
$foogallery->paging apply_filters'foogallery_paging'$paging$foogallery );
        }

        
/**
         * Add the required paging options if needed
         *
         * @param $attributes array
         * @param $gallery FooGallery
         *
         * @return array
         */
        
function add_paging_options($options$gallery$attributes) {
            if ( isset( 
$gallery->paging ) && true === $gallery->paging) {

                
//check if we have arguments from the shortcode and override the saved settings
                
$paging $this->get_foogallery_argument$gallery'paging_type''paging''' );

                if ( 
'' !== $paging ) {
                    
$paging_position $this->get_foogallery_argument$gallery'paging_position''paging_position''both' );
                    
$paging_theme    $this->get_foogallery_argument$gallery'paging_theme''paging_theme''fg-light' );
                    
$paging_size     intval$this->get_foogallery_argument$gallery'paging_size''paging_size''30' ) );
                    
$paging_scroll   $this->get_foogallery_argument$gallery'paging_scroll''paging_scroll''true' ) === 'true';

                    
//force bottom position for infinite and loadMore paging
                    
if ( 'infinite' === $paging || 'loadMore' === $paging ) {
                        
$paging_position 'bottom';
                    }

                    
$paging_options = array(
                        
'type'        => $paging,
                        
'theme'       => $paging_theme,
                        
'size'        => $paging_size,
                        
'position'    => $paging_position,
                        
'scrollToTop' => $paging_scroll
                    
);

                    if ( 
'pagination' === $paging ) {
                        
$paging_options['limit'] = intval$this->get_foogallery_argument$gallery'paging_limit''paging_limit''5' ) );;
                        
$paging_options['showFirstLast'] = $this->get_foogallery_argument$gallery'paging_showFirstLast''paging_showFirstLast''true' ) === 'true';;
                        
$paging_options['showPrevNext'] = $this->get_foogallery_argument$gallery'paging_showPrevNext''paging_showPrevNext''true' ) === 'true';;
                        
$paging_options['showPrevNextMore'] = $this->get_foogallery_argument$gallery'paging_showPrevNextMore''paging_showPrevNextMore''true' ) === 'true';;
                    }

                    
$options['paging'] = $gallery->paging_options $paging_options;

                    if ( 
'on' !== foogallery_get_setting'output_json_to_script_block''') ) {
                        
$paging_output $this->get_foogallery_argument$gallery'paging_output''paging_output''' );
                        
//add the items to the option if paging_output is set to JSON
                        
if ( '' === $paging_output && $paging_size ) {
                            
//build up the arguments from the gallery template

                            
$attachments      array_slice$gallery->attachments(), $paging_size );
                            
$json_objects     array_map'foogallery_build_json_object_from_attachment'$attachments );
                            
$options['items'] = $json_objects;
                        }
                    }
                }
            }
            return 
$options;
        }

        private function 
get_foogallery_argument$gallery$setting_id$argument_name$default_value ) {
            global 
$current_foogallery_arguments;

            if ( isset( 
$current_foogallery_arguments ) && isset( $current_foogallery_arguments[$argument_name] ) ) {
                return 
$current_foogallery_arguments[$argument_name];
            } else {
                return 
$gallery->get_setting$setting_id$default_value );
            }
        }

        
/**
         * Build up a arguments used in the preview of the gallery
         *
         * @param $args
         * @param $post_data
         * @param $template
         *
         * @return mixed
         */
        
function preview_arguments$args$post_data$template ) {
            
$template_data foogallery_get_gallery_template$template );

            
//check the template supports paging
            
if ( $template_data && array_key_exists'paging_support'$template_data ) && true === $template_data['paging_support'] ) {
                
$args['paging'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_type'];
                
$args['paging_position'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_position'];
                
$args['paging_theme'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_theme'];
                
$args['paging_size'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_size'];
                
$args['paging_scroll'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_scroll'];
                
$args['paging_output'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_output'];

                
$args['paging_limit'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_limit'];
                
$args['paging_showFirstLast'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_showFirstLast'];
                
$args['paging_showPrevNext'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_showPrevNext'];
                
$args['paging_showPrevNextMore'] = $post_data[FOOGALLERY_META_SETTINGS][$template'_paging_showPrevNextMore'];
            }

            return 
$args;
        }

        
/**
         * Checks if the gallery output is JSON
         *
         * @param FooGallery $gallery
         * @return bool
         */
        
function is_paging_output_json($gallery) {
            if ( isset( 
$gallery->paging ) && true === $gallery->paging ) {
                
$paging_output $this->get_foogallery_argument$gallery'paging_output''paging_output''' );
                return 
'' === $paging_output;
            }
            return 
false;
        }

        
/**
         * Override the attachments returned for rendering a paginated gallery
         *
         * @param bool $override
         * @param FooGallery $gallery
         * @return bool|array
         */
        
function attachments_override$override$gallery ) {

            if ( 
$this->is_paging_output_json$gallery ) ) {

                
$page_size = isset( $gallery->paging_options ) && array_key_exists'size'$gallery->paging_options ) ? $gallery->paging_options['size'] : 0;

                if ( 
$page_size ) {

                    
$attachments $gallery->attachments();

                    
//return the first N attachments for the gallery
                    
return array_splice$attachments0$page_size );

                }
            }

            return 
$override;
        }

        
/**
         * Output a script block with all the gallery attachments as json
         *
         * @param FooGallery $gallery
         */
        
function output_paging_script_block$gallery ) {
            if ( 
'on' === foogallery_get_setting'output_json_to_script_block''') ) {
                if ( 
$this->is_paging_output_json$gallery ) ) {
                    
$page_size = isset( $gallery->paging_options ) && array_key_exists'size'$gallery->paging_options ) ? $gallery->paging_options['size'] : 0;
                    if ( 
$page_size ) {
                        
//build up the arguments from the gallery template
                        
$attachments      array_slice$gallery->attachments(), $page_size );
                        
$attachments_json array_map'foogallery_build_json_from_attachment'$attachments );
                        echo 
'<script type="text/javascript">';
                        echo 
'  window["foogallery-gallery-' $gallery->ID '-items"] = [';
                        echo 
implode', '$attachments_json );
                        echo 
'  ];';
                        echo 
'</script>';
                    }
                }
            }
        }
    }
}