Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Last active November 14, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosfreitas/027200a9e2e1b28ba93a5b532180d498 to your computer and use it in GitHub Desktop.
Save marcosfreitas/027200a9e2e1b28ba93a5b532180d498 to your computer and use it in GitHub Desktop.
<?php
namespace ITTRoutes;
use ITTRoutes\HelpersTrait;
use ITTRoutes\ContentTrait;
use ITTRoutes\CategoryTrait;
class ConsumerRoutes extends AbstractRoutes
{
use ContentTrait, CategoryTrait;
public function __construct()
{
parent::__construct();
add_action('rest_api_init', [$this, 'addRestRoute']);
add_filter('rest_post_dispatch', [$this, 'removeContentFromRestRequests'], 10, 3);
add_filter( 'wp_rest_cache/allowed_endpoints', [$this, 'wprc_add_acf_posts_endpoint'], 10, 1);
}
public function addRestRoute()
{
( some other routes )
register_rest_route(
'itt/v1',
'/posts/',
[
'methods' => 'GET',
'callback' => [$this, 'getPostList']
]
);
}
public function getPostList()
{
(...)
}
/**
* Filtering WP API response for any post, clearing the content to prevent unathorized access
* @version 1.0.1 -fix foreach over empty variable $_data
* @param array $rest_response
* @param array $rest_server
* @param array $rest_request
* @return array
*/
function removeContentFromRestRequests($rest_response, $rest_server, $rest_request)
{
( make some validations... )
return $rest_response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment