-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoast.php
80 lines (49 loc) · 1.73 KB
/
yoast.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
<?php
namespace App;
const SEO_TYPE = 'SEO';
add_action( 'graphql_register_types', function () {
register_graphql_object_type( SEO_TYPE, [
'description' => 'SEO data for a given post',
'fields' => [
'title' => [
'type' => 'String',
'description' => 'The SEO title for the post',
'resolve' => function ( $source, $args, $info, $context ) {
/* @var $yoast \WPSEO_Frontend */
/* @var $post \WPGraphQL\Model\Post */
$yoast = $source['yoast'];
$post = $source['post'];
$title = $yoast->get_seo_title( $post );
// Provide a fallback if yoast title wasn't set
if ( empty( $title ) ) {
// Go get the Yoast formatted title
$site_title_format = $yoast->get_title_from_options( 'title-' . $post->post_type, $post );
$title = sprintf( '%s %s', $post->titleRaw, $site_title_format );
}
$yoast->reset();
return $title;
},
],
'description' => [
'type' => 'String',
'description' => 'The SEO meta description for the post',
'resolve' => function ( $source, $args, $info, $context ) {
$meta = get_post_meta( $source['post']->ID, '_yoast_wpseo_metadesc', true );
return ! empty( $meta ) ? $meta : null;
},
],
],
] );
$allowed_post_types = get_post_types( [ 'show_in_graphql' => true ], 'objects' );
if ( ! empty( $allowed_post_types ) ) {
foreach ( $allowed_post_types as $allowed_post_type ) {
register_graphql_field( $allowed_post_type->graphql_single_name, 'seo', [
'type' => SEO_TYPE,
'resolve' => function ( $post, $args, $info, $context ) {
$yoast = \WPSEO_Frontend::get_instance();
return [ 'post' => $post, 'yoast' => $yoast ];
},
] );
}
}
} );