PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.9
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.9, at src/generators/schema/main-image.php

66 lines 2.2 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 $generated_schema = $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->indexable->open_graph_image_id );
35 $this->context->main_image_url = $generated_schema['url'];
36 $this->context->main_image_id = $this->context->indexable->open_graph_image_id;
37
38 return $generated_schema;
39 }
40
41 // The Twitter image.
42 if ( isset( $this->context->indexable->twitter_image_id ) && $this->context->indexable->twitter_image_source === 'set-by-user' ) {
43 $generated_schema = $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->indexable->twitter_image_id );
44 $this->context->main_image_url = $generated_schema['url'];
45 $this->context->main_image_id = $this->context->indexable->twitter_image_id;
46
47 return $generated_schema;
48 }
49
50 // The featured image.
51 if ( $this->context->main_image_id ) {
52 $generated_schema = $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->main_image_id );
53 $this->context->main_image_url = $generated_schema['url'];
54
55 return $generated_schema;
56 }
57
58 // The first image in the content.
59 if ( $this->context->main_image_url ) {
60 return $this->helpers->schema->image->generate_from_url( $image_id, $this->context->main_image_url );
61 }
62
63 return false;
64 }
65 }
66