| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Api\Admin; |
| 13 |
|
| 14 |
use WindPress\WindPress\Api\AbstractApi; |
| 15 |
use WindPress\WindPress\Api\ApiInterface; |
| 16 |
use WindPress\WindPress\Core\Volume as CoreVolume; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
use WP_REST_Server; |
| 20 |
class Volume extends AbstractApi implements ApiInterface |
| 21 |
{ |
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
} |
| 25 |
public function get_prefix(): string |
| 26 |
{ |
| 27 |
return 'admin/volume'; |
| 28 |
} |
| 29 |
public function register_custom_endpoints(): void |
| 30 |
{ |
| 31 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/index', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(WP_REST_Request $wprestRequest): WP_REST_Response => $this->index($wprestRequest), 'permission_callback' => fn(WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 32 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/store', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(WP_REST_Request $wprestRequest): WP_REST_Response => $this->store($wprestRequest), 'permission_callback' => fn(WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 33 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/handlers', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(WP_REST_Request $wprestRequest): WP_REST_Response => $this->handlers($wprestRequest), 'permission_callback' => fn(WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 34 |
} |
| 35 |
public function index(WP_REST_Request $wprestRequest): WP_REST_Response |
| 36 |
{ |
| 37 |
return new WP_REST_Response(['entries' => CoreVolume::get_entries()]); |
| 38 |
} |
| 39 |
public function store(WP_REST_Request $wprestRequest): WP_REST_Response |
| 40 |
{ |
| 41 |
$payload = $wprestRequest->get_json_params(); |
| 42 |
$entries = $payload['volume']['entries']; |
| 43 |
$result = CoreVolume::save_entries($entries); |
| 44 |
if (!empty($result['errors']) || !empty($result['skipped'])) { |
| 45 |
return new WP_REST_Response(['success' => \false, 'message' => __('Some entries could not be saved.', 'windpress'), 'details' => $result], empty($result['errors']) ? 400 : 500); |
| 46 |
} |
| 47 |
return new WP_REST_Response(['success' => \true, 'message' => __('data stored successfully', 'windpress')]); |
| 48 |
} |
| 49 |
public function handlers(WP_REST_Request $wprestRequest): WP_REST_Response |
| 50 |
{ |
| 51 |
return new WP_REST_Response(['handlers' => CoreVolume::get_available_handlers()]); |
| 52 |
} |
| 53 |
} |
| 54 |
|