Output.php
4.41 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
<?php
namespace AIOSEO\Plugin\Common\Social;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Outputs our social meta.
*
* @since 4.0.0
*/
class Output {
/**
* Checks if the current page should have social meta.
*
* @since 4.0.0
*
* @return bool Whether or not the page should have social meta.
*/
public function isAllowed() {
if (
! is_front_page() &&
! is_home() &&
! is_singular() &&
! is_post_type_archive() &&
! aioseo()->helpers->isWooCommerceShopPage()
) {
return false;
}
return true;
}
/**
* Returns the Open Graph meta.
*
* @since 4.0.0
*
* @return array The Open Graph meta.
*/
public function getFacebookMeta() {
if ( ! $this->isAllowed() || ! aioseo()->options->social->facebook->general->enable ) {
return [];
}
$meta = [
'og:locale' => aioseo()->social->facebook->getLocale(),
'og:site_name' => aioseo()->helpers->encodeOutputHtml( aioseo()->social->facebook->getSiteName() ),
'og:type' => aioseo()->social->facebook->getObjectType(),
'og:title' => aioseo()->helpers->encodeOutputHtml( aioseo()->social->facebook->getTitle() ),
'og:description' => aioseo()->helpers->encodeOutputHtml( aioseo()->social->facebook->getDescription() ),
'og:url' => esc_url( aioseo()->helpers->canonicalUrl() ),
'fb:app_id' => aioseo()->options->social->facebook->advanced->appId,
'fb:admins' => implode( ',', array_map( 'trim', explode( ',', aioseo()->options->social->facebook->advanced->adminId ) ) ),
];
$image = aioseo()->social->facebook->getImage();
if ( $image ) {
$image = is_array( $image ) ? $image[0] : $image;
$image = aioseo()->helpers->makeUrlAbsolute( $image );
$image = set_url_scheme( esc_url( $image ) );
$meta += [
'og:image' => $image,
'og:image:secure_url' => is_ssl() ? $image : '',
'og:image:width' => aioseo()->social->facebook->getImageWidth(),
'og:image:height' => aioseo()->social->facebook->getImageHeight(),
];
}
$video = aioseo()->social->facebook->getVideo();
if ( $video ) {
$video = set_url_scheme( esc_url( $video ) );
$meta += [
'og:video' => $video,
'og:video:secure_url' => is_ssl() ? $video : '',
'og:video:width' => aioseo()->social->facebook->getVideoWidth(),
'og:video:height' => aioseo()->social->facebook->getVideoHeight(),
];
}
if ( ! empty( $meta['og:type'] ) && 'article' === $meta['og:type'] ) {
$meta += [
'article:section' => aioseo()->social->facebook->getSection(),
'article:tag' => aioseo()->social->facebook->getArticleTags(),
'article:published_time' => aioseo()->social->facebook->getPublishedTime(),
'article:modified_time' => aioseo()->social->facebook->getModifiedTime(),
'article:publisher' => aioseo()->social->facebook->getPublisher(),
'article:author' => aioseo()->social->facebook->getAuthor()
];
}
return array_filter( apply_filters( 'aioseo_facebook_tags', $meta ) );
}
/**
* Returns the Twitter meta.
*
* @since 4.0.0
*
* @return array The Twitter meta.
*/
public function getTwitterMeta() {
if ( ! $this->isAllowed() || ! aioseo()->options->social->twitter->general->enable ) {
return [];
}
$meta = [
'twitter:card' => aioseo()->social->twitter->getCardType(),
'twitter:site' => aioseo()->social->twitter->prepareUsername( aioseo()->social->twitter->getTwitterUrl() ),
'twitter:title' => aioseo()->helpers->encodeOutputHtml( aioseo()->social->twitter->getTitle() ),
'twitter:description' => aioseo()->helpers->encodeOutputHtml( aioseo()->social->twitter->getDescription() ),
'twitter:creator' => aioseo()->social->twitter->getCreator()
];
$image = aioseo()->social->twitter->getImage();
if ( $image ) {
$image = is_array( $image ) ? $image[0] : $image;
$image = aioseo()->helpers->makeUrlAbsolute( $image );
// Set the twitter image meta.
$meta['twitter:image'] = $image;
}
if ( is_singular() ) {
$additionalData = apply_filters( 'aioseo_social_twitter_additional_data', aioseo()->social->twitter->getAdditionalData() );
if ( $additionalData ) {
$i = 1;
foreach ( $additionalData as $data ) {
$meta[ "twitter:label$i" ] = $data['label'];
$meta[ "twitter:data$i" ] = $data['value'];
$i++;
}
}
}
return array_filter( apply_filters( 'aioseo_twitter_tags', $meta ) );
}
}