feat: add rest package endpoints
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* REST package receive/status controller.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Rest;
|
||||
|
||||
use WPContentSync\Package\ContentPackage;
|
||||
use WPContentSync\Package\PackageValidator;
|
||||
|
||||
final class RestPackageController {
|
||||
private PackageValidator $validator;
|
||||
|
||||
public function __construct( PackageValidator $validator ) {
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'rest_api_init', array( $this, 'registerRoutes' ) );
|
||||
}
|
||||
|
||||
public function registerRoutes(): void {
|
||||
register_rest_route(
|
||||
'wp-content-sync/v1',
|
||||
'/status',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'status' ),
|
||||
'permission_callback' => array( $this, 'canReceivePackage' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
'wp-content-sync/v1',
|
||||
'/package',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( $this, 'receivePackage' ),
|
||||
'permission_callback' => array( $this, 'canReceivePackage' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function canReceivePackage(): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function status(): array {
|
||||
return array(
|
||||
'ok' => true,
|
||||
'plugin' => 'wp-content-sync',
|
||||
'version' => WPCS_VERSION,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $request REST request or decoded request array.
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function receivePackage( $request ): array {
|
||||
$data = $this->requestData( $request );
|
||||
|
||||
if ( ! isset( $data['package'] ) || ! is_array( $data['package'] ) ) {
|
||||
return array(
|
||||
'accepted' => false,
|
||||
'errors' => array( 'package is required and must be an object.' ),
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->validator->validate( $data['package'] );
|
||||
|
||||
if ( ! $result->isValid() ) {
|
||||
return array(
|
||||
'accepted' => false,
|
||||
'errors' => $result->errors(),
|
||||
);
|
||||
}
|
||||
|
||||
$package = ContentPackage::fromArray( $data['package'] );
|
||||
|
||||
return array(
|
||||
'accepted' => true,
|
||||
'schema_version' => $package->schemaVersion(),
|
||||
'manifest' => $package->manifest(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $request REST request or decoded request array.
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function requestData( $request ): array {
|
||||
if ( is_array( $request ) ) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
if ( is_object( $request ) && method_exists( $request, 'get_json_params' ) ) {
|
||||
$params = $request->get_json_params();
|
||||
|
||||
return is_array( $params ) ? $params : array();
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user