Skip to content

Commit e7bae21

Browse files
committed
Merge branch 'hotfix/validate-callback-fix'
2 parents 66cd98c + b4e209b commit e7bae21

6 files changed

+88
-22
lines changed

better-wp-endpoints.php

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

includes/create_cpt_endpoints.php

+32-9
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,42 @@ function bwe_build_cpt_endpoints() {
123123
},
124124
'args' => array(
125125
'per_page' => array(
126-
'validate_callback' => 'is_numeric'
126+
'description' => 'Maxiumum number of items to show per page.',
127+
'type' => 'integer',
128+
'validate_callback' => function( $param, $request, $key ) {
129+
return is_numeric( $param );
130+
},
131+
'sanitize_callback' => 'absint',
127132
),
128133
'page' => array(
129-
'validate_callback' => 'is_numeric'
130-
),
131-
'category' => array(
132-
'validate_callback' => 'is_numeric'
133-
),
134-
'tag' => array(
135-
'validate_callback' => 'is_numeric'
134+
'description' => 'Current page of the collection.',
135+
'type' => 'integer',
136+
'validate_callback' => function( $param, $request, $key ) {
137+
return is_numeric( $param );
138+
},
139+
'sanitize_callback' => 'absint'
136140
),
137141
'content' => array(
138-
'validate_callback' => 'is_boolean'
142+
'description' => 'Hide or show the_content from the collection.',
143+
'type' => 'boolean',
144+
'validate_callback' => function( $param, $request, $key ) {
145+
146+
if ( $param == 'true' || $param == 'TRUE' ) {
147+
$param = true;
148+
} else if( $param == 'false' || $param == 'FALSE') {
149+
$param = false;
150+
}
151+
152+
return is_bool( $param );
153+
}
154+
),
155+
'orderby' => array(
156+
'description' => 'The sort order of the collection.',
157+
'type' => 'string',
158+
'validate_callback' => function($param, $request, $key) {
159+
return is_string( $param );
160+
},
161+
'sanitize_callback' => 'sanitize_text_field'
139162
),
140163
),
141164
) );

includes/get_pages.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -125,29 +125,41 @@ function bwe_get_pages( WP_REST_Request $request ) {
125125
'per_page' => array(
126126
'description' => 'Maxiumum number of items to show per page.',
127127
'type' => 'integer',
128-
'validate_callback' => function( $v ) { return is_numeric( $v );},
128+
'validate_callback' => function( $param, $request, $key ) {
129+
return is_numeric( $param );
130+
},
129131
'sanitize_callback' => 'absint',
130132
),
131133
'page' => array(
132134
'description' => 'Current page of the collection.',
133135
'type' => 'integer',
134-
'validate_callback' => 'is_numeric',
136+
'validate_callback' => function( $param, $request, $key ) {
137+
return is_numeric( $param );
138+
},
135139
'sanitize_callback' => 'absint',
136140
),
137141
'exclude' => array(
138142
'description' => 'Exclude an item from the collection.',
139143
'type' => 'integer',
140-
'validate_callback' => 'is_numeric',
144+
'validate_callback' => function( $param, $request, $key ) {
145+
return is_numeric( $param );
146+
},
141147
'sanitize_callback' => 'absint',
142148
),
143149
'order' => array(
144150
'description' => 'Change order of the collection.',
145151
'type' => 'string',
152+
'validate_callback' => function($param, $request, $key) {
153+
return is_string( $param );
154+
},
146155
'sanitize_callback' => 'sanitize_text_field',
147156
),
148157
'orderby' => array(
149158
'description' => 'Change how the collection is ordered.',
150159
'type' => 'string',
160+
'validate_callback' => function($param, $request, $key) {
161+
return is_string( $param );
162+
},
151163
'sanitize_callback' => 'sanitize_text_field',
152164
),
153165
),

includes/get_posts.php

+38-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function bwe_get_posts( WP_REST_Request $request ) {
1414
$page = $request['page']?: '1';
1515
$category = $request['category']?: null;
1616
$tag = $request['tag']?: null;
17-
$show_content = $request['content']?: 'true';
17+
$show_content = $request['content']?: true;
1818

1919
// WP_Query arguments
2020
$args = array(
@@ -47,7 +47,7 @@ function bwe_get_posts( WP_REST_Request $request ) {
4747
$bwe_post->excerpt = get_the_excerpt();
4848

4949
// show post content unless parameter is false
50-
if( $show_content === 'true' ) {
50+
if( $show_content == true ) {
5151
$bwe_post->content = apply_filters('the_content', get_the_content());
5252
}
5353

@@ -147,19 +147,50 @@ function bwe_get_posts( WP_REST_Request $request ) {
147147
'callback' => 'bwe_get_posts',
148148
'args' => array(
149149
'per_page' => array(
150-
'validate_callback' => 'is_numeric'
150+
'description' => 'Maxiumum number of items to show per page.',
151+
'type' => 'integer',
152+
'validate_callback' => function( $param, $request, $key ) {
153+
return is_numeric( $param );
154+
},
155+
'sanitize_callback' => 'absint',
151156
),
152157
'page' => array(
153-
'validate_callback' => 'is_numeric'
158+
'description' => 'Current page of the collection.',
159+
'type' => 'integer',
160+
'validate_callback' => function( $param, $request, $key ) {
161+
return is_numeric( $param );
162+
},
163+
'sanitize_callback' => 'absint'
154164
),
155165
'category' => array(
156-
'validate_callback' => 'is_numeric'
166+
'description' => 'Get a category from the collection.',
167+
'type' => 'integer',
168+
'validate_callback' => function( $param, $request, $key ) {
169+
return is_numeric( $param );
170+
},
171+
'sanitize_callback' => 'absint'
157172
),
158173
'tag' => array(
159-
'validate_callback' => 'is_numeric'
174+
'description' => 'Get a tag from the collection.',
175+
'type' => 'integer',
176+
'validate_callback' => function( $param, $request, $key ) {
177+
return is_numeric( $param );
178+
},
179+
'sanitize_callback' => 'absint'
160180
),
161181
'content' => array(
162-
'validate_callback' => 'is_boolean'
182+
'description' => 'Hide or show the_content from the collection.',
183+
'type' => 'boolean',
184+
'validate_callback' => function( $param, $request, $key ) {
185+
186+
if ( $param == 'true' || $param == 'TRUE' ) {
187+
$param = true;
188+
} else if( $param == 'false' || $param == 'FALSE') {
189+
$param = false;
190+
}
191+
192+
return is_bool( $param );
193+
}
163194
),
164195
),
165196
) );

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "better-wp-endpoints",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"description": "Serves up slimmer WordPress Rest API endpoints.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)