Social.php
4.34 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace AIOSEO\Plugin\Common\Social;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the Social Meta.
*
* @package AIOSEO\Plugin\Common\Social
*
* @since 4.0.0
*/
class Social {
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
$this->image = new Image();
if ( wp_doing_ajax() || wp_doing_cron() ) {
return;
}
$this->facebook = new Facebook();
$this->twitter = new Twitter();
$this->output = new Output();
$this->hooks();
}
/**
* Registers our hooks.
*
* @since 4.0.0
*/
protected function hooks() {
if ( ! is_admin() || wp_doing_ajax() ) {
add_filter( 'language_attributes', [ $this, 'addAttributes' ] );
}
// To avoid duplicate sets of meta tags.
add_filter( 'jetpack_enable_open_graph', '__return_false' );
// Adds special filters.
add_filter( 'user_contactmethods', [ $this, 'addContactMethods' ] );
// @TODO: [V4+] Needs access token for authentication.
// Forces a refresh of the Facebook cache.
//add_action( 'post_updated', [ $this, 'forceFbRefreshCache' ], 10, 2 );
}
/**
* Returns the social media contact methods.
*
* @since 4.0.0
*
* @param array $contactMethods The contact methods.
* @return array $contactMethods The filtered contact methods.
*/
public function addContactMethods( $contactMethods ) {
if ( aioseo()->options->social->twitter->general->enable && aioseo()->options->social->twitter->general->showAuthor ) {
$contactMethods['aioseo_contact_methods_header'] = AIOSEO_PLUGIN_NAME;
$contactMethods['aioseo_twitter'] = 'Twitter'; // @TODO: Will need to migrate these from old installs. `twitter` becomes `aioseo_twitter`
}
if ( aioseo()->options->social->facebook->general->enable && aioseo()->options->social->facebook->general->showAuthor ) {
if ( ! isset( $contactMethods['aioseo_contact_methods_header'] ) ) {
$contactMethods['aioseo_contact_methods_header'] = AIOSEO_PLUGIN_NAME;
}
$contactMethods['aioseo_facebook'] = 'Facebook'; // @TODO: Will need to migrate these from old installs. `facebook` becomes `aioseo_facebook`
}
return $contactMethods;
}
/**
* Adds our attributes to the registered language attributes.
*
* @since 4.0.0
*
* @param string $htmlTag The 'html' tag as a string.
* @return string The filtered 'html' tag as a string.
*/
public function addAttributes( $htmlTag ) {
if ( ! aioseo()->options->social->facebook->general->enable ) {
return $htmlTag;
}
// Avoid having duplicate meta tags.
$type = aioseo()->social->facebook->getObjectType();
if ( empty( $type ) ) {
$type = 'website';
}
$schemaTypes = [
'album' => 'MusicAlbum',
'article' => 'Article',
'bar' => 'BarOrPub',
'blog' => 'Blog',
'book' => 'Book',
'cafe' => 'CafeOrCoffeeShop',
'city' => 'City',
'country' => 'Country',
'episode' => 'Episode',
'food' => 'FoodEvent',
'game' => 'Game',
'hotel' => 'Hotel',
'landmark' => 'LandmarksOrHistoricalBuildings',
'movie' => 'Movie',
'product' => 'Product',
'profile' => 'ProfilePage',
'restaurant' => 'Restaurant',
'school' => 'School',
'sport' => 'SportsEvent',
'website' => 'WebSite',
];
if ( ! empty( $schemaTypes[ $type ] ) ) {
$type = $schemaTypes[ $type ];
} else {
$type = 'WebSite';
}
$attributes = apply_filters( 'aioseo_opengraph_attributes', [ 'prefix="og: https://ogp.me/ns#"' ] );
foreach ( $attributes as $attr ) {
if ( strpos( $htmlTag, $attr ) === false ) {
$htmlTag .= "\n\t$attr ";
}
}
return $htmlTag;
}
/**
* Forces FaceBook Open Graph to refresh on update.
*
* @since 4.0.0
*
* @see https://developers.facebook.com/docs/sharing/opengraph/using-objects#update
*
* @param int $postId The post ID.
* @param WP_Post $post The post object.
* @return void
*/
public function forceFbRefreshCache( $postId, $post ) {
if ( 'publish' !== $post->post_status ) {
return;
}
$postUrl = urldecode( get_permalink( $postId ) );
// @TODO: [V4+] Needs access token for authentication.
$endpoint = sprintf(
'https://graph.facebook.com/?%s',
http_build_query(
[
'id' => $postUrl,
'scrape' => true,
]
)
);
wp_remote_post( $endpoint, [ 'blocking' => false ] );
}
}