PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / generators / schema / main-image.php

main-image.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at src/generators/schema/main-image.php

55 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Generators\Schema;
4
5 use Yoast\WP\SEO\Config\Schema_IDs;
6
7 /**
8 * Returns ImageObject schema data.
9 */
10 class Main_Image extends Abstract_Schema_Piece {
11
12 /**
13 * Determines whether or not a piece should be added to the graph.
14 *
15 * @return bool
16 */
17 public function is_needed() {
18 return $this->context->indexable->object_type === 'post';
19 }
20
21 /**
22 * Adds a main image for the current URL to the schema if there is one.
23 *
24 * This can be either a social image (Open Graph or Twitter), the featured image,
25 * or fall back to the first image in the content of the page.
26 *
27 * @return false|array Image Schema.
28 */
29 public function generate() {
30 $image_id = $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH;
31
32 // The Open Graph image.
33 if ( isset( $this->context->indexable->open_graph_image_id ) && $this->context->indexable->open_graph_image_source === 'set-by-user' ) {
34 return $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->indexable->open_graph_image_id );
35 }
36
37 // The Twitter image.
38 if ( isset( $this->context->indexable->twitter_image_id ) && $this->context->indexable->twitter_image_source === 'set-by-user' ) {
39 return $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->indexable->twitter_image_id );
40 }
41
42 // The featured image.
43 if ( $this->context->main_image_id ) {
44 return $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->main_image_id );
45 }
46
47 // The first image in the content.
48 if ( $this->context->main_image_url ) {
49 return $this->helpers->schema->image->generate_from_url( $image_id, $this->context->main_image_url );
50 }
51
52 return false;
53 }
54 }
55