Article.php
4.99 KB
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
namespace AIOSEO\Plugin\Common\Schema\Graphs\Article;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Schema\Graphs;
/**
* Article graph class.
*
* @since 4.0.0
*/
class Article extends Graphs\Graph {
/**
* Returns the graph data.
*
* @since 4.2.5
*
* @param Object $graphData The graph data.
* @return array The parsed graph data.
*/
public function get( $graphData = null ) {
$post = aioseo()->helpers->getPost();
if ( ! is_a( $post, 'WP_Post' ) ) {
return [];
}
$data = [
'@type' => 'Article',
'@id' => ! empty( $graphData->id ) ? aioseo()->schema->context['url'] . $graphData->id : aioseo()->schema->context['url'] . '#article',
'name' => ! empty( $graphData->properties->name ) ? $graphData->properties->name : aioseo()->schema->context['name'],
'headline' => ! empty( $graphData->properties->headline ) ? $graphData->properties->headline : get_the_title(),
'description' => ! empty( $graphData->properties->description ) ? $graphData->properties->description : '',
'author' => [
'@type' => 'Person',
'name' => ! empty( $graphData->properties->author->name ) ? $graphData->properties->author->name : get_the_author_meta( 'display_name' ),
'url' => ! empty( $graphData->properties->author->url ) ? $graphData->properties->author->url : '',
],
'publisher' => [ '@id' => trailingslashit( home_url() ) . '#' . aioseo()->options->searchAppearance->global->schema->siteRepresents ],
'image' => ! empty( $graphData->properties->image ) ? $this->image( $graphData->properties->image ) : $this->postImage( $post ),
'datePublished' => ! empty( $graphData->properties->dates->datePublished )
? mysql2date( DATE_W3C, $graphData->properties->dates->datePublished, false )
: mysql2date( DATE_W3C, $post->post_date_gmt, false ),
'dateModified' => ! empty( $graphData->properties->dates->dateModified )
? mysql2date( DATE_W3C, $graphData->properties->dates->dateModified, false )
: mysql2date( DATE_W3C, $post->post_modified_gmt, false ),
'inLanguage' => aioseo()->helpers->currentLanguageCodeBCP47(),
'commentCount' => get_comment_count( $post->ID )['approved'],
'mainEntityOfPage' => empty( $graphData ) ? [ '@id' => aioseo()->schema->context['url'] . '#webpage' ] : '',
'isPartOf' => empty( $graphData ) ? [ '@id' => aioseo()->schema->context['url'] . '#webpage' ] : ''
];
if ( empty( $graphData->properties->author->name ) ) {
if ( ! in_array( 'PersonAuthor', aioseo()->schema->graphs, true ) ) {
aioseo()->schema->graphs[] = 'PersonAuthor';
}
$data['author'] = [
'@id' => get_author_posts_url( $post->post_author ) . '#author'
];
}
if ( ! empty( $graphData->properties->keywords ) ) {
$keywords = json_decode( $graphData->properties->keywords, true );
$keywords = array_map( function ( $keywordObject ) {
return $keywordObject['value'];
}, $keywords );
$data['keywords'] = implode( ',', $keywords );
}
if ( isset( $graphData->properties->dates->include ) && ! $graphData->properties->dates->include ) {
unset( $data['datePublished'] );
unset( $data['dateModified'] );
}
$postTaxonomies = get_post_taxonomies( $post );
$postTerms = [];
foreach ( $postTaxonomies as $taxonomy ) {
$terms = get_the_terms( $post, $taxonomy );
if ( $terms ) {
$postTerms = array_merge( $postTerms, wp_list_pluck( $terms, 'name' ) );
}
}
if ( ! empty( $postTerms ) ) {
$data['articleSection'] = implode( ', ', $postTerms );
}
$pageNumber = aioseo()->helpers->getPageNumber();
if ( 1 < $pageNumber ) {
$data['pagination'] = $pageNumber;
}
return $data;
}
/**
* Returns the graph data for the post image.
*
* @since 4.0.0
*
* @param WP_Post $post The post object.
* @return array The image graph data.
*/
private function postImage( $post ) {
$featuredImage = $this->getFeaturedImage();
if ( $featuredImage ) {
return $featuredImage;
}
preg_match_all( '#<img[^>]+src="([^">]+)"#', $post->post_content, $matches );
if ( isset( $matches[1] ) && isset( $matches[1][0] ) ) {
$url = aioseo()->helpers->removeImageDimensions( $matches[1][0] );
$imageId = aioseo()->helpers->attachmentUrlToPostId( $url );
if ( $imageId ) {
return $this->image( $imageId, 'articleImage' );
} else {
return $this->image( $url, 'articleImage' );
}
}
if ( 'organization' === aioseo()->options->searchAppearance->global->schema->siteRepresents ) {
$logo = ( new Graphs\KnowledgeGraph\KgOrganization() )->logo();
if ( ! empty( $logo ) ) {
$logo['@id'] = trailingslashit( home_url() ) . '#articleImage';
return $logo;
}
} else {
$avatar = $this->avatar( $post->post_author, 'articleImage' );
if ( $avatar ) {
return $avatar;
}
}
$imageId = aioseo()->helpers->getSiteLogoId();
if ( $imageId ) {
return $this->image( $imageId, 'articleImage' );
}
return [];
}
}