| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Controllers; |
| 4 |
|
| 5 |
use FL\Assistant\Data\Repository\AttachmentsRepository; |
| 6 |
use FL\Assistant\Data\Transformers\AttachmentTransformer; |
| 7 |
use FL\Assistant\System\Contracts\ControllerAbstract; |
| 8 |
use WP_REST_Request; |
| 9 |
use WP_REST_Response; |
| 10 |
use WP_REST_Server; |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* REST API logic for attachments. |
| 15 |
*/ |
| 16 |
class AttachmentsController extends ControllerAbstract { |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* @var AttachmentsRepository |
| 21 |
*/ |
| 22 |
protected $attachments; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var AttachmentTransformer|callable |
| 26 |
*/ |
| 27 |
protected $transformer; |
| 28 |
|
| 29 |
/** |
| 30 |
* AttachmentsController constructor. |
| 31 |
* |
| 32 |
* @param AttachmentsRepository $attachments |
| 33 |
* @param AttachmentTransformer $transformer |
| 34 |
*/ |
| 35 |
public function __construct( AttachmentsRepository $attachments, AttachmentTransformer $transformer ) { |
| 36 |
$this->attachments = $attachments; |
| 37 |
$this->transformer = $transformer; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register routes. |
| 42 |
*/ |
| 43 |
public function register_routes() { |
| 44 |
$this->route( |
| 45 |
'/attachments', |
| 46 |
[ |
| 47 |
[ |
| 48 |
'methods' => WP_REST_Server::READABLE, |
| 49 |
'callback' => [ $this, 'index' ], |
| 50 |
'permission_callback' => function () { |
| 51 |
return current_user_can( 'edit_others_posts' ); |
| 52 |
}, |
| 53 |
], |
| 54 |
|
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
$this->route( |
| 59 |
'/attachments/upload', |
| 60 |
[ |
| 61 |
[ |
| 62 |
'methods' => WP_REST_Server::CREATABLE, |
| 63 |
'callback' => [ $this, 'upload_media' ], |
| 64 |
'permission_callback' => function () { |
| 65 |
return current_user_can( 'edit_others_posts' ); |
| 66 |
}, |
| 67 |
], |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
$this->route( |
| 72 |
'/attachments/count', |
| 73 |
[ |
| 74 |
[ |
| 75 |
'methods' => WP_REST_Server::READABLE, |
| 76 |
'callback' => [ $this, 'count' ], |
| 77 |
'permission_callback' => function () { |
| 78 |
return current_user_can( 'edit_others_posts' ); |
| 79 |
}, |
| 80 |
], |
| 81 |
] |
| 82 |
); |
| 83 |
|
| 84 |
$this->route( |
| 85 |
'/attachments/(?P<id>\d+)', |
| 86 |
[ |
| 87 |
[ |
| 88 |
'methods' => WP_REST_Server::READABLE, |
| 89 |
'callback' => [ $this, 'find' ], |
| 90 |
'args' => [ |
| 91 |
'id' => [ |
| 92 |
'required' => true, |
| 93 |
'type' => 'number', |
| 94 |
], |
| 95 |
], |
| 96 |
'permission_callback' => function () { |
| 97 |
return current_user_can( 'edit_others_posts' ); |
| 98 |
}, |
| 99 |
], |
| 100 |
[ |
| 101 |
'methods' => WP_REST_Server::CREATABLE, |
| 102 |
'callback' => [ $this, 'update' ], |
| 103 |
'args' => [ |
| 104 |
'id' => [ |
| 105 |
'required' => true, |
| 106 |
'type' => 'number', |
| 107 |
], |
| 108 |
'action' => [ |
| 109 |
'required' => true, |
| 110 |
'type' => 'string', |
| 111 |
], |
| 112 |
], |
| 113 |
'permission_callback' => function () { |
| 114 |
return current_user_can( 'edit_others_posts' ); |
| 115 |
}, |
| 116 |
], |
| 117 |
[ |
| 118 |
'methods' => WP_REST_Server::DELETABLE, |
| 119 |
'callback' => [ $this, 'delete' ], |
| 120 |
'args' => [ |
| 121 |
'id' => [ |
| 122 |
'required' => true, |
| 123 |
'type' => 'number', |
| 124 |
], |
| 125 |
], |
| 126 |
'permission_callback' => function () { |
| 127 |
return current_user_can( 'edit_others_posts' ); |
| 128 |
}, |
| 129 |
], |
| 130 |
] |
| 131 |
); |
| 132 |
|
| 133 |
$this->route( |
| 134 |
'/attachments/restore/(?P<id>\d+)', |
| 135 |
[ |
| 136 |
[ |
| 137 |
'methods' => WP_REST_Server::EDITABLE, |
| 138 |
'callback' => [ $this, 'restore' ], |
| 139 |
'args' => [ |
| 140 |
'id' => [ |
| 141 |
'required' => true, |
| 142 |
'type' => 'number', |
| 143 |
], |
| 144 |
], |
| 145 |
'permission_callback' => function () { |
| 146 |
return current_user_can( 'edit_others_posts' ); |
| 147 |
}, |
| 148 |
], |
| 149 |
] |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns an array of counts by attachment type. |
| 155 |
* |
| 156 |
* @param WP_REST_Request $request |
| 157 |
* |
| 158 |
* @return mixed|WP_REST_Response |
| 159 |
*/ |
| 160 |
public function count( WP_REST_Request $request ) { |
| 161 |
$counts = wp_count_attachments(); |
| 162 |
$response = []; |
| 163 |
|
| 164 |
foreach ( $counts as $type => $count ) { |
| 165 |
$parts = explode( '/', $type ); |
| 166 |
$type = array_shift( $parts ); |
| 167 |
|
| 168 |
if ( isset( $response[ $type ] ) ) { |
| 169 |
$response[ $type ] += $count; |
| 170 |
} else { |
| 171 |
$response[ $type ] = $count; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return rest_ensure_response( $response ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns an array of attachments and related data. |
| 180 |
* |
| 181 |
* @param WP_REST_Request $request |
| 182 |
* |
| 183 |
* @return mixed|WP_REST_Response |
| 184 |
*/ |
| 185 |
public function index( WP_REST_Request $request ) { |
| 186 |
|
| 187 |
$args = $request->get_params(); |
| 188 |
$type = isset( $args['post_mime_type'] ) ? $args['post_mime_type'] : 'all'; |
| 189 |
|
| 190 |
switch ( $type ) { |
| 191 |
case 'document': |
| 192 |
$all_mimes = get_allowed_mime_types(); |
| 193 |
$doc_mimes = []; |
| 194 |
$exclude = [ 'image', 'video', 'audio' ]; |
| 195 |
|
| 196 |
foreach ( $all_mimes as $key => $mime ) { |
| 197 |
$parts = explode( '/', $mime ); |
| 198 |
if ( in_array( $parts[0], $exclude, true ) ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
$doc_mimes[ $key ] = $mime; |
| 202 |
} |
| 203 |
|
| 204 |
$args['post_mime_type'] = $doc_mimes; |
| 205 |
break; |
| 206 |
case 'spreadsheets': |
| 207 |
$args['post_mime_type'] = 'application/vnd.apple.numbers,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel.sheet.macroEnabled.12,application/vnd.ms-excel.sheet.binary.macroEnabled.12'; |
| 208 |
break; |
| 209 |
case 'archives': |
| 210 |
$args['post_mime_type'] = 'application/x-gzip,application/rar,application/x-tar,application/zip,application/x-7z-compressed'; |
| 211 |
break; |
| 212 |
case 'doc': |
| 213 |
$args['post_mime_type'] = 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-word.document.macroEnabled.12,application/vnd.ms-word.template.macroEnabled.12,application/vnd.oasis.opendocument.text,application/vnd.apple.pages,application/pdf,application/vnd.ms-xpsdocument,application/oxps,application/rtf,application/wordperfect,application/octet-stream'; |
| 214 |
break; |
| 215 |
case 'mine': |
| 216 |
$args['post_author'] = 1; |
| 217 |
$args['post_mime_type'] = ''; |
| 218 |
break; |
| 219 |
case 'all': |
| 220 |
$args['post_mime_type'] = ''; |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
return $this->attachments->paginate( $args ) |
| 225 |
->apply_transform( $this->transformer ) |
| 226 |
->to_rest_response(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Returns data for a single attachment. |
| 232 |
* |
| 233 |
* @param WP_REST_Request $request |
| 234 |
* |
| 235 |
* @return array|mixed |
| 236 |
*/ |
| 237 |
public function find( WP_REST_Request $request ) { |
| 238 |
$id = $request->get_param( 'id' ); |
| 239 |
|
| 240 |
if ( current_user_can( 'edit_post', $id ) ) { |
| 241 |
return $this->attachments->find( $id, $this->transformer ); |
| 242 |
} |
| 243 |
|
| 244 |
// @todo: implement this on the client side. |
| 245 |
// return new \WP_REST_Response(["message" => "Resource not found"], 404); |
| 246 |
|
| 247 |
return []; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Updates a single attachment based on the specified action. |
| 253 |
* |
| 254 |
* @param WP_REST_Request $request |
| 255 |
* |
| 256 |
* @return mixed|WP_REST_Response |
| 257 |
*/ |
| 258 |
public function update( WP_REST_Request $request ) { |
| 259 |
$id = $request->get_param( 'id' ); |
| 260 |
$action = $request->get_param( 'action' ); |
| 261 |
|
| 262 |
if ( ! current_user_can( 'edit_post', $id ) ) { |
| 263 |
return rest_ensure_response( [ 'error' => true ] ); |
| 264 |
} |
| 265 |
|
| 266 |
$deprecated = 'This route is deprecated. '; |
| 267 |
switch ( $action ) { |
| 268 |
case 'data': |
| 269 |
$data = (array) $request->get_param( 'data' ); |
| 270 |
if ( isset( $data['meta'] ) ) { |
| 271 |
if ( isset( $data['meta']['alt'] ) ) { |
| 272 |
update_post_meta( $id, '_wp_attachment_image_alt', $data['meta']['alt'] ); |
| 273 |
} else { |
| 274 |
wp_update_post( |
| 275 |
array_merge( |
| 276 |
$data, |
| 277 |
[ |
| 278 |
'ID' => $id, |
| 279 |
] |
| 280 |
) |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
unset( $data['meta'] ); |
| 285 |
} |
| 286 |
wp_update_post( |
| 287 |
array_merge( |
| 288 |
$data, |
| 289 |
[ |
| 290 |
'ID' => $id, |
| 291 |
] |
| 292 |
) |
| 293 |
); |
| 294 |
break; |
| 295 |
case 'trash': |
| 296 |
wp_delete_attachment( $id ); |
| 297 |
$deprecated .= "Please use 'DELETE /attachments/delete/{id}"; |
| 298 |
break; |
| 299 |
case 'untrash': |
| 300 |
$deprecated .= "Please use 'POST /attachments/restore/{id}"; |
| 301 |
wp_untrash_post( $id ); |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
$response = new WP_REST_Response( [ 'success' => true ] ); |
| 306 |
|
| 307 |
$response->header( 'Warn', "299 {$deprecated}" ); |
| 308 |
|
| 309 |
return $response; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* @param WP_REST_Request $request |
| 314 |
* |
| 315 |
* @return WP_REST_Response |
| 316 |
*/ |
| 317 |
public function delete( WP_REST_Request $request ) { |
| 318 |
$id = $request->get_param( 'id' ); |
| 319 |
|
| 320 |
if ( ! current_user_can( 'edit_post', $id ) ) { |
| 321 |
return rest_ensure_response( [ 'error' => true ] ); |
| 322 |
} |
| 323 |
|
| 324 |
$this->attachments->delete( $id ); |
| 325 |
|
| 326 |
return rest_ensure_response( [ 'success' => true ] ); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* @param WP_REST_Request $request |
| 331 |
* |
| 332 |
* @return WP_REST_Response |
| 333 |
*/ |
| 334 |
public function restore( WP_REST_Request $request ) { |
| 335 |
$id = $request->get_param( 'id' ); |
| 336 |
|
| 337 |
if ( ! current_user_can( 'edit_post', $id ) ) { |
| 338 |
return rest_ensure_response( [ 'error' => true ] ); |
| 339 |
} |
| 340 |
|
| 341 |
$attachment = $this->attachments->restore( $id, $this->transformer ); |
| 342 |
|
| 343 |
return rest_ensure_response( |
| 344 |
[ |
| 345 |
'success' => true, |
| 346 |
'data' => $attachment, |
| 347 |
] |
| 348 |
); |
| 349 |
} |
| 350 |
} |
| 351 |
|