Social.php
4.95 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?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 {
/**
* The name of the action to bust the OG cache.
*
* @since 4.2.0
*
* @var string
*/
private $bustOgCacheActionName = 'aioseo_og_cache_bust_post';
/**
* Image class instance.
*
* @since 4.2.7
*
* @var Image
*/
public $image = null;
/**
* Facebook class instance.
*
* @since 4.2.7
*
* @var Facebook
*/
public $facebook = null;
/**
* Twitter class instance.
*
* @since 4.2.7
*
* @var Twitter
*/
public $twitter = null;
/**
* Output class instance.
*
* @since 4.2.7
*
* @var Output
*/
public $output = null;
/**
* 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() {
add_action( $this->bustOgCacheActionName, [ $this, 'bustOgCachePost' ] );
// To avoid duplicate sets of meta tags.
add_filter( 'jetpack_enable_open_graph', '__return_false' );
if ( ! is_admin() ) {
add_filter( 'language_attributes', [ $this, 'addAttributes' ] );
return;
}
// Forces a refresh of the Facebook cache.
add_action( 'post_updated', [ $this, 'scheduleBustOgCachePost' ], 10, 2 );
}
/**
* 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;
}
/**
* Schedule a ping to bust the OG cache.
*
* @since 4.2.0
*
* @param int $postId The post ID.
* @param WP_Post $post The post object.
* @return void
*/
public function scheduleBustOgCachePost( $postId, $post ) {
if ( ! aioseo()->helpers->isSbCustomFacebookFeedActive() || ! aioseo()->helpers->isValidPost( $post ) ) {
return;
}
if ( aioseo()->actionScheduler->isScheduled( $this->bustOgCacheActionName, [ 'postId' => $postId ] ) ) {
return;
}
// Schedule the new ping.
aioseo()->actionScheduler->scheduleAsync( $this->bustOgCacheActionName, [ 'postId' => $postId ] );
}
/**
* Pings Facebook and asks them to bust the OG cache for a particular post.
*
* @since 4.2.0
*
* @see https://developers.facebook.com/docs/sharing/opengraph/using-objects#update
*
* @param int $postId The post ID.
* @return void
*/
public function bustOgCachePost( $postId ) {
$post = get_post( $postId );
$customAccessToken = apply_filters( 'aioseo_facebook_access_token', '' );
if (
! aioseo()->helpers->isValidPost( $post ) ||
( ! aioseo()->helpers->isSbCustomFacebookFeedActive() && ! $customAccessToken )
) {
return;
}
$permalink = get_permalink( $postId );
$this->bustOgCacheHelper( $permalink );
}
/**
* Helper function for bustOgCache().
*
* @since 4.2.0
*
* @param string $permalink The permalink.
* @return void
*/
protected function bustOgCacheHelper( $permalink ) {
$accessToken = aioseo()->helpers->getSbAccessToken();
$accessToken = apply_filters( 'aioseo_facebook_access_token', $accessToken );
if ( ! $accessToken ) {
return;
}
$url = sprintf(
'https://graph.facebook.com/?%s',
http_build_query(
[
'id' => $permalink,
'scrape' => true,
'access_token' => $accessToken
]
)
);
wp_remote_post( $url, [ 'blocking' => false ] );
}
}