-
Notifications
You must be signed in to change notification settings - Fork 66
/
plugin.php
executable file
·216 lines (178 loc) · 5.15 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
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
<?php
add_action( 'plugins_loaded', 'WPAPIYoast_init' );
/**
* Plugin Name: Yoast to REST API
* Description: Adds Yoast fields to page and post metadata to WP REST API responses
* Author: Niels Garve, Pablo Postigo, Tedy Warsitha, Charlie Francis
* Author URI: https://github.com/niels-garve
* Version: 1.4.2
* Plugin URI: https://github.com/niels-garve/yoast-to-rest-api
*/
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'
);
function __construct() {
add_action( 'rest_api_init', array( $this, 'add_yoast_data' ) );
}
function add_yoast_data() {
// Posts
register_rest_field( 'post',
'yoast_meta',
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_meta',
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_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_category' ),
'update_callback' => null,
'schema' => null,
)
);
// Tag
register_rest_field( 'tag',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_tag' ),
'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_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
}
}
/**
* 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( $p, $field_name, $request ) {
$wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance();
$wpseo_frontend->reset();
query_posts( array(
'p' => $p['id'], // ID of a page, post, or custom type
'post_type' => 'any'
) );
the_post();
$yoast_meta = array(
'yoast_wpseo_title' => $wpseo_frontend->get_content_title(),
'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ),
'yoast_wpseo_canonical' => $wpseo_frontend->canonical( false ),
);
/**
* Filter the returned yoast meta.
*
* @since 1.4.2
* @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.
* @return array $yoast_meta Filtered meta array.
*/
$yoast_meta = apply_filters( 'wpseo_to_api_yoast_meta', $yoast_meta, $p, $request );
wp_reset_query();
return (array) $yoast_meta;
}
private function wp_api_encode_taxonomy() {
$wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance();
$wpseo_frontend->reset();
$yoast_meta = array(
'yoast_wpseo_title' => $wpseo_frontend->get_taxonomy_title(),
'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ),
);
/**
* Filter the returned yoast meta for a taxonomy.
*
* @since 1.4.2
* @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 );
return (array) $yoast_meta;
}
function wp_api_encode_yoast_category( $category ) {
query_posts( array(
'cat' => $category['id'],
) );
the_post();
$res = $this->wp_api_encode_taxonomy();
wp_reset_query();
return $res;
}
function wp_api_encode_yoast_tag( $tag ) {
query_posts( array(
'tag_id' => $tag['id'],
) );
the_post();
$res = $this->wp_api_encode_taxonomy();
wp_reset_query();
return $res;
}
}
function WPAPIYoast_init() {
if ( class_exists( 'WPSEO_Frontend' ) ) {
include __DIR__ . '/classes/class-wpseo-frontend-to-rest-api.php';
$yoast_To_REST_API = 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.' )
);
}