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 ( 26e005...f3e37c )
by Brad
02:49
created

render-functions.php ➔ foogallery_build_json_from_attachment()   C

Complexity

Conditions 11
Paths 181

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 34
nc 181
nop 2
dl 0
loc 53
rs 5.8259
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=" . '"' . 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
    $html = foogallery_attachment_html_item_opening( $foogallery_attachment, $args );
358
    $html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args );
359
    $html .= foogallery_attachment_html_image( $foogallery_attachment, $args );
360
    $html .= '</a>';
361
    $html .= foogallery_attachment_html_caption( $foogallery_attachment, $args );
362
    $html .= '</figure></div>';
363
    return $html;
364
}
365
366
/**
367
 * Build up a JSON string for a FooGallery Attachment
368
 *
369
 * @param FooGalleryAttachment $foogallery_attachment
370
 * @param array $args
371
 *
372
 * @since 1.4.9
373
 *
374
 * @returns string
375
 */
376
function foogallery_build_json_from_attachment( $foogallery_attachment, $args = array() ) {
377
	if ( isset( $foogallery_attachment ) ) {
378
379
		$anchor_attributes = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args );
380
		$image_attributes = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args );
381
		$captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
382
383
		if ( array_key_exists( 'src', $image_attributes ) ) {
384
		    $src = $image_attributes['src'];
385
        } else if ( array_key_exists( 'data-src-fg', $image_attributes ) ) {
386
		    $src = $image_attributes['data-src-fg'];
387
        }
388
389
        if ( array_key_exists( 'srcset', $image_attributes ) ) {
390
            $srcset = $image_attributes['srcset'];
391
        } else if ( array_key_exists( 'data-srcset-fg', $image_attributes ) ) {
392
            $srcset = $image_attributes['data-srcset-fg'];
393
        }
394
395
        $json_object = new stdClass();
396
        $json_object->href      = $anchor_attributes['href'];
397
        $json_object->src       = $src;
0 ignored issues
show
Bug introduced by
The variable $src does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
398
        $json_object->srcset    = $srcset;
0 ignored issues
show
Bug introduced by
The variable $srcset does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
399
        if ( array_key_exists( 'width', $image_attributes ) ) {
400
            $json_object->width = $image_attributes['width'];
401
        }
402
        if ( array_key_exists( 'height', $image_attributes ) ) {
403
            $json_object->height = $image_attributes['height'];
404
        }
405
        $json_object->title     = $foogallery_attachment->title;
406
        $json_object->alt       = $foogallery_attachment->alt;
407
408
        $json_object_attr_anchor = new stdClass();
409
        $json_object_attr_anchor->{'data-attachment-id'} = $foogallery_attachment->ID;
410
411
        if ( $captions !== false ) {
412
            if ( array_key_exists( 'title', $captions ) ) {
413
                $json_object->caption = $json_object_attr_anchor->{'data-caption-title'} = $captions['title'];
414
415
            }
416
            if ( array_key_exists( 'desc', $captions ) ) {
417
                $json_object->description = $json_object_attr_anchor->{'data-caption-desc'} = $captions['desc'];
418
            }
419
        }
420
421
        $json_object->attr = new stdClass();
422
        $json_object->attr->anchor = $json_object_attr_anchor;
423
424
		return json_encode( $json_object );
425
	}
426
427
	return '';
428
}
429
430