GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/attachment-taxonomies ( 9fe273...54ac97 )
by Brad
06:02 queued 02:51
created

FooGallery_Paging   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 417
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 417
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 2
B add_paging_fields() 0 217 4
A determine_paging() 0 8 3
C add_paging_options() 0 37 7
A get_foogallery_argument() 0 9 3
A preview_arguments() 0 20 4
A is_paging_output_json() 0 7 3
B attachments_override() 0 18 5
B output_paging_script_block() 0 20 5
1
<?php
2
/**
3
 * Class used to handle paging for gallery templates
4
 */
5
if ( ! class_exists( 'FooGallery_Paging' ) ) {
6
7
	class FooGallery_Paging {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
		function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
10
			if ( is_admin() ) {
11
				//add extra fields to the templates that support lazy loading
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
            //output a script block with the rest of the attachments as json
28
            add_action( 'foogallery_loaded_template', array( $this, 'output_paging_script_block' ) );
29
		}
30
31
		/**
32
		 * Add paging fields to the gallery template
33
		 *
34
		 * @uses "foogallery_override_gallery_template_fields"
35
		 * @param $fields
36
		 * @param $template
37
		 *
38
		 * @return array
39
		 */
40
		function add_paging_fields( $fields, $template ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
			if ( $template && array_key_exists( 'paging_support', $template ) && true === $template['paging_support'] ) {
42
				$fields[] = array(
43
					'id'      => 'paging_type',
44
					'title'   => __( 'Paging Type', 'foogallery' ),
45
					'desc'    => __( 'Add paging to a large gallery.', 'foogallery' ),
46
					'section' => __( 'Paging', 'foogallery' ),
47
					'spacer'  => '<span class="spacer"></span>',
48
					'type'    => 'radio',
49
					'default' => '',
50
					'choices' => apply_filters( 'foogallery_gallery_template_paging_type_choices', array(
51
						''  => __( 'None', 'foogallery' ),
52
						'dots'   => __( 'Dots', 'foogallery' )
53
					) ),
54
					'row_data'=> array(
55
						'data-foogallery-change-selector' => 'input',
56
						'data-foogallery-preview' => 'shortcode',
57
						'data-foogallery-value-selector' => 'input:checked',
58
					)
59
				);
60
61
				$fields[] = array(
62
					'id'      => 'paging_size',
63
					'title'   => __( 'Page Size', 'foogallery' ),
64
					'desc'    => __( 'The size of your pages.', 'foogallery' ),
65
					'section' => __( 'Paging', 'foogallery' ),
66
					'type'    => 'number',
67
					'class'   => 'small-text',
68
					'default' => 20,
69
					'step'    => '1',
70
					'min'     => '0',
71
					'row_data'=> array(
72
						'data-foogallery-change-selector' => 'input',
73
						'data-foogallery-preview' => 'shortcode',
74
						'data-foogallery-hidden'                   => true,
75
						'data-foogallery-show-when-field'          => 'paging_type',
76
						'data-foogallery-show-when-field-operator' => '!==',
77
						'data-foogallery-show-when-field-value'    => '',
78
					)
79
				);
80
81
				$fields[] = array(
82
					'id'      => 'paging_position',
83
					'title'   => __( 'Position', 'foogallery' ),
84
					'desc'    => __( 'The position of the paging for either dots or pagination.', 'foogallery' ),
85
					'section' => __( 'Paging', 'foogallery' ),
86
					'spacer'  => '<span class="spacer"></span>',
87
					'type'    => 'radio',
88
					'default' => 'both',
89
					'choices' => apply_filters( 'foogallery_gallery_template_paging_position_choices', array(
90
						''  => __( 'None', 'foogallery' ),
91
						'top'   => __( 'Top', 'foogallery' ),
92
						'bottom'   => __( 'Bottom', 'foogallery' ),
93
						'both'   => __( 'Both', 'foogallery' )
94
					) ),
95
					'row_data'=> array(
96
						'data-foogallery-hidden' => true,
97
						'data-foogallery-show-when-field-operator' => 'regex',
98
						'data-foogallery-show-when-field' => 'paging_type',
99
						'data-foogallery-show-when-field-value' => 'dots|pagination',
100
						'data-foogallery-change-selector' => 'input',
101
						'data-foogallery-preview' => 'shortcode'
102
					)
103
				);
104
105
				$fields[] = array(
106
					'id'      => 'paging_theme',
107
					'title'   => __( 'Theme', 'foogallery' ),
108
					'desc'    => __( 'The theme used for paging.', 'foogallery' ),
109
					'section' => __( 'Paging', 'foogallery' ),
110
					'spacer'  => '<span class="spacer"></span>',
111
					'type'    => 'radio',
112
					'default' => 'fg-light',
113
					'choices' => apply_filters( 'foogallery_gallery_template_paging_position_choices', array(
114
						'fg-light'  => __( 'Light', 'foogallery' ),
115
						'fg-dark'   => __( 'Dark', 'foogallery' ),
116
					) ),
117
					'row_data'=> array(
118
						'data-foogallery-change-selector' => 'input',
119
						'data-foogallery-preview' => 'shortcode',
120
						'data-foogallery-hidden'                   => true,
121
						'data-foogallery-show-when-field'          => 'paging_type',
122
						'data-foogallery-show-when-field-operator' => '!==',
123
						'data-foogallery-show-when-field-value'    => '',
124
					)
125
				);
126
127
				$fields[] = array(
128
					'id'      => 'paging_scroll',
129
					'title'   => __( 'Scroll To Top', 'foogallery' ),
130
					'desc'    => __( 'Whether or not it should scroll to the top of the gallery when paging is changed.', 'foogallery' ),
131
					'section' => __( 'Paging', 'foogallery' ),
132
					'type'    => 'radio',
133
					'spacer'  => '<span class="spacer"></span>',
134
					'default' => 'true',
135
					'choices' => array(
136
						'true'  => __( 'Yes', 'foogallery' ),
137
						'false'  => __( 'No', 'foogallery' ),
138
					),
139
					'row_data'=> array(
140
						'data-foogallery-hidden' => true,
141
						'data-foogallery-show-when-field-operator' => 'regex',
142
						'data-foogallery-show-when-field' => 'paging_type',
143
						'data-foogallery-show-when-field-value' => 'dots|pagination',
144
						'data-foogallery-change-selector' => 'input',
145
						'data-foogallery-preview' => 'shortcode'
146
					)
147
				);
148
149
				$fields[] = array(
150
					'id'      => 'paging_limit',
151
					'title'   => __( 'Paging Limit', 'foogallery' ),
152
					'desc'    => __( 'The maximum number of page links to display for the gallery.', 'foogallery' ),
153
					'section' => __( 'Paging', 'foogallery' ),
154
					'type'    => 'number',
155
					'class'   => 'small-text',
156
					'default' => 5,
157
					'step'    => '1',
158
					'min'     => '0',
159
					'row_data'=> array(
160
						'data-foogallery-hidden' => true,
161
						'data-foogallery-show-when-field' => 'paging_type',
162
						'data-foogallery-show-when-field-value' => 'pagination',
163
						'data-foogallery-change-selector' => 'input',
164
						'data-foogallery-preview' => 'shortcode'
165
					)
166
				);
167
168
				$fields[] = array(
169
					'id'      => 'paging_showFirstLast',
170
					'title'   => __( 'First &amp; Last Buttons', 'foogallery' ),
171
					'desc'    => __( 'Whether or not to show the first &amp; last buttons for pagination.', 'foogallery' ),
172
					'section' => __( 'Paging', 'foogallery' ),
173
					'type'    => 'radio',
174
					'spacer'  => '<span class="spacer"></span>',
175
					'default' => 'true',
176
					'choices' => array(
177
						'true'  => __( 'Show', 'foogallery' ),
178
						'false'  => __( 'Hide', 'foogallery' ),
179
					),
180
					'row_data'=> array(
181
						'data-foogallery-hidden' => true,
182
						'data-foogallery-show-when-field' => 'paging_type',
183
						'data-foogallery-show-when-field-value' => 'pagination',
184
						'data-foogallery-change-selector' => 'input',
185
						'data-foogallery-preview' => 'shortcode'
186
					)
187
				);
188
189
				$fields[] = array(
190
					'id'      => 'paging_showPrevNext',
191
					'title'   => __( 'Prev &amp; Next Buttons', 'foogallery' ),
192
					'desc'    => __( 'Whether or not to show the previous &amp; next buttons for pagination.', 'foogallery' ),
193
					'section' => __( 'Paging', 'foogallery' ),
194
					'type'    => 'radio',
195
					'spacer'  => '<span class="spacer"></span>',
196
					'default' => 'true',
197
					'choices' => array(
198
						'true'  => __( 'Show', 'foogallery' ),
199
						'false'  => __( 'Hide', 'foogallery' ),
200
					),
201
					'row_data'=> array(
202
						'data-foogallery-hidden' => true,
203
						'data-foogallery-show-when-field' => 'paging_type',
204
						'data-foogallery-show-when-field-value' => 'pagination',
205
						'data-foogallery-change-selector' => 'input',
206
						'data-foogallery-preview' => 'shortcode'
207
					)
208
				);
209
210
				$fields[] = array(
211
					'id'      => 'paging_showPrevNextMore',
212
					'title'   => __( 'More Buttons', 'foogallery' ),
213
					'desc'    => __( 'Whether or not to show the previous &amp; next more buttons for pagination.', 'foogallery' ),
214
					'section' => __( 'Paging', 'foogallery' ),
215
					'type'    => 'radio',
216
					'spacer'  => '<span class="spacer"></span>',
217
					'default' => 'true',
218
					'choices' => array(
219
						'true'  => __( 'Show', 'foogallery' ),
220
						'false'  => __( 'Hide', 'foogallery' ),
221
					),
222
					'row_data'=> array(
223
						'data-foogallery-hidden' => true,
224
						'data-foogallery-show-when-field' => 'paging_type',
225
						'data-foogallery-show-when-field-value' => 'pagination',
226
						'data-foogallery-change-selector' => 'input',
227
						'data-foogallery-preview' => 'shortcode'
228
					)
229
				);
230
231
				$fields[] = array(
232
					'id'      => 'paging_output',
233
					'title'   => __( 'Paging Output', 'foogallery' ),
234
					'desc'    => __( 'How the paging items are output. We recommend that very large galleries output as JSON.', 'foogallery' ),
235
					'section' => __( 'Paging', 'foogallery' ),
236
					'spacer'  => '<span class="spacer"></span>',
237
					'type'    => 'radio',
238
					'default' => '',
239
					'choices' => apply_filters( 'foogallery_gallery_template_paging_output_choices', array(
240
						''  => __( 'JSON', 'foogallery' ),
241
						'html'   => __( 'HTML', 'foogallery' )
242
					) ),
243
					'row_data'=> array(
244
						'data-foogallery-change-selector' => 'input',
245
						'data-foogallery-preview' => 'shortcode',
246
						'data-foogallery-value-selector' => 'input:checked',
247
						'data-foogallery-hidden'                   => true,
248
						'data-foogallery-show-when-field'          => 'paging_type',
249
						'data-foogallery-show-when-field-operator' => '!==',
250
						'data-foogallery-show-when-field-value'    => '',
251
					)
252
				);
253
			}
254
255
			return $fields;
256
		}
257
258
		/**
259
		 * Determine if the gallery has paging enabled
260
		 *
261
		 * @param $foogallery FooGallery
262
		 */
263
		function determine_paging( $foogallery ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
264
			$template_data = foogallery_get_gallery_template( $foogallery->gallery_template );
265
266
			//check the template supports paging
267
			$paging = $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support'];
268
269
			$foogallery->paging = apply_filters( 'foogallery_paging', $paging, $foogallery );
270
		}
271
272
		/**
273
		 * Add the required paging options if needed
274
		 *
275
		 * @param $attributes array
276
		 * @param $gallery FooGallery
277
		 *
278
		 * @return array
279
		 */
280
		function add_paging_options($options, $gallery, $attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $attributes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
281
			if ( isset( $gallery->paging ) && true === $gallery->paging) {
282
283
				//check if we have arguments from the shortcode and override the saved settings
284
				$paging = $this->get_foogallery_argument( $gallery, 'paging_type', 'paging', '' );
285
286
				if ( '' !== $paging ) {
287
					$paging_position = $this->get_foogallery_argument( $gallery, 'paging_position', 'paging_position', 'both' );
288
					$paging_theme    = $this->get_foogallery_argument( $gallery, 'paging_theme', 'paging_theme', 'fg-light' );
289
					$paging_size     = intval( $this->get_foogallery_argument( $gallery, 'paging_size', 'paging_size', '30' ) );
290
					$paging_scroll   = $this->get_foogallery_argument( $gallery, 'paging_scroll', 'paging_scroll', 'true' ) === 'true';
291
292
					//force bottom position for infinite and loadMore paging
293
					if ( 'infinite' === $paging || 'loadMore' === $paging ) {
294
						$paging_position = 'bottom';
295
					}
296
297
                    $paging_options = array(
298
						'type'        => $paging,
299
						'theme'       => $paging_theme,
300
						'size'        => $paging_size,
301
						'position'    => $paging_position,
302
						'scrollToTop' => $paging_scroll
303
					);
304
305
					if ( 'pagination' === $paging ) {
306
                        $paging_options['limit'] = intval( $this->get_foogallery_argument( $gallery, 'paging_limit', 'paging_limit', '5' ) );;
307
						$paging_options['showFirstLast'] = $this->get_foogallery_argument( $gallery, 'paging_showFirstLast', 'paging_showFirstLast', 'true' ) === 'true';;
308
						$paging_options['showPrevNext'] = $this->get_foogallery_argument( $gallery, 'paging_showPrevNext', 'paging_showPrevNext', 'true' ) === 'true';;
309
						$paging_options['showPrevNextMore'] = $this->get_foogallery_argument( $gallery, 'paging_showPrevNextMore', 'paging_showPrevNextMore', 'true' ) === 'true';;
310
					}
311
312
                    $options['paging'] = $gallery->paging_options = $paging_options;
313
				}
314
			}
315
			return $options;
316
		}
317
318
		private function get_foogallery_argument( $gallery, $setting_id, $argument_name, $default_value ) {
319
			global $current_foogallery_arguments;
320
321
			if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments[$argument_name] ) ) {
322
				return $current_foogallery_arguments[$argument_name];
323
			} else {
324
				return $gallery->get_setting( $setting_id, $default_value );
325
			}
326
		}
