forked from ChazUK/wp-api-yoast-meta
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.php
117 lines (102 loc) · 4.48 KB
/
plugin.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
<?php
/**
* Plugin Name: WP REST API Yoast SEO
* Description: Adds Yoast fields to page and post metadata to WP REST API responses
* Author: Charlie Francis
* Author URI: https://github.com/ChazUK
* Version: 1.2.0
* Plugin URI: https://github.com/ChazUK/wp-api-yoast-seo
*/
class WPAPIYoastMeta {
function __construct() {
add_action('rest_api_init', array($this, 'add_yoast_data'));
}
function add_yoast_data() {
// Posts
register_rest_field( 'post',
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_post' ),
'update_callback' => null,
'schema' => null,
)
);
// Pages
register_rest_field( 'page',
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_post' ),
'update_callback' => null,
'schema' => null,
)
);
// Public custom post types
$types = get_post_types(array(
'public' => true,
'_builtin' => false
));
foreach($types as $key => $type) {
register_rest_field( $type,
'yoast',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_post' ),
'update_callback' => null,
'schema' => null,
)
);
}
}
function wp_api_encode_yoast_post($post, $field_name, $request) {
include __DIR__ . '/classes/class-frontend.php';
include __DIR__ . '/classes/class-opengraph.php';
include __DIR__ . '/classes/class-twitter.php';
wp_reset_query();
query_posts([
'p' => $post['id'],
'post_type' => $post['type']
]);
$wpseo_frontend = WPAPI_WPSEO_Frontend::get_instance();
$wpseo_opengraph = new WPAPI_WPSEO_OpenGraph();
$wpseo_twitter = new WPAPI_WPSEO_Twitter();
$yoastMeta = array(
'metas' => array(
'focuskw' => get_post_meta($post['id'],'_yoast_wpseo_focuskw', true),
'title' => get_post_meta($post['id'], '_yoast_wpseo_title', true),
'metadesc' => get_post_meta($post['id'], '_yoast_wpseo_metadesc', true),
'linkdex' => get_post_meta($post['id'], '_yoast_wpseo_linkdex', true),
'metakeywords' => get_post_meta($post['id'], '_yoast_wpseo_metakeywords', true),
'meta-robots-noindex' => get_post_meta($post['id'], '_yoast_wpseo_meta-robots-noindex', true),
'meta-robots-nofollow' => get_post_meta($post['id'], '_yoast_wpseo_meta-robots-nofollow', true),
'meta-robots-adv' => get_post_meta($post['id'], '_yoast_wpseo_meta-robots-adv', true),
'canonical' => get_post_meta($post['id'], '_yoast_wpseo_canonical', true),
'redirect' => get_post_meta($post['id'], '_yoast_wpseo_redirect', true),
'opengraph-title' => get_post_meta($post['id'], '_yoast_wpseo_opengraph-title', true),
'opengraph-description' => get_post_meta($post['id'], '_yoast_wpseo_opengraph-description', true),
'opengraph-image' => get_post_meta($post['id'], '_yoast_wpseo_opengraph-image', true),
'twitter-title' => get_post_meta($post['id'], '_yoast_wpseo_twitter-title', true),
'twitter-description' => get_post_meta($post['id'], '_yoast_wpseo_twitter-description', true),
'twitter-image' => get_post_meta($post['id'], '_yoast_wpseo_twitter-image', true)
),
'title' => $wpseo_frontend->title(wp_get_document_title()),
'canonical' => $wpseo_frontend->canonical(false),
'metatags' => array(
'description' => $wpseo_frontend->metadesc(false),
'keywords' => $wpseo_frontend->metakeywords(),
'robots' => $wpseo_frontend->robots(),
'og:title' => $wpseo_opengraph->og_title(false),
'og:description' => $wpseo_opengraph->description(false),
'twitter:title' => $wpseo_twitter->title(),
'twitter:description' => $wpseo_twitter->description(),
'twitter:image' => $wpseo_twitter->image()
)
);
$ogimages = $wpseo_opengraph->image();
if (is_array($ogimages) && !empty($ogimages)) {
foreach($ogimages as $tag) {
$yoastMeta['metatags'][$tag['name']] = $tag['value'];
}
}
return (array) $yoastMeta;
}
}
$WPAPIYoastMeta = new WPAPIYoastMeta();