forked from ChazUK/wp-api-yoast-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
116 lines (99 loc) · 3.44 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
<?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, Tedy Warsitha
* Author URI: https://github.com/ChazUK
* Version: 1.1.0
* Plugin URI: https://github.com/ChazUK/wp-api-yoast-seo
*/
class WPAPIYoastMeta {
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_api_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_api_field( 'page',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
// Public custom post types
$types = get_post_types( array(
'public' => true,
'_builtin' => false
) );
foreach ( $types as $key => $type ) {
register_api_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( $post, $field_name, $request ) {
foreach (array_keys(get_taxonomies(['public' => true, 'hierarchical' => true])) as $taxonomy) {
$this->keys[] = 'yoast_wpseo_primary_' . $taxonomy;
}
$frontend = WPSEO_Frontend::get_instance();
$object = get_post($post['id']);
foreach ($this->keys as $key) {
if ('yoast_wpseo_title' === $key) {
$yoastMeta[$key] = $frontend->get_content_title($object);
} else {
$yoastMeta[$key] = get_post_meta($post['id'], '_' . $key, true);
}
}
return (array) $yoastMeta;
}
}
$WPAPIYoastMeta = new WPAPIYoastMeta();