Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

WP REST API integration #68

Open
robertdevore opened this issue Dec 3, 2015 · 13 comments
Open

WP REST API integration #68

robertdevore opened this issue Dec 3, 2015 · 13 comments
Labels
4.0.0 Subtitles 4.0.0 Release Target enhancement needs testing

Comments

@robertdevore
Copy link

I've been doing some work with the WP REST API recently and noticed that the subtitles aren't in the data being shown for any of the content types (posts, pages, custom post types).

I've got some code that I'm currently using to add in the subtitles for each of the custom post types I'm working with, but I'm trying to find a way to elegantly write it so it loops through and automatically adds the subtitle to every content type. Doing this will allow it to work for any CPT that a website is using.

I figured I'd open this up at the very least to remind myself to get the code wrote and pushed for inclusion into the plugin. If anyone beats me to it, great. If not, I'm still working on it 👍

@philiparthurmoore
Copy link
Member

I'll leave this open. Cheers.

@philiparthurmoore
Copy link
Member

Any luck? I plan on revisiting this soon.

@robertdevore
Copy link
Author

I still want to take care of this. May take me a few days to get the free time, but I'll do my best to take care of it ASAP 👍

@grappler
Copy link

grappler commented Apr 6, 2018

@robertdevore Are you still planning on working on this?

I am looking into Gutenberg support and expect REST API support will be needed.

@robertdevore
Copy link
Author

@grappler I honestly haven't looked much into it, but in the next couple of weeks after I launch v2.0 of WP Dispensary, I'll have more time to put together some code for this 👍

@philiparthurmoore
Copy link
Member

I'll be adding this into the next release. Not sure about full-blown Gutenberg support but definitely REST API support, and then Gutenberg shortly after. The endpoint will probably just be at a location like https://subtitles.app/wp-json/wp/v2/posts/1241 with subtitle: { rendered: [subtitle] },. If you have any specific hiccups you'd like to preemptively avoid let me know.

@philiparthurmoore philiparthurmoore added the 4.0.0 Subtitles 4.0.0 Release Target label Apr 7, 2018
@philiparthurmoore
Copy link
Member

2018-04-07_20-24-40

@grappler
Copy link

grappler commented Apr 7, 2018

There are two ways of adding support

You can directly register the meta key which means it will get added to the meta section.

add_action( 'init', function() {
	register_meta(
		'post',
		self::SUBTITLE_META_KEY,
		[
			'type'              => 'string',
			'description'       => '',
			'single'            => true,
			'sanitize_callback' => [ &$this, 'sanitize_subtitle' ],
			'show_in_rest'      => true,
			'auth_callback'     => true,
		]
	);
} );

The second method is define a seperate section to display the subtitle. You can do that with

add_action( 'rest_api_init', function() {
	register_rest_field(
		'post',
		'subtitle',
		array(
			'get_callback' => function( $post ) {
				return get_post_meta( $post['id'], '_subtitle', true );
			},
			'schema'       => [
				'type'    => 'string',
				'context' => [ 'view' ],
			],
		)
	);
} );

Both snippets have been tested but I not sure if they are following the best practices.

@philiparthurmoore
Copy link
Member

philiparthurmoore commented Apr 11, 2018

Quick-and-dirty kind-of-works solution. This can be better. I'll work on DocBlocks + better code organization before release:

diff --git a/public/class-subtitles.php b/public/class-subtitles.php
index 80c35c7..5d00d84 100644
--- a/public/class-subtitles.php
+++ b/public/class-subtitles.php
@@ -202,6 +202,14 @@ if ( ! class_exists( 'Subtitles' ) ) {
 			 */
 			add_action( 'wp_head', array( &$this, 'subtitle_styling' ) );
 
+			/**
+			 * Enable REST API Integration
+			 *
+			 * @see add_action()
+			 * @since 4.0.0
+			 */
+			 add_action( 'rest_api_init', array( &$this, 'subtitle_register_rest_field' ) );
+
 			/**
 			 * Filter post titles to display subtitles properly.
 			 *
@@ -249,6 +257,28 @@ if ( ! class_exists( 'Subtitles' ) ) {
 			}
 		} // end method __construct
 
+		/**
+		 * Reveal a post or supported post type's subtitle to the REST API.
+		 *
+		 * @see https://github.com/wecobble/Subtitles/issues/68
+		 * @since 4.0.0
+		 */
+		public function subtitle_register_rest_field() {
+			global $post;
+
+			register_rest_field(
+				'post',
+				'subtitle',
+				array(
+					'get_callback' => function ( $post ) { return get_post_meta( $post['id'], '_subtitle', true ); },
+					'schema' => array(
+						'type' => 'string',
+						'context' => array( 'view' ),
+					),
+				)
+			);
+		}
+
 		/**
 		 * Make sure that Subtitles plays nice with WordPress SEO plugin by Yoast.
 		 *

2018-04-11_23-36-46

@philiparthurmoore
Copy link
Member

Issues:

  1. Posts are not the only post type supported. Pages, posts, Jetpack custom post types, and anything that someone manually declared support for has subtitles support.
  2. DocBlocks need improvement.
  3. I do not like putting functions inside of callbacks directly. I'll make a helper function or adjust get_the_subtitle to make this work better.

@grappler
Copy link

Looks good. Though global $post; should not be needed.

@philiparthurmoore
Copy link
Member

philiparthurmoore commented Apr 12, 2018

@grappler It should not be needed but it is. Without it, NULL is returned. In fact, I should only need to call get_the_subtitle, which already relies on get_post. I'll figure it out but no global wasn't working at all. Will do some digging.

@philiparthurmoore
Copy link
Member

Guess I should probably take care of this now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4.0.0 Subtitles 4.0.0 Release Target enhancement needs testing
Projects
None yet
Development

No branches or pull requests

3 participants