| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CloudKitLibrary\Data; |
| 3 |
|
| 4 |
use Elementor\Modules\CloudKitLibrary\Connect\Cloud_Kits; |
| 5 |
use Elementor\Modules\CloudKitLibrary\Module as CloudKitLibrary; |
| 6 |
use Elementor\App\Modules\KitLibrary\Data\Base_Controller; |
| 7 |
use Elementor\Core\Utils\Collection; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Controller extends Base_Controller { |
| 14 |
|
| 15 |
public function get_name() { |
| 16 |
return 'cloud-kits'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_items( $request ) { |
| 20 |
$data = $this->get_app()->get_all(); |
| 21 |
|
| 22 |
if ( is_wp_error( $data ) ) { |
| 23 |
return [ |
| 24 |
'data' => [], |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
$kits = ( new Collection( $data ) )->map( function ( $kit ) { |
| 29 |
return [ |
| 30 |
'id' => $kit['id'], |
| 31 |
'title' => $kit['title'], |
| 32 |
'thumbnail_url' => $kit['thumbnailUrl'], |
| 33 |
'created_at' => $kit['createdAt'], |
| 34 |
'updated_at' => $kit['updatedAt'], |
| 35 |
'status' => isset( $kit['status'] ) ? $kit['status'] : 'active', |
| 36 |
]; |
| 37 |
} ); |
| 38 |
|
| 39 |
return [ |
| 40 |
'data' => $kits->values(), |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function delete_item( $request ) { |
| 45 |
return [ |
| 46 |
'data' => $this->get_app()->delete_kit( $request->get_param( 'id' ) ), |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
public function get_item( $request ) { |
| 51 |
return [ |
| 52 |
'data' => $this->get_app()->get_kit( [ 'id' => $request->get_param( 'id' ) ] ), |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
public function register_endpoints() { |
| 57 |
$this->index_endpoint->register_item_route( \WP_REST_Server::DELETABLE, [ |
| 58 |
'id' => [ |
| 59 |
'description' => 'Unique identifier for the object.', |
| 60 |
'type' => 'integer', |
| 61 |
'required' => true, |
| 62 |
], |
| 63 |
] ); |
| 64 |
|
| 65 |
$this->register_endpoint( new Endpoints\Eligibility( $this ) ); |
| 66 |
$this->register_endpoint( new Endpoints\Quota( $this ) ); |
| 67 |
} |
| 68 |
|
| 69 |
public function get_permission_callback( $request ) { |
| 70 |
return current_user_can( 'manage_options' ); |
| 71 |
} |
| 72 |
|
| 73 |
protected function get_app(): Cloud_Kits { |
| 74 |
return CloudKitLibrary::get_app(); |
| 75 |
} |
| 76 |
} |
| 77 |
|