|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Jsonld; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift_Post_To_Jsonld_Converter; |
|
6
|
|
|
|
|
7
|
|
|
class Jsonld_Article_Wrapper { |
|
8
|
|
|
|
|
9
|
|
|
private static $article_types = array( |
|
10
|
|
|
'Article', |
|
11
|
|
|
'AdvertiserContentArticle', |
|
12
|
|
|
'NewsArticle', |
|
13
|
|
|
'AnalysisNewsArticle', |
|
14
|
|
|
'AskPublicNewsArticle', |
|
15
|
|
|
'BackgroundNewsArticle', |
|
16
|
|
|
'OpinionNewsArticle', |
|
17
|
|
|
'ReportageNewsArticle', |
|
18
|
|
|
'ReviewNewsArticle', |
|
19
|
|
|
'Report', |
|
20
|
|
|
'SatiricalArticle', |
|
21
|
|
|
'ScholarlyArticle', |
|
22
|
|
|
'MedicalScholarlyArticle', |
|
23
|
|
|
'SocialMediaPosting', |
|
24
|
|
|
'BlogPosting', |
|
25
|
|
|
'LiveBlogPosting', |
|
26
|
|
|
'DiscussionForumPosting', |
|
27
|
|
|
'TechArticle', |
|
28
|
|
|
'APIReference' |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Wordlift_Post_To_Jsonld_Converter |
|
33
|
|
|
*/ |
|
34
|
|
|
private $post_to_jsonld_converter; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( $post_to_jsonld_converter ) { |
|
37
|
|
|
|
|
38
|
|
|
$this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
39
|
|
|
|
|
40
|
|
|
add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), 10, 3 ); |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
45
|
|
|
|
|
46
|
|
|
if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
47
|
|
|
|| ! is_array( $jsonld[0] ) ) { |
|
48
|
|
|
return $jsonld; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Copy the 1st array element |
|
52
|
|
|
$post_jsonld = $jsonld[0]; |
|
53
|
|
|
|
|
54
|
|
|
// Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
55
|
|
|
// in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
56
|
|
|
if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
57
|
|
|
return $jsonld; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Convert the post as Article. |
|
61
|
|
|
$article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
62
|
|
|
$article_jsonld['@id'] = $post_jsonld['@id'] . '/wrapper'; |
|
63
|
|
|
// Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
64
|
|
|
$article_jsonld['@type'] = 'Article'; |
|
65
|
|
|
$article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
66
|
|
|
|
|
67
|
|
|
// Copy over the URLs. |
|
68
|
|
|
if ( isset( $post_jsonld['url'] ) ) { |
|
69
|
|
|
$article_jsonld['url'] = $post_jsonld['url']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
array_unshift( $jsonld, $article_jsonld ); |
|
73
|
|
|
|
|
74
|
|
|
return $jsonld; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function is_article( $schema_types ) { |
|
78
|
|
|
|
|
79
|
|
|
$array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
80
|
|
|
|
|
81
|
|
|
return ! empty( $array_intersect ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |