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 ( 563189...ded6c2 )
by Brad
02:25
created

FooGallery_Paging::output_paging_script_block()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 1
dl 0
loc 17
rs 9.0777
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 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' ), 20, 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_theme_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' => 'html',
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
					if ( 'on' !== foogallery_get_setting( 'output_json_to_script_block', '') ) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
315
						$paging_output = $this->get_foogallery_argument( $gallery, 'paging_output', 'paging_output', '' );
316
						//add the items to the option if paging_output is set to JSON
317
						if ( '' === $paging_output && $paging_size > 0 ) {
318
							//build up the arguments from the gallery template
319
320
							$attachments      = array_slice( $gallery->attachments(), $paging_size );
321
							$json_objects     = array_map( 'foogallery_build_json_object_from_attachment', $attachments );
322
							$options['items'] = $json_objects;
323
						}
324
					}
325
				}
326
			}
327
			return $options;
328
		}
329
330
		private function get_foogallery_argument( $gallery, $setting_id, $argument_name, $default_value ) {
331
			global $current_foogallery_arguments;
332
333
			if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments[$argument_name] ) ) {
334
				return $current_foogallery_arguments[$argument_name];
335
			} else {
336
				return $gallery->get_setting( $setting_id, $default_value );
337
			}
338
		}
339
340
		/**
341
		 * Build up a arguments used in the preview of the gallery
342
		 *
343
		 * @param $args
344
		 * @param $post_data
345
		 * @param $template
346
		 *
347
		 * @return mixed
348
		 */
349
		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...
350
			$template_data = foogallery_get_gallery_template( $template );
351
352
			//check the template supports paging
353
			if ( $template_data && array_key_exists( 'paging_support', $template_data ) && true === $template_data['paging_support'] ) {
354
				$args['paging'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_type'];
355
				$args['paging_position'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_position'];
356
				$args['paging_theme'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_theme'];
357
				$args['paging_size'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_size'];
358
				$args['paging_scroll'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_scroll'];
359
				$args['paging_output'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_output'];
360
361
				$args['paging_limit'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_limit'];
362
				$args['paging_showFirstLast'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showFirstLast'];
363
				$args['paging_showPrevNext'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNext'];
364
				$args['paging_showPrevNextMore'] = $post_data[FOOGALLERY_META_SETTINGS][$template. '_paging_showPrevNextMore'];
365
			}
366
367
			return $args;
368
		}
369
370
		/**
371
		 * Checks if the gallery output is JSON
372
		 *
373
		 * @param FooGallery $gallery
374
		 * @return bool
375
		 */
376
		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...
377
			if ( isset( $gallery->paging ) && true === $gallery->paging ) {
378
				$paging_output = $this->get_foogallery_argument( $gallery, 'paging_output', 'paging_output', '' );
379
				return '' === $paging_output;
380
			}
381
			return false;
382
		}
383
384
        /**
385
         * Override the attachments returned for rendering a paginated gallery
386
         *
387
         * @param bool $override
388
         * @param FooGallery $gallery
389
         * @return bool|array
390
         */
391
		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...
392
393
            if ( $this->is_paging_output_json( $gallery ) ) {
394
395
                $page_size = isset( $gallery->paging_options ) && array_key_exists( 'size', $gallery->paging_options ) ? $gallery->paging_options['size'] : 0;
396
397
                if ( $page_size > 0 ) {
398
399
                    $attachments = $gallery->attachments();
400
401
                    //return the first N attachments for the gallery
402
                    return array_splice( $attachments, 0, $page_size );
403
404
                }
405
            }
406
407
            return $override;
408
        }
409
410
		/**
411
		 * Output a script block with all the gallery attachments as json
412
		 *
413
		 * @param FooGallery $gallery
414
		 */
415
		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...
416
			if ( 'on' === foogallery_get_setting( 'output_json_to_script_block', '') ) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
417
				if ( $this->is_paging_output_json( $gallery ) ) {
418
					$page_size = isset( $gallery->paging_options ) && array_key_exists( 'size', $gallery->paging_options ) ? $gallery->paging_options['size'] : 0;
419
					if ( $page_size > 0 ) {
420
						//build up the arguments from the gallery template
421
						$attachments      = array_slice( $gallery->attachments(), $page_size );
422
						$attachments_json = array_map( 'foogallery_build_json_from_attachment', $attachments );
423
						echo '<script type="text/javascript">';
424
						echo '  window["foogallery-gallery-' . $gallery->ID . '-items"] = [';
425
						echo implode( ', ', $attachments_json );
426
						echo '  ];';
427
						echo '</script>';
428
					}
429
				}
430
			}
431
		}
432
	}
433
}