Completed
Pull Request — develop (#1260)
by Naveen
02:51
created

Srcset_Util::get_image_width()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Widgets;
4
/**
5
 * @since 3.28.0
6
 * @author Naveen Muthusamy <[email protected]>
7
 */
8
class Srcset_Util {
9
10
	const NAVIGATOR_WIDGET = 'navigator';
11
12
	const FACETED_SEARCH_WIDGET = 'faceted_search';
13
14
	public static function get_srcset( $post_id, $widget_name ) {
15
16
		$srcset = array();
17
		$medium = get_the_post_thumbnail_url( $post_id, 'medium' );
18
		$large  = get_the_post_thumbnail_url( $post_id, 'large' );
19
20 View Code Duplication
		if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
			$srcset[] = $medium . ' ' . $width . 'w';
22
		}
23
24 View Code Duplication
		if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
			$srcset[] = $large . ' ' . $width . 'w';
26
		}
27
28
		$srcset_string = join( ",", $srcset );
29
30
		/**
31
		 * Filter name: wordlift_${widget_name}_thumbnail_srcset
32
		 * Filters the srcset string supplied to widgets for each post.
33
		 *
34
		 * @param $srcset_string string The srcset string
35
		 *
36
		 * @since 3.28.0
37
		 */
38
		$srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string );
39
40
		return $srcset_string;
41
42
	}
43
44
45
	private static function get_image_width( $post_id, $size ) {
46
		$thumbnail_id = get_post_thumbnail_id( $post_id );
47
		if ( ! $thumbnail_id ) {
48
			return false;
49
		}
50
		$data = wp_get_attachment_image_src( $thumbnail_id, $size );
51
		if ( ! $data ) {
52
			return false;
53
		}
54
55
		return array_key_exists( 2, $data ) ? $data[2] : false;
56
	}
57
58
59
}