Completed
Push — add/jetpack-start-reconnect-us... ( 7f1fbb )
by
unknown
10:36
created

featured-images-fallback.php ➔ jetpack_featured_images_fallback_get_image_src()   C

Complexity

Conditions 16
Paths 20

Size

Total Lines 50
Code Lines 30

Duplication

Lines 11
Ratio 22 %

Importance

Changes 0
Metric Value
cc 16
eloc 30
nc 20
nop 3
dl 11
loc 50
rs 5.3533
c 0
b 0
f 0

How to fix   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
 * Get one image from a specified post in the following order:
4
 * Featured Image then first image from the_content HTML
5
 * and filter the post_thumbnail_html
6
 *
7
 * @param string       $html              The HTML for the image markup.
8
 * @param int          $post_id           The post ID to check.
9
 * @param int          $post_thumbnail_id The ID of the featured image.
10
 * @param string       $size              The image size to return, defaults to 'post-thumbnail'.
11
 * @param string|array $attr              Optional. Query string or array of attributes.
12
 *
13
 * @return string      $html              Thumbnail image with markup.
14
 */
15
function jetpack_featured_images_fallback_get_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
16
	$opts = jetpack_featured_images_get_settings();
17
18
	if ( ! empty( $html ) || (bool) 1 !== (bool) $opts['fallback-option'] ) {
19
		return trim( $html );
20
	}
21
22 View Code Duplication
	if ( jetpack_featured_images_should_load() ) {
23
		if ( 
24
			( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] )
25
			|| ( true === $opts['post'] && is_single() && ! $opts['post-option'] )
26
			|| ! $opts['fallback-option']
27
		) {
28
			return trim( $html );
29
		}
30
	}
31
32
	if ( class_exists( 'Jetpack_PostImages' ) ) {
33
		global $_wp_additional_image_sizes;
34
35
		$args = array(
36
			'from_thumbnail'  => false,
37
			'from_slideshow'  => true,
38
			'from_gallery'    => true,
39
			'from_attachment' => false,
40
		);
41
42
		$image = Jetpack_PostImages::get_image( $post_id, $args );
43
44
		if ( ! empty( $image ) ) {
45
			$image['width']  = '';
46
			$image['height'] = '';
47
			$image['crop']   = '';
48
49 View Code Duplication
			if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) {
50
				$image['width']  = $_wp_additional_image_sizes[ $size ]['width'];
51
				$image['height'] = $_wp_additional_image_sizes[ $size ]['height'];
52
				$image['crop']   = $_wp_additional_image_sizes[ $size ]['crop'];
53
			}
54
55
			$image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] );
56
57
			// Use the theme's crop setting rather than forcing to true
58
			$image_src = add_query_arg( 'crop', $image['crop'], $image_src );
59
60
			$html      = '<img src="' . esc_url( $image_src ) . '" title="' . esc_attr( strip_tags( get_the_title() ) ) . '" class="attachment-' . esc_attr( $size ) . ' wp-post-image" />';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 6 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
61
62
			return trim( $html );
63
		}
64
	}
65
66
	return trim( $html );
67
}
68
add_filter( 'post_thumbnail_html', 'jetpack_featured_images_fallback_get_image', 10, 5 );
69
70
/**
71
 * Get URL of one image from a specified post in the following order:
72
 * Featured Image then first image from the_content HTML
73
 *
74
 * @param int          $post_id           The post ID to check.
75
 * @param int          $post_thumbnail_id The ID of the featured image.
76
 * @param string       $size              The image size to return, defaults to 'post-thumbnail'.
77
 *
78
 * @return string|null $image_src         The URL of the thumbnail image.
79
 */
80
function jetpack_featured_images_fallback_get_image_src( $post_id, $post_thumbnail_id, $size ) {
81
	$image_src = wp_get_attachment_image_src( $post_thumbnail_id, $size );
82
	$image_src = ( ! empty( $image_src[0] ) ) ? $image_src[0] : null;
83
	$opts      = jetpack_featured_images_get_settings();
84
85
	if ( ! empty( $image_src ) || (bool) 1 !== (bool) $opts['fallback-option'] ) {
86
		return esc_url( $image_src );
87
	}
88
89 View Code Duplication
	if ( jetpack_featured_images_should_load() ) {
90
		if ( ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] )
91
			|| ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) ) {
92
				return esc_url( $image_src );
93
		}
94
	}
95
96
	if ( class_exists( 'Jetpack_PostImages' ) ) {
97
		global $_wp_additional_image_sizes;
98
99
		$args = array(
100
			'from_thumbnail'  => false,
101
			'from_slideshow'  => true,
102
			'from_gallery'    => true,
103
			'from_attachment' => false,
104
		);
105
106
		$image = Jetpack_PostImages::get_image( $post_id, $args );
107
108
		if ( ! empty( $image ) ) {
109
			$image['width']  = '';
110
			$image['height'] = '';
111
			$image['crop']   = '';
112
113 View Code Duplication
			if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) {
114
				$image['width']  = $_wp_additional_image_sizes[ $size ]['width'];
115
				$image['height'] = $_wp_additional_image_sizes[ $size ]['height'];
116
				$image['crop']   = $_wp_additional_image_sizes[ $size ]['crop'];
117
			}
118
119
			$image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] );
120
121
			// Use the theme's crop setting rather than forcing to true
122
			$image_src = add_query_arg( 'crop', $image['crop'], $image_src );
123
124
			return esc_url( $image_src );
125
		}
126
	}
127
128
	return esc_url( $image_src );
129
}
130
131
/**
132
 * Check if post has an image attached, including a fallback.
133
 *
134
 * @param  int $post The post ID to check.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $post not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
135
 *
136
 * @return bool
137
 */
138
function jetpack_has_featured_image( $post = null ) {
139
	return (bool) get_the_post_thumbnail( $post );
140
}
141
142
/**
143
 * Adds custom class to the array of post classes.
144
 *
145
 * @param array  $classes Classes for the post element.
146
 * @param array  $class   Optional. Comma separated list of additional classes.
147
 * @param array  $post_id Unique The post ID to check
148
 *
149
 * @return array $classes
150
 */
151
function jetpack_featured_images_post_class( $classes, $class, $post_id ) {
152
	$post_password_required = post_password_required( $post_id );
153
	$opts                   = jetpack_featured_images_get_settings();
154
155
	if ( jetpack_has_featured_image( $post_id ) && (bool) 1 === (bool) $opts['fallback-option'] && ! is_attachment() && ! $post_password_required ) {
0 ignored issues
show
Documentation introduced by
$post_id is of type array, but the function expects a integer|null.

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...
156
		$classes[] = 'has-post-thumbnail';
157
	}
158
159
	return $classes;
160
}
161
add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 );
162