Skip to content

Commit

Permalink
Feature/yoast 14 (#31)
Browse files Browse the repository at this point in the history
* Using new public Yoast functions to grab data
* breadcrumbs for post types
* Changelog and readme update
* Yoast version requirements
  • Loading branch information
ashhitch authored May 3, 2020
1 parent 1488830 commit 94d913a
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 66 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ 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.0.1] - 2020-05-03

### Added

- Support for Yoast 14
- Breadcrumbs for post type and taxonomies
- canonical url for post type and taxonomies

## [3.3.0] - 2020-04-01

### Added
Expand All @@ -14,7 +22,7 @@ Support for Yoast configuration data including:
- Webmaster verification
- Social profiles
- Schemas
- Breadcrumbs
- Breadcrumbs (global config)

## [3.2.0] - 2020-03-09

Expand Down
74 changes: 59 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
[![Total Downloads](https://poser.pugx.org/ashhitch/wp-graphql-yoast-seo/downloads)](https://packagist.org/packages/ashhitch/wp-graphql-yoast-seo)
[![Monthly Downloads](https://poser.pugx.org/ashhitch/wp-graphql-yoast-seo/d/monthly)](https://packagist.org/packages/ashhitch/wp-graphql-yoast-seo)

## Please note version 14 of the Yoast Plugin is a major rewrite and will cause issues with this plugin
I am working on an update and will release this ASAP, while you wait you can use version 13 of Yoast without issues.
## Please note version 14 of the Yoast Plugin is a major rewrite.

> When version v4 is released it will require vesrion v14 of the Yoast plugin to work.
If you are stuck on version of Yoast before V14 then use vV3 of this plugin.

Alpha release for use with Yoast 14 can be found here:
https://github.com/ashhitch/wp-graphql-yoast-seo/releases/tag/v4.0.0Alpha

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

Expand Down Expand Up @@ -43,9 +40,19 @@ This is an extension to the WPGraphQL plugin (https://github.com/wp-graphql/wp-g
composer require ashhitch/wp-graphql-yoast-seo
```

## V4 breaking change

Plugin now requires at least Yoast 14.0.0

## V3 breaking change

Image urls are now returned as `mediaItem` type.

This applies to `twitterImage` and `opengraphImage`

## Usage

To query for the Yoast Data as the seo object to your query:
To query for the Yoast Data simply add the seo object to your query:

```
{
Expand All @@ -55,12 +62,14 @@ To query for the Yoast Data as the seo object to your query:
id
title
seo {
canonical
title
metaDesc
focuskw
metaKeywords
metaRobotsNoindex
metaRobotsNofollow
opengraphAuthor
opengraphDescription
opengraphTitle
opengraphDescription
opengraphImage {
Expand All @@ -74,16 +83,57 @@ To query for the Yoast Data as the seo object to your query:
altText
sourceUrl
srcSet
}
}
breadcrumbs {
url
text
}
}
}
}
}
categories(first: 10) {
edges {
node {
id
seo {
canonical
title
metaDesc
focuskw
metaRobotsNoindex
metaRobotsNofollow
opengraphAuthor
opengraphDescription
opengraphTitle
opengraphDescription
opengraphImage {
altText
sourceUrl
srcSet
}
twitterTitle
twitterDescription
twitterImage {
altText
sourceUrl
srcSet
}
breadcrumbs {
url
text
}
}
name
}
}
}
}
```
```

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

Expand Down Expand Up @@ -159,12 +209,6 @@ To query for the site configuration data you can query from the root.
}
```

## V3 breaking change.

Image urls are now returned as `mediaItem` type.

This applies to `twitterImage` and `opengraphImage`

## Notes

This can be used in production, however it is still under active development.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
}],
"require": {
"wp-graphql/wp-graphql": ">=0.3.8",
"yoast/wordpress-seo": ">=12.3"
"yoast/wordpress-seo": ">=14.0"
}
}
117 changes: 68 additions & 49 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.3.0
* Version: 4.0.1
*
* @package WP_Graphql_YOAST_SEO
*/
Expand All @@ -19,6 +19,27 @@
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;

function get_og_image($images)
{

if (empty($images)) {
return;
}

$image = reset($images);

if (empty($image)) {
return;
}


if (!isset($image['url'])) {
return;
}

return attachment_url_to_postid($image['url']);
}

add_action('graphql_register_types', function () {
$post_types = \WPGraphQL::get_allowed_post_types();
$taxonomies = \WPGraphQL::get_allowed_taxonomies();
Expand Down Expand Up @@ -50,12 +71,24 @@
'metaRobotsNoindex' => ['type' => 'String'],
'metaRobotsNofollow' => ['type' => 'String'],
'opengraphTitle' => ['type' => 'String'],
'opengraphType' => ['type' => 'String'],
'opengraphAuthor' => ['type' => 'String'],
'opengraphPublisher' => ['type' => 'String'],
'opengraphDescription' => ['type' => 'String'],
'opengraphImage' => ['type' => 'MediaItem'],
'twitterTitle' => ['type' => 'String'],
'twitterDescription' => ['type' => 'String'],
'twitterImage' => ['type' => 'MediaItem'],
'canonical' => ['type' => 'String']
'canonical' => ['type' => 'String'],
'breadcrumbs' => ['type' => ['list_of' => 'SEOPostTypeBreadcrumbs']],
]
]);

register_graphql_object_type('SEOPostTypeBreadcrumbs', [
'fields' => [
'url' => ['type' => 'String'],
'text' => ['type' => 'String'],

]
]);

Expand Down Expand Up @@ -231,36 +264,32 @@
'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
$wpseo_frontend = WPSEO_Frontend::get_instance();
$wpseo_frontend->reset();

// Base array
$seo = array();

query_posts(array(
'p' => $post->ID,
'post_type' => 'any'
));
the_post();

// https://developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/
$robots = YoastSEO()->meta->for_post($post->ID)->robots;
// Get data
$seo = array(
'title' => trim($wpseo_frontend->title($post->post_title)),
'metaDesc' => trim($wpseo_frontend->metadesc(false)),
'title' => trim(YoastSEO()->meta->for_post($post->ID)->title),
'metaDesc' => trim(YoastSEO()->meta->for_post($post->ID)->description),
'focuskw' => trim(get_post_meta($post->ID, '_yoast_wpseo_focuskw', true)),
'metaKeywords' => trim(get_post_meta($post->ID, '_yoast_wpseo_metakeywords', true)),
'metaRobotsNoindex' => trim(get_post_meta($post->ID, '_yoast_wpseo_meta-robots-noindex', true)),
'metaRobotsNofollow' => trim(get_post_meta($post->ID, '_yoast_wpseo_meta-robots-nofollow', true)),
'opengraphTitle' => trim(get_post_meta($post->ID, '_yoast_wpseo_opengraph-title', true)),
'opengraphDescription' => trim(get_post_meta($post->ID, '_yoast_wpseo_opengraph-description', true)),
'opengraphImage' => DataSource::resolve_post_object(get_post_meta($post->ID, '_yoast_wpseo_opengraph-image-id', true), $context),
'twitterTitle' => trim(get_post_meta($post->ID, '_yoast_wpseo_twitter-title', true)),
'twitterDescription' => trim(get_post_meta($post->ID, '_yoast_wpseo_twitter-description', true)),
'twitterImage' => DataSource::resolve_post_object(get_post_meta($post->ID, '_yoast_wpseo_twitter-image-id', true), $context),
'canonical' => trim(get_post_meta($post->ID, '_yoast_wpseo_canonical', true))
'metaRobotsNoindex' => $robots['index'],
'metaRobotsNofollow' => $robots['follow'],
'opengraphTitle' => trim(YoastSEO()->meta->for_post($post->ID)->open_graph_title),
'opengraphType' => trim(YoastSEO()->meta->for_post($post->ID)->open_graph_type),
'opengraphAuthor' => trim(YoastSEO()->meta->for_post($post->ID)->open_graph_article_author),
'opengraphPublisher' => trim(YoastSEO()->meta->for_post($post->ID)->open_graph_article_publisher),
'opengraphDescription' => trim(YoastSEO()->meta->for_post($post->ID)->open_graph_description),
'opengraphImage' => DataSource::resolve_post_object(get_og_image(YoastSEO()->meta->for_post($post->ID)->open_graph_images), $context),
'twitterCardType' => trim(YoastSEO()->meta->for_post($post->ID)->twitter_card),
'twitterTitle' => trim(YoastSEO()->meta->for_post($post->ID)->twitter_title),
'twitterDescription' => trim(YoastSEO()->meta->for_post($post->ID)->twitter_description),
'twitterImage' => DataSource::resolve_post_object(attachment_url_to_postid(YoastSEO()->meta->for_post($post->ID)->twitter_image), $context),
'canonical' => trim(YoastSEO()->meta->for_post($post->ID)->canonical),
'breadcrumbs' => YoastSEO()->meta->for_post($post->ID)->breadcrumbs
);
wp_reset_query();

return !empty($seo) ? $seo : null;
}
Expand All @@ -286,39 +315,29 @@

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

query_posts(
array(
'tax_query' => array(
array(
'taxonomy' => $term_obj->taxonomy,
'terms' => $term_obj->term_id,
'field' => 'term_id'
)
)
)
);
the_post();

$wpseo_frontend = WPSEO_Frontend::get_instance();
$wpseo_frontend->reset();

$meta = WPSEO_Taxonomy_Meta::get_term_meta((int) $term_obj->term_id, $term_obj->taxonomy);
$robots = YoastSEO()->meta->for_term($term->term_id)->robots;

// Get data
$seo = array(
'title' => trim($wpseo_frontend->title($post->post_title)),
'metaDesc' => trim($wpseo_frontend->metadesc(false)),
'title' => trim(YoastSEO()->meta->for_term($term->term_id)->title),
'metaDesc' => trim(YoastSEO()->meta->for_term($term->term_id)->description),
'focuskw' => trim($meta['wpseo_focuskw']),
'metaKeywords' => trim($meta['wpseo_metakeywords']),
'metaRobotsNoindex' => trim($meta['wpseo_meta-robots-noindex']),
'metaRobotsNofollow' => trim($meta['wpseo_meta-robots-nofollow']),
'opengraphTitle' => trim($meta['wpseo_opengraph-title']),
'opengraphDescription' => trim($meta['wpseo_opengraph-description']),
'metaRobotsNoindex' => $robots['index'],
'metaRobotsNofollow' => $robots['follow'],
'opengraphTitle' => trim(YoastSEO()->meta->for_term($term->term_id)->open_graph_title),
'opengraphType' => trim(YoastSEO()->meta->for_term($term->term_id)->open_graph_type),
'opengraphAuthor' => trim(YoastSEO()->meta->for_term($term->term_id)->open_graph_article_author),
'opengraphPublisher' => trim(YoastSEO()->meta->for_term($term->term_id)->open_graph_article_publisher),
'opengraphDescription' => trim(YoastSEO()->meta->for_term($term->term_id)->open_graph_description),
'opengraphImage' => DataSource::resolve_post_object($meta['wpseo_opengraph-image-id'], $context),
'twitterTitle' => trim($meta['wpseo_twitter-title']),
'twitterDescription' => trim($meta['wpseo_twitter-description']),
'twitterCardType' => trim(YoastSEO()->meta->for_term($term->term_id)->twitter_card),
'twitterTitle' => trim(YoastSEO()->meta->for_term($term->term_id)->twitter_title),
'twitterDescription' => trim(YoastSEO()->meta->for_term($term->term_id)->twitter_description),
'twitterImage' => DataSource::resolve_post_object($meta['wpseo_twitter-image-id'], $context),
'canonical' => trim($meta['canonical'])
'canonical' => trim($meta['canonical']),
'breadcrumbs' => YoastSEO()->meta->for_term($term->term_id)->breadcrumbs
);
wp_reset_query();

Expand Down

0 comments on commit 94d913a

Please sign in to comment.