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
|
|
|
if ( is_admin() ) { |
11
|
|
|
//add extra fields to the templates that support paging |
12
|
|
|
add_filter( 'foogallery_override_gallery_template_fields', array( $this, 'add_paging_fields' ), 10, 2 ); |
13
|
|
|
|
14
|
|
|
//build up any preview arguments |
15
|
|
|
add_filter( 'foogallery_preview_arguments', array( $this, 'preview_arguments' ), 10, 3 ); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
//adds the paging property to a FooGallery |
19
|
|
|
add_action( 'foogallery_located_template', array( $this, 'determine_paging' ), 10, 2 ); |
20
|
|
|
|
21
|
|
|
//add the paging attributes to the gallery container |
22
|
|
|
add_filter( 'foogallery_build_container_data_options', array( $this, 'add_paging_options' ), 10, 3 ); |
23
|
|
|
|
24
|
|
|
//limit the number of attachments returned when rendering a gallery |
25
|
|
|
add_filter( 'foogallery_gallery_attachments_override_for_rendering', array( $this, 'attachments_override' ), 10, 3 ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add paging fields to the gallery template |
30
|
|
|
* |
31
|
|
|
* @uses "foogallery_override_gallery_template_fields" |
32
|
|
|
* @param $fields |
33
|
|
|
* @param $template |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
function add_paging_fields( $fields, $template ) { |
|
|
|
|
38
|
|
|
if ( $template && array_key_exists( 'paging_support', $template ) && true === $template['paging_support'] ) { |
39
|
|
|
$fields[] = array( |
40
|
|
|
'id' => 'paging_type', |
41
|
|
|
'title' => __( 'Paging Type', 'foogallery' ), |
42
|
|
|
'desc' => __( 'Add paging to a large gallery.', 'foogallery' ), |
43
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
44
|
|
|
'spacer' => '<span class="spacer"></span>', |
45
|
|
|
'type' => 'radio', |
46
|
|
|
'default' => '', |
47
|
|
|
'choices' => apply_filters( 'foogallery_gallery_template_paging_type_choices', array( |
48
|
|
|
'' => __( 'None', 'foogallery' ), |
49
|
|
|
'dots' => __( 'Dots', 'foogallery' ) |
50
|
|
|
) ), |
51
|
|
|
'row_data'=> array( |
52
|
|
|
'data-foogallery-change-selector' => 'input', |
53
|
|
|
'data-foogallery-preview' => 'shortcode', |
54
|
|
|
'data-foogallery-value-selector' => 'input:checked', |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$fields[] = array( |
59
|
|
|
'id' => 'paging_size', |
60
|
|
|
'title' => __( 'Page Size', 'foogallery' ), |
61
|
|
|
'desc' => __( 'The size of your pages.', 'foogallery' ), |
62
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
63
|
|
|
'type' => 'number', |
64
|
|
|
'class' => 'small-text', |
65
|
|
|
'default' => 20, |
66
|
|
|
'step' => '1', |
67
|
|
|
'min' => '0', |
68
|
|
|
'row_data'=> array( |
69
|
|
|
'data-foogallery-change-selector' => 'input', |
70
|
|
|
'data-foogallery-preview' => 'shortcode', |
71
|
|
|
'data-foogallery-hidden' => true, |
72
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
73
|
|
|
'data-foogallery-show-when-field-operator' => '!==', |
74
|
|
|
'data-foogallery-show-when-field-value' => '', |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$fields[] = array( |
79
|
|
|
'id' => 'paging_position', |
80
|
|
|
'title' => __( 'Position', 'foogallery' ), |
81
|
|
|
'desc' => __( 'The position of the paging for either dots or pagination.', 'foogallery' ), |
82
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
83
|
|
|
'spacer' => '<span class="spacer"></span>', |
84
|
|
|
'type' => 'radio', |
85
|
|
|
'default' => 'both', |
86
|
|
|
'choices' => apply_filters( 'foogallery_gallery_template_paging_position_choices', array( |
87
|
|
|
'' => __( 'None', 'foogallery' ), |
88
|
|
|
'top' => __( 'Top', 'foogallery' ), |
89
|
|
|
'bottom' => __( 'Bottom', 'foogallery' ), |
90
|
|
|
'both' => __( 'Both', 'foogallery' ) |
91
|
|
|
) ), |
92
|
|
|
'row_data'=> array( |
93
|
|
|
'data-foogallery-hidden' => true, |
94
|
|
|
'data-foogallery-show-when-field-operator' => 'regex', |
95
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
96
|
|
|
'data-foogallery-show-when-field-value' => 'dots|pagination', |
97
|
|
|
'data-foogallery-change-selector' => 'input', |
98
|
|
|
'data-foogallery-preview' => 'shortcode' |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$fields[] = array( |
103
|
|
|
'id' => 'paging_theme', |
104
|
|
|
'title' => __( 'Theme', 'foogallery' ), |
105
|
|
|
'desc' => __( 'The theme used for paging.', 'foogallery' ), |
106
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
107
|
|
|
'spacer' => '<span class="spacer"></span>', |
108
|
|
|
'type' => 'radio', |
109
|
|
|
'default' => 'fg-light', |
110
|
|
|
'choices' => apply_filters( 'foogallery_gallery_template_paging_theme_choices', array( |
111
|
|
|
'fg-light' => __( 'Light', 'foogallery' ), |
112
|
|
|
'fg-dark' => __( 'Dark', 'foogallery' ), |
113
|
|
|
) ), |
114
|
|
|
'row_data'=> array( |
115
|
|
|
'data-foogallery-change-selector' => 'input', |
116
|
|
|
'data-foogallery-preview' => 'shortcode', |
117
|
|
|
'data-foogallery-hidden' => true, |
118
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
119
|
|
|
'data-foogallery-show-when-field-operator' => '!==', |
120
|
|
|
'data-foogallery-show-when-field-value' => '', |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$fields[] = array( |
125
|
|
|
'id' => 'paging_scroll', |
126
|
|
|
'title' => __( 'Scroll To Top', 'foogallery' ), |
127
|
|
|
'desc' => __( 'Whether or not it should scroll to the top of the gallery when paging is changed.', 'foogallery' ), |
128
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
129
|
|
|
'type' => 'radio', |
130
|
|
|
'spacer' => '<span class="spacer"></span>', |
131
|
|
|
'default' => 'true', |
132
|
|
|
'choices' => array( |
133
|
|
|
'true' => __( 'Yes', 'foogallery' ), |
134
|
|
|
'false' => __( 'No', 'foogallery' ), |
135
|
|
|
), |
136
|
|
|
'row_data'=> array( |
137
|
|
|
'data-foogallery-hidden' => true, |
138
|
|
|
'data-foogallery-show-when-field-operator' => 'regex', |
139
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
140
|
|
|
'data-foogallery-show-when-field-value' => 'dots|pagination', |
141
|
|
|
'data-foogallery-change-selector' => 'input', |
142
|
|
|
'data-foogallery-preview' => 'shortcode' |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$fields[] = array( |
147
|
|
|
'id' => 'paging_limit', |
148
|
|
|
'title' => __( 'Paging Limit', 'foogallery' ), |
149
|
|
|
'desc' => __( 'The maximum number of page links to display for the gallery.', 'foogallery' ), |
150
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
151
|
|
|
'type' => 'number', |
152
|
|
|
'class' => 'small-text', |
153
|
|
|
'default' => 5, |
154
|
|
|
'step' => '1', |
155
|
|
|
'min' => '0', |
156
|
|
|
'row_data'=> array( |
157
|
|
|
'data-foogallery-hidden' => true, |
158
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
159
|
|
|
'data-foogallery-show-when-field-value' => 'pagination', |
160
|
|
|
'data-foogallery-change-selector' => 'input', |
161
|
|
|
'data-foogallery-preview' => 'shortcode' |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$fields[] = array( |
166
|
|
|
'id' => 'paging_showFirstLast', |
167
|
|
|
'title' => __( 'First & Last Buttons', 'foogallery' ), |
168
|
|
|
'desc' => __( 'Whether or not to show the first & last buttons for pagination.', 'foogallery' ), |
169
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
170
|
|
|
'type' => 'radio', |
171
|
|
|
'spacer' => '<span class="spacer"></span>', |
172
|
|
|
'default' => 'true', |
173
|
|
|
'choices' => array( |
174
|
|
|
'true' => __( 'Show', 'foogallery' ), |
175
|
|
|
'false' => __( 'Hide', 'foogallery' ), |
176
|
|
|
), |
177
|
|
|
'row_data'=> array( |
178
|
|
|
'data-foogallery-hidden' => true, |
179
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
180
|
|
|
'data-foogallery-show-when-field-value' => 'pagination', |
181
|
|
|
'data-foogallery-change-selector' => 'input', |
182
|
|
|
'data-foogallery-preview' => 'shortcode' |
183
|
|
|
) |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$fields[] = array( |
187
|
|
|
'id' => 'paging_showPrevNext', |
188
|
|
|
'title' => __( 'Prev & Next Buttons', 'foogallery' ), |
189
|
|
|
'desc' => __( 'Whether or not to show the previous & next buttons for pagination.', 'foogallery' ), |
190
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
191
|
|
|
'type' => 'radio', |
192
|
|
|
'spacer' => '<span class="spacer"></span>', |
193
|
|
|
'default' => 'true', |
194
|
|
|
'choices' => array( |
195
|
|
|
'true' => __( 'Show', 'foogallery' ), |
196
|
|
|
'false' => __( 'Hide', 'foogallery' ), |
197
|
|
|
), |
198
|
|
|
'row_data'=> array( |
199
|
|
|
'data-foogallery-hidden' => true, |
200
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
201
|
|
|
'data-foogallery-show-when-field-value' => 'pagination', |
202
|
|
|
'data-foogallery-change-selector' => 'input', |
203
|
|
|
'data-foogallery-preview' => 'shortcode' |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$fields[] = array( |
208
|
|
|
'id' => 'paging_showPrevNextMore', |
209
|
|
|
'title' => __( 'More Buttons', 'foogallery' ), |
210
|
|
|
'desc' => __( 'Whether or not to show the previous & next more buttons for pagination.', 'foogallery' ), |
211
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
212
|
|
|
'type' => 'radio', |
213
|
|
|
'spacer' => '<span class="spacer"></span>', |
214
|
|
|
'default' => 'true', |
215
|
|
|
'choices' => array( |
216
|
|
|
'true' => __( 'Show', 'foogallery' ), |
217
|
|
|
'false' => __( 'Hide', 'foogallery' ), |
218
|
|
|
), |
219
|
|
|
'row_data'=> array( |
220
|
|
|
'data-foogallery-hidden' => true, |
221
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
222
|
|
|
'data-foogallery-show-when-field-value' => 'pagination', |
223
|
|
|
'data-foogallery-change-selector' => 'input', |
224
|
|
|
'data-foogallery-preview' => 'shortcode' |
225
|
|
|
) |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$fields[] = array( |
229
|
|
|
'id' => 'paging_output', |
230
|
|
|
'title' => __( 'Paging Output', 'foogallery' ), |
231
|
|
|
'desc' => __( 'How the paging items are output. We recommend that very large galleries output as JSON.', 'foogallery' ), |
232
|
|
|
'section' => __( 'Paging', 'foogallery' ), |
233
|
|
|
'spacer' => '<span class="spacer"></span>', |
234
|
|
|
'type' => 'radio', |
235
|
|
|
'default' => '', |
236
|
|
|
'choices' => apply_filters( 'foogallery_gallery_template_paging_output_choices', array( |
237
|
|
|
'' => __( 'JSON', 'foogallery' ), |
238
|
|
|
'html' => __( 'HTML', 'foogallery' ) |
239
|
|
|
) ), |
240
|
|
|
'row_data'=> array( |
241
|
|
|
'data-foogallery-change-selector' => 'input', |
242
|
|
|
'data-foogallery-preview' => 'shortcode', |
243
|
|
|
'data-foogallery-value-selector' => 'input:checked', |
244
|
|
|
'data-foogallery-hidden' => true, |
245
|
|
|
'data-foogallery-show-when-field' => 'paging_type', |
246
|
|
|
'data-foogallery-show-when-field-operator' => '!==', |
247
|
|
|
'data-foogallery-show-when-field-value' => '', |
248
|
|
|
) |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $fields; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Determine if the gallery has paging enabled |
257
|
|
|
* |
258
|
|
|
* @param $foogallery FooGallery |
259
|
|
|
*/ |
260
|
|
|
function determine_paging( $foogallery ) { |
|
|
|
|
261
|
|
|
$template_data = foogallery_get_gallery_template( $foogallery->gallery_template ); |
262
|
|
|
|
263
|
|
|
//check the template supports paging |
264
|
|
|
$paging = $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support']; |
265
|
|
|
|
266
|
|
|
$foogallery->paging = apply_filters( 'foogallery_paging', $paging, $foogallery ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Add the required paging options if needed |
271
|
|
|
* |
272
|
|
|
* @param $attributes array |
273
|
|
|
* @param $gallery FooGallery |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
function add_paging_options($options, $gallery, $attributes) { |
|
|
|
|
278
|
|
|
if ( isset( $gallery->paging ) && true === $gallery->paging) { |
279
|
|
|
|
280
|
|
|
//check if we have arguments from the shortcode and override the saved settings |
281
|
|
|
$paging = $this->get_foogallery_argument( $gallery, 'paging_type', 'paging', '' ); |
282
|
|
|
|
283
|
|
|
if ( '' !== $paging ) { |
284
|
|
|
$paging_position = $this->get_foogallery_argument( $gallery, 'paging_position', 'paging_position', 'both' ); |
285
|
|
|
$paging_theme = $this->get_foogallery_argument( $gallery, 'paging_theme', 'paging_theme', 'fg-light' ); |
286
|
|
|
$paging_size = intval( $this->get_foogallery_argument( $gallery, 'paging_size', 'paging_size', '30' ) ); |
287
|
|
|
$paging_scroll = $this->get_foogallery_argument( $gallery, 'paging_scroll', 'paging_scroll', 'true' ) === 'true'; |
288
|
|
|
|
289
|
|
|
//force bottom position for infinite and loadMore paging |
290
|
|
|
if ( 'infinite' === $paging || 'loadMore' === $paging ) { |
291
|
|
|
$paging_position = 'bottom'; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$paging_options = array( |
295
|
|
|
'type' => $paging, |
296
|
|
|
'theme' => $paging_theme, |
297
|
|
|
'size' => $paging_size, |
298
|
|
|
'position' => $paging_position, |
299
|
|
|
'scrollToTop' => $paging_scroll |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
if ( 'pagination' === $paging ) { |
303
|
|
|
$paging_options['limit'] = intval( $this->get_foogallery_argument( $gallery, 'paging_limit', 'paging_limit', '5' ) );; |
304
|
|
|
$paging_options['showFirstLast'] = $this->get_foogallery_argument( $gallery, 'paging_showFirstLast', 'paging_showFirstLast', 'true' ) === 'true';; |
305
|
|
|
$paging_options['showPrevNext'] = $this->get_foogallery_argument( $gallery, 'paging_showPrevNext', 'paging_showPrevNext', 'true' ) === 'true';; |
306
|
|
|
$paging_options['showPrevNextMore'] = $this->get_foogallery_argument( $gallery, 'paging_showPrevNextMore', 'paging_showPrevNextMore', 'true' ) === 'true';; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$options['paging'] = $gallery->paging_options = $paging_options; |
310
|
|
|
|
311
|
|
|
$paging_output = $this->get_foogallery_argument( $gallery, 'paging_output', 'paging_output', '' ); |
312
|
|
|
//add the items to the option if paging_output is set to JSON |
313
|
|
|
if ('' === $paging_output && $paging_size > 0) { |
314
|
|
|
//build up the arguments from the gallery template |
315
|
|
|
|
316
|
|
|
$attachments = array_slice( $gallery->attachments(), $paging_size ); |
317
|
|
|
$json_objects = array_map( 'foogallery_build_json_object_from_attachment', $attachments ); |
318
|
|
|
$options['items'] = $json_objects; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
return $options; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
private function get_foogallery_argument( $gallery, $setting_id, $argument_name, $default_value ) { |
326
|
|
|
global $current_foogallery_arguments; |
327
|
|
|
|
328
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments[$argument_name] ) ) { |
329
|
|
|
return $current_foogallery_arguments[$argument_name]; |
330
|
|
|
} else { |
331
|
|
|
return $gallery->get_setting( $setting_id, $default_value ); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Build up a arguments used in the preview of the gallery |
337
|
|
|
* |
338
|
|
|
* @param $args |
339
|
|
|
* @param $post_data |
340
|
|
|
* @param $template |
341
|
|
|
* |
342
|
|
|
* @return mixed |
343
|
|
|
*/ |
344
|
|
|
function preview_arguments( $args, $post_data, $template ) { |
|
|
|
|
345
|
|
|
$template_data = foogallery_get_gallery_template( $template ); |
346
|
|
|
|
347
|
|
|
//check the template supports paging |
348
|
|
|
if ( $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support'] ) { |
349
|
|
|
$args['paging'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_type']; |
350
|
|
|
$args['paging_position'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_position']; |
351
|
|
|
$args['paging_theme'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_theme']; |
352
|
|
|
$args['paging_size'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_size']; |
353
|
|
|
$args['paging_scroll'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_scroll']; |
354
|
|
|
$args['paging_output'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_output']; |
355
|
|
|
|
356
|
|
|
$args['paging_limit'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_limit']; |
357
|
|
|
$args['paging_showFirstLast'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showFirstLast']; |
358
|
|
|
$args['paging_showPrevNext'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNext']; |
359
|
|
|
$args['paging_showPrevNextMore'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNextMore']; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $args; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Checks if the gallery output is JSON |
367
|
|
|
* |
368
|
|
|
* @param FooGallery $gallery |
369
|
|
|
* @return bool |
370
|
|
|
*/ |
371
|
|
|
function is_paging_output_json($gallery) { |
|
|
|
|
372
|
|
|
if ( isset( $gallery->paging ) && true === $gallery->paging ) { |
373
|
|
|
$paging_output = $this->get_foogallery_argument( $gallery, 'paging_output', 'paging_output', '' ); |
374
|
|
|
return '' === $paging_output; |
375
|
|
|
} |
376
|
|
|
return false; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Override the attachments returned for rendering a paginated gallery |
381
|
|
|
* |
382
|
|
|
* @param bool $override |
383
|
|
|
* @param FooGallery $gallery |
384
|
|
|
* @return bool|array |
385
|
|
|
*/ |
386
|
|
|
function attachments_override( $override, $gallery ) { |
|
|
|
|
387
|
|
|
|
388
|
|
|
if ( $this->is_paging_output_json( $gallery ) ) { |
389
|
|
|
|
390
|
|
|
$page_size = isset( $gallery->paging_options ) && array_key_exists( 'size', $gallery->paging_options ) ? $gallery->paging_options['size'] : 0; |
391
|
|
|
|
392
|
|
|
if ( $page_size > 0 ) { |
393
|
|
|
|
394
|
|
|
$attachments = $gallery->attachments(); |
395
|
|
|
|
396
|
|
|
//return the first N attachments for the gallery |
397
|
|
|
return array_splice( $attachments, 0, $page_size ); |
398
|
|
|
|
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return $override; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.