author-bio.php
4 years ago
blog-display.php
4 years ago
customizer.js
6 years ago
customizer.php
3 years ago
featured-images-fallback.php
4 years ago
featured-images.php
4 years ago
post-details.php
4 years ago
featured-images-fallback.php
168 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Theme Tools: functions for Featured Images fallback. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Get one image from a specified post in the following order: |
| 10 | * Featured Image then first image from the_content HTML |
| 11 | * and filter the post_thumbnail_html |
| 12 | * |
| 13 | * @param string $html The HTML for the image markup. |
| 14 | * @param int $post_id The post ID to check. |
| 15 | * @param int $post_thumbnail_id The ID of the featured image. |
| 16 | * @param string $size The image size to return, defaults to 'post-thumbnail'. |
| 17 | * @param string|array $attr Optional. Query string or array of attributes. |
| 18 | * |
| 19 | * @return string $html Thumbnail image with markup. |
| 20 | */ |
| 21 | function jetpack_featured_images_fallback_get_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 22 | $opts = jetpack_featured_images_get_settings(); |
| 23 | |
| 24 | if ( ! empty( $html ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { |
| 25 | return trim( $html ); |
| 26 | } |
| 27 | |
| 28 | if ( jetpack_featured_images_should_load() ) { |
| 29 | if ( |
| 30 | ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) |
| 31 | || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) |
| 32 | || ! $opts['fallback-option'] |
| 33 | ) { |
| 34 | return trim( $html ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if ( class_exists( 'Jetpack_PostImages' ) ) { |
| 39 | global $_wp_additional_image_sizes; |
| 40 | |
| 41 | $args = array( |
| 42 | 'from_thumbnail' => false, |
| 43 | 'from_slideshow' => true, |
| 44 | 'from_gallery' => true, |
| 45 | 'from_attachment' => false, |
| 46 | ); |
| 47 | |
| 48 | $image = Jetpack_PostImages::get_image( $post_id, $args ); |
| 49 | |
| 50 | if ( ! empty( $image ) ) { |
| 51 | $image['width'] = ''; |
| 52 | $image['height'] = ''; |
| 53 | $image['crop'] = ''; |
| 54 | |
| 55 | if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { |
| 56 | $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; |
| 57 | $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; |
| 58 | $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; |
| 59 | } |
| 60 | |
| 61 | $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); |
| 62 | |
| 63 | // Use the theme's crop setting rather than forcing to true. |
| 64 | $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); |
| 65 | |
| 66 | $html = '<img src="' . esc_url( $image_src ) . '" title="' . esc_attr( wp_strip_all_tags( get_the_title() ) ) . '" class="attachment-' . esc_attr( $size ) . ' wp-post-image" />'; |
| 67 | |
| 68 | return trim( $html ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return trim( $html ); |
| 73 | } |
| 74 | add_filter( 'post_thumbnail_html', 'jetpack_featured_images_fallback_get_image', 10, 5 ); |
| 75 | |
| 76 | /** |
| 77 | * Get URL of one image from a specified post in the following order: |
| 78 | * Featured Image then first image from the_content HTML |
| 79 | * |
| 80 | * @param int $post_id The post ID to check. |
| 81 | * @param int $post_thumbnail_id The ID of the featured image. |
| 82 | * @param string $size The image size to return, defaults to 'post-thumbnail'. |
| 83 | * |
| 84 | * @return string|null $image_src The URL of the thumbnail image. |
| 85 | */ |
| 86 | function jetpack_featured_images_fallback_get_image_src( $post_id, $post_thumbnail_id, $size ) { |
| 87 | $image_src = wp_get_attachment_image_src( $post_thumbnail_id, $size ); |
| 88 | $image_src = ( ! empty( $image_src[0] ) ) ? $image_src[0] : null; |
| 89 | $opts = jetpack_featured_images_get_settings(); |
| 90 | |
| 91 | if ( ! empty( $image_src ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { |
| 92 | return esc_url( $image_src ); |
| 93 | } |
| 94 | |
| 95 | if ( jetpack_featured_images_should_load() ) { |
| 96 | if ( ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) |
| 97 | || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) ) { |
| 98 | return esc_url( $image_src ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if ( class_exists( 'Jetpack_PostImages' ) ) { |
| 103 | global $_wp_additional_image_sizes; |
| 104 | |
| 105 | $args = array( |
| 106 | 'from_thumbnail' => false, |
| 107 | 'from_slideshow' => true, |
| 108 | 'from_gallery' => true, |
| 109 | 'from_attachment' => false, |
| 110 | ); |
| 111 | |
| 112 | $image = Jetpack_PostImages::get_image( $post_id, $args ); |
| 113 | |
| 114 | if ( ! empty( $image ) ) { |
| 115 | $image['width'] = ''; |
| 116 | $image['height'] = ''; |
| 117 | $image['crop'] = ''; |
| 118 | |
| 119 | if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { |
| 120 | $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; |
| 121 | $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; |
| 122 | $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; |
| 123 | } |
| 124 | |
| 125 | $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); |
| 126 | |
| 127 | // Use the theme's crop setting rather than forcing to true. |
| 128 | $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); |
| 129 | |
| 130 | return esc_url( $image_src ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return esc_url( $image_src ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check if post has an image attached, including a fallback. |
| 139 | * |
| 140 | * @param int $post The post ID to check. |
| 141 | * |
| 142 | * @return bool |
| 143 | */ |
| 144 | function jetpack_has_featured_image( $post = null ) { |
| 145 | return (bool) get_the_post_thumbnail( $post ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Adds custom class to the array of post classes. |
| 150 | * |
| 151 | * @param array $classes Classes for the post element. |
| 152 | * @param array $class Optional. Comma separated list of additional classes. |
| 153 | * @param array $post_id Unique The post ID to check. |
| 154 | * |
| 155 | * @return array $classes |
| 156 | */ |
| 157 | function jetpack_featured_images_post_class( $classes, $class, $post_id ) { |
| 158 | $post_password_required = post_password_required( $post_id ); |
| 159 | $opts = jetpack_featured_images_get_settings(); |
| 160 | |
| 161 | if ( jetpack_has_featured_image( $post_id ) && (bool) 1 === (bool) $opts['fallback-option'] && ! is_attachment() && ! $post_password_required && 'post' === get_post_type() ) { |
| 162 | $classes[] = 'has-post-thumbnail'; |
| 163 | } |
| 164 | |
| 165 | return $classes; |
| 166 | } |
| 167 | add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 ); |
| 168 |