Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
erwstout committed Jan 11, 2018
2 parents 2a47c51 + 48d4c99 commit 54a88d0
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 57 deletions.
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# better-wp-endpoints
# better-rest-endpoints
A WordPress plugin that serves up slimmer WP Rest API endpoints - WIP

## Endpoints

### Posts
**`better-wp-endpoints/v1/posts`**
**`better-rest-endpoints/v1/posts`**
Gets a collection of posts. Accepts the following parameters:

- page (int)
Expand Down Expand Up @@ -34,7 +34,7 @@ It returns a JSON response with the following:
- ACF fields, if applicable

### Post
**`better-wp-endpoints/v1/post/{id}`**
**`better-rest-endpoints/v1/post/{id}`**
Get a post by ID.

Accepts the following parameters:
Expand All @@ -58,7 +58,7 @@ Returns a JSON response with the following:
- ACF fields, if applicable

### Pages
**`better-wp-endpoints/v1/pages`**
**`better-rest-endpoints/v1/pages`**
Gets a collection of pages. Accepts the following parameters:

- exclude (int)
Expand All @@ -67,6 +67,7 @@ Gets a collection of pages. Accepts the following parameters:
- per_page (int)
- page (int)
- content (boolean - setting to false hides the content from the response)
- exclude (int) a post ID to exclude from the response

Returns the following JSON Response:

Expand All @@ -79,7 +80,7 @@ Returns the following JSON Response:
- all possible thumbnail sizes & URLs

### Page by ID
**`better-wp-endpoints/v1/page/{id}`**
**`better-rest-endpoints/v1/page/{id}`**
Get a page by ID.

Accepts the following parameters:
Expand All @@ -97,13 +98,14 @@ Returns a JSON response with the following:
- ACF fields, if applicable

### Custom Post Type Collection
**`better-wp-endpoints/v1/{custom_post_type}`**
**`better-rest-endpoints/v1/{custom_post_type}`**
Gets a collection of posts from a custom post type. Accepts the following parameters:

- per_page (int)
- page (int)
- content (boolean - setting to false omits `the_content` from being returned)
- orderby (string) - see the [codex](https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters) for options, currently does not support multiple values
- exclude (int) a post ID to exclude from the response

Returns the following JSON response:

Expand All @@ -119,7 +121,7 @@ Returns the following JSON response:
- ACF fields if applicable

### Custom Post Type Post by ID
**`better-wp-endpoints/v1/{custom_post_type}/{id}`**
**`better-rest-endpoints/v1/{custom_post_type}/{id}`**
Gets a single custom post type item. Accepts the following parameters:

- ID
Expand All @@ -138,7 +140,7 @@ Returns the following JSON Response:
- ACF Fields, if applicable

### Custom Post Type Post by Slug
**`better-wp-endpoints/v1/{custom_post_type}/{slug}`**
**`better-rest-endpoints/v1/{custom_post_type}/{slug}`**
Gets a single custom post type item. Accepts the following parameters:

- slug
Expand All @@ -157,13 +159,14 @@ Returns the following JSON Response:
- ACF Fields, if applicable

### Get Posts Belonging To A Taxonomy Term
**`better-wp-endpoints/v1/{taxonomy}/{term}`**
Gets posts from a taxonomy term. Accepts rthe following parameters:
**`better-rest-endpoints/v1/{taxonomy}/{term}`**
Gets posts from a taxonomy term. Accepts the following parameters:

- per_page (int)
- page (int)
- content (boolean - setting to false omits `the_content` from being returned)
- orderby (string) - see the [codex](https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters) for options, currently does not support multiple values
- exclude (int) a post ID to exclude from the response

Returns the following JSON Response:

Expand All @@ -179,7 +182,7 @@ Returns the following JSON Response:
- ACF Fields, if applicable

### Menus
**`better-wp-endpoints/v1/menus/{menu-slug}`**
**`better-rest-endpoints/v1/menus/{menu-slug}`**
Gets a WordPress Menu by slug. Accepts no parameters.

