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 — develop ( f3e37c...e64063 )
by Brad
07:56 queued 03:40
created

render-functions.php ➔ foogallery_gallery_template_arguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * FooGallery helper functions for rendering HTML
5
 * Created by Brad Vincent
6
 * Date: 11/07/2017
7
 *
8
 * @since 1.4.0
9
 */
10
11
/**
12
 * Returns the attachment image source only
13
 *
14
 * @param FooGalleryAttachment $foogallery_attachment
15
 * @param array $args
16
 *
17
 * @since 1.4.0
18
 *
19
 * @return string
20
 */
21
function foogallery_attachment_html_image_src( $foogallery_attachment, $args = array() ) {
22
	return apply_filters( 'foogallery_attachment_resize_thumbnail', $foogallery_attachment->url, $args, $foogallery_attachment );
23
}
24
25
/**
26
 * Returns the attachment img HTML
27
 *
28
 * @param FooGalleryAttachment $foogallery_attachment
29
 * @param array $args
30
 *
31
 * @since 1.4.0
32
 *
33
 * @return string
34
 */
35
function foogallery_attachment_html_image( $foogallery_attachment, $args = array() ) {
36
	$attr = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args );
37
38
	$html = '<img ';
39
	foreach ( $attr as $name => $value ) {
40
        $name = str_replace(' ', '', $name); //ensure we have no spaces!
41
		$html .= " $name=" . '"' . esc_attr($value) . '"';
42
	}
43
	$html .= ' />';
44
45
	return apply_filters( 'foogallery_attachment_html_image', $html, $args, $foogallery_attachment );
46
}
47
48
/**
49
 * Returns the attachment img HTML
50
 *
51
 * @param FooGalleryAttachment $foogallery_attachment
52
 * @param array $args
53
 *
54
 * @since 1.4.9
55
 *
56
 * @return array
57
 */
58
function foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args = array() ) {
59
	$attr['src'] = foogallery_attachment_html_image_src( $foogallery_attachment, $args );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
60
61
	if ( ! empty( $foogallery_attachment->alt ) ) {
62
		$attr['alt'] = $foogallery_attachment->alt;
63
	}
64
65
	if ( ! empty( $foogallery_attachment->caption ) ) {
66
		$attr['title'] = $foogallery_attachment->caption;
67
	}
68
69
	//pull any custom attributes out the args
70
	if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) {
71
		$attr = array_merge( $attr, $args['image_attributes'] );
72
	}
73
74
	//check for width and height args and add those to the image
75
	if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) {
76
		$attr['width'] = $args['width'];
77
	}
78
	if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) {
79
		$attr['height'] = $args['height'];
80
	}
81
82
	$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $foogallery_attachment );
83
84
	if ( array_key_exists( 'class', $attr ) ) {
85
		$attr['class'] .= ' fg-image';
86
	} else {
87
		$attr['class'] = 'fg-image';
88
	}
89
90
	return $attr;
91
}
92
93
/**
94
 * Returns the attachment anchor HTML opening tag
95
 *
96
 * @param FooGalleryAttachment $foogallery_attachment
97
 * @param array $args
98
 *
99
 * @since 1.4.0
100
 *
101
 * @return string
102
 */
103
function foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args = array() ) {
104
	$attr = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args );
105
106
    $html = '<a ';
107
    foreach ( $attr as $name => $value ) {
108
		$name = str_replace(' ', '', $name); //ensure we have no spaces!
109
        $html .= " $name=" . '"' . esc_attr($value) . '"';
110
    }
111
    $html .= '>';
112
113
    return apply_filters( 'foogallery_attachment_html_anchor_opening', $html, $args, $foogallery_attachment );
114
}
115
116
/**
117
 * Returns the array of attributes that will be used on the anchor for a FooGalleryAttachment
118
 *
119
 * @param FooGalleryAttachment $foogallery_attachment
120
 * @param array $args
121
 *
122
 * @since 1.4.9
123
 *
124
 * @return array
125
 */
