Files
WPS3Media/classes/pro/api/v1/tools.php
Malin 3248cbb029 feat: add S3-compatible storage provider (MinIO, Ceph, R2, etc.)
Adds a new 'S3-Compatible Storage' provider that works with any
S3-API-compatible object storage service, including MinIO, Ceph,
Cloudflare R2, Backblaze B2, and others.

Changes:
- New provider class: classes/providers/storage/s3-compatible-provider.php
  - Provider key: s3compatible
  - Reads user-configured endpoint URL from settings
  - Uses path-style URL access (required by most S3-compatible services)
  - Supports credentials via AS3CF_S3COMPAT_ACCESS_KEY_ID /
    AS3CF_S3COMPAT_SECRET_ACCESS_KEY wp-config.php constants
  - Disables AWS-specific features (Block Public Access, Object Ownership)
- New provider SVG icons (s3compatible.svg, -link.svg, -round.svg)
- Registered provider in main plugin class with endpoint setting support
- Updated StorageProviderSubPage to show endpoint URL input for S3-compatible
- Built pro settings bundle with rollup (Svelte 4.2.19)
- Added package.json and updated rollup.config.mjs for pro-only builds
2026-03-03 12:30:18 +01:00

154 lines
4.2 KiB
PHP

<?php
namespace DeliciousBrains\WP_Offload_Media\Pro\API\V1;
use DeliciousBrains\WP_Offload_Media\Pro\API\API;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
class Tools extends API {
/** @var int */
protected static $version = 1;
/** @var string */
protected static $name = 'tools';
/**
* Register REST API routes.
*/
public function register_routes() {
register_rest_route(
static::api_namespace(),
static::route(),
array(
'methods' => 'GET',
'callback' => array( $this, 'get_tools' ),
'permission_callback' => array( $this, 'check_permissions' ),
)
);
register_rest_route(
static::api_namespace(),
static::route(),
array(
'methods' => 'PUT',
'callback' => array( $this, 'put_tools' ),
'permission_callback' => array( $this, 'check_permissions' ),
)
);
register_rest_route(
static::api_namespace(),
static::route(),
array(
'methods' => 'DELETE',
'callback' => array( $this, 'delete_tools' ),
'permission_callback' => array( $this, 'check_permissions' ),
)
);
}
/**
* Processes a REST GET request and returns the current tools.
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response|mixed
*/
public function get_tools( WP_REST_Request $request ) {
return $this->rest_ensure_response( 'get', static::name(), array(
'tools' => $this->as3cf->get_tools_info(),
) );
}
/**
* Processes a REST PUT request to perform an action on a tool and returns confirmation of whether it was ok or not.
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response|mixed
*/
public function put_tools( WP_REST_Request $request ) {
$data = $request->get_json_params();
if ( empty( $data['id'] ) ) {
return $this->rest_ensure_response( 'put', static::name(),
new WP_Error( 'missing-tool-id', __( 'Tool ID not supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
if ( empty( $data['action'] ) ) {
return $this->rest_ensure_response( 'put', static::name(),
new WP_Error( 'missing-tool-action', __( 'Action not supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
if ( ! in_array( $data['action'], array( 'start', 'cancel', 'pause_resume' ) ) ) {
return $this->rest_ensure_response( 'put', static::name(),
new WP_Error( 'invalid-tool-action', __( 'Invalid tool action supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
$result = $this->as3cf->perform_tool_action( $data['id'], $data['action'] );
if ( is_wp_error( $result ) ) {
return $this->rest_ensure_response( 'put', static::name(), $result );
}
return $this->rest_ensure_response( 'put', static::name(), array(
'ok' => $result,
'tools' => $this->as3cf->get_tools_info(),
) );
}
/**
* Processes a REST DELETE request to dismiss a tool's errors.
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response|mixed
*/
public function delete_tools( WP_REST_Request $request ) {
$data = $request->get_json_params();
if ( empty( $data['id'] ) ) {
return $this->rest_ensure_response( 'delete', static::name(),
new WP_Error( 'missing-tool-id', __( 'Tool ID not supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
if ( empty( $data['blog_id'] ) ) {
return $this->rest_ensure_response( 'delete', static::name(),
new WP_Error( 'missing-blog-id', __( 'Blog ID not supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
if ( empty( $data['source_type'] ) ) {
return $this->rest_ensure_response( 'delete', static::name(),
new WP_Error( 'missing-source-type', __( 'Source Type not supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
if ( empty( $data['source_id'] ) ) {
return $this->rest_ensure_response( 'delete', static::name(),
new WP_Error( 'missing-source-id', __( 'Source ID not supplied.', 'amazon-s3-and-cloudfront' ) )
);
}
if ( ! isset( $data['errors'] ) ) {
$data['errors'] = 'all';
}
$result = $this->as3cf->dismiss_tool_errors( $data['id'], $data['blog_id'], $data['source_type'], $data['source_id'], $data['errors'] );
if ( is_wp_error( $result ) ) {
return $this->rest_ensure_response( 'delete', static::name(), $result );
}
return $this->rest_ensure_response( 'delete', static::name(), array(
'ok' => true,
) );
}
}