Returns the following JSON Response in each item object:
Expand All @@ -194,8 +197,19 @@ Returns the following JSON Response in each item object:
- classes (array)
- menu item parent

### Taxonomies
**`better-rest-endpoints/v1/taxonomies`**
Gets a list of taxonomies used by WordPress. Accepts no parameters.

Returns the following JSON response in each item object:

- Name
- Slug
- Description
- Hierarchical (true/false)

### Search
**`better-wp-endpoints/v1/search`**
**`better-rest-endpoints/v1/search`**
Gets a collection of posts and pages based on the search parameter. Accepts the following parameters:

- page (int)
Expand Down
17 changes: 10 additions & 7 deletions better-wp-endpoints.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?php
/*
Plugin Name: Better WordPress Endpoints
Plugin URI: https://github.com/factor1/better-wp-endpoints/
Plugin Name: Better Rest Endpoints
Plugin URI: https://github.com/factor1/better-rest-endpoints/
Description: Serves up slimmer WordPress Rest API endpoints, with some great enhancements.
Version: 0.1.23
Version: 0.2.0
Author: Eric Stout, Factor1 Studios
Author URI: https://factor1studios.com/
License: GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: better-wp-endpoints
Text Domain: better-rest-endpoints
Domain Path: /languages
Better WordPress Endpoints is free software: you can redistribute it and/or modify
Better Rest Endpoints is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
Better WordPress Endpoints is distributed in the hope that it will be useful,
Better Rest Endpoints is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Better WordPress Endpoints. If not, see {URI to Plugin License}.
along with Better Rest Endpoints. If not, see {URI to Plugin License}.
*/

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -109,6 +109,9 @@ private function includes() {

// get search endpoint
include_once self::$plugin_dir . 'includes/get_search.php';

// get taxonomies endpoint
include_once self::$plugin_dir . 'includes/get_taxonomies.php';
}

}
Expand Down
2 changes: 1 addition & 1 deletion copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs-extra'),
target = '/Users/ericstout/Documents/projects/tallwave/app/public/wp-content/plugins/better-wp-endpoints',
target = '/Users/ericstout/Documents/projects/better-rest-endpoints-wp/app/public/wp-content/plugins/better-rest-endpoints',
files = './';

