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.

render-functions.php ➔ foogallery_build_json_object_from_attachment()   F
last analyzed

Complexity

Conditions 14
Paths 1441

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 1441
nop 2
dl 0
loc 63
rs 2.1
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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=" . '"' . foogallery_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=" . '"' . foogallery_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
	} else {
288
		$captions = false;
289
	}
290
291
	return apply_filters( 'foogallery_build_attachment_html_caption', $captions, $foogallery_attachment, $args );
292
}
293
294
/**
295
 * Returns generic html for captions
296
 *
297
 * @param FooGalleryAttachment $foogallery_attachment
298
 * @param array $args
299
 *
300
 * @since 1.4.0
301
 *
302
 * @return string
303
 */
304
function foogallery_attachment_html_caption( $foogallery_attachment, $args = array() ) {
305
	$captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
306
	$html = '';
307
308
	if ( $captions !== false ) {
309
		$html = '<figcaption class="fg-caption"><div class="fg-caption-inner">';
310
		if ( array_key_exists( 'title', $captions ) ) {
311
			$html .= '<div class="fg-caption-title">' . $captions['title'] . '</div>';
312
		}
313
		if ( array_key_exists( 'desc', $captions ) ) {
314
			$html .= '<div class="fg-caption-desc">' . $captions['desc'] . '</div>';
315
		}
316
		$html .= '</div></figcaption>';
317
	}
318
319
    return apply_filters( 'foogallery_attachment_html_caption', $html, $foogallery_attachment, $args );
320
}
321
322
/**
323
 * Returns the attachment item opening HTML
324
 *
325
 * @param FooGalleryAttachment $foogallery_attachment
326
 * @param array $args
327
 *
328
 * @since 1.4.0
329
 *
330
 * @return string
331
 */
332
function foogallery_attachment_html_item_opening($foogallery_attachment, $args = array() ) {
333
334
	$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...
335
336
	$classes = apply_filters( 'foogallery_attachment_html_item_classes', $classes, $foogallery_attachment, $args );
337
338
	$class_list = '';
339
	if ( is_array( $classes ) ) {
340
        $class_list = implode( ' ', $classes );
341
    }
342
343
    $attachment_item_figure_class = apply_filters( 'foogallery_attachment_html_item_figure_class', 'fg-item-inner', $foogallery_attachment, $args );
344
	$html = '<div class="' . $class_list . '"><figure class="'. esc_attr( $attachment_item_figure_class ) . '">';
345
	return apply_filters( 'foogallery_attachment_html_item_opening', $html, $foogallery_attachment, $args );
346
}
347
348
/**
349
 * Returns generic html for an attachment
350
 *
351
 * @param FooGalleryAttachment $foogallery_attachment
352
 * @param array $args
353
 *
354
 * @since 1.4.0
355
 *
356
 * @return string
357
 */
358
function foogallery_attachment_html( $foogallery_attachment, $args = array() ) {
359
360
    //check if no arguments were passed in, and build them up if so
361
    if ( empty( $args ) ) {
362
        $args = foogallery_gallery_template_arguments();
363
    }
364
365
    $html = foogallery_attachment_html_item_opening( $foogallery_attachment, $args );
366
    $html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args );
367
    $html .= foogallery_attachment_html_image( $foogallery_attachment, $args );
368
    $html .= '</a>';
369
    $html .= foogallery_attachment_html_caption( $foogallery_attachment, $args );
370
    $html .= '</figure></div>';
371
    return $html;
372
}
373
374
/**
375
 * Get the foogallery template arguments for the current foogallery that is being output to the frontend
376
 *
377
 * @return array
378
 */
379
function foogallery_gallery_template_arguments() {
380
    global $current_foogallery_template;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
381
382
    return apply_filters( 'foogallery_gallery_template_arguments-' . $current_foogallery_template, array() );
383
}
384
385
/**
386
 * Build up an object that will be encoded to JSON for a FooGallery Attachment
387
 *
388
 * @param FooGalleryAttachment $foogallery_attachment
389
 * @param array                $args
390
 *
391
 * @since 1.6.0
392
 *
393
 * @returns string
394
 * @return string
395
 */
396
function foogallery_build_json_object_from_attachment( $foogallery_attachment, $args = array() ) {
397
	if ( isset( $foogallery_attachment ) ) {
398
399
		//check if no arguments were passed in, and build them up if so
400
		if ( empty( $args ) ) {
401
			$args = foogallery_gallery_template_arguments();
402
		}
403
404
		$anchor_attributes = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args );
405
		$image_attributes  = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args );
406
		$captions          = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
407
408
		if ( array_key_exists( 'src', $image_attributes ) ) {
409
			$src = $image_attributes['src'];
410
		} else if ( array_key_exists( 'data-src-fg', $image_attributes ) ) {
411
			$src = $image_attributes['data-src-fg'];
412
		}
413
414
		if ( array_key_exists( 'srcset', $image_attributes ) ) {
415
			$srcset = $image_attributes['srcset'];
416
		} else if ( array_key_exists( 'data-srcset-fg', $image_attributes ) ) {
417
			$srcset = $image_attributes['data-srcset-fg'];
418
		}
419
420
		$json_object       = new stdClass();
421
		$json_object->href = $anchor_attributes['href'];
422
		if ( isset( $src ) ) {
423
			$json_object->src = $src;
424
		}
425
		if ( isset( $srcset ) ) {
426
			$json_object->srcset = $srcset;
427
		}
428
		if ( array_key_exists( 'width', $image_attributes ) ) {
429
			$json_object->width = $image_attributes['width'];
430
		}
431
		if ( array_key_exists( 'height', $image_attributes ) ) {
432
			$json_object->height = $image_attributes['height'];
433
		}
434
435
		$json_object->alt = $foogallery_attachment->alt;
436
437
		$json_object_attr_anchor                         = new stdClass();
438
		$json_object_attr_anchor->{'data-attachment-id'} = $foogallery_attachment->ID;
439
440
		if ( $captions !== false ) {
441
			if ( array_key_exists( 'title', $captions ) ) {
442
				$json_object->caption = $json_object->title = $json_object_attr_anchor->{'data-caption-title'} = $captions['title'];
443
			}
444
			if ( array_key_exists( 'desc', $captions ) ) {
445
				$json_object->description = $json_object_attr_anchor->{'data-caption-desc'} = $captions['desc'];
446
			}
447
		}
448
449
		$json_object->attr         = new stdClass();
450
		$json_object->attr->anchor = $json_object_attr_anchor;
451
452
		$json_object = apply_filters( 'foogallery_build_attachment_json', $json_object, $foogallery_attachment, $args, $anchor_attributes, $image_attributes, $captions );
453
454
		return $json_object;
455
	}
456
457
	return false;
458
}
459
460
/**
461
 * Build up a JSON string for a FooGallery Attachment
462
 *
463
 * @param FooGalleryAttachment $foogallery_attachment
464
 * @param array                $args
465
 *
466
 * @since 1.4.9
467
 *
468
 * @returns string
469
 * @return string
470
 */
471
function foogallery_build_json_from_attachment( $foogallery_attachment, $args = array() ) {
472
	if ( isset( $foogallery_attachment ) ) {
473
474
		$json_object = foogallery_build_json_object_from_attachment( $foogallery_attachment, $args );
475
476
		if ( defined( 'JSON_UNESCAPED_UNICODE' ) ) {
477
			return json_encode( $json_object, JSON_UNESCAPED_UNICODE );
478
		} else {
479
			return json_encode( $json_object );
480
		}
481
	}
482
483
	return '';
484
}
485
486