Skip to content

Commit

Permalink
Merge pull request #24 from elevati/development
Browse files Browse the repository at this point in the history
override get_items_permissions_check
  • Loading branch information
Salil Kothadia authored Jul 12, 2018
2 parents e8f3220 + d596a77 commit 25e4b58
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject
54 changes: 38 additions & 16 deletions lib/endpoints/class-wp-rest-multiple-posttype-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
* @author Ruben Vreeken
* @author Elevati Team
*/
class WP_REST_Multiple_PostType_Controller extends WP_REST_Controller {
class WP_REST_Multiple_PostType_Controller extends WP_REST_Controller
{

public function __construct() {
/**
* Constructor
*/
public function __construct()
{
$this->version = '2';
$this->namespace = 'wp/v' . $this->version;
$this->rest_base = 'multiple-post-type';
Expand All @@ -22,7 +27,8 @@ public function __construct() {
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
public function register_routes()
{
register_rest_route($this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
Expand All @@ -36,9 +42,22 @@ public function register_routes() {
/**
* Check if a given request has access to get items
*
* @return bool
* @param WP_REST_Request $request
* @return boolean
*/
public function get_items_permissions_check($request)
{
return true;
}

/**
* Check if a given request has access to get items
*
* @param WP_REST_Request $request
* @return boolean
*/
public function get_items_permissions_check($request) {
public function has_items_permissions_check($request)
{
return true;
}

Expand All @@ -48,7 +67,8 @@ public function get_items_permissions_check($request) {
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items($request) {
public function get_items($request)
{
$args = array();
$args['author__in'] = $request['author'];
$args['author__not_in'] = $request['author_exclude'];
Expand All @@ -73,14 +93,12 @@ public function get_items($request) {
}

$args['date_query'] = array();
// Set before into date query. Date query must be specified as an array
// of an array.
// Set before into date query. Date query must be specified as an array of an array.
if (isset($request['before'])) {
$args['date_query'][0]['before'] = $request['before'];
}

// Set after into date query. Date query must be specified as an array
// of an array.
// Set after into date query. Date query must be specified as an array of an array.
if (isset($request['after'])) {
$args['date_query'][0]['after'] = $request['after'];
}
Expand Down Expand Up @@ -205,8 +223,8 @@ public function get_items($request) {
*
* @return array $query_args
*/
protected function prepare_items_query($prepared_args = array(), $request = null) {

protected function prepare_items_query($prepared_args = array(), $request = null)
{
$valid_vars = array_flip($this->get_allowed_query_vars($request['type']));
$query_args = array();
foreach ($valid_vars as $var => $index) {
Expand Down Expand Up @@ -239,11 +257,13 @@ protected function prepare_items_query($prepared_args = array(), $request = null
/**
* Get all the WP Query vars that are allowed for the API request.
*
* @global object $wp
* @param array $post_types
* @return array
*/
protected function get_allowed_query_vars($post_types) {
protected function get_allowed_query_vars($post_types)
{
global $wp;
$editPosts = true;

/**
* Filter the publicly allowed query vars.
Expand Down Expand Up @@ -326,7 +346,8 @@ protected function get_allowed_query_vars($post_types) {
*
* @return array
*/
public function get_collection_params() {
public function get_collection_params()
{
$params = parent::get_collection_params();

$params['context']['default'] = 'view';
Expand Down Expand Up @@ -467,7 +488,8 @@ public function get_collection_params() {
*
* @return WP_Error|boolean
*/
public function validate_user_can_query_private_statuses($value, $request, $parameter) {
public function validate_user_can_query_private_statuses($value, $request, $parameter)
{
if ('publish' === $value) {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
/**
* WP_REST_Multiple_PostType_Controller class.
*/

function init_wp_rest_multiple_posttype_endpoint() {
function init_wp_rest_multiple_posttype_endpoint()
{
if (!class_exists('WP_REST_Multiple_PostType_Controller')) {
require_once dirname(__FILE__) . '/lib/endpoints/class-wp-rest-multiple-posttype-controller.php';
}
Expand All @@ -26,4 +26,3 @@ function init_wp_rest_multiple_posttype_endpoint() {
* REST INIT
*/
add_action('rest_api_init', 'init_wp_rest_multiple_posttype_endpoint');

0 comments on commit 25e4b58

Please sign in to comment.