forked from ChazUK/wp-api-yoast-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
executable file
·485 lines (389 loc) · 13.9 KB
/
Copy pathplugin.php
File metadata and controls
executable file
·485 lines (389 loc) · 13.9 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* Plugin Name: Yoast to REST API
* Description: Adds Yoast fields to page and post metadata to WP REST API responses. Version for composer.
* Author: Lopatin Daniil
* Author URI: https://github.com/mupic
* Version: 2.0.0
* Plugin URI: https://github.com/mupic/yoast-to-rest-api
*/
//GET params will take precedence over constants
if(!defined('YOAST_REST_META')) //false - Disable automatic meta seo input
define('YOAST_REST_META', false);
if(!defined('YOAST_REST_OG')) //false - Disable automatic open graph input
define('YOAST_REST_OG', false);
if(!defined('YOAST_REST_TW')) //false - Disable automatic meta twitter input
define('YOAST_REST_TW', false);
if(!defined('YOAST_REST_BC')) //true - Return json breadcrumbs. "html" - Return html generated breadcrumbs.
define('YOAST_REST_BC', false);
if(!defined('YOAST_REST_SCHEMA')) //false - Disable automatic microdata input.
define('YOAST_REST_SCHEMA', false);
if(!defined('YOAST_REST_ENABLE_EMBED')) //false - Disable yoast in fields called from _embed=true.
define('YOAST_REST_ENABLE_EMBED', false);
class Yoast_To_REST_API {
protected $keys = array(
'yoast_wpseo_focuskw',
'yoast_wpseo_title',
'yoast_wpseo_metadesc',
'yoast_wpseo_linkdex',
'yoast_wpseo_metakeywords',
'yoast_wpseo_meta-robots-noindex',
'yoast_wpseo_meta-robots-nofollow',
'yoast_wpseo_meta-robots-adv',
'yoast_wpseo_canonical',
'yoast_wpseo_redirect',
'yoast_wpseo_opengraph-title',
'yoast_wpseo_opengraph-description',
'yoast_wpseo_opengraph-image',
'yoast_wpseo_twitter-title',
'yoast_wpseo_twitter-description',
'yoast_wpseo_twitter-image'
);
protected $allow = array();
function __construct() {
add_action( 'rest_api_init', array( $this, 'add_yoast_data' ) );
}
private function classes_init(){
$this->wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance();
$this->schema_rest = new WPSEO_Schema_To_REST_API();
}
private function allowed(){
$this->allow['yoast_embed'] = isset($_GET['yoast_embed']) && ($this->is_bool($_GET['yoast_embed']) || $_GET['yoast_embed'])? $this->parse_bool($_GET['yoast_embed']) : YOAST_REST_ENABLE_EMBED;
$this->allow['meta'] = isset($_GET['yoast_meta']) && ($this->is_bool($_GET['yoast_meta']) || $_GET['yoast_meta'])? $this->parse_bool($_GET['yoast_meta']) : YOAST_REST_META;
$this->allow['opengraph'] = isset($_GET['opengraph']) && ($this->is_bool($_GET['opengraph']) || $_GET['opengraph'])? $this->parse_bool($_GET['opengraph']) : YOAST_REST_META;
$this->allow['twitter'] = isset($_GET['twitter']) && ($this->is_bool($_GET['twitter']) || $_GET['twitter'])? $this->parse_bool($_GET['twitter']) : YOAST_REST_META;
$this->allow['breadcrumbs'] = isset($_GET['breadcrumbs'])? ($this->is_bool($_GET['breadcrumbs'])? $this->parse_bool($_GET['breadcrumbs']) : ($_GET['breadcrumbs']? $_GET['breadcrumbs'] : YOAST_REST_META)) : YOAST_REST_META;
$this->allow['schema'] = isset($_GET['schema']) && ($this->is_bool($_GET['schema']) || $_GET['schema'])? $this->parse_bool($_GET['schema']) : YOAST_REST_META;
}
function add_yoast_data() {
$this->allowed();
$this->classes_init();
// Posts
register_rest_field( 'post',
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
// Pages
register_rest_field( 'page',
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
// Category
register_rest_field( 'category',
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_tax' ),
'update_callback' => null,
'schema' => null,
)
);
// Tag
register_rest_field( 'tag',
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_tax' ),
'update_callback' => null,
'schema' => null,
)
);
$taxonomies = get_taxonomies(array(
'show_in_rest' => true,
'_builtin' => false
));
foreach( $taxonomies as $taxonomy ) {
register_rest_field( $taxonomy,
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_tax' ),
'update_callback' => null,
'schema' => null,
)
);
}
// Public custom post types
$types = get_post_types( array(
'public' => true,
'_builtin' => false
) );
foreach ( $types as $type ) {
register_rest_field( $type,
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
}
/* register route <*/
register_rest_route( 'yoast_api/v1', '/home', array(
'methods' => 'GET',
'callback' => array($this, 'wp_api_encode_yoast_home'),
) );
/*> register route */
}
protected $is_main_request = false;
protected function is_main_request($func_name){
if($this->is_main_request === false){
$this->is_main_request = $func_name;
}
return $this->is_main_request === $func_name;
}
/**
* Updates post meta with values from post/put request.
*
* @param array $value
* @param object $data
* @param string $field_name
*
* @return array
*/
function wp_api_update_yoast( $value, $data, $field_name ) {
foreach ( $value as $k => $v ) {
if ( in_array( $k, $this->keys ) ) {
! empty( $k ) ? update_post_meta( $data->ID, '_' . $k, $v ) : null;
}
}
return $this->wp_api_encode_yoast( $data->ID, null, null );
}
function wp_api_encode_yoast_home( $request ) {
if(!$this->allow['yoast_embed'] && !$this->is_main_request(__FUNCTION__))
return false;
$this->wpseo_frontend->reset();
/* big crutch for yoast */
global $post, $wp_the_query;
$ID = get_option( 'page_on_front' );
if($ID){
query_posts( array(
'p' => $ID, // ID of a page, post, or custom type
'post_type' => 'page',
'no_found_rows' => true,
) );
the_post();
$wp_the_query->queried_object = $post;
$wp_the_query->is_page = true;
}else{
// $wp_the_query->queried_object = (object) array();
$wp_the_query->is_home = true;
}
wp_reset_query();
$yoast_meta = array(
'title' => $this->wpseo_frontend->title(''),
'description' => $this->wpseo_frontend->metadesc( false ),
'canonical' => $this->wpseo_frontend->canonical( false ),
);
$wpseo_og = new WPSEO_OpenGraph_Twitter_To_REST_API();
$og = $wpseo_og->get_og_data($this->allow);
$yoast_meta = array_merge($yoast_meta, $og);
/**
* Filter the returned yoast meta.
*
* @param array $yoast_meta Array of metadata to return from Yoast.
* @param \WP_REST_Request $request The REST request.
*/
$yoast_meta = apply_filters( 'wpseo_to_api_yoast_home_meta', $yoast_meta, $request );
$breadcrumbs = $this->wp_api_get_breadcrumbs(null, $request, 'home');
$schema = $this->wp_api_get_schema(null, $request, 'home');
$result = array();
$result['meta'] = $yoast_meta;
if($breadcrumbs)
$result['breadcrumbs'] = $breadcrumbs;
if($schema)
$result['schema'] = $schema;
return $result;
}
function wp_api_encode_yoast( $p, $field_name, $request ) {
if(!$this->allow['yoast_embed'] && !$this->is_main_request(__FUNCTION__))
return false;
if(!$this->allow['meta'])
return false;
$this->wpseo_frontend->reset();
query_posts( array(
'p' => $p['id'], // ID of a page, post, or custom type
'post_type' => 'any',
'no_found_rows' => true,
) );
the_post();
/* big crutch for yoast */
global $post, $wp_the_query;
$wp_the_query->queried_object = $post;
$wp_the_query->is_singular = true;
if($p['type'] == 'post')
$wp_the_query->is_single = true;
if($p['type'] == 'page')
$wp_the_query->is_page = true;
wp_reset_query();
$yoast_meta = array(
'title' => $this->wpseo_frontend->title(''),
'description' => $this->wpseo_frontend->metadesc( false ),
'canonical' => $this->wpseo_frontend->canonical( false ),
);
$wpseo_og = new WPSEO_OpenGraph_Twitter_To_REST_API();
$og = $wpseo_og->get_og_data($this->allow);
$yoast_meta = array_merge($yoast_meta, $og);
/**
* Filter the returned yoast meta.
*
* @param array $yoast_meta Array of metadata to return from Yoast.
* @param \WP_Post $p The current post object.
* @param \WP_REST_Request $request The REST request.
*/
$yoast_meta = apply_filters( 'wpseo_to_api_yoast_meta', $yoast_meta, $p, $request );
$breadcrumbs = $this->wp_api_get_breadcrumbs($p, $request, 'post');
$schema = $this->wp_api_get_schema($p, $request, 'post');
$result = array();
$result['meta'] = $yoast_meta;
if($breadcrumbs)
$result['breadcrumbs'] = $breadcrumbs;
if($schema)
$result['schema'] = $schema;
return $result;
}
function wp_api_encode_yoast_tax( $tax, $field_name, $request ) {
if(!$this->allow['yoast_embed'] && !$this->is_main_request(__FUNCTION__))
return false;
if(!$this->allow['meta'])
return false;
/* big crutch for yoast */
global $wp_the_query;
$wp_the_query->queried_object = get_term( $tax['id'], $tax['taxonomy'] );
$wp_the_query->is_tax = true;
$wp_the_query->tax_query->queried_terms[ $tax['taxonomy'] ]['terms'] = false;
wp_reset_query();
$res = $this->wp_api_encode_taxonomy($tax, $request);
return $res;
}
private function wp_api_encode_taxonomy($tax, $request) {
$this->wpseo_frontend->reset();
$yoast_meta = array(
'title' => $this->wpseo_frontend->title(''),
'description' => $this->wpseo_frontend->metadesc( false ),
);
$wpseo_og = new WPSEO_OpenGraph_Twitter_To_REST_API();
$og = $wpseo_og->get_og_data($this->allow);
$yoast_meta = array_merge($yoast_meta, $og);
/**
* Filter the returned yoast meta for a taxonomy.
*
* @param array $yoast_meta Array of metadata to return from Yoast.
* @return array $yoast_meta Filtered meta array.
*/
$yoast_meta = apply_filters( 'wpseo_to_api_yoast_taxonomy_meta', $yoast_meta );
$breadcrumbs = $this->wp_api_get_breadcrumbs($tax, $request, 'tax');
$schema = $this->wp_api_get_schema($tax, $request, 'tax');
$result = array();
$result['meta'] = $yoast_meta;
if($breadcrumbs)
$result['breadcrumbs'] = $breadcrumbs;
if($schema)
$result['schema'] = $schema;
return $result;
}
private function wp_api_get_breadcrumbs($object = null, $request, $type) {
if(!$this->allow['breadcrumbs'])
return false;
$breadcrumbs = array();
if($this->allow['breadcrumbs'] === 'html'){
$breadcrumbs['html'] = WPSEO_Breadcrumbs::breadcrumb( '', '', false );
/**
* Filter the returned breadcrumbs html.
*
* @param string $breadcrumbs Generated breadcrumbs.
* @param \WP_{object} $object The current post object or taxonomy.
* @param \WP_REST_Request $request The REST request.
*/
if($type == 'post'){
$breadcrumbs['html'] = apply_filters( 'wpseo_to_api_yoast_breadcrumbs_html', $breadcrumbs['html'], $object, $request );
}elseif($type == 'tax'){
$breadcrumbs['html'] = apply_filters( 'wpseo_to_api_yoast_taxonomy_breadcrumbs_html', $breadcrumbs['html'], $object, $request );
}elseif($type == 'home'){
$breadcrumbs['html'] = apply_filters( 'wpseo_to_api_yoast_home_breadcrumbs_html', $breadcrumbs['html'], $request );
}
}else{
$WPSEO_Breadcrumbs = WPSEO_Breadcrumbs::get_instance();
$breadcrumbs['links'] = $WPSEO_Breadcrumbs->get_links();
$breadcrumbs['separator'] = apply_filters( 'wpseo_breadcrumb_separator', WPSEO_Options::get( 'breadcrumbs-sep' ) );
/**
* Filter the returned breadcrumbs array.
*
* @param array $breadcrumbs Array of metadata to return from Yoast.
* @param \WP_{object} $object The current post object or taxonomy.
* @param \WP_REST_Request $request The REST request.
*/
if($type == 'post'){
$breadcrumbs['links'] = apply_filters( 'wpseo_to_api_yoast_breadcrumbs', $breadcrumbs['links'], $object, $request );
}elseif($type == 'tax'){
$breadcrumbs['links'] = apply_filters( 'wpseo_to_api_yoast_taxonomy_breadcrumbs', $breadcrumbs['links'], $object, $request );
}elseif($type == 'home'){
$breadcrumbs['links'] = apply_filters( 'wpseo_to_api_yoast_home_breadcrumbs', $breadcrumbs['links'], $request );
}
}
return $breadcrumbs;
}
private function wp_api_get_schema($object = null, $request, $type) {
if(!$this->allow['schema'])
return false;
$schema = $this->schema_rest->get_schema_for_current_query();
/**
* Filter the returned microdata json string.
*
* @param string $schema String of json microdata.
* @param \WP_Post $p The current post object.
* @param \WP_REST_Request $request The REST request.
*/
if($type == 'post'){
$schema = apply_filters( 'wpseo_to_api_yoast_schema', $schema, $object, $request );
}elseif($type == 'tax'){
$schema = apply_filters( 'wpseo_to_api_yoast_taxonomy_schema', $schema, $object, $request );
}elseif($type == 'home'){
$schema = apply_filters( 'wpseo_to_api_yoast_home_schema', $schema, $request );
}
return $schema;
}
function is_bool($var){
if(is_bool($var))
return true;
if($var === 'false')
return true;
if($var === 'true')
return true;
return false;
}
function parse_bool($str){
if(is_bool($str))
return $str;
if($str === 'false')
return false;
if($str === 'true')
return true;
return !!$str;
}
}
if (
class_exists( 'WPSEO_Frontend' )
&& class_exists( 'WPSEO_OpenGraph' )
&& class_exists( 'WPSEO_Twitter' )
&& class_exists( 'WPSEO_Breadcrumbs' )
&& class_exists( 'WPSEO_Schema' )
) {
include __DIR__ . '/classes/class-wpseo-frontend-to-rest-api.php';
include __DIR__ . '/classes/class-wpseo-opengraph-twitter-to-rest-api.php';
include __DIR__ . '/classes/class-wpseo-schema-to-rest-api.php';
new Yoast_To_REST_API();
} else {
add_action( 'admin_notices', 'wpseo_not_loaded' );
}
function wpseo_not_loaded() {
printf(
'<div class="error"><p>%s</p></div>',
__( '<b>Yoast to REST API</b> plugin not working because <b>Yoast SEO</b> plugin is not active.' )
);
}