| 1 |
<?php //phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* Image Upload API |
| 5 |
* |
| 6 |
* Provides REST API endpoints to upload, retrieve, and delete |
| 7 |
* images used for Product Badges and Text Highlights. |
| 8 |
* |
| 9 |
* @package Disco |
| 10 |
* @subpackage Disco\Rest |
| 11 |
* @since 1.3.21 |
| 12 |
* @category Rest |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Disco\Rest; |
| 16 |
|
| 17 |
use Disco\App\Upload\ImageUpload; |
| 18 |
use WP_REST_Controller; |
| 19 |
use WP_REST_Server; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class ImageUploadApi |
| 23 |
* |
| 24 |
* Registers and handles the following REST API routes: |
| 25 |
* |
| 26 |
* - POST /disco/v1/image-upload — Upload an image. |
| 27 |
* - GET /disco/v1/image-upload/{type} — Retrieve images by upload type. |
| 28 |
* - DELETE /disco/v1/image-upload/{id} — Delete an image by attachment ID. |
| 29 |
* |
| 30 |
* @package Disco |
| 31 |
* @subpackage Disco\Rest |
| 32 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 33 |
* @link https://webappick.com |
| 34 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 35 |
* @category Rest |
| 36 |
*/ |
| 37 |
class ImageUploadApi extends WP_REST_Controller {//phpcs:ignore |
| 38 |
|
| 39 |
/** |
| 40 |
* The route base name for image upload endpoints. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
public const ROUTE_NAME = 'image-upload'; |
| 45 |
|
| 46 |
/** |
| 47 |
* ImageUploadApi constructor. |
| 48 |
* |
| 49 |
* Sets the namespace and rest base for all image upload routes. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
*/ |
| 53 |
public function __construct() { |
| 54 |
$this->namespace = Api::NAMESPACE_NAME . '/' . Api::VERSION; |
| 55 |
$this->rest_base = self::ROUTE_NAME; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Registers the REST API routes for image upload operations. |
| 60 |
* |
| 61 |
* Registers three routes: |
| 62 |
* 1. POST /image-upload — Upload an image file. |
| 63 |
* 2. GET /image-upload/{type} — Get all images for a given upload type. |
| 64 |
* 3. DELETE /image-upload/{id} — Delete an image by its attachment ID. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function register_routes() { //phpcs:ignore |
| 70 |
register_rest_route( |
| 71 |
$this->namespace, |
| 72 |
'/' . $this->rest_base, |
| 73 |
array( |
| 74 |
array( |
| 75 |
'methods' => WP_REST_Server::CREATABLE, |
| 76 |
'callback' => array( $this, 'upload_image' ), |
| 77 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 78 |
), |
| 79 |
) |
| 80 |
); |
| 81 |
|
| 82 |
register_rest_route( |
| 83 |
$this->namespace, |
| 84 |
'/' . $this->rest_base . '/(?P<type>[a-z-]+)', |
| 85 |
array( |
| 86 |
array( |
| 87 |
'methods' => WP_REST_Server::READABLE, |
| 88 |
'callback' => array( $this, 'get_images' ), |
| 89 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 90 |
'args' => array( |
| 91 |
'type' => array( |
| 92 |
'description' => __( 'Upload type: product-badge or text-highlight.', 'disco' ), |
| 93 |
'type' => 'string', |
| 94 |
'enum' => array_keys( ImageUpload::UPLOAD_DIRS ), |
| 95 |
), |
| 96 |
), |
| 97 |
), |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
register_rest_route( |
| 102 |
$this->namespace, |
| 103 |
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
| 104 |
array( |
| 105 |
array( |
| 106 |
'methods' => WP_REST_Server::DELETABLE, |
| 107 |
'callback' => array( $this, 'delete_image' ), |
| 108 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 109 |
'args' => array( |
| 110 |
'id' => array( |
| 111 |
'description' => __( 'Attachment ID.', 'disco' ), |
| 112 |
'type' => 'integer', |
| 113 |
), |
| 114 |
), |
| 115 |
), |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Checks if the current user has permission to access image upload endpoints. |
| 122 |
* |
| 123 |
* Requires either 'manage_options' or 'manage_woocommerce' capability. |
| 124 |
* |
| 125 |
* @since 1.0.0 |
| 126 |
* @param \WP_REST_Request $request Full data about the request. |
| 127 |
* @return bool|\WP_Error True if the user has permission, WP_Error otherwise. |
| 128 |
*/ |
| 129 |
public function permissions_check( $request ) {//phpcs:ignore |
| 130 |
$permission = current_user_can( 'manage_options' ) || current_user_can( 'manage_woocommerce' ); // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 131 |
|
| 132 |
if ( ! $permission ) { |
| 133 |
return new \WP_Error( |
| 134 |
'rest_forbidden', |
| 135 |
__( 'Sorry, Permission Denied.', 'disco' ), |
| 136 |
array( 'status' => 403 ) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Handles image upload via POST request. |
| 145 |
* |
| 146 |
* Accepts a multipart form-data request with an 'image' file field |
| 147 |
* and a 'type' parameter. Validates and stores the image in the |
| 148 |
* WordPress media library under a type-specific directory. |
| 149 |
* |
| 150 |
* @since 1.0.0 |
| 151 |
* @param \WP_REST_Request $request Full data about the request. |
| 152 |
* @return \WP_REST_Response|\WP_Error Response with attachment id, url, and type on success. |
| 153 |
*/ |
| 154 |
public function upload_image( $request ) { |
| 155 |
$files = $request->get_file_params(); |
| 156 |
$type = $request->get_param( 'type' ); |
| 157 |
$result = ( new ImageUpload )->upload( $files['image'] ?? array(), is_string( $type ) ? $type : '' ); |
| 158 |
|
| 159 |
if ( is_wp_error( $result ) ) { |
| 160 |
return $result; |
| 161 |
} |
| 162 |
|
| 163 |
return rest_ensure_response( $result ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Retrieves all uploaded images for a given upload type. |
| 168 |
* |
| 169 |
* Queries the WordPress media library for attachments stored |
| 170 |
* in the type-specific upload directory (e.g., disco-product-badges). |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* @param \WP_REST_Request $request Full data about the request. |
| 174 |
* @return \WP_REST_Response|\WP_Error Response with array of images (id, name, url) on success. |
| 175 |
*/ |
| 176 |
public function get_images( $request ) { |
| 177 |
$type = $request->get_param( 'type' ); |
| 178 |
$result = ( new ImageUpload )->get_images( is_string( $type ) ? $type : '' ); |
| 179 |
|
| 180 |
if ( is_wp_error( $result ) ) { |
| 181 |
return $result; |
| 182 |
} |
| 183 |
|
| 184 |
return rest_ensure_response( $result ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Deletes an uploaded image by its attachment ID. |
| 189 |
* |
| 190 |
* Permanently removes the attachment and its associated file |
| 191 |
* from the WordPress media library. |
| 192 |
* |
| 193 |
* @since 1.0.0 |
| 194 |
* @param \WP_REST_Request $request Full data about the request. |
| 195 |
* @return \WP_REST_Response|\WP_Error Response with id and deleted status on success. |
| 196 |
*/ |
| 197 |
public function delete_image( $request ) { |
| 198 |
$id = absint( $request->get_param( 'id' ) ); |
| 199 |
$result = ( new ImageUpload )->delete( $id ); |
| 200 |
|
| 201 |
if ( is_wp_error( $result ) ) { |
| 202 |
return $result; |
| 203 |
} |
| 204 |
|
| 205 |
return rest_ensure_response( $result ); |
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|