fix: indentations

This commit is contained in:
vnmedeiros 2021-04-16 10:27:44 -03:00
parent 03b093db58
commit 1b096e5103
1 changed files with 203 additions and 203 deletions

View File

@ -13,79 +13,79 @@ use Tainacan\Entities\Collection;
* @uses Entities\Collection and Repositories\Collections * @uses Entities\Collection and Repositories\Collections
* */ * */
class REST_Collections_Controller extends REST_Controller { class REST_Collections_Controller extends REST_Controller {
private $collections_repository; private $collections_repository;
private $collection; private $collection;
/** /**
* REST_Collections_Controller constructor. * REST_Collections_Controller constructor.
* Define the namespace, rest base and instantiate your attributes. * Define the namespace, rest base and instantiate your attributes.
*/ */
public function __construct(){ public function __construct(){
$this->rest_base = 'collections'; $this->rest_base = 'collections';
parent::__construct(); parent::__construct();
add_action('init', array(&$this, 'init_objects'), 11); add_action('init', array(&$this, 'init_objects'), 11);
} }
/** /**
* Initialize objects after post_type register * Initialize objects after post_type register
*/ */
public function init_objects() { public function init_objects() {
$this->collections_repository = Repositories\Collections::get_instance(); $this->collections_repository = Repositories\Collections::get_instance();
$this->collection = new Entities\Collection(); $this->collection = new Entities\Collection();
} }
/** /**
* Register the collections route and their endpoints * Register the collections route and their endpoints
*/ */
public function register_routes(){ public function register_routes(){
register_rest_route($this->namespace, '/' . $this->rest_base, array( register_rest_route($this->namespace, '/' . $this->rest_base, array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE), 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE),
), ),
array( array(
'methods' => \WP_REST_Server::CREATABLE, 'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check'), 'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE),
), ),
'schema' => [$this, 'get_schema'], 'schema' => [$this, 'get_schema'],
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'), 'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check'), 'permission_callback' => array($this, 'get_item_permissions_check'),
'args' => $this->get_wp_query_params(), 'args' => $this->get_wp_query_params(),
), ),
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'), 'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check'), 'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE),
), ),
array( array(
'methods' => \WP_REST_Server::DELETABLE, 'methods' => \WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'), 'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check'), 'permission_callback' => array($this, 'delete_item_permissions_check'),
'args' => array( 'args' => array(
'permanently' => array( 'permanently' => array(
'description' => __('To delete permanently, you can pass \'permanently\' as 1. By default this will only trash collection'), 'description' => __('To delete permanently, you can pass \'permanently\' as 1. By default this will only trash collection'),
'default' => '0', 'default' => '0',
), ),
) )
), ),
'schema' => [$this, 'get_schema'], 'schema' => [$this, 'get_schema'],
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)/metadata_order', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)/metadata_order', array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_metadata_order'), 'callback' => array($this, 'update_metadata_order'),
'permission_callback' => array($this, 'update_metadata_order_permissions_check'), 'permission_callback' => array($this, 'update_metadata_order_permissions_check'),
'args' => [ 'args' => [
'metadata_order' => [ 'metadata_order' => [
'description' => __( 'The order of the metadata in the collection, an array of objects with integer id and bool enabled.', 'tainacan' ), 'description' => __( 'The order of the metadata in the collection, an array of objects with integer id and bool enabled.', 'tainacan' ),
'required' => true, 'required' => true,
@ -96,11 +96,11 @@ class REST_Collections_Controller extends REST_Controller {
'schema' => [$this, 'get_schema'], 'schema' => [$this, 'get_schema'],
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)/filters_order', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)/filters_order', array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_filters_order'), 'callback' => array($this, 'update_filters_order'),
'permission_callback' => array($this, 'update_filters_order_permissions_check'), 'permission_callback' => array($this, 'update_filters_order_permissions_check'),
'args' => [ 'args' => [
'filters_order' => [ 'filters_order' => [
'description' => __( 'The order of the filters in the collection, an array of objects with integer id and bool enabled.', 'tainacan' ), 'description' => __( 'The order of the filters in the collection, an array of objects with integer id and bool enabled.', 'tainacan' ),
'required' => true, 'required' => true,
@ -109,8 +109,8 @@ class REST_Collections_Controller extends REST_Controller {
], ],
), ),
'schema' => [$this, 'get_schema'], 'schema' => [$this, 'get_schema'],
)); ));
} }
/** /**
* Return a array of Collections objects in JSON * Return a array of Collections objects in JSON
@ -123,7 +123,7 @@ class REST_Collections_Controller extends REST_Controller {
public function get_items($request){ public function get_items($request){
$args = $this->prepare_filters($request); $args = $this->prepare_filters($request);
$collections = $this->collections_repository->fetch($args); $collections = $this->collections_repository->fetch($args);
$response = []; $response = [];
if($collections->have_posts()){ if($collections->have_posts()){
@ -141,17 +141,17 @@ class REST_Collections_Controller extends REST_Controller {
$total_collections = $collections->found_posts; $total_collections = $collections->found_posts;
$max_pages = ceil($total_collections / (int) $collections->query_vars['posts_per_page']); $max_pages = ceil($total_collections / (int) $collections->query_vars['posts_per_page']);
$rest_response = new \WP_REST_Response($response, 200); $rest_response = new \WP_REST_Response($response, 200);
$rest_response->header('X-WP-Total', (int) $total_collections); $rest_response->header('X-WP-Total', (int) $total_collections);
$rest_response->header('X-WP-TotalPages', (int) $max_pages); $rest_response->header('X-WP-TotalPages', (int) $max_pages);
$total_collections = wp_count_posts( 'tainacan-collection', 'readable' ); $total_collections = wp_count_posts( 'tainacan-collection', 'readable' );
if (isset($total_collections->publish) || if (isset($total_collections->publish) ||
isset($total_collections->private) || isset($total_collections->private) ||
isset($total_collections->trash) || isset($total_collections->trash) ||
isset($total_collections->draft)) { isset($total_collections->draft)) {
$rest_response->header('X-Tainacan-total-collections-trash', $total_collections->trash); $rest_response->header('X-Tainacan-total-collections-trash', $total_collections->trash);
$rest_response->header('X-Tainacan-total-collections-publish', $total_collections->publish); $rest_response->header('X-Tainacan-total-collections-publish', $total_collections->publish);
@ -159,8 +159,8 @@ class REST_Collections_Controller extends REST_Controller {
$rest_response->header('X-Tainacan-total-collections-private', $total_collections->private); $rest_response->header('X-Tainacan-total-collections-private', $total_collections->private);
} }
return $rest_response; return $rest_response;
} }
/** /**
* Return a Collection object in JSON * Return a Collection object in JSON
@ -170,13 +170,13 @@ class REST_Collections_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response * @return \WP_Error|\WP_REST_Response
*/ */
public function get_item($request){ public function get_item($request){
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$collection = $this->collections_repository->fetch($collection_id); $collection = $this->collections_repository->fetch($collection_id);
$response = $this->prepare_item_for_response($collection, $request ); $response = $this->prepare_item_for_response($collection, $request );
return new \WP_REST_Response($response, 200); return new \WP_REST_Response($response, 200);
} }
/** /**
* *
@ -188,14 +188,14 @@ class REST_Collections_Controller extends REST_Controller {
* @return mixed|string|void|\WP_Error|\WP_REST_Response * @return mixed|string|void|\WP_Error|\WP_REST_Response
*/ */
public function prepare_item_for_response($item, $request){ public function prepare_item_for_response($item, $request){
if(!empty($item)){ if(!empty($item)){
if(!isset($request['fetch_only'])) { if(!isset($request['fetch_only'])) {
$item_arr = $item->_toArray(); $item_arr = $item->_toArray();
if ( $request['context'] === 'edit' ) { if ( $request['context'] === 'edit' ) {
$item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_edit'] = $item->can_edit();
$item_arr['current_user_can_delete'] = $item->can_delete(); $item_arr['current_user_can_delete'] = $item->can_delete();
$collection_caps = \tainacan_roles()->get_collection_caps_slugs(); $collection_caps = \tainacan_roles()->get_collection_caps_slugs();
@ -208,24 +208,24 @@ class REST_Collections_Controller extends REST_Controller {
$item_arr[$cap_key] = current_user_can( $cap_check ); $item_arr[$cap_key] = current_user_can( $cap_check );
} }
} }
} else { } else {
$attributes_to_filter = $request['fetch_only']; $attributes_to_filter = $request['fetch_only'];
# Always returns id # Always returns id
if(is_array($attributes_to_filter)) { if(is_array($attributes_to_filter)) {
$attributes_to_filter[] = 'id'; $attributes_to_filter[] = 'id';
} elseif(!strstr($attributes_to_filter, ',')){ } elseif(!strstr($attributes_to_filter, ',')){
$attributes_to_filter = array($attributes_to_filter, 'id'); $attributes_to_filter = array($attributes_to_filter, 'id');
} else { } else {
$attributes_to_filter .= ',id'; $attributes_to_filter .= ',id';
} }
$item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter); $item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter);
if ( $request['context'] === 'edit' ) { if ( $request['context'] === 'edit' ) {
$item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_edit'] = $item->can_edit();
$item_arr['current_user_can_delete'] = $item->can_delete(); $item_arr['current_user_can_delete'] = $item->can_delete();
$collection_caps = \tainacan_roles()->get_collection_caps_slugs(); $collection_caps = \tainacan_roles()->get_collection_caps_slugs();
@ -307,10 +307,10 @@ class REST_Collections_Controller extends REST_Controller {
} }
return $item_arr; return $item_arr;
} }
return $item; return $item;
} }
/** /**
* *
@ -391,8 +391,8 @@ class REST_Collections_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function create_item_permissions_check( $request ) { public function create_item_permissions_check( $request ) {
return current_user_can($this->collections_repository->get_capabilities()->edit_posts); return current_user_can($this->collections_repository->get_capabilities()->edit_posts);
} }
/** /**
* Prepare collection for insertion on database * Prepare collection for insertion on database
@ -408,8 +408,8 @@ class REST_Collections_Controller extends REST_Controller {
if(method_exists($this->collection, $set_)) $this->collection->$set_($value); if(method_exists($this->collection, $set_)) $this->collection->$set_($value);
} }
return $this->collection; return $this->collection;
} }
/** /**
* Delete a collection * Delete a collection
@ -424,9 +424,9 @@ class REST_Collections_Controller extends REST_Controller {
if(! $collection instanceof Entities\Collection) { if(! $collection instanceof Entities\Collection) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Collection with this ID was not found', 'tainacan' ), 'error_message' => __('Collection with this ID was not found', 'tainacan' ),
'collection_id' => $collection_id 'collection_id' => $collection_id
], 400); ], 400);
} }
if($permanently == true) { if($permanently == true) {
@ -438,7 +438,7 @@ class REST_Collections_Controller extends REST_Controller {
$prepared_collection = $this->prepare_item_for_response($collection, $request); $prepared_collection = $this->prepare_item_for_response($collection, $request);
return new \WP_REST_Response($prepared_collection, 200); return new \WP_REST_Response($prepared_collection, 200);
} }
/** /**
* Verify if current user has permission to delete a collection * Verify if current user has permission to delete a collection
@ -456,7 +456,7 @@ class REST_Collections_Controller extends REST_Controller {
} }
return false; return false;
} }
/** /**
* Update a collection * Update a collection
@ -466,48 +466,48 @@ class REST_Collections_Controller extends REST_Controller {
* @return string|\WP_Error|\WP_REST_Response * @return string|\WP_Error|\WP_REST_Response
*/ */
public function update_item( $request ) { public function update_item( $request ) {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$attributes = []; $attributes = [];
foreach ($body as $att => $value){ foreach ($body as $att => $value){
$attributes[$att] = $value; $attributes[$att] = $value;
} }
$collection = $this->collections_repository->fetch($collection_id); $collection = $this->collections_repository->fetch($collection_id);
if($collection) { if($collection) {
$prepared_collection = $this->prepare_item_for_updating( $collection, $attributes ); $prepared_collection = $this->prepare_item_for_updating( $collection, $attributes );
if ( $prepared_collection->validate() ) { if ( $prepared_collection->validate() ) {
$updated_collection = $this->collections_repository->update( $collection ); $updated_collection = $this->collections_repository->update( $collection );
$response = $this->prepare_item_for_response($updated_collection, $request); $response = $this->prepare_item_for_response($updated_collection, $request);
return new \WP_REST_Response( $response, 200 ); return new \WP_REST_Response( $response, 200 );
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_collection->get_errors(), 'errors' => $prepared_collection->get_errors(),
'collection' => $this->prepare_item_for_response($prepared_collection, $request) 'collection' => $this->prepare_item_for_response($prepared_collection, $request)
], 400); ], 400);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Collection with this ID was not found', 'tainacan' ), 'error_message' => __('Collection with this ID was not found', 'tainacan' ),
'collection_id' => $collection_id 'collection_id' => $collection_id
], 400); ], 400);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('The body could not be empty', 'tainacan'), 'error_message' => __('The body could not be empty', 'tainacan'),
'body' => $body 'body' => $body
], 400); ], 400);
} }
/** /**
@ -557,43 +557,43 @@ class REST_Collections_Controller extends REST_Controller {
* @return string|\WP_Error|\WP_REST_Response * @return string|\WP_Error|\WP_REST_Response
*/ */
public function update_metadata_order( $request ) { public function update_metadata_order( $request ) {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if( !empty($body) && isset($body['metadata_order']) ) { if( !empty($body) && isset($body['metadata_order']) ) {
$collection = $this->collections_repository->fetch($collection_id); $collection = $this->collections_repository->fetch($collection_id);
if( $collection instanceof Entities\Collection) { if( $collection instanceof Entities\Collection) {
$collection->set_metadata_order( $body['metadata_order'] ); $collection->set_metadata_order( $body['metadata_order'] );
if ( $collection->validate() ) { if ( $collection->validate() ) {
$updated_collection = $this->collections_repository->update( $collection ); $updated_collection = $this->collections_repository->update( $collection );
$response = $this->prepare_item_for_response($updated_collection, $request); $response = $this->prepare_item_for_response($updated_collection, $request);
return new \WP_REST_Response( $response, 200 ); return new \WP_REST_Response( $response, 200 );
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $collection->get_errors(), 'errors' => $collection->get_errors(),
'collection' => $this->prepare_item_for_response($collection, $request) 'collection' => $this->prepare_item_for_response($collection, $request)
], 400); ], 400);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Collection with this ID was not found', 'tainacan' ), 'error_message' => __('Collection with this ID was not found', 'tainacan' ),
'collection_id' => $collection_id 'collection_id' => $collection_id
], 400); ], 400);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('The body could not be empty', 'tainacan'), 'error_message' => __('The body could not be empty', 'tainacan'),
'body' => $body 'body' => $body
], 400); ], 400);
} }
/** /**
@ -622,43 +622,43 @@ class REST_Collections_Controller extends REST_Controller {
* @return string|\WP_Error|\WP_REST_Response * @return string|\WP_Error|\WP_REST_Response
*/ */
public function update_filters_order( $request ) { public function update_filters_order( $request ) {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if( !empty($body) && isset($body['filters_order']) ) { if( !empty($body) && isset($body['filters_order']) ) {
$collection = $this->collections_repository->fetch($collection_id); $collection = $this->collections_repository->fetch($collection_id);
if( $collection instanceof Entities\Collection) { if( $collection instanceof Entities\Collection) {
$collection->set_filters_order( $body['filters_order'] ); $collection->set_filters_order( $body['filters_order'] );
if ( $collection->validate() ) { if ( $collection->validate() ) {
$updated_collection = $this->collections_repository->update( $collection ); $updated_collection = $this->collections_repository->update( $collection );
$response = $this->prepare_item_for_response($updated_collection, $request); $response = $this->prepare_item_for_response($updated_collection, $request);
return new \WP_REST_Response( $response, 200 ); return new \WP_REST_Response( $response, 200 );
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_collection->get_errors(), 'errors' => $prepared_collection->get_errors(),
'collection' => $this->prepare_item_for_response($prepared_collection, $request) 'collection' => $this->prepare_item_for_response($prepared_collection, $request)
], 400); ], 400);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Collection with this ID was not found', 'tainacan' ), 'error_message' => __('Collection with this ID was not found', 'tainacan' ),
'collection_id' => $collection_id 'collection_id' => $collection_id
], 400); ], 400);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('The body could not be empty', 'tainacan'), 'error_message' => __('The body could not be empty', 'tainacan'),
'body' => $body 'body' => $body
], 400); ], 400);
} }
/** /**
@ -677,7 +677,7 @@ class REST_Collections_Controller extends REST_Controller {
} }
return false; return false;
} }
/** /**
* @param string $method * @param string $method
@ -688,17 +688,17 @@ class REST_Collections_Controller extends REST_Controller {
$endpoint_args = []; $endpoint_args = [];
if($method === \WP_REST_Server::READABLE) { if($method === \WP_REST_Server::READABLE) {
$endpoint_args['name'] = array( $endpoint_args['name'] = array(
'description' => __('Limits the result set to collections with a specific name'), 'description' => __('Limits the result set to collections with a specific name'),
'type' => 'string', 'type' => 'string',
); );
$endpoint_args = array_merge( $endpoint_args = array_merge(
$endpoint_args, $endpoint_args,
parent::get_wp_query_params(), parent::get_wp_query_params(),
parent::get_fetch_only_param(), parent::get_fetch_only_param(),
parent::get_meta_queries_params() parent::get_meta_queries_params()
); );
} elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) { } elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) {
$map = $this->collections_repository->get_map(); $map = $this->collections_repository->get_map();
@ -715,8 +715,8 @@ class REST_Collections_Controller extends REST_Controller {
$endpoint_args = $map; $endpoint_args = $map;
} }
return $endpoint_args; return $endpoint_args;
} }
function get_schema() { function get_schema() {
$schema = [ $schema = [