Skip to content

Commit

Permalink
Merge pull request #5 from niels-garve/master
Browse files Browse the repository at this point in the history
A bunch of contributions
  • Loading branch information
ChazUK authored Apr 10, 2018
2 parents 2b5ee9b + b685d41 commit bd801c2
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
26 changes: 23 additions & 3 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@

## [WP API Yoast SEO](https://wordpress.org/plugins/wp-api-yoast-meta/)
# Yoast to REST API - WordPress plugin

![Yoast](Yoast_Logo_Small_RGB.png)

For use with the new [WP REST API](http://v2.wp-api.org/)

Returns Yoast post or page metadata in a normal post or page request. Stores the metadata in the `yoast_meta` field of the returned data.

Supports pages, posts and any *public* custom post type
```
{
id: 123,
...
yoast_meta: {
yoast_wpseo_title: "Testy Test | My WordPress site",
yoast_wpseo_metadesc: "My description",
yoast_wpseo_canonical: "http://my-wordpress-site.test/testy-test"
}
}
```

Supports pages, posts, categories, tags and any *public* custom post types

Currently fetching:

- `yoast_wpseo_title`
- `yoast_wpseo_metadesc`
- `yoast_wpseo_canonical`

Currently updating:

- `yoast_wpseo_focuskw`
- `yoast_wpseo_title`
- `yoast_wpseo_metadesc`
Expand All @@ -26,4 +46,4 @@ Currently fetching:
- `yoast_wpseo_twitter-description`
- `yoast_wpseo_twitter-image`

Thanks to [jmfurlott/wp-api-yoast](https://github.com/jmfurlott/wp-api-yoast)
Thanks to Pablo Postigo, Tedy Warsitha and Charlie Francis for amazing contributions!
Binary file added Yoast_Logo_Small_RGB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions classes/class-wpseo-frontend-to-rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @package WPSEO\Frontend
*/

/**
* Main frontend class for Yoast SEO, responsible for the SEO output as well as removing
* default WordPress output.
*/
class WPSEO_Frontend_To_REST_API extends WPSEO_Frontend {
/**
* Get the singleton instance of this class.
*
* @return WPSEO_Frontend
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}

return self::$instance;
}
}
129 changes: 103 additions & 26 deletions plugin.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

add_action( 'plugins_loaded', 'WPAPIYoast_init' );

/**
* Plugin Name: WP REST API Yoast SEO
* Plugin Name: Yoast to REST API
* Description: Adds Yoast fields to page and post metadata to WP REST API responses
* Author: Charlie Francis, Tedy Warsitha
* Author URI: https://github.com/ChazUK
* Version: 1.1.0
* Plugin URI: https://github.com/ChazUK/wp-api-yoast-seo
* Author: Niels Garve, Pablo Postigo, Tedy Warsitha, Charlie Francis
* Author URI: https://github.com/niels-garve
* Version: 1.4.1
* Plugin URI: https://github.com/niels-garve/yoast-to-rest-api
*/
class WPAPIYoastMeta {
class Yoast_To_REST_API {

protected $keys = array(
'yoast_wpseo_focuskw',
Expand Down Expand Up @@ -54,11 +56,32 @@ function add_yoast_data() {
)
);

// Category
register_rest_field( 'category',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_category' ),
'update_callback' => null,
'schema' => null,
)
);

// Tag
register_rest_field( 'tag',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_tag' ),
'update_callback' => null,
'schema' => null,
)
);

// Public custom post types
$types = get_post_types( array(
'public' => true,
'_builtin' => false
) );

foreach ( $types as $key => $type ) {
register_rest_field( $type,
'yoast_meta',
Expand All @@ -73,6 +96,7 @@ function add_yoast_data() {

/**
* Updates post meta with values from post/put request.
*
* @param array $value
* @param object $data
* @param string $field_name
Expand All @@ -91,29 +115,82 @@ function wp_api_update_yoast( $value, $data, $field_name ) {
return $this->wp_api_encode_yoast( $data->ID, null, null );
}

function wp_api_encode_yoast( $post, $field_name, $request ) {
$yoastMeta = array(
'yoast_wpseo_focuskw' => get_post_meta( $post['id'], '_yoast_wpseo_focuskw', true ),
'yoast_wpseo_title' => get_post_meta( $post['id'], '_yoast_wpseo_title', true ),
'yoast_wpseo_metadesc' => get_post_meta( $post['id'], '_yoast_wpseo_metadesc', true ),
'yoast_wpseo_linkdex' => get_post_meta( $post['id'], '_yoast_wpseo_linkdex', true ),
'yoast_wpseo_metakeywords' => get_post_meta( $post['id'], '_yoast_wpseo_metakeywords', true ),
'yoast_wpseo_meta-robots-noindex' => get_post_meta( $post['id'], '_yoast_wpseo_meta-robots-noindex', true ),
'yoast_wpseo_meta-robots-nofollow' => get_post_meta( $post['id'], '_yoast_wpseo_meta-robots-nofollow', true ),
'yoast_wpseo_meta-robots-adv' => get_post_meta( $post['id'], '_yoast_wpseo_meta-robots-adv', true ),
'yoast_wpseo_canonical' => get_post_meta( $post['id'], '_yoast_wpseo_canonical', true ),
'yoast_wpseo_redirect' => get_post_meta( $post['id'], '_yoast_wpseo_redirect', true ),
'yoast_wpseo_opengraph-title' => get_post_meta( $post['id'], '_yoast_wpseo_opengraph-title', true ),
'yoast_wpseo_opengraph-description' => get_post_meta( $post['id'], '_yoast_wpseo_opengraph-description', true ),
'yoast_wpseo_opengraph-image' => get_post_meta( $post['id'], '_yoast_wpseo_opengraph-image', true ),
'yoast_wpseo_twitter-title' => get_post_meta( $post['id'], '_yoast_wpseo_twitter-title', true ),
'yoast_wpseo_twitter-description' => get_post_meta( $post['id'], '_yoast_wpseo_twitter-description', true ),
'yoast_wpseo_twitter-image' => get_post_meta( $post['id'], '_yoast_wpseo_twitter-image', true )
function wp_api_encode_yoast( $p, $field_name, $request ) {
$wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance();
$wpseo_frontend->reset();

query_posts( array(
'p' => $p['id'], // ID of a page, post, or custom type
'post_type' => 'any'
) );

the_post();

$yoast_meta = array(
'yoast_wpseo_title' => $wpseo_frontend->get_content_title(),
'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ),
'yoast_wpseo_canonical' => $wpseo_frontend->canonical( false ),
);

wp_reset_query();

return (array) $yoast_meta;
}

private function wp_api_encode_taxonomy() {
$wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance();
$wpseo_frontend->reset();

$yoast_meta = array(
'yoast_wpseo_title' => $wpseo_frontend->get_taxonomy_title(),
'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ),
);

return (array) $yoastMeta;
return (array) $yoast_meta;
}

function wp_api_encode_yoast_category( $category ) {
query_posts( array(
'cat' => $category['id'],
) );

the_post();

$res = $this->wp_api_encode_taxonomy();

wp_reset_query();

return $res;
}

function wp_api_encode_yoast_tag( $tag ) {
query_posts( array(
'tag_id' => $tag['id'],
) );

the_post();

$res = $this->wp_api_encode_taxonomy();

wp_reset_query();

return $res;
}
}

function WPAPIYoast_init() {
if ( class_exists( 'WPSEO_Frontend' ) ) {
include __DIR__ . '/classes/class-wpseo-frontend-to-rest-api.php';

$yoast_To_REST_API = new Yoast_To_REST_API();
} else {
add_action( 'admin_notices', 'wpseo_not_loaded' );
}
}

$WPAPIYoastMeta = new WPAPIYoastMeta();
function wpseo_not_loaded() {
printf(
'<div class="error"><p>%s</p></div>',
__( '<b>Yoast to REST API</b> plugin not working because <b>Yoast SEO</b> plugin is not active.' )
);
}
86 changes: 75 additions & 11 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
=== WP API Yoast SEO ===
Contributors: ChazUK
=== Yoast to REST API ===
Contributors: Niels Garve, Pablo Postigo, Tedy Warsitha, Charlie Francis
Tags: yoast, wp-api, rest, seo
Requires at least: 4.4
Tested up to: 4.5.3
Tested up to: 4.9.1
Stable tag: trunk
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -11,21 +11,85 @@ Returns Yoast post or page metadata in a normal post or page request.

== Description ==

Returns Yoast post or page metadata in a normal post or page request. Stores the metadata in the yoast_meta field of the returned data.
Returns Yoast post or page metadata in a normal post or page request. Stores the metadata in the `yoast_meta` field of the returned data.

```
{
id: 123,
...
yoast_meta: {
yoast_wpseo_title: "Testy Test | My WordPress site",
yoast_wpseo_metadesc: "My description",
yoast_wpseo_canonical: "http://my-wordpress-site.test/testy-test"
}
}
```

Supports pages, posts, categories, tags and any *public* custom post types

Currently fetching:

- `yoast_wpseo_title`
- `yoast_wpseo_metadesc`
- `yoast_wpseo_canonical`

Currently updating:

- `yoast_wpseo_focuskw`
- `yoast_wpseo_title`
- `yoast_wpseo_metadesc`
- `yoast_wpseo_linkdex`
- `yoast_wpseo_metakeywords`
- `yoast_wpseo_meta-robots-noindex`
- `yoast_wpseo_meta-robots-nofollow`
- `yoast_wpseo_meta-robots-adv`
- `yoast_wpseo_canonical`
- `yoast_wpseo_redirect`
- `yoast_wpseo_opengraph-title`
- `yoast_wpseo_opengraph-description`
- `yoast_wpseo_opengraph-image`
- `yoast_wpseo_twitter-title`
- `yoast_wpseo_twitter-description`
- `yoast_wpseo_twitter-image`

Thanks to Pablo Postigo, Tedy Warsitha and Charlie Francis for amazing contributions!

== Installation ==

Upload the plugin files to the /wp-content/plugins/wp-api-yoast-meta directory, or install the plugin through the WordPress plugins screen directly.
Activate the plugin through the 'Plugins' screen in WordPress
...
Profit
1. Upload the plugin files to the `/wp-content/plugins/yoast-to-rest-api` directory, or install the plugin through the 'Plugins' menu in WordPress
2. Activate the plugin through the 'Plugins' menu in WordPress

== Changelog ==

= 1.4.1 =

- Documentation

= 1.4.0 =

- Fixed broken meta descriptions in REST collections
- PHP Coding Standards (https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#php)

= 1.4.0-alpha =

- Fixed retrieval of the meta description
- Generalized Worona PWA dependencies

= 1.3 =

- Adapted to the needs of Worona PWA

= 1.2 =

- Changed `register_api_field` to `register_rest_field` as it's depracated
- Changed the metadata name to `yoast` rather than `yoast_meta`
- Removed the `yoast_wpseo_` prefix from the returned meta as it seems undeed

= 1.1 =

Using Class instead of plain function
Added output to public custom post types
- Using Class instead of plain function
- Added output to public custom post types

= 1.0 =

Launched!
- Launched!

0 comments on commit bd801c2

Please sign in to comment.