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

Related Posts: add posts to the WP REST API Post Response. #3425

Merged
merged 3 commits into from
Oct 21, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions modules/related-posts/jetpack-related-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function __construct( $blog_id_local, $blog_id_wpcom ) {
if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
jetpack_require_lib( 'class.media-summary' );
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function register_rest_field is not in core but in WP-API, this should be wrapped in a conditional checking if the function exists to avoid breaking all REST API environment:

// Add Related Posts to the REST API Post response.
if ( function_exists( 'register_rest_field' ) ) {
    add_action( 'rest_api_init',  array( $this, 'rest_register_related_posts' ) );
}

Otherwise, when WP-API is not installed or active, all endpoints (included ours like wp-json/jetpack/v4/module/photon) fail with a fatal error:

Fatal error: Call to undefined function register_rest_field() in /home/.../public_html/wp-content/plugins/jetpack/modules/related-posts/jetpack-related-posts.php on line 1307

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Somehow I thought this was already in core. I made the change you've mentioned (and will update my own plugins to fix that problem).

// Add Related Posts to the REST API Post response.
add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) );
}

/**
Expand Down Expand Up @@ -1282,6 +1285,49 @@ protected function _allow_feature_toggle() {
}
return $this->_allow_feature_toggle;
}

/**
* ===================================================
* FUNCTIONS EXPOSING RELATED POSTS IN THE WP REST API
* ===================================================
*/

/**
* Add Related Posts to the REST API Post response.
*
* @since 4.4.0
*
* @action rest_api_init
* @uses register_rest_field, self::rest_get_related_posts
* @return null
*/
public function rest_register_related_posts() {
register_rest_field( 'post',
'jetpack-related-posts',
array(
'get_callback' => array( $this, 'rest_get_related_posts' ),
'update_callback' => null,
'schema' => null,
)
);
}

/**
* Build an array of Related Posts.
*
* @since 4.4.0
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @uses self::get_for_post_id
*
* @return array
*/
public function rest_get_related_posts( $object, $field_name, $request ) {
return $this->get_for_post_id( $object['id'], array() );
}
}

class Jetpack_RelatedPosts_Raw extends Jetpack_RelatedPosts {
Expand Down