126
function foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args = array() ) {
127
	$arg_defaults = array(
128
		'link' => 'image',
129
		'custom_link' => $foogallery_attachment->custom_url
130
	);
131
132
	$args = wp_parse_args( $args, $arg_defaults );
133
134
	$link = $args['link'];
135
136
	if ( 'page' === $link ) {
137
		//get the URL to the attachment page
138
		$url = get_attachment_link( $foogallery_attachment->ID );
139
	} else if ( 'custom' === $link ) {
140
		$url = $args['custom_link'];
141
	} else {
142
		$url = $foogallery_attachment->url;
143
	}
144
145
	//fallback for images that might not have a custom url
146
	if ( empty( $url ) ) {
147
		$url = $foogallery_attachment->url;
148
	}
149
150
	$attr = array();
151
152
	//only add href and target attributes to the anchor if the link is NOT set to 'none'
153
	if ( $link !== 'none' ){
154
		$attr['href'] = $url;
155
		if ( ! empty( $foogallery_attachment->custom_target ) && 'default' !== $foogallery_attachment->custom_target ) {
156
			$attr['target'] = $foogallery_attachment->custom_target;
157
		}
158
	}
159
160
	if ( ! empty( $foogallery_attachment->caption ) ) {
161
		$attr['data-caption-title'] = $foogallery_attachment->caption;
162
	}
163
164
	if ( !empty( $foogallery_attachment->description ) ) {
165
		$attr['data-caption-desc'] = $foogallery_attachment->description;
166
	}
167
168
	$attr['data-attachment-id'] = $foogallery_attachment->ID;
169
170
	//pull any custom attributes out the args
171
	if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) {
172
		$attr = array_merge( $attr, $args['link_attributes'] );
173
	}
174
175
	$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $foogallery_attachment );
176
177
	//always add the fg-thumb class
178
	if ( array_key_exists( 'class', $attr ) ) {
179
		$attr['class'] .= ' fg-thumb';
180
	} else {
181
		$attr['class'] = 'fg-thumb';
182
	}
183
184
	return $attr;
185
}
186
187
/**
188
 * Returns the attachment anchor HTML
189
 *
190
 * @param FooGalleryAttachment $foogallery_attachment
191
 * @param array $args
192
 * @param bool $output_image
193
 * @param bool $output_closing_tag
194
 *
195
 * @since 1.4.0
196
 *
197
 * @return string
198
 */
199
function foogallery_attachment_html_anchor( $foogallery_attachment, $args = array(), $output_image = true, $output_closing_tag = true ) {
200
	if ( empty ( $foogallery_attachment->url ) )  {
201
		return '';
202
	}
203
204
    $html = foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args );
205
206
	if ( $output_image ) {
207
		$html .= foogallery_attachment_html_image( $foogallery_attachment, $args );;
208
	}
209
210
	if ( $output_closing_tag ) {
211
		$html .= '</a>';
212
	}
213
214
	return apply_filters( 'foogallery_attachment_html_anchor', $html, $args, $foogallery_attachment );
215
}
216
217
/**
218
 * Builds up the captions for an attachment
219
 *
220
 * @param FooGalleryAttachment $foogallery_attachment
221
 * @param array $args
222
 *
223
 * @since 1.4.9
224
 *
225
 * @return array|bool
226
 */
