Skip to content

Commit e1e660f

Browse files
committed
Merge branch 'develop'
2 parents 5237da1 + 5edef6f commit e1e660f

11 files changed

+270
-80
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
n/a
99

10+
## [1.2.0] - 2018-02-07
11+
### Added
12+
- ACF query in endpoint url to hide acf values from the response where applicable (all collections)
13+
- Media query in endpoint url to hide featured media from the response where applicable (all collections)
14+
1015
## [1.1.2] - 2018-01-25
1116
### Update
1217
- Fix issue where get post by slug was returning just the first post
13-
- Fix instance of lefover $bwe variable naming
18+
- Fix instance of lefover $bwe variable naming
1419

1520
## [1.1.1] - 2018-01-25
1621
### Update

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Gets a collection of posts. Accepts the following parameters:
1717
- order (string - 'ASC' vs 'DESC')
1818
- exclude (int) a post ID to exclude from the response
1919
- author (string) limit posts by author nice name (user_nicename)
20+
- acf (boolean - setting to false omits `acf` from being returned)
21+
- media (boolean - setting to false omits `media` (featured media) from being returned)
2022

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

96100
Returns the following JSON Response:
97101

@@ -130,6 +134,8 @@ Gets a collection of posts from a custom post type. Accepts the following parame
130134
- content (boolean - setting to false omits `the_content` from being returned)
131135
- 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
132136
- exclude (int) a post ID to exclude from the response
137+
- acf (boolean - setting to false omits `acf` from being returned)
138+
- media (boolean - setting to false omits `media` (featured media) from being returned)
133139

134140
Returns the following JSON response:
135141

@@ -191,6 +197,8 @@ Gets posts from a taxonomy term. Accepts the following parameters:
191197
- content (boolean - setting to false omits `the_content` from being returned)
192198
- 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
193199
- exclude (int) a post ID to exclude from the response
200+
- acf (boolean - setting to false omits `acf` from being returned)
201+
- media (boolean - setting to false omits `media` (featured media) from being returned)
194202

195203
Returns the following JSON Response:
196204

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

246256
It returns a JSON response with the following (returns an empty array if no posts found):
247257
- id

better-wp-endpoints.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Better Rest Endpoints
44
Plugin URI: https://github.com/factor1/better-rest-endpoints/
55
Description: Serves up slimmer WordPress Rest API endpoints, with some great enhancements.
6-
Version: 1.1.2
6+
Version: 1.2.0
77
Author: Eric Stout, Factor1 Studios
88
Author URI: https://factor1studios.com/
99
License: GPL3

includes/create_cpt_endpoints.php

+35-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ function bre_build_cpt_endpoints() {
2828
$page = $request['page']?: '1';
2929
$content = $request['content'];
3030
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
31+
$acf = $request['acf'];
32+
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
33+
$media = $request['media'];
34+
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
3135
$orderby = $request['orderby']?: null;
3236
$order = $request['order']?: null;
3337
$exclude = $request['exclude']?: null;
@@ -94,27 +98,32 @@ function bre_build_cpt_endpoints() {
9498

9599
/*
96100
*
97-
* return acf fields if they exist
101+
* return acf fields if they exist and depending on query string
98102
*
99103
*/
100-
$bre_post->acf = bre_get_acf();
104+
if( $acf === null || $show_acf === true ) {
105+
$bre_post->acf = bre_get_acf();
106+
}
101107

102108
/*
103109
*
104-
* get possible thumbnail sizes and urls
110+
* get possible thumbnail sizes and urls if query set to true or by default
105111
*
106112
*/
107-
$thumbnail_names = get_intermediate_image_sizes();
108-
$bre_thumbnails = new stdClass();
109113

110-
if( has_post_thumbnail() ){
111-
foreach ($thumbnail_names as $key => $name) {
112-
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
113-
}
114+
if( $media === null || $show_media === true ) {
115+
$thumbnail_names = get_intermediate_image_sizes();
116+
$bre_thumbnails = new stdClass();
114117

115-
$bre_post->media = $bre_thumbnails;
116-
} else {
117-
$bre_post->media = false;
118+
if( has_post_thumbnail() ){
119+
foreach ($thumbnail_names as $key => $name) {
120+
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
121+
}
122+
123+
$bre_post->media = $bre_thumbnails;
124+
} else {
125+
$bre_post->media = false;
126+
}
118127
}
119128

120129
// Push the post to the main $post array
@@ -167,6 +176,20 @@ function bre_build_cpt_endpoints() {
167176
return is_bool( $param );
168177
}
169178
),
179+
'acf' => array(
180+
'description' => 'Hide or show acf fields from the collection.',
181+
'type' => 'boolean',
182+
'validate_callback' => function( $param, $request, $key ) {
183+
184+
if ( $param == 'true' || $param == 'TRUE' ) {
185+
$param = true;
186+
} else if( $param == 'false' || $param == 'FALSE') {
187+
$param = false;
188+
}
189+
190+
return is_bool( $param );
191+
}
192+
),
170193
'order' => array(
171194
'description' => 'Change order of the collection.',
172195
'type' => 'string',

includes/get_pages.php

+55-18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ function bre_get_pages( WP_REST_Request $request ) {
1515
$page = $request['page']?: '1';
1616
$content = $request['content'];
1717
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
18+
$acf = $request['acf'];
19+
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
20+
$media = $request['media'];
21+
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
1822
$orderby = $request['orderby']?: null;
1923
$order = $request['order']?: null;
2024
$exclude = $request['exclude']?: null;
@@ -97,25 +101,30 @@ function bre_get_pages( WP_REST_Request $request ) {
97101
* return acf fields if they exist
98102
*
99103
*/
100-
$bre_page->acf = bre_get_acf();
101-
102-
/*
103-
*
104-
* get possible thumbnail sizes and urls
105-
*
106-
*/
107-
$thumbnail_names = get_intermediate_image_sizes();
108-
$bre_thumbnails = new stdClass();
109-
110-
if( has_post_thumbnail() ){
111-
foreach ($thumbnail_names as $key => $name) {
112-
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
113-
}
104+
if( $acf === null || $show_acf === true ) {
105+
$bre_page->acf = bre_get_acf();
106+
}
107+
108+
/*
109+
*
110+
* get possible thumbnail sizes and urls if query set to true or by default
111+
*
112+
*/
113+
114+
if( $media === null || $show_media === true ) {
115+
$thumbnail_names = get_intermediate_image_sizes();
116+
$bre_thumbnails = new stdClass();
117+
118+
if( has_post_thumbnail() ){
119+
foreach ($thumbnail_names as $key => $name) {
120+
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
121+
}
114122

115-
$bre_page->media = $bre_thumbnails;
116-
} else {
117-
$bre_page->media = false;
118-
}
123+
$bre_page->media = $bre_thumbnails;
124+
} else {
125+
$bre_page->media = false;
126+
}
127+
}
119128

120129
// Push the post to the main $post array
121130
array_push($pages, $bre_page);
@@ -204,6 +213,34 @@ function bre_get_pages( WP_REST_Request $request ) {
204213
return is_bool( $status );
205214
}
206215
),
216+
'acf' => array(
217+
'description' => 'Hide or show acf fields from the collection.',
218+
'type' => 'boolean',
219+
'validate_callback' => function( $param, $request, $key ) {
220+
221+
if ( $param == 'true' || $param == 'TRUE' ) {
222+
$param = true;
223+
} else if( $param == 'false' || $param == 'FALSE') {
224+
$param = false;
225+
}
226+
227+
return is_bool( $param );
228+
}
229+
),
230+
'media' => array(
231+
'description' => 'Hide or show featured media from the collection.',
232+
'type' => 'boolean',
233+
'validate_callback' => function( $param, $request, $key ) {
234+
235+
if ( $param == 'true' || $param == 'TRUE' ) {
236+
$param = true;
237+
} else if( $param == 'false' || $param == 'FALSE') {
238+
$param = false;
239+
}
240+
241+
return is_bool( $param );
242+
}
243+
),
207244
),
208245
) );
209246
} );

