Skip to content

Commit 183656c

Browse files
committed
The SEO Framework - Add image to Schema.org/WebPage
1 parent 1ecf3fc commit 183656c

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

schema/tsf-image-graph.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Plugin Name: The SEO Framework - Add image to Schema.org/WebPage
4+
* Plugin URI: https://theseoframework.com/
5+
* Description: Adds image data to The SEO Framework's Schema.org graph.
6+
* Version: 1.0.0
7+
* Author: Sybre Waaijer
8+
* Author URI: https://theseoframework.com/
9+
* License: GPLv3
10+
* Requires at least: 5.9
11+
* Requires PHP: 7.4.0
12+
*
13+
* @package My_The_SEO_Framework\SchemaImage
14+
*/
15+
16+
namespace My_The_SEO_Framework\SchemaImage;
17+
18+
\add_filter(
19+
'the_seo_framework_schema_graph_data', // should be 'the_seo_framework_schema_queued_graph_data' for TSF v5.0.7+ to make references work.
20+
function ( $graph, $args ) {
21+
22+
foreach ( $graph as &$data ) {
23+
if ( 'WebPage' === $data['@type'] ) {
24+
$data['primaryImageOfPage'] = My_Graph_Image::get_instant_ref( $args );
25+
$data['image'] = &My_Graph_Image::get_dynamic_ref( $args );
26+
}
27+
}
28+
29+
return $graph;
30+
},
31+
10,
32+
2,
33+
);
34+
35+
/**
36+
* Holds ImageObject generator for Schema.org structured data.
37+
*/
38+
class My_Graph_Image extends \The_SEO_Framework\Meta\Schema\Entities\Reference {
39+
40+
/**
41+
* @var string|string[] $type The Schema @type.
42+
*/
43+
public static $type = 'ImageObject';
44+
45+
/**
46+
* @var int $image_id The current image iterated. 0 is the PrimaryImage.
47+
*/
48+
public static $image_id = 0;
49+
50+
/**
51+
* @param array|null $args The query arguments. Accepts 'id', 'tax', 'pta', and 'uid'.
52+
* Leave null to autodetermine query.
53+
* @return string The entity ID for $args.
54+
*/
55+
public static function get_id( $args = null ) { // phpcs:ignore, VariableAnalysis.CodeAnalysis.VariableAnalysis -- abstract ref.
56+
return \tsf()->uri()->get_bare_front_page_url()
57+
. '#/schema/' . current( (array) static::$type ) . '/' . ( static::$image_id ?: 'PrimaryImage' );
58+
}
59+
60+
/**
61+
* @param array|null $args The query arguments. Accepts 'id', 'tax', 'pta', and 'uid'.
62+
* Leave null to autodetermine query.
63+
* @return ?array $entity The Schema.org graph entity. Null on failure.
64+
*/
65+
public static function build( $args = null ) {
66+
67+
$entity = [];
68+
69+
// TODO: We'll probably turn 'social' into 'search' when this gets implemented.
70+
foreach ( \tsf()->image()->get_image_details( $args, false, 'social' ) as $image ) {
71+
$details = [
72+
'@id' => static::get_id( $args ),
73+
'@type' => &static::$type,
74+
'url' => $image['url'],
75+
];
76+
77+
// Don't report dimensions if 0; 0 doesn't get scrubbed.
78+
if ( $image['width'] && $image['height'] ) {
79+
$details += [
80+
'width' => $image['width'],
81+
'height' => $image['height'],
82+
];
83+
}
84+
85+
if ( $image['caption'] ) {
86+
$details += [
87+
// We could store this call, but it's not needed once embedded in TSF.
88+
'inLanguage' => \tsf()->data()->blog()->get_language(),
89+
'caption' => $image['caption'],
90+
];
91+
}
92+
// Don't report filesize if 0; 0 doesn't get scrubbed.
93+
if ( $image['filesize'] )
94+
$details += [ 'contentSize' => (string) $image['filesize'] ];
95+
96+
$entity[] = $details;
97+
98+
static::$image_id++;
99+
}
100+
101+
// Reset counter.
102+
static::$image_id = 0;
103+
104+
return $entity ?: null;
105+
}
106+
}

0 commit comments

Comments
 (0)