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" />'; |
|
|
|
|
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. |
|
|
|
|
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 ) { |
|
|
|
|
156
|
|
|
$classes[] = 'has-post-thumbnail'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $classes; |
160
|
|
|
} |
161
|
|
|
add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 ); |
162
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.