Skip to content

Commit

Permalink
🧘🏼‍♂️ Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobarriola committed Mar 30, 2020
0 parents commit f76c715
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Empty file added .gitignore
Empty file.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Registering Types and Fields: Pair Programming with Jacob Arriola & Jason Bahl

This is the reference repo used during a pair programming session with Jacob Arriola and Jason
Bahl. The essence of this session was to learn how to extend the WPGraphQL plugin's schema.

## References
* [Jason Bahl Twitter](https://twitter.com/jasonbahl/)
* [WPGraphQL](https://docs.wpgraphql.com/)
* [YouTube of session](https://www.wpgraphql.com/2020/03/30/registering-types-and-fields-pair-programming-with-jacob-arriola/)
80 changes: 80 additions & 0 deletions yoast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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 ];
},
] );
}
}


} );










0 comments on commit f76c715

Please sign in to comment.