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
|
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/** * Returns ImageObject schema data. */ class Main_Image extends Abstract_Schema_Piece {
/** * Determines whether or not a piece should be added to the graph. * * @return bool */ public function is_needed() { return $this->context->indexable->object_type === 'post'; }
/** * 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 . 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->helpers->schema->image->generate_from_attachment_id( $image_id, \get_post_thumbnail_id( $post_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->helpers->image->get_post_content_image( $post_id );
if ( $image_url === '' ) { return null; }
return $this->helpers->schema->image->generate_from_url( $image_id, $image_url ); } }
|