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 — master ( a2a740...13efa0 )
by Brad
03:00
created

FooGallery_Paging::attachments_override()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 6
nop 2
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
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
232
			return $fields;
233
		}
234
235
		/**
236
		 * Determine if the gallery has paging enabled
237
		 *
238
		 * @param $foogallery FooGallery
239
		 */
240
		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...
241
			$template_data = foogallery_get_gallery_template( $foogallery->gallery_template );
242
243
			//check the template supports paging
244
			$paging = $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support'];
245
246
			$foogallery->paging = apply_filters( 'foogallery_paging', $paging, $foogallery );
247
		}
248
249
		/**
250
		 * Add the required paging options if needed
251
		 *
252
		 * @param $attributes array
253
		 * @param $gallery FooGallery
254
		 *
255
		 * @return array
256
		 */
257
		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...
258
			if ( isset( $gallery->paging ) && true === $gallery->paging) {
259
260
				//check if we have arguments from the shortcode and override the saved settings
261
				$paging = $this->get_foogallery_argument( $gallery, 'paging_type', 'paging', '' );
262
263
				if ( '' !== $paging ) {
264
					$paging_position = $this->get_foogallery_argument( $gallery, 'paging_position', 'paging_position', 'both' );
265
					$paging_theme    = $this->get_foogallery_argument( $gallery, 'paging_theme', 'paging_theme', 'fg-light' );
266
					$paging_size     = intval( $this->get_foogallery_argument( $gallery, 'paging_size', 'paging_size', '30' ) );
267
					$paging_scroll   = $this->get_foogallery_argument( $gallery, 'paging_scroll', 'paging_scroll', 'true' ) === 'true';
268
269
					//force bottom position for infinite and loadMore paging
270
					if ( 'infinite' === $paging || 'loadMore' === $paging ) {
271
						$paging_position = 'bottom';
272
					}
273
274
                    $paging_options = array(
275
						'type'        => $paging,
276
						'theme'       => $paging_theme,
277
						'size'        => $paging_size,
278
						'position'    => $paging_position,
279
						'scrollToTop' => $paging_scroll
280
					);
281
282
					if ( 'pagination' === $paging ) {
283
                        $paging_options['limit'] = intval( $this->get_foogallery_argument( $gallery, 'paging_limit', 'paging_limit', '5' ) );;
284
						$paging_options['showFirstLast'] = $this->get_foogallery_argument( $gallery, 'paging_showFirstLast', 'paging_showFirstLast', 'true' ) === 'true';;
285
						$paging_options['showPrevNext'] = $this->get_foogallery_argument( $gallery, 'paging_showPrevNext', 'paging_showPrevNext', 'true' ) === 'true';;
286
						$paging_options['showPrevNextMore'] = $this->get_foogallery_argument( $gallery, 'paging_showPrevNextMore', 'paging_showPrevNextMore', 'true' ) === 'true';;
287
					}
288
289
                    $options['paging'] = $gallery->paging_options = $paging_options;
290
				}
291
			}
292
			return $options;
293
		}
294
295
		private function get_foogallery_argument( $gallery, $setting_id, $argument_name, $default_value ) {
296
			global $current_foogallery_arguments;
297
298
			if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments[$argument_name] ) ) {
299
				return $current_foogallery_arguments[$argument_name];
300
			} else {
301
				return $gallery->get_setting( $setting_id, $default_value );
302
			}
303
		}
304
305
		/**
306
		 * Build up a arguments used in the preview of the gallery
307
		 *
308
		 * @param $args
309
		 * @param $post_data
310
		 * @param $template
311
		 *
312
		 * @return mixed
313
		 */
314
		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...
315
			$template_data = foogallery_get_gallery_template( $template );
316
317
			//check the template supports paging
318
			if ( $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support'] ) {
319
				$args['paging'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_type'];
320
				$args['paging_position'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_position'];
321
				$args['paging_theme'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_theme'];
322
				$args['paging_size'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_size'];
323
				$args['paging_scroll'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_scroll'];
324
325
				$args['paging_limit'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_limit'];
326
				$args['paging_showFirstLast'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showFirstLast'];
327
				$args['paging_showPrevNext'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNext'];
328
				$args['paging_showPrevNextMore'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNextMore'];
329
			}
330
331
			return $args;
332
		}
333
334
        /**
335
         * Override the attachments returned for rendering a paginated gallery
336
         *
337
         * @param bool $override
338
         * @param FooGallery $gallery
339
         * @return bool|array
340
         */
341
		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...
342
343
            if ( isset( $gallery->paging ) && true === $gallery->paging ) {
344
                $page_size = isset( $gallery->paging_options ) && array_key_exists( 'size', $gallery->paging_options ) ? $gallery->paging_options['size'] : 0;
345
346
                if ( $page_size > 0 ) {
347
348
                    $attachments = $gallery->attachments();
349
350
                    //return the first N attachments for the gallery
351
                    return array_splice( $attachments, 0, $page_size );
352
353
                }
354
            }
355
356
            return $override;
357
        }
358
359
        /**
360
         * Output a script block with all the gallery attachments as json
361
         *
362
         * @param FooGallery $gallery
363
         */
364
        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...
365
            if ( isset( $gallery->paging ) && true === $gallery->paging ) {
366
                $page_size = isset($gallery->paging_options) && array_key_exists('size', $gallery->paging_options) ? $gallery->paging_options['size'] : 0;
367
368
                if ($page_size > 0) {
369
                    //build up the arguments from the gallery template
370
371
                    $attachments = array_slice( $gallery->attachments(), $page_size );
372
                    $attachments_json = array_map( 'foogallery_build_json_from_attachment', $attachments );
373
374
                    echo '<script type="text/javascript">';
375
                    echo '  window["foogallery-gallery-' .$gallery->ID . '-items"] = [';
376
                    echo implode( ', ', $attachments_json );
377
                    echo '  ];';
378
                    echo '</script>';
379
                }
380
            }
381
        }
382
	}
383
}