Skip to content

Commit

Permalink
Yoast SEO config data (#25)
Browse files Browse the repository at this point in the history
SEO Config data
  • Loading branch information
ashhitch authored Apr 1, 2020
1 parent 9dd94d8 commit 3f6a96e
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 5 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ 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).

## [3.3.0] - 2020-04-01

### Added

Support for Yoast configuration data including:

- Webmaster verification
- Social profiles
- Schemas
- Breadcrumbs

## [3.2.0] - 2020-03-09

### Added
Expand Down
92 changes: 90 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

This is an extension to the WPGraphQL plugin (https://github.com/wp-graphql/wp-graphql) that returns Yoast SEO data.

Currently returning SEO data for pages, posts, custom post types, categories and custom taxonomies.
> Using this plugin? I would love to see what you make with it. 😃 [@ash_hitchcock](https://twitter.com/ash_hitchcock)
> Using this plugin? I would love to see what you make with it. [@ash_hitchcock](https://twitter.com/ash_hitchcock)
**Currently returning SEO data for:**

- Pages
- Posts
- Custom post types
- Categories
- Custom taxonomies
- WooCommerce Products
- Yoast Configuration
- Webmaster verification
- Social profiles
- Schemas
- Breadcrumbs

> If there is any Yoast data that is not currently returned, please raise an issue so we can add it to the roadmap.
## Quick Install

Expand Down Expand Up @@ -53,10 +67,84 @@ To query for the Yoast Data as the seo object to your query:
}
}
}
}
```


To query for the site configuration data you can query from the root.

```
{
posts {
}
seo {
webmaster {
googleVerify
yandexVerify
msVerify
baiduVerify
}
schema {
companyLogo {
mediaItemUrl
}
logo {
mediaItemUrl
}
personLogo {
mediaItemUrl
}
}
breadcrumbs {
showBlogPage
separator
searchPrefix
prefix
homeText
enabled
boldLast
archivePrefix
notFoundText
}
social {
facebook {
url
defaultImage {
mediaItemUrl
}
}
instagram {
url
}
linkedIn {
url
}
mySpace {
url
}
pinterest {
url
metaTag
}
twitter {
cardType
username
}
wikipedia {
url
}
youTube {
url
}
}
}
}
```

## V3 breaking change.

Image urls are now returned as `mediaItem` type.
Expand Down
179 changes: 176 additions & 3 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: 3.2.1
* Version: 3.3.0
*
* @package WP_Graphql_YOAST_SEO
*/
Expand All @@ -29,6 +29,18 @@
array_push($taxonomies, 'productCategory');
}

register_graphql_enum_type('SEOCardType', [
'description' => __('Types of cards', 'wp-graphql-yoast-seo'),
'values' => [
'summary_large_image' => [
'value' => 'summary_large_image'
],
'summary' => [
'value' => 'summary'
],
],
]);

register_graphql_object_type('SEO', [
'fields' => [
'title' => ['type' => 'String'],
Expand All @@ -47,6 +59,167 @@
]
]);

register_graphql_object_type('SEOSchema', [
'fields' => [
'companyName' => ['type' => 'String'],
'companyOrPerson' => ['type' => 'String'],
'companyLogo' => ['type' => 'MediaItem'],
'personLogo' => ['type' => 'MediaItem'],
'logo' => ['type' => 'MediaItem'],
]
]);

register_graphql_object_type('SEOWebmaster', [
'fields' => [
'baiduVerify' => ['type' => 'String'],
'googleVerify' => ['type' => 'String'],
'msVerify' => ['type' => 'String'],
'yandexVerify' => ['type' => 'String'],
]
]);

register_graphql_object_type('SEOBreadcrumbs', [
'fields' => [
'enabled' => ['type' => 'Boolean'],
'boldLast' => ['type' => 'Boolean'],
'showBlogPage' => ['type' => 'Boolean'],
'notFoundText' => ['type' => 'String'],
'archivePrefix' => ['type' => 'String'],
'homeText' => ['type' => 'String'],
'prefix' => ['type' => 'String'],
'searchPrefix' => ['type' => 'String'],
'separator' => ['type' => 'String'],
]
]);

register_graphql_object_type('SEOSocialFacebook', [
'fields' => [
'url' => ['type' => 'String'],
'defaultImage' => ['type' => 'MediaItem'],
]
]);

register_graphql_object_type('SEOSocialTwitter', [
'fields' => [
'username' => ['type' => 'String'],
'cardType' => ['type' => 'SEOCardType'],
]
]);

register_graphql_object_type('SEOSocialInstagram', [
'fields' => [
'url' => ['type' => 'String'],
]
]);
register_graphql_object_type('SEOSocialLinkedIn', [
'fields' => [
'url' => ['type' => 'String'],
]
]);
register_graphql_object_type('SEOSocialMySpace', [
'fields' => [
'url' => ['type' => 'String'],
]
]);

register_graphql_object_type('SEOSocialPinterest', [
'fields' => [
'url' => ['type' => 'String'],
'metaTag' => ['type' => 'String'],
]
]);

register_graphql_object_type('SEOSocialYoutube', [
'fields' => [
'url' => ['type' => 'String'],
]
]);
register_graphql_object_type('SEOSocialWikipedia', [
'fields' => [
'url' => ['type' => 'String'],
]
]);


register_graphql_object_type('SEOSocial', [
'fields' => [
'facebook' => ['type' => 'SEOSocialFacebook'],
'twitter' => ['type' => 'SEOSocialTwitter'],
'instagram' => ['type' => 'SEOSocialInstagram'],
'linkedIn' => ['type' => 'SEOSocialLinkedIn'],
'mySpace' => ['type' => 'SEOSocialMySpace'],
'pinterest' => ['type' => 'SEOSocialPinterest'],
'youTube' => ['type' => 'SEOSocialYoutube'],
'wikipedia' => ['type' => 'SEOSocialWikipedia'],
]
]);

register_graphql_object_type('SEOConfig', [
'description' => __('The Yoast SEO site level configuration data', 'wp-graphql-yoast-seo'),
'fields' => [
'schema' => ['type' => 'SEOSchema'],
'webmaster' => ['type' => 'SEOWebmaster'],
'social' => ['type' => 'SEOSocial'],
'breadcrumbs' => ['type' => 'SEOBreadcrumbs'],
]
]);

register_graphql_field('RootQuery', 'seo', [
'type' => 'SEOConfig',
'description' => __('Returns seo site data', 'wp-graphql-yoast-seo'),
'resolve' => function ($source, array $args, AppContext $context) {

$wpseo_options = WPSEO_Options::get_instance();
$all = $wpseo_options->get_all();

return array(
'webmaster' => array(
'baiduVerify' => trim($all['baiduverify']),
'googleVerify' => trim($all['googleverify']),
'msVerify' => trim($all['msverify']),
'yandexVerify' => trim($all['yandexverify']),
),
'social' => array(
'facebook' => array(
'url' => trim($all['facebook_site']),
'defaultImage' => DataSource::resolve_post_object($all['og_default_image_id'], $context)
),
'twitter' => array(
'username' => trim($all['twitter_site']),
'cardType' => trim($all['twitter_card_type']),
),
'instagram' => array('url' => trim($all['instagram_url'])),
'linkedIn' => array('url' => trim($all['linkedin_url'])),
'mySpace' => array('url' => trim($all['myspace_url'])),
'pinterest' => array(
'url' => trim($all['pinterest_url']),
'metaTag' => trim($all['pinterestverify']),
),
'youTube' => array('url' => trim($all['youtube_url'])),
'wikipedia' => array('url' => trim($all['wikipedia_url'])),
),
'breadcrumbs' => array(
'enabled' => trim($all['breadcrumbs-enable']),
'boldLast' => trim($all['breadcrumbs-boldlast']),
'showBlogPage' => trim($all['breadcrumbs-display-blog-page']),
'archivePrefix' => trim($all['breadcrumbs-archiveprefix']),
'prefix' => trim($all['breadcrumbs-prefix']),
'notFoundText' => trim($all['breadcrumbs-404crumb']),
'homeText' => trim($all['breadcrumbs-home']),
'searchPrefix' => trim($all['breadcrumbs-searchprefix']),
'separator' => trim($all['breadcrumbs-sep']),
),
'schema' => array(
'companyName' => trim($all['company_name']),
'companyLogo' => DataSource::resolve_post_object($all['company_logo_id'], $context),
'personLogo' => DataSource::resolve_post_object($all['person_logo_id'], $context),
'logo' => DataSource::resolve_post_object($all['company_or_person'] === 'company' ? $all['company_logo_id'] : $all['person_logo_id'], $context),
'companyOrPerson' => trim($all['company_or_person']),
)
);
},
]);


if (!empty($post_types) && is_array($post_types)) {
foreach ($post_types as $post_type) {
Expand All @@ -55,7 +228,7 @@
if (isset($post_type_object->graphql_single_name)) :
register_graphql_field($post_type_object->graphql_single_name, 'seo', [
'type' => 'SEO',
'description' => __('The Yoast SEO data of the ' . $post_type_object->graphql_single_name, 'wp-graphql'),
'description' => __('The Yoast SEO data of the ' . $post_type_object->graphql_single_name, 'wp-graphql-yoast-seo'),
'resolve' => function ($post, array $args, AppContext $context) {

// Connect to Yoast
Expand Down Expand Up @@ -108,7 +281,7 @@

register_graphql_field($taxonomy->graphql_single_name, 'seo', [
'type' => 'SEO',
'description' => __('The Yoast SEO data of the ' . $taxonomy->label . ' taxonomy.', 'wp-graphql'),
'description' => __('The Yoast SEO data of the ' . $taxonomy->label . ' taxonomy.', 'wp-graphql-yoast-seo'),
'resolve' => function ($term, array $args, AppContext $context) {

$term_obj = get_term($term->term_id);
Expand Down

0 comments on commit 3f6a96e

Please sign in to comment.