| @@ -19,14 +19,14 @@ | ||
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Image tags that we output for each image. |
| 22 | 22 | * |
| 23 | - * @var array | |
| 23 | + * @var array<string> | |
| 24 | 24 | */ |
| 25 | 25 | protected static $image_tags = [ |
| 26 | - 'width' => 'width', | |
| 27 | - 'height' => 'height', | |
| 28 | - 'type' => 'type', | |
| 26 | + 'width' => 'width', | |
| 27 | + 'height' => 'height', | |
| 28 | + 'type' => 'type', | |
| 29 | 29 | ]; |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Returns the image for a post. |
| @@ -40,19 +40,26 @@ | ||
| 40 | 40 | return ''; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | $return = ''; |
| 44 | - foreach ( $images as $image_index => $image_meta ) { | |
| 44 | + foreach ( $images as $image_meta ) { | |
| 45 | 45 | $image_url = $image_meta['url']; |
| 46 | 46 | |
| 47 | - $return .= '<meta property="og:image" content="' . \esc_url( $image_url ) . '" />'; | |
| 47 | + if ( \is_attachment() ) { | |
| 48 | + global $wp; | |
| 49 | + $image_url = \home_url( $wp->request ); | |
| 50 | + } | |
| 48 | 51 | |
| 52 | + $class = \is_admin_bar_showing() ? ' class="yoast-seo-meta-tag"' : ''; | |
| 53 | + | |
| 54 | + $return .= '<meta property="og:image" content="' . \esc_url( $image_url, null, 'attribute' ) . '"' . $class . ' />'; | |
| 55 | + | |
| 49 | 56 | foreach ( static::$image_tags as $key => $value ) { |
| 50 | 57 | if ( empty( $image_meta[ $key ] ) ) { |
| 51 | 58 | continue; |
| 52 | 59 | } |
| 53 | 60 | |
| 54 | - $return .= \PHP_EOL . "\t" . '<meta property="og:image:' . \esc_attr( $key ) . '" content="' . $image_meta[ $key ] . '" />'; | |
| 61 | + $return .= \PHP_EOL . "\t" . '<meta property="og:image:' . \esc_attr( $key ) . '" content="' . \esc_attr( $image_meta[ $key ] ) . '"' . $class . ' />'; | |
| 55 | 62 | } |
| 56 | 63 | } |
| 57 | 64 | |
| 58 | 65 | return $return; |
| @@ -60,9 +67,9 @@ | ||
| 60 | 67 | |
| 61 | 68 | /** |
| 62 | 69 | * Gets the raw value of a presentation. |
| 63 | 70 | * |
| 64 | - * @return array The raw value. | |
| 71 | + * @return array<string, int> The raw value. | |
| 65 | 72 | */ |
| 66 | 73 | public function get() { |
| 67 | 74 | $images = []; |
| 68 | 75 | |
| @@ -70,9 +77,9 @@ | ||
| 70 | 77 | $images[] = \array_intersect_key( |
| 71 | 78 | // First filter the object. |
| 72 | 79 | $this->filter( $open_graph_image ), |
| 73 | 80 | // Then strip all keys that aren't in the image tags or the url. |
| 74 | - \array_flip( \array_merge( static::$image_tags, [ 'url' ] ) ) | |
| 81 | + \array_flip( \array_merge( static::$image_tags, [ 'url' ] ) ), | |
| 75 | 82 | ); |
| 76 | 83 | } |
| 77 | 84 | |
| 78 | 85 | return \array_filter( $images ); |
| @@ -80,23 +87,67 @@ | ||
| 80 | 87 | |
| 81 | 88 | /** |
| 82 | 89 | * Run the image content through the `wpseo_opengraph_image` filter. |
| 83 | 90 | * |
| 84 | - * @param array $image The image. | |
| 91 | + * @param array<string, string|int> $image The image. | |
| 85 | 92 | * |
| 86 | - * @return array The filtered image. | |
| 93 | + * @return array<string, string|int> The filtered image. | |
| 87 | 94 | */ |
| 88 | 95 | protected function filter( $image ) { |
| 89 | 96 | /** |
| 90 | - * Filter: 'wpseo_opengraph_image' - Allow changing the Open Graph image. | |
| 97 | + * Filter: 'wpseo_opengraph_image' - Allow changing the Open Graph image url. | |
| 91 | 98 | * |
| 92 | - * @api string - The URL of the Open Graph image. | |
| 99 | + * @param string $image_url The URL of the Open Graph image. | |
| 100 | + * @param Indexable_Presentation $presentation The presentation of an indexable. | |
| 101 | + */ | |
| 102 | + $image_url = \apply_filters( 'wpseo_opengraph_image', $image['url'], $this->presentation ); | |
| 103 | + if ( ! empty( $image_url ) && \is_string( $image_url ) ) { | |
| 104 | + $image['url'] = \trim( $image_url ); | |
| 105 | + } | |
| 106 | + | |
| 107 | + $image_type = ( $image['type'] ?? '' ); | |
| 108 | + /** | |
| 109 | + * Filter: 'wpseo_opengraph_image_type' - Allow changing the Open Graph image type. | |
| 93 | 110 | * |
| 111 | + * @param string $image_type The type of the Open Graph image. | |
| 94 | 112 | * @param Indexable_Presentation $presentation The presentation of an indexable. |
| 95 | 113 | */ |
| 96 | - $image_url = \trim( \apply_filters( 'wpseo_opengraph_image', $image['url'], $this->presentation ) ); | |
| 97 | - if ( ! empty( $image_url ) && \is_string( $image_url ) ) { | |
| 98 | - $image['url'] = $image_url; | |
| 114 | + $image_type = \apply_filters( 'wpseo_opengraph_image_type', $image_type, $this->presentation ); | |
| 115 | + if ( ! empty( $image_type ) && \is_string( $image_type ) ) { | |
| 116 | + $image['type'] = \trim( $image_type ); | |
| 117 | + } | |
| 118 | + else { | |
| 119 | + $image['type'] = ''; | |
| 120 | + } | |
| 121 | + | |
| 122 | + $image_width = ( $image['width'] ?? '' ); | |
| 123 | + /** | |
| 124 | + * Filter: 'wpseo_opengraph_image_width' - Allow changing the Open Graph image width. | |
| 125 | + * | |
| 126 | + * @param int $image_width The width of the Open Graph image. | |
| 127 | + * @param Indexable_Presentation $presentation The presentation of an indexable. | |
| 128 | + */ | |
| 129 | + $image_width = (int) \apply_filters( 'wpseo_opengraph_image_width', $image_width, $this->presentation ); | |
| 130 | + if ( ! empty( $image_width ) && $image_width > 0 ) { | |
| 131 | + $image['width'] = $image_width; | |
| 132 | + } | |
| 133 | + else { | |
| 134 | + $image['width'] = ''; | |
| 135 | + } | |
| 136 | + | |
| 137 | + $image_height = ( $image['height'] ?? '' ); | |
| 138 | + /** | |
| 139 | + * Filter: 'wpseo_opengraph_image_height' - Allow changing the Open Graph image height. | |
| 140 | + * | |
| 141 | + * @param int $image_height The height of the Open Graph image. | |
| 142 | + * @param Indexable_Presentation $presentation The presentation of an indexable. | |
| 143 | + */ | |
| 144 | + $image_height = (int) \apply_filters( 'wpseo_opengraph_image_height', $image_height, $this->presentation ); | |
| 145 | + if ( ! empty( $image_height ) && $image_height > 0 ) { | |
| 146 | + $image['height'] = $image_height; | |
| 147 | + } | |
| 148 | + else { | |
| 149 | + $image['height'] = ''; | |
| 99 | 150 | } |
| 100 | 151 | |
| 101 | 152 | return $image; |
| 102 | 153 | } |