fs.copy(files, target)
Expand Down
31 changes: 26 additions & 5 deletions includes/create_cpt_endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@ function bwe_build_cpt_endpoints() {
*
*/

register_rest_route( 'better-wp-endpoints/v1', '/'.$cpt.'/', array(
register_rest_route( 'better-rest-endpoints/v1', '/'.$cpt.'/', array(
'methods' => 'GET',
'callback' => function ( WP_REST_Request $request ) use($cpt) {

// check for params
$posts_per_page = $request['per_page']?: '10';
$page = $request['page']?: '1';
$show_content = $request['content']?: 'true';
$orderby = $request['orderby']? : null;
$content = $request['content'];
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
$orderby = $request['orderby']?: null;
$order = $request['order']?: null;
$exclude = $request['exclude']?: null;

// WP_Query arguments
$args = array(
'post_type' => $cpt,
'nopaging' => false,
'posts_per_page' => $posts_per_page,
'paged' => $page,
'orderby' => $orderby
'post__not_in' => array($exclude),
'order' => $order?:'DESC',
'orderby' => $orderby?:'date'
);

// The Query
Expand Down Expand Up @@ -65,7 +70,7 @@ function bwe_build_cpt_endpoints() {
$bwe_post->excerpt = get_the_excerpt();

// show post content unless parameter is false
if( $show_content === 'true' ) {
if( $content === null || $show_content === true ) {
$bwe_post->content = apply_filters('the_content', get_the_content());
}

Expand Down Expand Up @@ -162,6 +167,14 @@ function bwe_build_cpt_endpoints() {
return is_bool( $param );
}
),
'order' => array(
'description' => 'Change order of the collection.',
'type' => 'string',
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
},
'sanitize_callback' => 'sanitize_text_field',
),
'orderby' => array(
'description' => 'The sort order of the collection.',
'type' => 'string',
Expand All @@ -170,6 +183,14 @@ function bwe_build_cpt_endpoints() {
},
'sanitize_callback' => 'sanitize_text_field'
),
'exclude' => array(
'description' => 'Exclude a post by ID.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint'
),
),
) );

Expand Down
2 changes: 1 addition & 1 deletion includes/get_cpt_by_id.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function bwe_build_single_cpt_endpoints() {
* Register Rest API Endpoint
*
*/
register_rest_route( 'better-wp-endpoints/v1', '/'.$cpt.'/(?P<id>\d+)', array(
register_rest_route( 'better-rest-endpoints/v1', '/'.$cpt.'/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => function ( WP_REST_Request $request ) use ($cpt) {

Expand Down
2 changes: 1 addition & 1 deletion includes/get_cpt_by_slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function bwe_build_single_cpt_endpoints_slug() {
* Register Rest API Endpoint
*
*/
register_rest_route( 'better-wp-endpoints/v1', '/'.$cpt.'/(?P<slug>\S+)', array(
register_rest_route( 'better-rest-endpoints/v1', '/'.$cpt.'/(?P<slug>\S+)', array(
'methods' => 'GET',
'callback' => function ( WP_REST_Request $request ) use ($cpt) {

Expand Down
2 changes: 1 addition & 1 deletion includes/get_page_by_id.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function get_page_by_id( WP_REST_Request $request ){
}

add_action( 'rest_api_init', function () {
register_rest_route( 'better-wp-endpoints/v1', '/page/(?P<id>\d+)', array(
register_rest_route( 'better-rest-endpoints/v1', '/page/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_page_by_id',
) );
Expand Down
26 changes: 22 additions & 4 deletions includes/get_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function bwe_get_pages( WP_REST_Request $request ) {
// check for params
$posts_per_page = $request['per_page']?: '10';
$page = $request['page']?: '1';
$show_content = $request['content']?: 'true';
$content = $request['content'];
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
$orderby = $request['orderby']?: null;
$order = $request['order']?: null;
$exclude = $request['exclude']?: null;
Expand All @@ -25,7 +26,8 @@ function bwe_get_pages( WP_REST_Request $request ) {
'posts_per_page' => $posts_per_page,
'paged' => $page,
'order' => $order?:'DESC',
'orderby' => $orderby?:'date'
'orderby' => $orderby?:'date',
'post__not_in' => array($exclude),
);

$query = new WP_Query( $args );
Expand Down Expand Up @@ -86,7 +88,7 @@ function bwe_get_pages( WP_REST_Request $request ) {


// show post content unless parameter is false
if( $show_content === 'true' ) {
if( $content === null || $show_content === true ) {
$bwe_page->content = apply_filters('the_content', get_the_content());
}

Expand Down Expand Up @@ -142,7 +144,7 @@ function bwe_get_pages( WP_REST_Request $request ) {
*
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'better-wp-endpoints/v1', '/pages/', array(
register_rest_route( 'better-rest-endpoints/v1', '/pages/', array(
'methods' => 'GET',
'callback' => 'bwe_get_pages',
'args' => array(
Expand Down Expand Up @@ -186,6 +188,22 @@ function bwe_get_pages( WP_REST_Request $request ) {
},
'sanitize_callback' => 'sanitize_text_field',
),
'content' => array(
'description' => 'Hide or show the_content from the collection.',
'type' => 'boolean',
'validate_callback' => function( $param, $request, $key ) {

if ( $param == 'true' || $param == 'TRUE' ) {
// $param = true;
$status = true;
} else if( $param == 'false' || $param == 'FALSE') {
//$param = false;
$status = false;
}

return is_bool( $status );
}
),
),
) );
} );
2 changes: 1 addition & 1 deletion includes/get_post_by_id.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function get_post_by_id( $data ) {
}

add_action( 'rest_api_init', function () {
register_rest_route( 'better-wp-endpoints/v1', '/post/(?P<id>\d+)', array(
register_rest_route( 'better-rest-endpoints/v1', '/post/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_post_by_id',
) );
Expand Down
Loading

0 comments on commit 54a88d0

Please sign in to comment.