Skip to content

Commit

Permalink
Add media and acf queries in endpoint url to hide them from responses
Browse files Browse the repository at this point in the history
  • Loading branch information
erwstout committed Feb 7, 2018
1 parent 5237da1 commit 1639a95
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 78 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
n/a

## [1.2.0] - 2018-02-07
### Added
- ACF query in endpoint url to hide acf values from the response where applicable (all collections)
- Media query in endpoint url to hide featured media from the response where applicable (all collections)

## [1.1.2] - 2018-01-25
### Update
- Fix issue where get post by slug was returning just the first post
- Fix instance of lefover $bwe variable naming
- Fix instance of lefover $bwe variable naming

## [1.1.1] - 2018-01-25
### Update
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Gets a collection of posts. Accepts the following parameters:
- order (string - 'ASC' vs 'DESC')
- exclude (int) a post ID to exclude from the response
- author (string) limit posts by author nice name (user_nicename)
- acf (boolean - setting to false omits `acf` from being returned)
- media (boolean - setting to false omits `media` (featured media) from being returned)

It returns a JSON response with the following:
- id
Expand Down Expand Up @@ -92,6 +94,8 @@ Gets a collection of pages. Accepts the following parameters:
- page (int)
- content (boolean - setting to false hides the content from the response)
- exclude (int) a post ID to exclude from the response
- acf (boolean - setting to false omits `acf` from being returned)
- media (boolean - setting to false omits `media` (featured media) from being returned)

Returns the following JSON Response:

Expand Down Expand Up @@ -130,6 +134,8 @@ Gets a collection of posts from a custom post type. Accepts the following parame
- 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
- acf (boolean - setting to false omits `acf` from being returned)
- media (boolean - setting to false omits `media` (featured media) from being returned)

Returns the following JSON response:

Expand Down Expand Up @@ -191,6 +197,8 @@ Gets posts from a taxonomy term. Accepts the following parameters:
- 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
- acf (boolean - setting to false omits `acf` from being returned)
- media (boolean - setting to false omits `media` (featured media) from being returned)

Returns the following JSON Response:

Expand Down Expand Up @@ -242,6 +250,8 @@ Gets a collection of posts and pages based on the search parameter. Accepts the
- tag id (int)
- content (boolean) set to false to omit content from showing in JSON response
- search (string | required)
- acf (boolean - setting to false omits `acf` from being returned)
- media (boolean - setting to false omits `media` (featured media) from being returned)

