Skip to content

Commit

Permalink
Merge pull request WebDevStudios#4 from WebDevStudios/feature/expose-…
Browse files Browse the repository at this point in the history
…new-social-meta

Feature/expose new social meta
  • Loading branch information
phatsk committed Jun 14, 2018
2 parents fdec0bc + 7dd252a commit b6c591c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 37 deletions.
43 changes: 35 additions & 8 deletions README.md
Expand Up @@ -26,24 +26,51 @@ Currently fetching:
- `yoast_wpseo_title`
- `yoast_wpseo_metadesc`
- `yoast_wpseo_canonical`
- `yoast_wpseo_facebook_title` aka `yoast_wpseo_opengraph-title`
- `yoast_wpseo_facebook_description` aka `yoast_wpseo_opengraph-description`
- `yoast_wpseo_facebook_type`
- `yoast_wpseo_facebook_image` aka `yoast_wpseo_opengraph-image`
- `yoast_wpseo_twitter_title`
- `yoast_wpseo_twitter_description`
- `yoast_wpseo_twitter_image`
- `yoast_wpseo_social_url`
- `yoast_wpseo_website_name`
- `yoast_wpseo_company_or_person`
- `yoast_wpseo_person_name`
- `yoast_wpseo_company_name`
- `yoast_wpseo_company_logo`
- `yoast_wpseo_website_name`
- `yoast_wpseo_social_defaults`

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`

`yoast_wpseo_defaults` includes the following:
- `facebook_site`
- `instagram_url`
- `linkedin_url`
- `myspace_url`
- `og_default_image`
- `og_frontpage_title`
- `og_frontpage_desc`
- `og_frontpage_image`
- `opengraph`
- `pinterest_url`
- `pinterestverify`
- `plus-publisher`
- `twitter`
- `twitter_site`,
- `twitter_card_type`
- `youtube_url`
- `google_plus_url`
- `fbadminapp`

Thanks to Pablo Postigo, Tedy Warsitha and Charlie Francis for amazing contributions!
80 changes: 51 additions & 29 deletions plugin.php
@@ -1,7 +1,4 @@
<?php

add_action( 'plugins_loaded', 'WPAPIYoast_init' );

<?php // @codingStandardsIgnoreStart
/**
* Plugin Name: Yoast to REST API
* Description: Adds Yoast fields to page and post metadata to WP REST API responses
Expand All @@ -28,7 +25,7 @@ class Yoast_To_REST_API {
'yoast_wpseo_opengraph-image',
'yoast_wpseo_twitter-title',
'yoast_wpseo_twitter-description',
'yoast_wpseo_twitter-image'
'yoast_wpseo_twitter-image',
);

function __construct() {
Expand All @@ -37,7 +34,8 @@ function __construct() {

function add_yoast_data() {
// Posts
register_rest_field( 'post',
register_rest_field(
'post',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
Expand All @@ -47,7 +45,8 @@ function add_yoast_data() {
);

// Pages
register_rest_field( 'page',
register_rest_field(
'page',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
Expand All @@ -57,7 +56,8 @@ function add_yoast_data() {
);

// Category
register_rest_field( 'category',
register_rest_field(
'category',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_category' ),
Expand All @@ -67,7 +67,8 @@ function add_yoast_data() {
);

// Tag
register_rest_field( 'tag',
register_rest_field(
'tag',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast_tag' ),
Expand All @@ -77,13 +78,16 @@ function add_yoast_data() {
);

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

foreach ( $types as $key => $type ) {
register_rest_field( $type,
register_rest_field(
$type,
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
Expand Down Expand Up @@ -119,17 +123,36 @@ 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'
) );
query_posts(
array(
'p' => $p['id'], // ID of a page, post, or custom type
'post_type' => 'any',
)
);

the_post();

// title options — defaults.
$yoast_titles = get_option( 'wpseo_titles' );

$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 ),
'yoast_wpseo_title' => $wpseo_frontend->get_content_title() ?? '',
'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ) ?? '',
'yoast_wpseo_canonical' => $wpseo_frontend->canonical( false ) ?? '',
'yoast_wpseo_facebook_title' => get_post_meta( $p['id'], '_yoast_wpseo_opengraph-title', true ),
'yoast_wpseo_facebook_description' => get_post_meta( $p['id'], '_yoast_wpseo_opengraph-description', true ),
'yoast_wpseo_facebook_type' => $p['type'] ?? '',
'yoast_wpseo_facebook_image' => get_post_meta( $p['id'], '_yoast_wpseo_opengraph-image', true ),
'yoast_wpseo_twitter_title' => get_post_meta( $p['id'], '_yoast_wpseo_twitter-title', true ),
'yoast_wpseo_twitter_description' => get_post_meta( $p['id'], '_yoast_wpseo_twitter-description', true ),
'yoast_wpseo_twitter_image' => get_post_meta( $p['id'], '_yoast_wpseo_twitter-image', true ),
'yoast_wpseo_social_url' => get_permalink( $p['id'] ) ?? '',
'yoast_wpseo_company_or_person' => $yoast_titles['company_or_person'] ?? '',
'yoast_wpseo_person_name' => $yoast_titles['person_name'] ?? '',
'yoast_wpseo_company_name' => $yoast_titles['company_name'] ?? '',
'yoast_wpseo_company_logo' => $yoast_titles['company_logo'] ?? '',
'yoast_wpseo_website_name' => $yoast_titles['website_name'] ?? '',
'yoast_wpseo_social_defaults' => get_option( 'wpseo_social' ),
);

/**
Expand All @@ -153,8 +176,9 @@ private function wp_api_encode_taxonomy() {
$wpseo_frontend->reset();

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

/**
Expand All @@ -170,9 +194,7 @@ private function wp_api_encode_taxonomy() {
}

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

the_post();

Expand All @@ -184,9 +206,7 @@ function wp_api_encode_yoast_category( $category ) {
}

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

the_post();

Expand Down Expand Up @@ -214,3 +234,5 @@ function wpseo_not_loaded() {
__( '<b>Yoast to REST API</b> plugin not working because <b>Yoast SEO</b> plugin is not active.' )
);
}

add_action( 'plugins_loaded', 'WPAPIYoast_init' );

0 comments on commit b6c591c

Please sign in to comment.