From de12a4aa2f886975b076e552bb1ae928397dc868 Mon Sep 17 00:00:00 2001 From: Jack Lenox Date: Tue, 29 Sep 2015 17:26:49 +0800 Subject: [PATCH 1/5] Compatibility updates for Picard_API_Comments --- functions.php | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/functions.php b/functions.php index 53a8d2a..cf48f29 100644 --- a/functions.php +++ b/functions.php @@ -160,13 +160,25 @@ function picard_api_init() { } add_action( 'wp_json_server_before_serve', 'picard_api_init' ); -class Picard_API_Comments extends WP_JSON_Comments { - public function register_routes( $routes ) { - $routes['/picard/comments'] = array( - array( array( $this, 'new_post' ), WP_JSON_Server::CREATABLE ), - ); +class Picard_API_Comments extends WP_REST_Comments_Controller { + //public function register_routes( $routes ) { + //$routes['/picard/comments'] = array( + //array( array( $this, 'new_post' ), WP_JSON_Server::CREATABLE ), + //); + + //return $routes; + //} + + public function register_routes() { + + register_rest_route( 'wp/v2', '/picard/comments', array( + array( + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'new_post' ), + ), + + ) ); - return $routes; } public function new_post() { @@ -184,7 +196,7 @@ public function new_post() { $new_comment = get_comment( $comment_id ); - $prepared_comment = $this->prepare_comment( $new_comment ); + $prepared_comment = $this->prepare_item_for_response( $new_comment ); return ( $comment_id ) ? $prepared_comment : array(); } From 2d891af5be54141603423521bfe37f2856605b1e Mon Sep 17 00:00:00 2001 From: Jack Lenox Date: Tue, 29 Sep 2015 18:07:19 +0800 Subject: [PATCH 2/5] Compatibility updates for routes and properties --- .../loop/comments/comment-list/comment/comment.jsx | 8 ++++---- components/content/loop/comments/comments.jsx | 2 +- .../content/loop/hentry/entry-content/entry-content.jsx | 4 ++-- components/router/router.jsx | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/components/content/loop/comments/comment-list/comment/comment.jsx b/components/content/loop/comments/comment-list/comment/comment.jsx index 5abf80e..9d9b0de 100644 --- a/components/content/loop/comments/comment-list/comment/comment.jsx +++ b/components/content/loop/comments/comment-list/comment/comment.jsx @@ -23,16 +23,16 @@ var Comment = React.createClass({
-
+
@@ -43,4 +43,4 @@ var Comment = React.createClass({ }); -module.exports = Comment; \ No newline at end of file +module.exports = Comment; diff --git a/components/content/loop/comments/comments.jsx b/components/content/loop/comments/comments.jsx index 506565b..755dc60 100644 --- a/components/content/loop/comments/comments.jsx +++ b/components/content/loop/comments/comments.jsx @@ -16,7 +16,7 @@ var CommentList = require( './comment-list/comment-list.jsx' ), var Comments = React.createClass({ loadCommentsFromServer: function() { - var repliesLink = '/wp-json/posts/' + this.props.postID + '/comments/'; + var repliesLink = '/wp-json/wp/v2/comments?post=' + this.props.postID; var self = this; diff --git a/components/content/loop/hentry/entry-content/entry-content.jsx b/components/content/loop/hentry/entry-content/entry-content.jsx index 9349c65..f397561 100644 --- a/components/content/loop/hentry/entry-content/entry-content.jsx +++ b/components/content/loop/hentry/entry-content/entry-content.jsx @@ -10,10 +10,10 @@ var EntryContent = React.createClass({ render: function() { return ( -
+
); } }); -module.exports = EntryContent; \ No newline at end of file +module.exports = EntryContent; diff --git a/components/router/router.jsx b/components/router/router.jsx index 306fa93..cded214 100644 --- a/components/router/router.jsx +++ b/components/router/router.jsx @@ -19,7 +19,7 @@ var Router = React.createClass({ page( '/', function ( ctx ) { var data, slug = ctx.params.slug, - url = "/wp-json/posts"; + url = "/wp-json/wp/v2/posts"; request .get( url ) .end( function( err, res ) { @@ -31,7 +31,7 @@ var Router = React.createClass({ page( '/:year/:month/:day/:slug', function ( ctx ) { var data, slug = ctx.params.slug, - url = "/wp-json/posts/?filter[name]=" + slug; + url = "/wp-json/wp/v2/posts/?filter[name]=" + slug; request .get( url ) .end( function( err, res ) { @@ -56,7 +56,7 @@ var Router = React.createClass({ slug = slug.substr(0, slug.length - 1); } var part = slug.substring(slug.lastIndexOf('/') + 1); - var url = "/wp-json/posts/?type[]=page&filter[name]=" + part; + var url = "/wp-json/wp/v2/pages/?filter[name]=" + part; request .get( url ) .end( function( err, res ) { @@ -82,4 +82,4 @@ var Router = React.createClass({ }); -module.exports = Router; \ No newline at end of file +module.exports = Router; From 30ea09422d4cefdb1a896e3650b63a3e47273dc8 Mon Sep 17 00:00:00 2001 From: Jack Lenox Date: Tue, 29 Sep 2015 18:51:50 +0800 Subject: [PATCH 3/5] Compatiblity updates - Remove Picard's comment handling as WP-API is now capable of taking care of this. - Fix URLs where need be - Fix properties where need be --- .../comments/comment-form/comment-form.jsx | 4 +- components/content/loop/comments/comments.jsx | 6 +-- components/content/loop/hentry/hentry.jsx | 2 +- components/content/loop/loop.jsx | 4 +- functions.php | 50 ------------------- 5 files changed, 8 insertions(+), 58 deletions(-) diff --git a/components/content/loop/comments/comment-form/comment-form.jsx b/components/content/loop/comments/comment-form/comment-form.jsx index 4fb4660..3badc21 100644 --- a/components/content/loop/comments/comment-form/comment-form.jsx +++ b/components/content/loop/comments/comment-form/comment-form.jsx @@ -16,7 +16,7 @@ var commentForm = React.createClass({ if ( !text || !author ) { return; } - this.props.onCommentSubmit({comment_author: author, comment_author_email: emailAddress, comment_author_url: website, content: text }); + this.props.onCommentSubmit({author_name: author, author_email: emailAddress, author_url: website, content: text }); this.refs.author.getDOMNode().value = ''; this.refs.emailAddress.getDOMNode().value = ''; this.refs.website.getDOMNode().value = ''; @@ -48,4 +48,4 @@ var commentForm = React.createClass({ }); -module.exports = commentForm; \ No newline at end of file +module.exports = commentForm; diff --git a/components/content/loop/comments/comments.jsx b/components/content/loop/comments/comments.jsx index 755dc60..0875a18 100644 --- a/components/content/loop/comments/comments.jsx +++ b/components/content/loop/comments/comments.jsx @@ -34,7 +34,7 @@ var Comments = React.createClass({ var newComment, self = this, - url = '/wp-json/picard/comments'; + url = '/wp-json/wp/v2/comments?post=' + this.props.postID; request .post( url ) .type( 'form' ) @@ -44,7 +44,7 @@ var Comments = React.createClass({ newComment = JSON.parse( res.text ); self.setState( { data: self.state.data.concat( [ newComment ] ) } ); } else { - console.error( '/wp-json/picard/comments', err.toString() ); + console.error( '/wp-json/wp/v2/comments?post=' + self.props.postID, err.toString() ); } }); @@ -70,4 +70,4 @@ var Comments = React.createClass({ }); -module.exports = Comments; \ No newline at end of file +module.exports = Comments; diff --git a/components/content/loop/hentry/hentry.jsx b/components/content/loop/hentry/hentry.jsx index f734b60..734d971 100644 --- a/components/content/loop/hentry/hentry.jsx +++ b/components/content/loop/hentry/hentry.jsx @@ -89,4 +89,4 @@ Hentry = React.createClass({ } }); -module.exports = Hentry; \ No newline at end of file +module.exports = Hentry; diff --git a/components/content/loop/loop.jsx b/components/content/loop/loop.jsx index a10e97d..6dde230 100644 --- a/components/content/loop/loop.jsx +++ b/components/content/loop/loop.jsx @@ -28,7 +28,7 @@ Loop = React.createClass({ var postNodes = this.props.data.map( function ( post ) { return ( - + ); }); @@ -51,4 +51,4 @@ Loop = React.createClass({ } }); -module.exports = Loop; \ No newline at end of file +module.exports = Loop; diff --git a/functions.php b/functions.php index cf48f29..9afdd18 100644 --- a/functions.php +++ b/functions.php @@ -151,53 +151,3 @@ function picard_get_json( $_post ) { } add_filter( 'json_prepare_post', 'picard_get_json' ); - -function picard_api_init() { - global $picard_api_comments; - - $picard_api_comments = new Picard_API_Comments(); - add_filter( 'json_endpoints', array( $picard_api_comments, 'register_routes' ) ); -} -add_action( 'wp_json_server_before_serve', 'picard_api_init' ); - -class Picard_API_Comments extends WP_REST_Comments_Controller { - //public function register_routes( $routes ) { - //$routes['/picard/comments'] = array( - //array( array( $this, 'new_post' ), WP_JSON_Server::CREATABLE ), - //); - - //return $routes; - //} - - public function register_routes() { - - register_rest_route( 'wp/v2', '/picard/comments', array( - array( - 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'new_post' ), - ), - - ) ); - - } - - public function new_post() { - - $commentdata = array( - 'comment_post_ID' => $_POST['comment_post_ID'], - 'comment_author' => $_POST['comment_author'], - 'comment_author_email' => $_POST['comment_author_email'], - 'comment_author_url' => $_POST['comment_author_url'], - 'comment_content' => $_POST['content'], - 'comment_author_IP' => $_SERVER['REMOTE_ADDR'], - 'comment_type' => '', - ); - $comment_id = wp_new_comment( $commentdata ); - - $new_comment = get_comment( $comment_id ); - - $prepared_comment = $this->prepare_item_for_response( $new_comment ); - - return ( $comment_id ) ? $prepared_comment : array(); - } -} From a03d44b76bfe873a636f1ea4a0b15ba1464c607b Mon Sep 17 00:00:00 2001 From: Jack Lenox Date: Wed, 30 Sep 2015 19:20:38 +0800 Subject: [PATCH 4/5] Remove whitespace --- components/content/loop/comments/comments.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/content/loop/comments/comments.jsx b/components/content/loop/comments/comments.jsx index 0875a18..71498ce 100644 --- a/components/content/loop/comments/comments.jsx +++ b/components/content/loop/comments/comments.jsx @@ -57,7 +57,7 @@ var Comments = React.createClass({ componentDidMount: function() { this.loadCommentsFromServer(); }, - + render: function() { return (
From 3cafbd2fc30fd8368a04e6e6172ab61460ba5cfd Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Wed, 30 Sep 2015 20:14:50 -0700 Subject: [PATCH 5/5] Update readme to specify requiring the v2 REST API plugin from the WordPress.org plugin repo. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 98c1f2f..142d7ed 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,11 @@ It means, that instead of loading new page every time you click a link, it uses ## Installation ### 1. Working WordPress installation -First, prepare a proper WordPress environment: +Ensure you have a proper WordPress development environment for working with the REST API. You will need: -* You will obviously need a working *WordPress* installation, -* You will also need *WP REST API*. It is a plugin (with plans to incorporate into the WordPress core) that creates REST API to be used by the theme. Plugin is currently **NOT** in plugins repository, you have to upload the files manually. Install and activate the [WP REST API](https://github.com/WP-API/WP-API/tree/master) plugin (make sure that you are using the `master` branch – the default is currently develop), -* Set your permlink structure to `/%year%/%monthnum%/%day%/%postname%/`. +* a working WordPress installation +* the REST API version 2 plugin from the WordPress.org plugin repository, installed and activated +* your permalink structure set to `/%year%/%monthnum%/%day%/%postname%/` ### 2. Theme building (for _team_ building go to your local meetup) Unlike other themes, this one uses a build process. JavaScript is an interpreted language, but we need to take certain steps (like transpiling React JSX syntax or SASS CSS syntax) to take the files from development phase to production. You will need the following tools: