Skip to content

Commit

Permalink
Merge branch 'hotfix/validate-callback-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
erwstout committed Nov 2, 2017
2 parents 66cd98c + b4e209b commit e7bae21
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 22 deletions.
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 WordPress Endpoints
Plugin URI: https://github.com/factor1/better-wp-endpoints/
Description: Serves up slimmer WordPress Rest API endpoints, with some great enhancements.
Version: 0.1.10
Version: 0.1.11
Author: Eric Stout, Factor1 Studios
Author URI: https://factor1studios.com/
License: GPL3
Expand Down
41 changes: 32 additions & 9 deletions includes/create_cpt_endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,42 @@ function bwe_build_cpt_endpoints() {
},
'args' => array(
'per_page' => array(
'validate_callback' => 'is_numeric'
'description' => 'Maxiumum number of items to show per page.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
),
'page' => array(
'validate_callback' => 'is_numeric'
),
'category' => array(
'validate_callback' => 'is_numeric'
),
'tag' => array(
'validate_callback' => 'is_numeric'
'description' => 'Current page of the collection.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint'
),
'content' => array(
'validate_callback' => 'is_boolean'
'description' => 'Hide or show the_content 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 );
}
),
'orderby' => array(
'description' => 'The sort order of the collection.',
'type' => 'string',
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
},
'sanitize_callback' => 'sanitize_text_field'
),
),
) );
Expand Down
18 changes: 15 additions & 3 deletions includes/get_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,41 @@ function bwe_get_pages( WP_REST_Request $request ) {
'per_page' => array(
'description' => 'Maxiumum number of items to show per page.',
'type' => 'integer',
'validate_callback' => function( $v ) { return is_numeric( $v );},
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
),
'page' => array(
'description' => 'Current page of the collection.',
'type' => 'integer',
'validate_callback' => 'is_numeric',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
),
'exclude' => array(
'description' => 'Exclude an item from the collection.',
'type' => 'integer',
'validate_callback' => 'is_numeric',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
),
'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' => 'Change how the collection is ordered.',
'type' => 'string',
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
},
'sanitize_callback' => 'sanitize_text_field',
),
),
Expand Down
45 changes: 38 additions & 7 deletions includes/get_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function bwe_get_posts( WP_REST_Request $request ) {
$page = $request['page']?: '1';
$category = $request['category']?: null;
$tag = $request['tag']?: null;
$show_content = $request['content']?: 'true';
$show_content = $request['content']?: true;

// WP_Query arguments
$args = array(
Expand Down Expand Up @@ -47,7 +47,7 @@ function bwe_get_posts( WP_REST_Request $request ) {
$bwe_post->excerpt = get_the_excerpt();

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

Expand Down Expand Up @@ -147,19 +147,50 @@ function bwe_get_posts( WP_REST_Request $request ) {
'callback' => 'bwe_get_posts',
'args' => array(
'per_page' => array(
'validate_callback' => 'is_numeric'
'description' => 'Maxiumum number of items to show per page.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
),
'page' => array(
'validate_callback' => 'is_numeric'
'description' => 'Current page of the collection.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint'
),
'category' => array(
'validate_callback' => 'is_numeric'
'description' => 'Get a category from the collection.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint'
),
'tag' => array(
'validate_callback' => 'is_numeric'
'description' => 'Get a tag from the collection.',
'type' => 'integer',
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint'
),
'content' => array(
'validate_callback' => 'is_boolean'
'description' => 'Hide or show the_content 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 );
}
),
),
) );
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "better-wp-endpoints",
"version": "0.1.10",
"version": "0.1.11",
"description": "Serves up slimmer WordPress Rest API endpoints.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e7bae21

Please sign in to comment.