227
function foogallery_build_attachment_html_caption( $foogallery_attachment, $args = array() ) {
228
	$captions = array();
229
230
	$preset = foogallery_gallery_template_setting( 'caption_preset', 'fg-custom' );
231
232
	if ( 'none' !== $preset ) {
233
		$caption_html = array();
0 ignored issues
show
Unused Code introduced by
$caption_html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
234
235
		$show_caption_title = false;
0 ignored issues
show
Unused Code introduced by
$show_caption_title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
236
		$show_caption_desc = false;
0 ignored issues
show
Unused Code introduced by
$show_caption_desc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
237
238
		//check if we have provided overrides for the caption title
239
		if ( isset( $args['override_caption_title'] ) ) {
240
			$caption_title = $args['override_caption_title'];
241
			$show_caption_title = true;
242
		} else {
243
			$caption_title_source = foogallery_gallery_template_setting( 'caption_title_source', '' );
244
245
			//if we need to use the settings, then make sure our source is false
246
			if ( empty( $caption_title_source ) ) { $caption_title_source = false; }
247
248
			if ( 'fg-custom' === $preset ) {
249
				$show_caption_title = $caption_title_source !== 'none';
250
			} else {
251
				//always show both title and desc for the presets
252
				$show_caption_title = true;
253
			}
254
255
			//get the correct captions
256
			$caption_title = foogallery_get_caption_title_for_attachment( $foogallery_attachment->_post, $caption_title_source );
257
		}
258
259
		//check if we have provided overrides for the caption description
260
		if ( isset( $args['override_caption_desc'] ) ) {
261
			$caption_desc = $args['override_caption_desc'];
262
			$show_caption_desc = true;
263
		} else {
264
265
			$caption_desc_source = foogallery_gallery_template_setting( 'caption_desc_source', '' );
266
267
			//if we need to use the settings, then make sure our source is false
268
			if ( empty( $caption_desc_source ) ) { $caption_desc_source = false; }
269
270
			if ( 'fg-custom' === $preset ) {
271
				$show_caption_desc = $caption_desc_source !== 'none';
272
			} else {
273
				//always show both title and desc for the presets
274
				$show_caption_desc = true;
275
			}
276
277
			$caption_desc = foogallery_get_caption_desc_for_attachment( $foogallery_attachment->_post, $caption_desc_source );
278
		}
279
280
		if ( $caption_title && $show_caption_title ) {
281
			$captions['title'] = $caption_title;
282
		}
283
		if ( $caption_desc && $show_caption_desc ) {
284
			$captions['desc'] = $caption_desc;
285
		}
286
287
		return $captions;
288
	}
289
290
	return false;
291
}
292
293
/**
294
 * Returns generic html for captions
295
 *
296
 * @param FooGalleryAttachment $foogallery_attachment
297
 * @param array $args
298
 *
299
 * @since 1.4.0
300
 *
301
 * @return string
302
 */
303
function foogallery_attachment_html_caption( $foogallery_attachment, $args = array() ) {
304
	$captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
305
	$html = '';
306
307
	if ( $captions !== false ) {
308
		$html = '<figcaption class="fg-caption"><div class="fg-caption-inner">';
309
		if ( array_key_exists( 'title', $captions ) ) {
310
			$html .= '<div class="fg-caption-title">' . $captions['title'] . '</div>';
311
		}
312
		if ( array_key_exists( 'desc', $captions ) ) {
313
			$html .= '<div class="fg-caption-desc">' . $captions['desc'] . '</div>';
314
		}
315
		$html .= '</div></figcaption>';
316
	}
317
318
    return apply_filters( 'foogallery_attachment_html_caption', $html, $foogallery_attachment, $args );
319
}
320
321
/**
322
 * Returns the attachment item opening HTML
323
 *
324
 * @param FooGalleryAttachment $foogallery_attachment
325
 * @param array $args
326
 *
327
 * @since 1.4.0
328
 *
329
 * @return string
330
 */
331
function foogallery_attachment_html_item_opening($foogallery_attachment, $args = array() ) {
332
333
	$classes[] = 'fg-item';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$classes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $classes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
334
335
	$classes = apply_filters( 'foogallery_attachment_html_item_classes', $classes, $foogallery_attachment, $args );
336
337
	$class_list = '';
338
	if ( is_array( $classes ) ) {
339
        $class_list = implode( ' ', $classes );
340
    }
341
342
	$html = '<div class="' . $class_list . '"><figure class="fg-item-inner">';
343
	return apply_filters( 'foogallery_attachment_html_item_opening', $html, $foogallery_attachment, $args );
344
}
345
346
/**
347
 * Returns generic html for an attachment
348
 *
349
 * @param FooGalleryAttachment $foogallery_attachment
350
 * @param array $args
351
 *
352
 * @since 1.4.0
353
 *
354
 * @return string
355
 */
