Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add primary taxonomies, composer #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "mvanmeerbeck/wp-api-yoast-meta",
"type": "wordpress-plugin",
"authors": [
{
"name": "Maxime Vanmeerbeck",
"email": "[email protected]"
}
],
"require": {}
}
203 changes: 100 additions & 103 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,109 +10,106 @@
*/
class WPAPIYoastMeta {

protected $keys = array(
'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'
);

function __construct() {
add_action( 'rest_api_init', array( $this, 'add_yoast_data' ) );
}

function add_yoast_data() {
// Posts
register_api_field( 'post',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);

// Pages
register_api_field( 'page',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);

// Public custom post types
$types = get_post_types( array(
'public' => true,
'_builtin' => false
) );
foreach ( $types as $key => $type ) {
register_api_field( $type,
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
}
}

/**
* Updates post meta with values from post/put request.
* @param array $value
* @param object $data
* @param string $field_name
*
* @return array
*/
function wp_api_update_yoast( $value, $data, $field_name ) {

foreach ( $value as $k => $v ) {

if ( in_array( $k, $this->keys ) ) {
! empty( $k ) ? update_post_meta( $data->ID, '_' . $k, $v ) : null;
}
}

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 )
);

return (array) $yoastMeta;
}
protected $keys = array(
'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'
);

function __construct() {
add_action( 'rest_api_init', array( $this, 'add_yoast_data' ) );
}

function add_yoast_data() {
// Posts
register_api_field( 'post',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);

// Pages
register_api_field( 'page',
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);

// Public custom post types
$types = get_post_types( array(
'public' => true,
'_builtin' => false
) );
foreach ( $types as $key => $type ) {
register_api_field( $type,
'yoast_meta',
array(
'get_callback' => array( $this, 'wp_api_encode_yoast' ),
'update_callback' => array( $this, 'wp_api_update_yoast' ),
'schema' => null,
)
);
}
}

/**
* Updates post meta with values from post/put request.
* @param array $value
* @param object $data
* @param string $field_name
*
* @return array
*/
function wp_api_update_yoast( $value, $data, $field_name ) {

foreach ( $value as $k => $v ) {

if ( in_array( $k, $this->keys ) ) {
! empty( $k ) ? update_post_meta( $data->ID, '_' . $k, $v ) : null;
}
}

return $this->wp_api_encode_yoast( $data->ID, null, null );
}

function wp_api_encode_yoast( $post, $field_name, $request ) {

foreach (array_keys(get_taxonomies(['public' => true, 'hierarchical' => true])) as $taxonomy) {
$this->keys[] = 'yoast_wpseo_primary_' . $taxonomy;
}

$frontend = WPSEO_Frontend::get_instance();
$object = get_post($post['id']);

foreach ($this->keys as $key) {
if ('yoast_wpseo_title' === $key) {
$yoastMeta[$key] = $frontend->get_content_title($object);
} else {
$yoastMeta[$key] = get_post_meta($post['id'], '_' . $key, true);
}
}

return (array) $yoastMeta;
}

}

Expand Down