Skip to content

Commit

Permalink
Feature/full schema v2 (#74)
Browse files Browse the repository at this point in the history
- Raw Schema returned as a string to be decoded in the client
  • Loading branch information
ashhitch authored Dec 7, 2020
1 parent 493318e commit 652a4d8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.10.0] - 2020-12-07

### Added

- Raw Schema data for post types, taxonomies, and users

## [4.9.0] - 2020-11-25

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-graphql-yoast-seo",
"version": "4.2.0",
"version": "4.10.0",
"description": "A WPGraphQL Extension that adds support for Yoast SEO",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: SEO, Yoast, WPGraphQL, GraphQL, Headless WordPress, Decoupled WordPress, J
Requires at least: 5.0
Tested up to: 5.4
Requires PHP: 7.1
Stable tag: 4.9.0
Stable tag: 4.10.0
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
52 changes: 50 additions & 2 deletions wp-graphql-yoast-seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Author URI: https://www.ashleyhitchcock.com
* Text Domain: wp-graphql-yoast-seo
* Domain Path: /languages
* Version: 4.9.0
* Version: 4.10.0
*
* @package WP_Graphql_YOAST_SEO
*/
Expand Down Expand Up @@ -196,6 +196,16 @@ function wp_gql_seo_build_content_type_data($types, $all)
'fields' => [
'pageType' => ['type' => ['list_of' => 'String']],
'articleType' => ['type' => ['list_of' => 'String']],
'raw' => ['type' => 'String'],
],
]);
register_graphql_object_type('SEOTaxonomySchema', [
'description' => __(
'The Schema types for Taxonomy',
'wp-graphql-yoast-seo'
),
'fields' => [
'raw' => ['type' => 'String'],
],
]);

Expand Down Expand Up @@ -225,7 +235,9 @@ function wp_gql_seo_build_content_type_data($types, $all)
];

register_graphql_object_type('TaxonomySEO', [
'fields' => $baseSEOFields,
'fields' => array_merge($baseSEOFields, [
'schema' => ['type' => 'SEOTaxonomySchema'],
]),
]);

register_graphql_object_type('PostTypeSEO', [
Expand Down Expand Up @@ -443,13 +455,21 @@ function wp_gql_seo_build_content_type_data($types, $all)
],
]);

register_graphql_object_type('SEOUserSchema', [
'description' => __('The Schema types for User', 'wp-graphql-yoast-seo'),
'fields' => [
'raw' => ['type' => 'String'],
],
]);

register_graphql_object_type('SEOUser', [
'fields' => [
'title' => ['type' => 'String'],
'metaDesc' => ['type' => 'String'],
'metaRobotsNoindex' => ['type' => 'String'],
'metaRobotsNofollow' => ['type' => 'String'],
'social' => ['type' => 'SEOUserSocial'],
'schema' => ['type' => 'SEOUserSchema'],
],
]);

Expand Down Expand Up @@ -645,6 +665,16 @@ function wp_gql_seo_build_content_type_data($types, $all)
// Base array
$seo = [];

$map = [
'@id' => 'id',
'@type' => 'type',
'@graph' => 'graph',
'@context' => 'context',
];

$schemaArray = YoastSEO()->meta->for_post($post->ID)
->schema;

// https://developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/
$robots = YoastSEO()->meta->for_post($post->ID)
->robots;
Expand Down Expand Up @@ -762,6 +792,10 @@ function wp_gql_seo_build_content_type_data($types, $all)
? YoastSEO()->meta->for_post($post->ID)
->schema_article_type
: [],
'raw' => json_encode(
$schemaArray,
JSON_UNESCAPED_SLASHES
),
],
];

Expand Down Expand Up @@ -826,6 +860,8 @@ function wp_gql_seo_build_content_type_data($types, $all)
'resolve' => function ($user, array $args, AppContext $context) {
$robots = YoastSEO()->meta->for_author($user->userId)->robots;

$schemaArray = YoastSEO()->meta->for_author($user->userId)->schema;

$userSeo = [
'title' => wp_gql_seo_format_string(
YoastSEO()->meta->for_author($user->userId)->title
Expand Down Expand Up @@ -865,6 +901,9 @@ function wp_gql_seo_build_content_type_data($types, $all)
get_the_author_meta('wikipedia', $user->userId)
),
],
'schema' => [
'raw' => json_encode($schemaArray, JSON_UNESCAPED_SLASHES),
],
];

return !empty($userSeo) ? $userSeo : [];
Expand Down Expand Up @@ -896,6 +935,9 @@ function wp_gql_seo_build_content_type_data($types, $all)
);
$robots = YoastSEO()->meta->for_term($term->term_id)->robots;

$schemaArray = YoastSEO()->meta->for_term($term->term_id)
->schema;

// Get data
$seo = [
'title' => wp_gql_seo_format_string(
Expand Down Expand Up @@ -984,6 +1026,12 @@ function wp_gql_seo_build_content_type_data($types, $all)
YoastSEO()->meta->for_term($term->term_id)
->is_cornerstone
),
'schema' => [
'raw' => json_encode(
$schemaArray,
JSON_UNESCAPED_SLASHES
),
],
];
wp_reset_query();

Expand Down

0 comments on commit 652a4d8

Please sign in to comment.