includes/get_posts.php

+49-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function bre_get_posts( WP_REST_Request $request ) {
1717
$tag = $request['tag']?: null;
1818
$content = $request['content'];
1919
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
20+
$acf = $request['acf'];
21+
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
22+
$media = $request['media'];
23+
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
2024
$orderby = $request['orderby']?: null;
2125
$order = $request['order']?: null;
2226
$exclude = $request['exclude']?: null;
@@ -113,27 +117,32 @@ function bre_get_posts( WP_REST_Request $request ) {
113117

114118
/*
115119
*
116-
* return acf fields if they exist
120+
* return acf fields if they exist and depending on query string
117121
*
118122
*/
119-
$bre_post->acf = bre_get_acf();
123+
if( $acf === null || $show_acf === true ) {
124+
$bre_post->acf = bre_get_acf();
125+
}
120126

121127
/*
122128
*
123-
* get possible thumbnail sizes and urls
129+
* get possible thumbnail sizes and urls if query set to true or by default
124130
*
125131
*/
126-
$thumbnail_names = get_intermediate_image_sizes();
127-
$bre_thumbnails = new stdClass();
128132

129-
if( has_post_thumbnail() ){
130-
foreach ($thumbnail_names as $key => $name) {
131-
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
132-
}
133+
if( $media === null || $show_media === true ) {
134+
$thumbnail_names = get_intermediate_image_sizes();
135+
$bre_thumbnails = new stdClass();
136+
137+
if( has_post_thumbnail() ){
138+
foreach ($thumbnail_names as $key => $name) {
139+
$bre_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
140+
}
133141

134-
$bre_post->media = $bre_thumbnails;
135-
} else {
136-
$bre_post->media = false;
142+
$bre_post->media = $bre_thumbnails;
143+
} else {
144+
$bre_post->media = false;
145+
}
137146
}
138147

139148
// Push the post to the main $post array
@@ -221,6 +230,34 @@ function bre_get_posts( WP_REST_Request $request ) {
221230
return is_bool( $status );
222231
}
223232
),
233+
'acf' => array(
234+
'description' => 'Hide or show acf fields from the collection.',
235+
'type' => 'boolean',
236+
'validate_callback' => function( $param, $request, $key ) {
237+
238+
if ( $param == 'true' || $param == 'TRUE' ) {
239+
$param = true;
240+
} else if( $param == 'false' || $param == 'FALSE') {
241+
$param = false;
242+
}
243+
244+
return is_bool( $param );
245+
}
246+
),
247+
'media' => array(
248+
'description' => 'Hide or show featured media from the collection.',
249+
'type' => 'boolean',
250+
'validate_callback' => function( $param, $request, $key ) {
251+
252+
if ( $param == 'true' || $param == 'TRUE' ) {
253+
$param = true;
254+
} else if( $param == 'false' || $param == 'FALSE') {
255+
$param = false;
256+
}
257+
258+
return is_bool( $param );
259+
}
260+
),
224261
'order' => array(
225262
'description' => 'Change order of the collection.',
226263
'type' => 'string',

0 commit comments

Comments
 (0)