It returns a JSON response with the following (returns an empty array if no posts found):
- id
Expand Down
2 changes: 1 addition & 1 deletion better-wp-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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: 1.1.2
Version: 1.2.0
Author: Eric Stout, Factor1 Studios
Author URI: https://factor1studios.com/
License: GPL3
Expand Down
47 changes: 35 additions & 12 deletions includes/create_cpt_endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function bre_build_cpt_endpoints() {
$page = $request['page']?: '1';
$content = $request['content'];
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
$acf = $request['acf'];
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
$media = $request['media'];
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
$orderby = $request['orderby']?: null;
$order = $request['order']?: null;
$exclude = $request['exclude']?: null;
Expand Down Expand Up @@ -94,27 +98,32 @@ function bre_build_cpt_endpoints() {

/*
*
* return acf fields if they exist
* return acf fields if they exist and depending on query string
*
*/
$bre_post->acf = bre_get_acf();
if( $acf === null || $show_acf === true ) {
$bre_post->acf = bre_get_acf();
}

/*
*
* get possible thumbnail sizes and urls
* get possible thumbnail sizes and urls if query set to true or by default
*
*/
$thumbnail_names = get_intermediate_image_sizes();
$bre_thumbnails = new stdClass();

if( has_post_thumbnail() ){
foreach ($thumbnail_names as $key => $name) {
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
}
if( $media === null || $show_media === true ) {
$thumbnail_names = get_intermediate_image_sizes();
$bre_thumbnails = new stdClass();

$bre_post->media = $bre_thumbnails;
} else {
$bre_post->media = false;
if( has_post_thumbnail() ){
foreach ($thumbnail_names as $key => $name) {
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
}

$bre_post->media = $bre_thumbnails;
} else {
$bre_post->media = false;
}
}

// Push the post to the main $post array
Expand Down Expand Up @@ -167,6 +176,20 @@ function bre_build_cpt_endpoints() {
return is_bool( $param );
}
),
'acf' => array(
'description' => 'Hide or show acf fields from the collection.',
'type' => 'boolean',
'validate_callback' => function( $param, $request, $key ) {

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

return is_bool( $param );
}
),
'order' => array(
'description' => 'Change order of the collection.',
'type' => 'string',
Expand Down
73 changes: 55 additions & 18 deletions includes/get_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function bre_get_pages( WP_REST_Request $request ) {
$page = $request['page']?: '1';
$content = $request['content'];
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
$acf = $request['acf'];
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
$media = $request['media'];
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
$orderby = $request['orderby']?: null;
$order = $request['order']?: null;
$exclude = $request['exclude']?: null;
Expand Down Expand Up @@ -97,25 +101,30 @@ function bre_get_pages( WP_REST_Request $request ) {
* return acf fields if they exist
*
*/
$bre_page->acf = bre_get_acf();

/*
*
* get possible thumbnail sizes and urls
*
*/
$thumbnail_names = get_intermediate_image_sizes();
$bre_thumbnails = new stdClass();

if( has_post_thumbnail() ){
foreach ($thumbnail_names as $key => $name) {
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
}
if( $acf === null || $show_acf === true ) {
$bre_page->acf = bre_get_acf();
}

/*
*
* get possible thumbnail sizes and urls if query set to true or by default
*
*/

if( $media === null || $show_media === true ) {
$thumbnail_names = get_intermediate_image_sizes();
$bre_thumbnails = new stdClass();

if( has_post_thumbnail() ){
foreach ($thumbnail_names as $key => $name) {
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
}

$bre_page->media = $bre_thumbnails;
} else {
$bre_page->media = false;
}
$bre_page->media = $bre_thumbnails;
} else {
$bre_page->media = false;
}
}

// Push the post to the main $post array
array_push($pages, $bre_page);
Expand Down Expand Up @@ -204,6 +213,34 @@ function bre_get_pages( WP_REST_Request $request ) {
return is_bool( $status );
}
),
'acf' => array(
'description' => 'Hide or show acf fields from the collection.',
'type' => 'boolean',
'validate_callback' => function( $param, $request, $key ) {

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

return is_bool( $param );
}
),
'media' => array(
'description' => 'Hide or show featured media from the collection.',
'type' => 'boolean',
'validate_callback' => function( $param, $request, $key ) {

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

return is_bool( $param );
}
),
),
) );
} );
61 changes: 49 additions & 12 deletions includes/get_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function bre_get_posts( WP_REST_Request $request ) {
$tag = $request['tag']?: null;
$content = $request['content'];
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
$acf = $request['acf'];
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
$media = $request['media'];
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
$orderby = $request['orderby']?: null;
$order = $request['order']?: null;
$exclude = $request['exclude']?: null;
Expand Down Expand Up @@ -113,27 +117,32 @@ function bre_get_posts( WP_REST_Request $request ) {

/*
*
* return acf fields if they exist
* return acf fields if they exist and depending on query string
*
*/
$bre_post->acf = bre_get_acf();
if( $acf === null || $show_acf === true ) {
$bre_post->acf = bre_get_acf();
}

/*
*
* get possible thumbnail sizes and urls
* get possible thumbnail sizes and urls if query set to true or by default
*
*/
$thumbnail_names = get_intermediate_image_sizes();
$bre_thumbnails = new stdClass();

if( has_post_thumbnail() ){
foreach ($thumbnail_names as $key => $name) {
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
}
if( $media === null || $show_media === true ) {
$thumbnail_names = get_intermediate_image_sizes();
$bre_thumbnails = new stdClass();

if( has_post_thumbnail() ){
foreach ($thumbnail_names as $key => $name) {
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
}

$bre_post->media = $bre_thumbnails;
} else {
$bre_post->media = false;
$bre_post->media = $bre_thumbnails;
} else {
$bre_post->media = false;
}
}

// Push the post to the main $post array
Expand Down Expand Up @@ -221,6 +230,34 @@ function bre_get_posts( WP_REST_Request $request ) {
return is_bool( $status );
}
),
'acf' => array(
'description' => 'Hide or show acf fields from the collection.',
'type' => 'boolean',
'validate_callback' => function( $param, $request, $key ) {

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

return is_bool( $param );
}
),
'media' => array(
'description' => 'Hide or show featured media from the collection.',
'type' => 'boolean',
'validate_callback' => function( $param, $request, $key ) {

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

return is_bool( $param );
}
),
'order' => array(
'description' => 'Change order of the collection.',
'type' => 'string',
Expand Down
Loading

0 comments on commit 1639a95

Please sign in to comment.