From 1639a953cc2c5047744b68b1bdcfa66449462a97 Mon Sep 17 00:00:00 2001 From: Eric Stout Date: Wed, 7 Feb 2018 14:49:27 -0700 Subject: [PATCH 1/2] Add media and acf queries in endpoint url to hide them from responses --- CHANGELOG.md | 7 ++- README.md | 10 +++++ better-wp-endpoints.php | 2 +- includes/create_cpt_endpoints.php | 47 ++++++++++++++----- includes/get_pages.php | 73 ++++++++++++++++++++++-------- includes/get_posts.php | 61 ++++++++++++++++++++----- includes/get_posts_tax.php | 75 +++++++++++++++++++++++-------- includes/get_search.php | 61 ++++++++++++++++++++----- readme.txt | 10 +++-- 9 files changed, 268 insertions(+), 78 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08db110..a114db0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 358864c..edd1646 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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: @@ -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: @@ -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 diff --git a/better-wp-endpoints.php b/better-wp-endpoints.php index 7ea4aeb..a409806 100644 --- a/better-wp-endpoints.php +++ b/better-wp-endpoints.php @@ -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 diff --git a/includes/create_cpt_endpoints.php b/includes/create_cpt_endpoints.php index abe0c2b..cc18040 100644 --- a/includes/create_cpt_endpoints.php +++ b/includes/create_cpt_endpoints.php @@ -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; @@ -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 @@ -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', diff --git a/includes/get_pages.php b/includes/get_pages.php index 47dbd19..579a791 100644 --- a/includes/get_pages.php +++ b/includes/get_pages.php @@ -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; @@ -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); @@ -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 ); + } + ), ), ) ); } ); diff --git a/includes/get_posts.php b/includes/get_posts.php index 4236615..8fd7769 100644 --- a/includes/get_posts.php +++ b/includes/get_posts.php @@ -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; @@ -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 @@ -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', diff --git a/includes/get_posts_tax.php b/includes/get_posts_tax.php index b8d7482..ab67c85 100644 --- a/includes/get_posts_tax.php +++ b/includes/get_posts_tax.php @@ -35,6 +35,10 @@ function bre_build_custom_tax_endpoint() { $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; @@ -107,25 +111,30 @@ function bre_build_custom_tax_endpoint() { * return acf fields if they exist * */ - $bre_tax_post->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)); - } - - $bre_tax_post->media = $bre_thumbnails; - } else { - $bre_tax_post->media = false; - } + if( $acf === null || $show_acf === true ) { + $bre_tax_post->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_tax_post->media = $bre_thumbnails; + } else { + $bre_tax_post->media = false; + } + } // push the post to the main array array_push($bre_tax_posts, $bre_tax_post); @@ -178,6 +187,34 @@ function bre_build_custom_tax_endpoint() { 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 ); + } + ), + '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', diff --git a/includes/get_search.php b/includes/get_search.php index ff9746c..38d8506 100644 --- a/includes/get_search.php +++ b/includes/get_search.php @@ -16,6 +16,10 @@ function bre_get_search( 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); $search = $request['search']?: null; // WP_Query arguments @@ -105,27 +109,32 @@ function bre_get_search( 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 @@ -203,6 +212,34 @@ function bre_get_search( WP_REST_Request $request ) { 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 ); + } + ), + '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 ); + } + ), 'search' => array( 'description' => 'The search term used to fetch the collection.', 'type' => 'string', diff --git a/readme.txt b/readme.txt index 5a7d372..a64ef03 100644 --- a/readme.txt +++ b/readme.txt @@ -5,8 +5,8 @@ Plugin URI: https://github.com/factor1/better-rest-endpoints Contributors: factor1, erwstout Tags: rest, api, endpoints, acf, json Requires at least: 4.7.1 -Tested up to: 4.9.2 -Stable Tag: 1.1.2 +Tested up to: 4.9.4 +Stable Tag: 1.2.0 License: GNU Version 3 or Any Later Version A WordPress plugin that serves up slimmer WP Rest API endpoints. @@ -42,9 +42,13 @@ apps endpoints to use Better Rest Endpoints. == Changelog == += 1.2.0, Febuary 7, 2018 = +* Add: ACF query in endpoint url to hide acf values from the response where applicable (all collections) +* Add: Media query in endpoint url to hide featured media from the response where applicable (all collections) + = 1.1.2, January 25, 2018 = * 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, January 25, 2018 = * Update: update plugin version to retrigger build. From 02913a53b837853cbb6a803e6be30372e8377149 Mon Sep 17 00:00:00 2001 From: Eric Stout Date: Wed, 7 Feb 2018 14:50:07 -0700 Subject: [PATCH 2/2] Bump package version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 42cf56a..3d156cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "better-rest-endpoints", - "version": "1.1.2", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 906d338..7c31ace 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "better-rest-endpoints", - "version": "1.1.2", + "version": "1.2.0", "description": "Serves up slimmer WordPress Rest API endpoints.", "main": "index.js", "scripts": {