-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributor-yoast-sync.php
350 lines (312 loc) · 11 KB
/
distributor-yoast-sync.php
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* Plugin Name: Distributor - Yoast Sync
* Plugin URI: https://github.com/timstl/distributor-yoast-sync
* Description: Sync social images, titles and descriptions and meta information from Yoast SEO when post is pushed or pulled by Distributor plugin.
* Version: 1.0.3
* Author: Tim Gieseking, [email protected]
* Author URI: http://timgweb.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: basetheme
*
* @package WordPress
* @subpackage Distributor / Yoast Sync
* @since 1.0
* @version 1.0
*/
/* Abort! */
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Utility function for logging.
*/
require_once plugin_dir_path( __FILE__ ) . 'lib/utils.php';
/**
* The Yoast meta keys to sync in key => type format.
* Image keys will have '-id' appended to the end of the key.
*
* Example: _yoast_wpseo_opengraph-image is the URL and _yoast_wpseo_opengraph-image-id is the media ID.
*
* @param string $prepend The string prepended to the key (should be `_` or sometimes blank).
*/
function dty_yoast_meta_keys( $prepend = '_' ) {
$meta_keys = apply_filters(
'dty_yoast_meta_keys',
array(
'yoast_wpseo_opengraph-image' => 'image',
'yoast_wpseo_twitter-image' => 'image',
'yoast_wpseo_title' => 'text',
'yoast_wpseo_opengraph-title' => 'text',
'yoast_wpseo_twitter-title' => 'text',
'yoast_wpseo_metadesc' => 'textarea',
'yoast_wpseo_opengraph-description' => 'textarea',
'yoast_wpseo_twitter-description' => 'textarea',
'yoast_wpseo_meta-robots-noindex' => 'int',
'yoast_wpseo_meta-robots-nofollow' => 'int',
)
);
if ( $prepend != '' ) {
$prepended = array();
foreach ( $meta_keys as $meta_key => $type ) {
$prepended[ $prepend . $meta_key ] = $type;
}
} else {
$prepended = $meta_keys;
}
return $prepended;
}
/**
* Return only image meta keys.
*
* @param string $prepend The string prepended to the key (should be `_` or sometimes blank).
*/
function dty_yoast_image_meta_keys( $prepend = '_' ) {
$meta_keys = dty_yoast_meta_keys( $prepend );
$image_keys = array();
foreach ( $meta_keys as $meta_key => $type ) {
if ( 'image' === $type ) {
$image_keys[] = $meta_key;
}
}
return $image_keys;
}
/**
* Update opengraph meta on sending site, prior to subscription pushing.
* When a notification is sent, Yoast has not yet saved the new Open Graph data. This filter fires prior to that.
* We'll take the $_POST data and update the $post_body using Yoast's sanitize functions.
* This feels like a big hack, but without it new opengraph meta is only sent on the next update.
*
* @param array $post_body The request body to send.
* @param object $post The WP_Post that is being pushed.
*/
function dty_fix_opengraph_meta_on_update( $post_body, $post ) {
/**
* We need Yoast.
*/
if ( ! class_exists( 'WPSEO_Utils' ) ) {
return false;
}
/**
* NOTE: The $_POST keys are not prepended with `_` but the $post_body keys are prepended.
*/
/**
* Fix yoast meta keys provided in dty_yoast_meta_keys().
*/
foreach ( dty_yoast_meta_keys( '' ) as $yoast_meta_key => $type ) {
/**
* Open graph images
*/
if ( 'image' === $type ) {
$image_id = 0;
$image_url = null;
if ( isset( $_POST[ $yoast_meta_key . '-id' ] ) ) {
/**
* If the new ID is set in $_POST, try to get the attachment using this URL.
* This seems more reliable and safe than using the URL from $_POST.
* If for some reason we can't get the attachment URL, use the one in $_POST.
*/
$image_id = intval( $_POST[ $yoast_meta_key . '-id' ] );
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
if ( ! $image_url ) {
$image_url = $_POST[ $yoast_meta_key ];
}
$image_url = WPSEO_Utils::sanitize_url( $image_url );
/**
* Update the distributor_meta in $post_body.
*/
if ( $image_id > 0 && $image_url ) {
$post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key . '-id' ][0] = $image_id;
$post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ][0] = $image_url;
}
}
if ( $image_id <= 0 || ! $image_url ) {
/**
* No ID in $post_body for this key. Unset from meta to delete from receiving site.
*/
if ( isset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ] ) ) {
unset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ] );
}
if ( isset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key . '-id' ] ) ) {
unset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key . '-id' ] );
}
}
} elseif ( 'text' === $type || 'textarea' === $type ) {
/**
* Sanitize or remove titles and descriptions.
*/
if ( isset( $_POST[ $yoast_meta_key ] ) ) {
$value = trim( $_POST[ $yoast_meta_key ] );
if ( 'textarea' === $type ) {
$value = str_replace( array( "\n", "\r", "\t", ' ' ), ' ', $value );
}
$post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ][0] = WPSEO_Utils::sanitize_text_field( $value );
} else {
/**
* Delete
*/
if ( isset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ] ) ) {
unset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ] );
}
}
} elseif ( 'int' === $type ) {
/**
* Sanitize integers (robot settings);
*/
if ( isset( $_POST[ $yoast_meta_key ] ) ) {
$post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ][0] = intval( $_POST[ $yoast_meta_key ] );
} else {
/**
* Delete
*/
if ( isset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ] ) ) {
unset( $post_body['post_data']['distributor_meta'][ '_' . $yoast_meta_key ] );
}
}
}
}
return $post_body;
}
add_filter( 'dt_subscription_post_args', 'dty_fix_opengraph_meta_on_update', 1, 2 );
/**
* Hook into Distributor's pull post action: dt_pull_post
*
* @param int $new_post The newly created post ID.
* @param ExternalConnection $connection The distributor connection pulling the post.
* @param array $post_array The original post data retrieved via the connection.
*/
function dty_sync_opengraph_image_pull( $new_post, $connection, $post_array ) {
foreach ( dty_yoast_image_meta_keys() as $yoast_meta_key ) {
if ( isset( $post_array['meta'][ $yoast_meta_key . '-id' ] ) ) {
dty_sync_opengraph_image( $new_post, $post_array['meta'][ $yoast_meta_key . '-id' ], $yoast_meta_key );
} else {
dty_remove_opengraph_image( $new_post, $yoast_meta_key );
}
}
}
add_action( 'dt_pull_post', 'dty_sync_opengraph_image_pull', 1, 3 );
/**
* Hook into Distributor's REST API actions:
* dt_process_distributor_attributes - Fires when a new post is created.
* dt_process_subscription_attributes - Fires when a post is updated.
*
* @param \WP_Post $new_post Inserted or updated post object.
* @param \WP_REST_Request $request Request object.
* @param bool $update True when creating a post, false when updating.
*/
function dty_process_attributes( $new_post, $request, $update = false ) {
$params = $request->get_params();
/**
* Params array format varies in dt_process_distributor_attributes and dt_process_subscription_attributes hooks.
*/
if ( isset( $params['distributor_meta'] ) ) {
$meta = $params['distributor_meta'];
} elseif ( isset( $params['post_data']['distributor_meta'] ) ) {
$meta = $params['post_data']['distributor_meta'];
}
foreach ( dty_yoast_image_meta_keys() as $yoast_meta_key ) {
if ( isset( $meta[ $yoast_meta_key . '-id' ] ) && ! empty( $meta[ $yoast_meta_key . '-id' ] ) ) {
dty_sync_opengraph_image( $new_post, $meta[ $yoast_meta_key . '-id' ], $yoast_meta_key );
} else {
dty_remove_opengraph_image( $new_post, $yoast_meta_key );
}
}
}
add_action( 'dt_process_distributor_attributes', 'dty_process_attributes', 1, 3 );
add_action( 'dt_process_subscription_attributes', 'dty_process_attributes', 1, 2 );
/**
* Sync Yoast Open Graph image ID and image URL.
*
* @param int|object $new_post The new post's ID or a post object.
* @param int|array $dt_original_media_id The media ID from the external site. Used to find new attachment using meta key dt_original_media_id.
* @param string $yoast_meta_key The meta key for the Yoast image. Example: _yoast_wpseo_opengraph-image or _yoast_wpseo_twitter-image. The suffix '-id' will be appended.
*/
function dty_sync_opengraph_image( $new_post, $dt_original_media_id, $yoast_meta_key = '_yoast_wpseo_opengraph-image' ) {
/**
* If $new_post is an object, get the ID. Otherwise use $new_post.
*/
$new_post_id = 0;
if ( is_numeric( $new_post ) ) {
$new_post_id = $new_post;
} elseif ( is_object( $new_post ) && isset( $new_post->ID ) ) {
$new_post_id = $new_post->ID;
}
/**
* No post ID, return.
*/
if ( $new_post_id === 0 ) {
return false;
}
/**
* If the media ID is an array, grab the first value.
*/
if ( is_array( $dt_original_media_id ) ) {
$dt_original_media_id = $dt_original_media_id[0];
}
$dt_original_media_id = intval( $dt_original_media_id );
/**
* WP_Query to find the new attachment ID.
*/
global $post;
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'dt_original_media_id',
'value' => $dt_original_media_id,
),
),
);
$c_query = new WP_Query( $args );
if ( $c_query->have_posts() ) {
while ( $c_query->have_posts() ) {
$c_query->the_post();
$new_media_id = $post->ID;
$new_media_url = wp_get_attachment_image_url( $new_media_id, 'full' );
/**
* If we find a new media ID and a new URL, update both Yoast meta keys.
*/
if ( $new_media_id && $new_media_url ) {
update_post_meta( $new_post_id, $yoast_meta_key . '-id', $new_media_id );
update_post_meta( $new_post_id, $yoast_meta_key, $new_media_url );
/**
* Sometimes there can be more than one image with the same dt_original_media_id. We only want the first.
*/
break;
}
}
wp_reset_postdata();
} else {
/**
* If we don't find the correct media, delete the meta keys.
*/
dty_remove_opengraph_image( $new_post_id, $yoast_meta_key );
}
}
/**
* Delete Yoast meta fields
*
* @param int|object $new_post The new post's ID or a post object.
* @param string $yoast_meta_key The meta key for the Yoast image. Example: _yoast_wpseo_opengraph-image or _yoast_wpseo_twitter-image. The suffix '-id' will be appended.
*/
function dty_remove_opengraph_image( $new_post, $yoast_meta_key ) {
/**
* If $new_post is an object, get the ID. Otherwise use $new_post.
*/
$new_post_id = 0;
if ( is_numeric( $new_post ) ) {
$new_post_id = $new_post;
} elseif ( is_object( $new_post ) && isset( $new_post->ID ) ) {
$new_post_id = $new_post->ID;
}
/**
* No post ID, return.
*/
if ( $new_post_id === 0 ) {
return false;
}
delete_post_meta( $new_post_id, $yoast_meta_key );
delete_post_meta( $new_post_id, $yoast_meta_key . '-id' );
}