356
function foogallery_attachment_html( $foogallery_attachment, $args = array() ) {
357
358
    //check if no arguments were passed in, and build them up if so
359
    if ( empty( $args ) ) {
360
        $args = foogallery_gallery_template_arguments();
361
    }
362
363
    $html = foogallery_attachment_html_item_opening( $foogallery_attachment, $args );
364
    $html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args );
365
    $html .= foogallery_attachment_html_image( $foogallery_attachment, $args );
366
    $html .= '</a>';
367
    $html .= foogallery_attachment_html_caption( $foogallery_attachment, $args );
368
    $html .= '</figure></div>';
369
    return $html;
370
}
371
372
/**
373
 * Get the foogallery template arguments for the current foogallery that is being output to the frontend
374
 *
375
 * @return array
376
 */
377
function foogallery_gallery_template_arguments() {
378
    global $current_foogallery_template;
379
380
    return apply_filters( 'foogallery_gallery_template_arguments-' . $current_foogallery_template, array() );
381
}
382
383
/**
384
 * Build up a JSON string for a FooGallery Attachment
385
 *
386
 * @param FooGalleryAttachment $foogallery_attachment
387
 * @param array $args
388
 *
389
 * @since 1.4.9
390
 *
391
 * @returns string
392
 */
393
function foogallery_build_json_from_attachment( $foogallery_attachment, $args = array() ) {
394
	if ( isset( $foogallery_attachment ) ) {
395
396
	    //check if no arguments were passed in, and build them up if so
397
	    if ( empty( $args ) ) {
398
            $args = foogallery_gallery_template_arguments();
399
        }
400
401
		$anchor_attributes = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args );
402
		$image_attributes = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args );
403
		$captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
404
405
		if ( array_key_exists( 'src', $image_attributes ) ) {
406
		    $src = $image_attributes['src'];
407
        } else if ( array_key_exists( 'data-src-fg', $image_attributes ) ) {
408
		    $src = $image_attributes['data-src-fg'];
409
        }
410
411
        if ( array_key_exists( 'srcset', $image_attributes ) ) {
412
            $srcset = $image_attributes['srcset'];
413
        } else if ( array_key_exists( 'data-srcset-fg', $image_attributes ) ) {
414
            $srcset = $image_attributes['data-srcset-fg'];
415
        }
416
417
        $json_object = new stdClass();
418
        $json_object->href      = $anchor_attributes['href'];
419
        if ( isset( $src ) ) {
420
            $json_object->src = $src;
421
        }
422
        if ( isset( $srcset ) ) {
423
            $json_object->srcset = $srcset;
424
        }
425
        if ( array_key_exists( 'width', $image_attributes ) ) {
426
            $json_object->width = $image_attributes['width'];
427
        }
428
        if ( array_key_exists( 'height', $image_attributes ) ) {
429
            $json_object->height = $image_attributes['height'];
430
        }
431
        $json_object->title     = $foogallery_attachment->title;
432
        $json_object->alt       = $foogallery_attachment->alt;
433
434
        $json_object_attr_anchor = new stdClass();
435
        $json_object_attr_anchor->{'data-attachment-id'} = $foogallery_attachment->ID;
436
437
        if ( $captions !== false ) {
438
            if ( array_key_exists( 'title', $captions ) ) {
439
                $json_object->caption = $json_object_attr_anchor->{'data-caption-title'} = $captions['title'];
440
441
            }
442
            if ( array_key_exists( 'desc', $captions ) ) {
443
                $json_object->description = $json_object_attr_anchor->{'data-caption-desc'} = $captions['desc'];
444
            }
445
        }
446
447
        $json_object->attr = new stdClass();
448
        $json_object->attr->anchor = $json_object_attr_anchor;
449
450
		return json_encode( $json_object );
451
	}
452
453
	return '';
454
}
455
456