327
328
		/**
329
		 * Build up a arguments used in the preview of the gallery
330
		 *
331
		 * @param $args
332
		 * @param $post_data
333
		 * @param $template
334
		 *
335
		 * @return mixed
336
		 */
337
		function preview_arguments( $args, $post_data, $template ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
338
			$template_data = foogallery_get_gallery_template( $template );
339
340
			//check the template supports paging
341
			if ( $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support'] ) {
342
				$args['paging'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_type'];
343
				$args['paging_position'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_position'];
344
				$args['paging_theme'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_theme'];
345
				$args['paging_size'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_size'];
346
				$args['paging_scroll'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_scroll'];
347
				$args['paging_output'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_output'];
348
349
				$args['paging_limit'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_limit'];
350
				$args['paging_showFirstLast'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showFirstLast'];
351
				$args['paging_showPrevNext'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNext'];
352
				$args['paging_showPrevNextMore'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNextMore'];
353
			}
354
355
			return $args;
356
		}
357
358
		/**
359
		 * Checks if the gallery output is JSON
360
		 *
361
		 * @param FooGallery $gallery
362
		 * @return bool
363
		 */
364
		function is_paging_output_json($gallery) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
365
			if ( isset( $gallery->paging ) && true === $gallery->paging ) {
366
				$paging_output = $this->get_foogallery_argument( $gallery, 'paging_output', 'paging_output', '' );
367
				return '' === $paging_output;
368
			}
369
			return false;
370
		}
371
372
        /**
373
         * Override the attachments returned for rendering a paginated gallery
374
         *
375
         * @param bool $override
376
         * @param FooGallery $gallery
377
         * @return bool|array
378
         */
379
		function attachments_override( $override, $gallery ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
380
381
            if ( $this->is_paging_output_json( $gallery ) ) {
382
383
                $page_size = isset( $gallery->paging_options ) && array_key_exists( 'size', $gallery->paging_options ) ? $gallery->paging_options['size'] : 0;
384
385
                if ( $page_size > 0 ) {
386
387
                    $attachments = $gallery->attachments();
388
389
                    //return the first N attachments for the gallery
390
                    return array_splice( $attachments, 0, $page_size );
391
392
                }
393
            }
394
395
            return $override;
396
        }
397
398
        /**
399
         * Output a script block with all the gallery attachments as json
400
         *
401
         * @param FooGallery $gallery
402
         */
403
        function output_paging_script_block( $gallery ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
404
405
			if ( $this->is_paging_output_json( $gallery ) ) {
406
407
                $page_size = isset($gallery->paging_options) && array_key_exists('size', $gallery->paging_options) ? $gallery->paging_options['size'] : 0;
408
409
                if ($page_size > 0) {
410
                    //build up the arguments from the gallery template
411
412
                    $attachments = array_slice( $gallery->attachments(), $page_size );
413
                    $attachments_json = array_map( 'foogallery_build_json_from_attachment', $attachments );
414
415
                    echo '<script type="text/javascript">';
416
                    echo '  window["foogallery-gallery-' .$gallery->ID . '-items"] = [';
417
                    echo implode( ', ', $attachments_json );
418
                    echo '  ];';
419
                    echo '</script>';
420
                }
421
            }
422
        }
423
	}
424
}