Subtitles icon indicating copy to clipboard operation
Subtitles copied to clipboard

WP REST API integration

Open robertdevore opened this issue 8 years ago • 13 comments

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 :+1:

robertdevore avatar Dec 03 '15 19:12 robertdevore

I'll leave this open. Cheers.

philiparthurmoore avatar Dec 09 '15 16:12 philiparthurmoore

Any luck? I plan on revisiting this soon.

philiparthurmoore avatar Oct 03 '17 19:10 philiparthurmoore

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 👍

robertdevore avatar Oct 04 '17 04:10 robertdevore

@robertdevore Are you still planning on working on this?

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

grappler avatar Apr 06 '18 11:04 grappler

@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 :+1:

robertdevore avatar Apr 06 '18 12:04 robertdevore

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 avatar Apr 07 '18 13:04 philiparthurmoore

2018-04-07_20-24-40

philiparthurmoore avatar Apr 07 '18 13:04 philiparthurmoore

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.

grappler avatar Apr 07 '18 15:04 grappler

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 avatar Apr 11 '18 16:04 philiparthurmoore

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.

philiparthurmoore avatar Apr 11 '18 16:04 philiparthurmoore

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

grappler avatar Apr 11 '18 20:04 grappler

@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 avatar Apr 12 '18 08:04 philiparthurmoore

Guess I should probably take care of this now.

philiparthurmoore avatar Dec 11 '18 16:12 philiparthurmoore