Created
December 13, 2022 20:05
-
-
Save joshuafredrickson/ed40356f211621997c4342460b020f76 to your computer and use it in GitHub Desktop.
The SEO Framework REST API endpoints for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* REST API | |
* The SEO Framework | |
* | |
* @package scout_rest_theme | |
* | |
* List of title methods | |
* @link https://github.com/sybrew/the-seo-framework/issues/202#issuecomment-412781194 | |
* | |
* Render class | |
* @link https://github.com/sybrew/the-seo-framework/blob/master/inc/classes/render.class.php | |
*/ | |
namespace Scout\REST\SEO; | |
use WP_Query; | |
/** | |
* Make SEO available to REST | |
* Appends to page/post requests | |
*/ | |
function init($data, $post, $context) | |
{ | |
// Is TSF activated? | |
if (! defined('THE_SEO_FRAMEWORK_PRESENT')) { | |
return $data; | |
} | |
query_posts([ | |
'p' => $post->ID, | |
'post_type' => $post->post_type, | |
]); | |
$args = [ | |
'id' => $post->ID, | |
'post_id' => $post->ID, | |
]; | |
$tsf = \the_seo_framework(); | |
$url = $tsf->create_canonical_url($args); | |
$published_time = $tsf->gmt2date($tsf->get_timestamp_format(), $post->post_date_gmt); | |
$modified_time = $post->post_modified_gmt !== '0000-00-00 00:00:00' ? | |
$tsf->gmt2date($tsf->get_timestamp_format(), $post->post_modified_gmt) | |
: | |
false; | |
/** | |
* Use header_image as a priority, fallback to TSF | |
*/ | |
$header_image = get_field('scout_header_image', $post->ID); | |
if ($header_image && \array_key_exists('url', $header_image)) { | |
$og_image_url = $header_image['url']; | |
} else { | |
$og_image_url = $tsf->get_social_image($args, true); | |
if (substr_count($og_image_url, ':') > 1) { | |
$og_image_url = str_replace(\wp_upload_dir()['baseurl'], '', $tsf->get_social_image($args, true)); | |
} | |
} | |
$data->data['seo'] = [ | |
'title' => $tsf->get_title($args), | |
'metatags' => [ | |
[ | |
'tag' => 'link', | |
'property_name' => 'rel', | |
'property_value' => 'canonical', | |
'attribute_name' => 'href', | |
'attribute_value' => $url, | |
], | |
// 'ld_json' => $tsf->ld_json(), // TODO | |
// 'robots' => \Scout\REST\SEO\robots(), // TODO | |
[ | |
'tag' => 'meta', | |
'property_name' => 'name', | |
'property_value' => 'description', | |
'attribute_name' => 'content', | |
'attribute_value' => \esc_attr($tsf->get_description($args)), | |
], | |
// 'og:site_name' => $tsf->og_sitename(), | |
[ | |
'tag' => 'meta', | |
'property_name' => 'name', | |
'property_value' => 'og:url', | |
'attribute_name' => 'content', | |
'attribute_value' => $url, | |
], | |
[ | |
'tag' => 'meta', | |
'property_name' => 'name', | |
'property_value' => 'og:title', | |
'attribute_name' => 'content', | |
'attribute_value' => \esc_attr($tsf->get_open_graph_title($args)), | |
], | |
[ | |
'tag' => 'meta', | |
'property_name' => 'name', | |
'property_value' => 'og:description', | |
'attribute_name' => 'content', | |
'attribute_value' => \esc_attr($tsf->get_open_graph_description($args)), | |
], | |
[ | |
'tag' => 'meta', | |
'property_name' => 'name', | |
'property_value' => 'og:image', | |
'attribute_name' => 'content', | |
'attribute_value' => $og_image_url, | |
], | |
[ | |
'tag' => 'meta', | |
'property_name' => 'name', | |
'property_value' => 'twitter:image', | |
'attribute_name' => 'content', | |
'attribute_value' => $og_image_url, | |
], | |
// 'og:type' => $tsf->og_type(), // TODO | |
// 'article:author' => $tsf->facebook_author(), | |
// 'article:publisher' => $tsf->facebook_publisher(), | |
// 'article:published_time'=> '<meta property="article:published_time" content="' . \esc_attr( $published_time ) . '" />' . "\r\n", | |
// 'article:modified_time' => '<meta property="article:modified_time" content="' . \esc_attr( $modified_time ) . '" />' . "\r\n" . | |
// ( | |
// $modified_time ? | |
// '<meta property="og:updated_time" content="' . \esc_attr( $modified_time ) . '" />' . "\r\n" | |
// : | |
// '' | |
// ), | |
// 'fb:app_id' => $tsf->facebook_app_id(), | |
// 'twitter:site' => $tsf->twitter_site(), | |
// 'twitter:creator' => $tsf->twitter_creator(), | |
// 'twitter:title' => '<meta name="twitter:title" content="' . \esc_attr( $tsf->get_twitter_title($args) ) . '" />' . "\r\n", | |
// 'twitter:card' => $tsf->twitter_card(), | |
// 'twitter:description' => '<meta name="twitter:description" content="' . \esc_attr( $tsf->get_twitter_description($args) ) . '" />' . "\r\n", | |
], | |
]; | |
wp_reset_query(); | |
return $data; | |
} | |
/** | |
* Make Yoast available to REST | |
* Appends to taxonomy requests | |
* | |
* TODO: Change to TSF | |
*/ | |
function taxonomy_init($data, $tax, $context) | |
{ | |
// Is Yoast activated? | |
if (! method_exists('WPSEO_Frontend', 'get_instance')) { | |
return $data; | |
} | |
include_once __DIR__ . '/classes/yoast-class-frontend.php'; | |
wp_reset_query(); | |
$wpseo_frontend = \WPSEO_Frontend_To_REST_API::get_instance(); | |
$wpseo_frontend->reset(); | |
$yoast_meta = [ | |
'metas' => [ | |
'title' => get_post_meta($tax->term_id, '_yoast_wpseo_title', true), | |
'metadesc' => get_post_meta($tax->term_id, '_yoast_wpseo_metadesc', true), | |
'meta-robots-noindex' => get_post_meta($tax->term_id, '_yoast_wpseo_meta-robots-noindex', true), | |
'meta-robots-nofollow' => get_post_meta($tax->term_id, '_yoast_wpseo_meta-robots-nofollow', true), | |
'meta-robots-adv' => get_post_meta($tax->term_id, '_yoast_wpseo_meta-robots-adv', true), | |
'canonical' => get_post_meta($tax->term_id, '_yoast_wpseo_canonical', true), | |
'redirect' => get_post_meta($tax->term_id, '_yoast_wpseo_redirect', true), | |
'opengraph-title' => get_post_meta($tax->term_id, '_yoast_wpseo_opengraph-title', true), | |
'opengraph-description' => get_post_meta($tax->term_id, '_yoast_wpseo_opengraph-description', true), | |
'opengraph-image' => get_post_meta($tax->term_id, '_yoast_wpseo_opengraph-image', true), | |
'twitter-title' => get_post_meta($tax->term_id, '_yoast_wpseo_twitter-title', true), | |
'twitter-description' => get_post_meta($tax->term_id, '_yoast_wpseo_twitter-description', true), | |
'twitter-image' => get_post_meta($tax->term_id, '_yoast_wpseo_twitter-image', true) | |
], | |
'title' => $tax->name . ' - ' . $wpseo_frontend->public_get_post_type_archive_title(), | |
'canonical' => $wpseo_frontend->canonical(false), | |
'metatags' => [ | |
// 'description' => $wpseo_frontend->metadesc( false ) ? $wpseo_frontend->metadesc( false ) : $wpseo_opengraph->description( false ), | |
'robots' => $wpseo_frontend->robots(), | |
'og:title' => $tax->name . ' - ' . $wpseo_frontend->public_get_post_type_archive_title(), | |
// 'og:description' => $wpseo_opengraph->description( false ), | |
'twitter:title' => $tax->name . ' - ' . $wpseo_frontend->public_get_post_type_archive_title(), | |
// 'twitter:description' => $wpseo_twitter->description(), | |
// 'twitter:image' => $wpseo_twitter->image() | |
], | |
]; | |
$data->data['yoast'] = $yoast_meta; | |
return $data; | |
} | |
/** | |
* Pulled from The SEO Framework | |
* Modified to always return tags. | |
* | |
* Renders Robots meta tags. | |
* Returns early if blog isn't public. WordPress Core will then output the meta tags. | |
* | |
* @since 2.0.0 | |
* | |
* @return string The Robots meta tags. | |
*/ | |
function robots() | |
{ | |
//* Don't do anything if the blog isn't set to public. | |
// if ( false === $this->is_blog_public() ) | |
// return ''; | |
$tsf = \the_seo_framework(); | |
/** | |
* @since 2.6.0 | |
* @param array $meta The robots meta. | |
* @param int $id The current post or term ID. | |
*/ | |
$meta = (array) \apply_filters_ref_array( | |
'the_seo_framework_robots_meta', | |
[ | |
$tsf->robots_meta(), | |
$tsf->get_the_real_ID(), | |
] | |
); | |
if (empty($meta)) { | |
return ''; | |
} | |
return sprintf('<meta name="robots" content="%s" />' . PHP_EOL, implode(',', $meta)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment