1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Frontend\Schema */
/** * Returns ImageObject schema data. * * @since 11.5 */ class WPSEO_Schema_MainImage implements WPSEO_Graph_Piece {
/** * A value object with context variables. * * @var WPSEO_Schema_Context */ private $context;
/** * WPSEO_Schema_WebPage constructor. * * @codeCoverageIgnore * * @param WPSEO_Schema_Context $context A value object with context variables. */ public function __construct( WPSEO_Schema_Context $context ) { $this->context = $context; }
/** * Determines whether or not a piece should be added to the graph. * * @return bool */ public function is_needed() { if ( is_singular() ) { return true; }
return false; }
/** * Adds a main image for the current URL to the schema if there is one. * * This can be either the featured image, or fall back to the first image in the content of the page. * * @return false|array $data Image Schema. */ public function generate() { $image_id = $this->context->canonical . WPSEO_Schema_IDs::PRIMARY_IMAGE_HASH;
$image_schema = $this->get_featured_image( $this->context->id, $image_id );
if ( $image_schema === null ) { $image_schema = $this->get_first_content_image( $this->context->id, $image_id ); }
if ( $image_schema === null ) { return false; }
$this->context->has_image = true;
return $image_schema; }
/** * Gets the image schema for the web page based on the featured image. * * @param int $post_id The post id. * @param string $image_id The image schema id. * * @return array|null The image schema object or null if there is no featured image. */ private function get_featured_image( $post_id, $image_id ) { if ( ! has_post_thumbnail( $post_id ) ) { return null; }
return $this->generate_image_schema_from_attachment_id( $image_id ); }
/** * Gets the image schema for the web page based on the first content image image. * * @param int $post_id The post id. * @param string $image_id The image schema id. * * @return array|null The image schema object or null if there is no image in the content. */ private function get_first_content_image( $post_id, $image_id ) { $image_url = $this->get_first_usable_content_image_for_post( $post_id );
if ( $image_url === null ) { return null; }
return $this->generate_image_schema_from_url( $image_id, $image_url ); }
/** * Gets the post's first usable content image. Null if none is available. * * @codeCoverageIgnore * * @param int $post_id The post id. * * @return string|null The image URL or null if there is no image. */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); }
/** * Generates image schema from the attachment id. * * @codeCoverageIgnore * * @param string $image_id The image schema id. * * @return array Schema ImageObject array. */ protected function generate_image_schema_from_attachment_id( $image_id ) { $schema_image = new WPSEO_Schema_Image( $image_id ); return $schema_image->generate_from_attachment_id( get_post_thumbnail_id() ); }
/** * Generates image schema from the url. * * @codeCoverageIgnore * * @param string $image_id The image schema id. * @param string $image_url The image URL. * * @return array Schema ImageObject array. */ protected function generate_image_schema_from_url( $image_id, $image_url ) { $schema_image = new WPSEO_Schema_Image( $image_id ); return $schema_image->generate_from_url( $image_url ); } }
|