No Description

class-wp-rest-attachments-controller.php 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. <?php
  2. /**
  3. * REST API: WP_REST_Attachments_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core controller used to access attachments via the REST API.
  11. *
  12. * @since 4.7.0
  13. *
  14. * @see WP_REST_Posts_Controller
  15. */
  16. class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
  17. /**
  18. * Registers the routes for attachments.
  19. *
  20. * @since 5.3.0
  21. *
  22. * @see register_rest_route()
  23. */
  24. public function register_routes() {
  25. parent::register_routes();
  26. register_rest_route(
  27. $this->namespace,
  28. '/' . $this->rest_base . '/(?P<id>[\d]+)/post-process',
  29. array(
  30. 'methods' => WP_REST_Server::CREATABLE,
  31. 'callback' => array( $this, 'post_process_item' ),
  32. 'permission_callback' => array( $this, 'post_process_item_permissions_check' ),
  33. 'args' => array(
  34. 'id' => array(
  35. 'description' => __( 'Unique identifier for the attachment.' ),
  36. 'type' => 'integer',
  37. ),
  38. 'action' => array(
  39. 'type' => 'string',
  40. 'enum' => array( 'create-image-subsizes' ),
  41. 'required' => true,
  42. ),
  43. ),
  44. )
  45. );
  46. register_rest_route(
  47. $this->namespace,
  48. '/' . $this->rest_base . '/(?P<id>[\d]+)/edit',
  49. array(
  50. 'methods' => WP_REST_Server::CREATABLE,
  51. 'callback' => array( $this, 'edit_media_item' ),
  52. 'permission_callback' => array( $this, 'edit_media_item_permissions_check' ),
  53. 'args' => $this->get_edit_media_item_args(),
  54. )
  55. );
  56. }
  57. /**
  58. * Determines the allowed query_vars for a get_items() response and
  59. * prepares for WP_Query.
  60. *
  61. * @since 4.7.0
  62. *
  63. * @param array $prepared_args Optional. Array of prepared arguments. Default empty array.
  64. * @param WP_REST_Request $request Optional. Request to prepare items for.
  65. * @return array Array of query arguments.
  66. */
  67. protected function prepare_items_query( $prepared_args = array(), $request = null ) {
  68. $query_args = parent::prepare_items_query( $prepared_args, $request );
  69. if ( empty( $query_args['post_status'] ) ) {
  70. $query_args['post_status'] = 'inherit';
  71. }
  72. $media_types = $this->get_media_types();
  73. if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) {
  74. $query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
  75. }
  76. if ( ! empty( $request['mime_type'] ) ) {
  77. $parts = explode( '/', $request['mime_type'] );
  78. if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) {
  79. $query_args['post_mime_type'] = $request['mime_type'];
  80. }
  81. }
  82. // Filter query clauses to include filenames.
  83. if ( isset( $query_args['s'] ) ) {
  84. add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
  85. }
  86. return $query_args;
  87. }
  88. /**
  89. * Checks if a given request has access to create an attachment.
  90. *
  91. * @since 4.7.0
  92. *
  93. * @param WP_REST_Request $request Full details about the request.
  94. * @return true|WP_Error Boolean true if the attachment may be created, or a WP_Error if not.
  95. */
  96. public function create_item_permissions_check( $request ) {
  97. $ret = parent::create_item_permissions_check( $request );
  98. if ( ! $ret || is_wp_error( $ret ) ) {
  99. return $ret;
  100. }
  101. if ( ! current_user_can( 'upload_files' ) ) {
  102. return new WP_Error(
  103. 'rest_cannot_create',
  104. __( 'Sorry, you are not allowed to upload media on this site.' ),
  105. array( 'status' => 400 )
  106. );
  107. }
  108. // Attaching media to a post requires ability to edit said post.
  109. if ( ! empty( $request['post'] ) && ! current_user_can( 'edit_post', (int) $request['post'] ) ) {
  110. return new WP_Error(
  111. 'rest_cannot_edit',
  112. __( 'Sorry, you are not allowed to upload media to this post.' ),
  113. array( 'status' => rest_authorization_required_code() )
  114. );
  115. }
  116. return true;
  117. }
  118. /**
  119. * Creates a single attachment.
  120. *
  121. * @since 4.7.0
  122. *
  123. * @param WP_REST_Request $request Full details about the request.
  124. * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
  125. */
  126. public function create_item( $request ) {
  127. if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
  128. return new WP_Error(
  129. 'rest_invalid_param',
  130. __( 'Invalid parent type.' ),
  131. array( 'status' => 400 )
  132. );
  133. }
  134. $insert = $this->insert_attachment( $request );
  135. if ( is_wp_error( $insert ) ) {
  136. return $insert;
  137. }
  138. $schema = $this->get_item_schema();
  139. // Extract by name.
  140. $attachment_id = $insert['attachment_id'];
  141. $file = $insert['file'];
  142. if ( isset( $request['alt_text'] ) ) {
  143. update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) );
  144. }
  145. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  146. $meta_update = $this->meta->update_value( $request['meta'], $attachment_id );
  147. if ( is_wp_error( $meta_update ) ) {
  148. return $meta_update;
  149. }
  150. }
  151. $attachment = get_post( $attachment_id );
  152. $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
  153. if ( is_wp_error( $fields_update ) ) {
  154. return $fields_update;
  155. }
  156. $request->set_param( 'context', 'edit' );
  157. /**
  158. * Fires after a single attachment is completely created or updated via the REST API.
  159. *
  160. * @since 5.0.0
  161. *
  162. * @param WP_Post $attachment Inserted or updated attachment object.
  163. * @param WP_REST_Request $request Request object.
  164. * @param bool $creating True when creating an attachment, false when updating.
  165. */
  166. do_action( 'rest_after_insert_attachment', $attachment, $request, true );
  167. wp_after_insert_post( $attachment, false, null );
  168. if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
  169. // Set a custom header with the attachment_id.
  170. // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
  171. header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
  172. }
  173. // Include media and image functions to get access to wp_generate_attachment_metadata().
  174. require_once ABSPATH . 'wp-admin/includes/media.php';
  175. require_once ABSPATH . 'wp-admin/includes/image.php';
  176. // Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta.
  177. // At this point the server may run out of resources and post-processing of uploaded images may fail.
  178. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
  179. $response = $this->prepare_item_for_response( $attachment, $request );
  180. $response = rest_ensure_response( $response );
  181. $response->set_status( 201 );
  182. $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $attachment_id ) ) );
  183. return $response;
  184. }
  185. /**
  186. * Inserts the attachment post in the database. Does not update the attachment meta.
  187. *
  188. * @since 5.3.0
  189. *
  190. * @param WP_REST_Request $request
  191. * @return array|WP_Error
  192. */
  193. protected function insert_attachment( $request ) {
  194. // Get the file via $_FILES or raw data.
  195. $files = $request->get_file_params();
  196. $headers = $request->get_headers();
  197. if ( ! empty( $files ) ) {
  198. $file = $this->upload_from_file( $files, $headers );
  199. } else {
  200. $file = $this->upload_from_data( $request->get_body(), $headers );
  201. }
  202. if ( is_wp_error( $file ) ) {
  203. return $file;
  204. }
  205. $name = wp_basename( $file['file'] );
  206. $name_parts = pathinfo( $name );
  207. $name = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) );
  208. $url = $file['url'];
  209. $type = $file['type'];
  210. $file = $file['file'];
  211. // Include image functions to get access to wp_read_image_metadata().
  212. require_once ABSPATH . 'wp-admin/includes/image.php';
  213. // Use image exif/iptc data for title and caption defaults if possible.
  214. $image_meta = wp_read_image_metadata( $file );
  215. if ( ! empty( $image_meta ) ) {
  216. if ( empty( $request['title'] ) && trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
  217. $request['title'] = $image_meta['title'];
  218. }
  219. if ( empty( $request['caption'] ) && trim( $image_meta['caption'] ) ) {
  220. $request['caption'] = $image_meta['caption'];
  221. }
  222. }
  223. $attachment = $this->prepare_item_for_database( $request );
  224. $attachment->post_mime_type = $type;
  225. $attachment->guid = $url;
  226. if ( empty( $attachment->post_title ) ) {
  227. $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
  228. }
  229. // $post_parent is inherited from $attachment['post_parent'].
  230. $id = wp_insert_attachment( wp_slash( (array) $attachment ), $file, 0, true, false );
  231. if ( is_wp_error( $id ) ) {
  232. if ( 'db_update_error' === $id->get_error_code() ) {
  233. $id->add_data( array( 'status' => 500 ) );
  234. } else {
  235. $id->add_data( array( 'status' => 400 ) );
  236. }
  237. return $id;
  238. }
  239. $attachment = get_post( $id );
  240. /**
  241. * Fires after a single attachment is created or updated via the REST API.
  242. *
  243. * @since 4.7.0
  244. *
  245. * @param WP_Post $attachment Inserted or updated attachment
  246. * object.
  247. * @param WP_REST_Request $request The request sent to the API.
  248. * @param bool $creating True when creating an attachment, false when updating.
  249. */
  250. do_action( 'rest_insert_attachment', $attachment, $request, true );
  251. return array(
  252. 'attachment_id' => $id,
  253. 'file' => $file,
  254. );
  255. }
  256. /**
  257. * Updates a single attachment.
  258. *
  259. * @since 4.7.0
  260. *
  261. * @param WP_REST_Request $request Full details about the request.
  262. * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
  263. */
  264. public function update_item( $request ) {
  265. if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
  266. return new WP_Error(
  267. 'rest_invalid_param',
  268. __( 'Invalid parent type.' ),
  269. array( 'status' => 400 )
  270. );
  271. }
  272. $attachment_before = get_post( $request['id'] );
  273. $response = parent::update_item( $request );
  274. if ( is_wp_error( $response ) ) {
  275. return $response;
  276. }
  277. $response = rest_ensure_response( $response );
  278. $data = $response->get_data();
  279. if ( isset( $request['alt_text'] ) ) {
  280. update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] );
  281. }
  282. $attachment = get_post( $request['id'] );
  283. $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
  284. if ( is_wp_error( $fields_update ) ) {
  285. return $fields_update;
  286. }
  287. $request->set_param( 'context', 'edit' );
  288. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
  289. do_action( 'rest_after_insert_attachment', $attachment, $request, false );
  290. wp_after_insert_post( $attachment, true, $attachment_before );
  291. $response = $this->prepare_item_for_response( $attachment, $request );
  292. $response = rest_ensure_response( $response );
  293. return $response;
  294. }
  295. /**
  296. * Performs post processing on an attachment.
  297. *
  298. * @since 5.3.0
  299. *
  300. * @param WP_REST_Request $request Full details about the request.
  301. * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
  302. */
  303. public function post_process_item( $request ) {
  304. switch ( $request['action'] ) {
  305. case 'create-image-subsizes':
  306. require_once ABSPATH . 'wp-admin/includes/image.php';
  307. wp_update_image_subsizes( $request['id'] );
  308. break;
  309. }
  310. $request['context'] = 'edit';
  311. return $this->prepare_item_for_response( get_post( $request['id'] ), $request );
  312. }
  313. /**
  314. * Checks if a given request can perform post processing on an attachment.
  315. *
  316. * @since 5.3.0
  317. *
  318. * @param WP_REST_Request $request Full details about the request.
  319. * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
  320. */
  321. public function post_process_item_permissions_check( $request ) {
  322. return $this->update_item_permissions_check( $request );
  323. }
  324. /**
  325. * Checks if a given request has access to editing media.
  326. *
  327. * @since 5.5.0
  328. *
  329. * @param WP_REST_Request $request Full details about the request.
  330. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  331. */
  332. public function edit_media_item_permissions_check( $request ) {
  333. if ( ! current_user_can( 'upload_files' ) ) {
  334. return new WP_Error(
  335. 'rest_cannot_edit_image',
  336. __( 'Sorry, you are not allowed to upload media on this site.' ),
  337. array( 'status' => rest_authorization_required_code() )
  338. );
  339. }
  340. return $this->update_item_permissions_check( $request );
  341. }
  342. /**
  343. * Applies edits to a media item and creates a new attachment record.
  344. *
  345. * @since 5.5.0
  346. *
  347. * @param WP_REST_Request $request Full details about the request.
  348. * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
  349. */
  350. public function edit_media_item( $request ) {
  351. require_once ABSPATH . 'wp-admin/includes/image.php';
  352. $attachment_id = $request['id'];
  353. // This also confirms the attachment is an image.
  354. $image_file = wp_get_original_image_path( $attachment_id );
  355. $image_meta = wp_get_attachment_metadata( $attachment_id );
  356. if (
  357. ! $image_meta ||
  358. ! $image_file ||
  359. ! wp_image_file_matches_image_meta( $request['src'], $image_meta, $attachment_id )
  360. ) {
  361. return new WP_Error(
  362. 'rest_unknown_attachment',
  363. __( 'Unable to get meta information for file.' ),
  364. array( 'status' => 404 )
  365. );
  366. }
  367. $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' );
  368. $mime_type = get_post_mime_type( $attachment_id );
  369. if ( ! in_array( $mime_type, $supported_types, true ) ) {
  370. return new WP_Error(
  371. 'rest_cannot_edit_file_type',
  372. __( 'This type of file cannot be edited.' ),
  373. array( 'status' => 400 )
  374. );
  375. }
  376. // The `modifiers` param takes precedence over the older format.
  377. if ( isset( $request['modifiers'] ) ) {
  378. $modifiers = $request['modifiers'];
  379. } else {
  380. $modifiers = array();
  381. if ( ! empty( $request['rotation'] ) ) {
  382. $modifiers[] = array(
  383. 'type' => 'rotate',
  384. 'args' => array(
  385. 'angle' => $request['rotation'],
  386. ),
  387. );
  388. }
  389. if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) {
  390. $modifiers[] = array(
  391. 'type' => 'crop',
  392. 'args' => array(
  393. 'left' => $request['x'],
  394. 'top' => $request['y'],
  395. 'width' => $request['width'],
  396. 'height' => $request['height'],
  397. ),
  398. );
  399. }
  400. if ( 0 === count( $modifiers ) ) {
  401. return new WP_Error(
  402. 'rest_image_not_edited',
  403. __( 'The image was not edited. Edit the image before applying the changes.' ),
  404. array( 'status' => 400 )
  405. );
  406. }
  407. }
  408. /*
  409. * If the file doesn't exist, attempt a URL fopen on the src link.
  410. * This can occur with certain file replication plugins.
  411. * Keep the original file path to get a modified name later.
  412. */
  413. $image_file_to_edit = $image_file;
  414. if ( ! file_exists( $image_file_to_edit ) ) {
  415. $image_file_to_edit = _load_image_to_edit_path( $attachment_id );
  416. }
  417. $image_editor = wp_get_image_editor( $image_file_to_edit );
  418. if ( is_wp_error( $image_editor ) ) {
  419. return new WP_Error(
  420. 'rest_unknown_image_file_type',
  421. __( 'Unable to edit this image.' ),
  422. array( 'status' => 500 )
  423. );
  424. }
  425. foreach ( $modifiers as $modifier ) {
  426. $args = $modifier['args'];
  427. switch ( $modifier['type'] ) {
  428. case 'rotate':
  429. // Rotation direction: clockwise vs. counter clockwise.
  430. $rotate = 0 - $args['angle'];
  431. if ( 0 !== $rotate ) {
  432. $result = $image_editor->rotate( $rotate );
  433. if ( is_wp_error( $result ) ) {
  434. return new WP_Error(
  435. 'rest_image_rotation_failed',
  436. __( 'Unable to rotate this image.' ),
  437. array( 'status' => 500 )
  438. );
  439. }
  440. }
  441. break;
  442. case 'crop':
  443. $size = $image_editor->get_size();
  444. $crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 );
  445. $crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 );
  446. $width = round( ( $size['width'] * $args['width'] ) / 100.0 );
  447. $height = round( ( $size['height'] * $args['height'] ) / 100.0 );
  448. if ( $size['width'] !== $width && $size['height'] !== $height ) {
  449. $result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
  450. if ( is_wp_error( $result ) ) {
  451. return new WP_Error(
  452. 'rest_image_crop_failed',
  453. __( 'Unable to crop this image.' ),
  454. array( 'status' => 500 )
  455. );
  456. }
  457. }
  458. break;
  459. }
  460. }
  461. // Calculate the file name.
  462. $image_ext = pathinfo( $image_file, PATHINFO_EXTENSION );
  463. $image_name = wp_basename( $image_file, ".{$image_ext}" );
  464. // Do not append multiple `-edited` to the file name.
  465. // The user may be editing a previously edited image.
  466. if ( preg_match( '/-edited(-\d+)?$/', $image_name ) ) {
  467. // Remove any `-1`, `-2`, etc. `wp_unique_filename()` will add the proper number.
  468. $image_name = preg_replace( '/-edited(-\d+)?$/', '-edited', $image_name );
  469. } else {
  470. // Append `-edited` before the extension.
  471. $image_name .= '-edited';
  472. }
  473. $filename = "{$image_name}.{$image_ext}";
  474. // Create the uploads sub-directory if needed.
  475. $uploads = wp_upload_dir();
  476. // Make the file name unique in the (new) upload directory.
  477. $filename = wp_unique_filename( $uploads['path'], $filename );
  478. // Save to disk.
  479. $saved = $image_editor->save( $uploads['path'] . "/$filename" );
  480. if ( is_wp_error( $saved ) ) {
  481. return $saved;
  482. }
  483. // Create new attachment post.
  484. $new_attachment_post = array(
  485. 'post_mime_type' => $saved['mime-type'],
  486. 'guid' => $uploads['url'] . "/$filename",
  487. 'post_title' => $image_name,
  488. 'post_content' => '',
  489. );
  490. // Copy post_content, post_excerpt, and post_title from the edited image's attachment post.
  491. $attachment_post = get_post( $attachment_id );
  492. if ( $attachment_post ) {
  493. $new_attachment_post['post_content'] = $attachment_post->post_content;
  494. $new_attachment_post['post_excerpt'] = $attachment_post->post_excerpt;
  495. $new_attachment_post['post_title'] = $attachment_post->post_title;
  496. }
  497. $new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true );
  498. if ( is_wp_error( $new_attachment_id ) ) {
  499. if ( 'db_update_error' === $new_attachment_id->get_error_code() ) {
  500. $new_attachment_id->add_data( array( 'status' => 500 ) );
  501. } else {
  502. $new_attachment_id->add_data( array( 'status' => 400 ) );
  503. }
  504. return $new_attachment_id;
  505. }
  506. // Copy the image alt text from the edited image.
  507. $image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
  508. if ( ! empty( $image_alt ) ) {
  509. // update_post_meta() expects slashed.
  510. update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  511. }
  512. if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
  513. // Set a custom header with the attachment_id.
  514. // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
  515. header( 'X-WP-Upload-Attachment-ID: ' . $new_attachment_id );
  516. }
  517. // Generate image sub-sizes and meta.
  518. $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );
  519. // Copy the EXIF metadata from the original attachment if not generated for the edited image.
  520. if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
  521. // Merge but skip empty values.
  522. foreach ( (array) $image_meta['image_meta'] as $key => $value ) {
  523. if ( empty( $new_image_meta['image_meta'][ $key ] ) && ! empty( $value ) ) {
  524. $new_image_meta['image_meta'][ $key ] = $value;
  525. }
  526. }
  527. }
  528. // Reset orientation. At this point the image is edited and orientation is correct.
  529. if ( ! empty( $new_image_meta['image_meta']['orientation'] ) ) {
  530. $new_image_meta['image_meta']['orientation'] = 1;
  531. }
  532. // The attachment_id may change if the site is exported and imported.
  533. $new_image_meta['parent_image'] = array(
  534. 'attachment_id' => $attachment_id,
  535. // Path to the originally uploaded image file relative to the uploads directory.
  536. 'file' => _wp_relative_upload_path( $image_file ),
  537. );
  538. /**
  539. * Filters the meta data for the new image created by editing an existing image.
  540. *
  541. * @since 5.5.0
  542. *
  543. * @param array $new_image_meta Meta data for the new image.
  544. * @param int $new_attachment_id Attachment post ID for the new image.
  545. * @param int $attachment_id Attachment post ID for the edited (parent) image.
  546. */
  547. $new_image_meta = apply_filters( 'wp_edited_image_metadata', $new_image_meta, $new_attachment_id, $attachment_id );
  548. wp_update_attachment_metadata( $new_attachment_id, $new_image_meta );
  549. $response = $this->prepare_item_for_response( get_post( $new_attachment_id ), $request );
  550. $response->set_status( 201 );
  551. $response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $new_attachment_id ) ) );
  552. return $response;
  553. }
  554. /**
  555. * Prepares a single attachment for create or update.
  556. *
  557. * @since 4.7.0
  558. *
  559. * @param WP_REST_Request $request Request object.
  560. * @return stdClass|WP_Error Post object.
  561. */
  562. protected function prepare_item_for_database( $request ) {
  563. $prepared_attachment = parent::prepare_item_for_database( $request );
  564. // Attachment caption (post_excerpt internally).
  565. if ( isset( $request['caption'] ) ) {
  566. if ( is_string( $request['caption'] ) ) {
  567. $prepared_attachment->post_excerpt = $request['caption'];
  568. } elseif ( isset( $request['caption']['raw'] ) ) {
  569. $prepared_attachment->post_excerpt = $request['caption']['raw'];
  570. }
  571. }
  572. // Attachment description (post_content internally).
  573. if ( isset( $request['description'] ) ) {
  574. if ( is_string( $request['description'] ) ) {
  575. $prepared_attachment->post_content = $request['description'];
  576. } elseif ( isset( $request['description']['raw'] ) ) {
  577. $prepared_attachment->post_content = $request['description']['raw'];
  578. }
  579. }
  580. if ( isset( $request['post'] ) ) {
  581. $prepared_attachment->post_parent = (int) $request['post'];
  582. }
  583. return $prepared_attachment;
  584. }
  585. /**
  586. * Prepares a single attachment output for response.
  587. *
  588. * @since 4.7.0
  589. *
  590. * @param WP_Post $post Attachment object.
  591. * @param WP_REST_Request $request Request object.
  592. * @return WP_REST_Response Response object.
  593. */
  594. public function prepare_item_for_response( $post, $request ) {
  595. $response = parent::prepare_item_for_response( $post, $request );
  596. $fields = $this->get_fields_for_response( $request );
  597. $data = $response->get_data();
  598. if ( in_array( 'description', $fields, true ) ) {
  599. $data['description'] = array(
  600. 'raw' => $post->post_content,
  601. /** This filter is documented in wp-includes/post-template.php */
  602. 'rendered' => apply_filters( 'the_content', $post->post_content ),
  603. );
  604. }
  605. if ( in_array( 'caption', $fields, true ) ) {
  606. /** This filter is documented in wp-includes/post-template.php */
  607. $caption = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
  608. /** This filter is documented in wp-includes/post-template.php */
  609. $caption = apply_filters( 'the_excerpt', $caption );
  610. $data['caption'] = array(
  611. 'raw' => $post->post_excerpt,
  612. 'rendered' => $caption,
  613. );
  614. }
  615. if ( in_array( 'alt_text', $fields, true ) ) {
  616. $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  617. }
  618. if ( in_array( 'media_type', $fields, true ) ) {
  619. $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
  620. }
  621. if ( in_array( 'mime_type', $fields, true ) ) {
  622. $data['mime_type'] = $post->post_mime_type;
  623. }
  624. if ( in_array( 'media_details', $fields, true ) ) {
  625. $data['media_details'] = wp_get_attachment_metadata( $post->ID );
  626. // Ensure empty details is an empty object.
  627. if ( empty( $data['media_details'] ) ) {
  628. $data['media_details'] = new stdClass;
  629. } elseif ( ! empty( $data['media_details']['sizes'] ) ) {
  630. foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {
  631. if ( isset( $size_data['mime-type'] ) ) {
  632. $size_data['mime_type'] = $size_data['mime-type'];
  633. unset( $size_data['mime-type'] );
  634. }
  635. // Use the same method image_downsize() does.
  636. $image_src = wp_get_attachment_image_src( $post->ID, $size );
  637. if ( ! $image_src ) {
  638. continue;
  639. }
  640. $size_data['source_url'] = $image_src[0];
  641. }
  642. $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
  643. if ( ! empty( $full_src ) ) {
  644. $data['media_details']['sizes']['full'] = array(
  645. 'file' => wp_basename( $full_src[0] ),
  646. 'width' => $full_src[1],
  647. 'height' => $full_src[2],
  648. 'mime_type' => $post->post_mime_type,
  649. 'source_url' => $full_src[0],
  650. );
  651. }
  652. } else {
  653. $data['media_details']['sizes'] = new stdClass;
  654. }
  655. }
  656. if ( in_array( 'post', $fields, true ) ) {
  657. $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
  658. }
  659. if ( in_array( 'source_url', $fields, true ) ) {
  660. $data['source_url'] = wp_get_attachment_url( $post->ID );
  661. }
  662. if ( in_array( 'missing_image_sizes', $fields, true ) ) {
  663. require_once ABSPATH . 'wp-admin/includes/image.php';
  664. $data['missing_image_sizes'] = array_keys( wp_get_missing_image_subsizes( $post->ID ) );
  665. }
  666. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  667. $data = $this->filter_response_by_context( $data, $context );
  668. $links = $response->get_links();
  669. // Wrap the data in a response object.
  670. $response = rest_ensure_response( $data );
  671. foreach ( $links as $rel => $rel_links ) {
  672. foreach ( $rel_links as $link ) {
  673. $response->add_link( $rel, $link['href'], $link['attributes'] );
  674. }
  675. }
  676. /**
  677. * Filters an attachment returned from the REST API.
  678. *
  679. * Allows modification of the attachment right before it is returned.
  680. *
  681. * @since 4.7.0
  682. *
  683. * @param WP_REST_Response $response The response object.
  684. * @param WP_Post $post The original attachment post.
  685. * @param WP_REST_Request $request Request used to generate the response.
  686. */
  687. return apply_filters( 'rest_prepare_attachment', $response, $post, $request );
  688. }
  689. /**
  690. * Retrieves the attachment's schema, conforming to JSON Schema.
  691. *
  692. * @since 4.7.0
  693. *
  694. * @return array Item schema as an array.
  695. */
  696. public function get_item_schema() {
  697. if ( $this->schema ) {
  698. return $this->add_additional_fields_schema( $this->schema );
  699. }
  700. $schema = parent::get_item_schema();
  701. $schema['properties']['alt_text'] = array(
  702. 'description' => __( 'Alternative text to display when attachment is not displayed.' ),
  703. 'type' => 'string',
  704. 'context' => array( 'view', 'edit', 'embed' ),
  705. 'arg_options' => array(
  706. 'sanitize_callback' => 'sanitize_text_field',
  707. ),
  708. );
  709. $schema['properties']['caption'] = array(
  710. 'description' => __( 'The attachment caption.' ),
  711. 'type' => 'object',
  712. 'context' => array( 'view', 'edit', 'embed' ),
  713. 'arg_options' => array(
  714. 'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
  715. 'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
  716. ),
  717. 'properties' => array(
  718. 'raw' => array(
  719. 'description' => __( 'Caption for the attachment, as it exists in the database.' ),
  720. 'type' => 'string',
  721. 'context' => array( 'edit' ),
  722. ),
  723. 'rendered' => array(
  724. 'description' => __( 'HTML caption for the attachment, transformed for display.' ),
  725. 'type' => 'string',
  726. 'context' => array( 'view', 'edit', 'embed' ),
  727. 'readonly' => true,
  728. ),
  729. ),
  730. );
  731. $schema['properties']['description'] = array(
  732. 'description' => __( 'The attachment description.' ),
  733. 'type' => 'object',
  734. 'context' => array( 'view', 'edit' ),
  735. 'arg_options' => array(
  736. 'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
  737. 'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
  738. ),
  739. 'properties' => array(
  740. 'raw' => array(
  741. 'description' => __( 'Description for the attachment, as it exists in the database.' ),
  742. 'type' => 'string',
  743. 'context' => array( 'edit' ),
  744. ),
  745. 'rendered' => array(
  746. 'description' => __( 'HTML description for the attachment, transformed for display.' ),
  747. 'type' => 'string',
  748. 'context' => array( 'view', 'edit' ),
  749. 'readonly' => true,
  750. ),
  751. ),
  752. );
  753. $schema['properties']['media_type'] = array(
  754. 'description' => __( 'Attachment type.' ),
  755. 'type' => 'string',
  756. 'enum' => array( 'image', 'file' ),
  757. 'context' => array( 'view', 'edit', 'embed' ),
  758. 'readonly' => true,
  759. );
  760. $schema['properties']['mime_type'] = array(
  761. 'description' => __( 'The attachment MIME type.' ),
  762. 'type' => 'string',
  763. 'context' => array( 'view', 'edit', 'embed' ),
  764. 'readonly' => true,
  765. );
  766. $schema['properties']['media_details'] = array(
  767. 'description' => __( 'Details about the media file, specific to its type.' ),
  768. 'type' => 'object',
  769. 'context' => array( 'view', 'edit', 'embed' ),
  770. 'readonly' => true,
  771. );
  772. $schema['properties']['post'] = array(
  773. 'description' => __( 'The ID for the associated post of the attachment.' ),
  774. 'type' => 'integer',
  775. 'context' => array( 'view', 'edit' ),
  776. );
  777. $schema['properties']['source_url'] = array(
  778. 'description' => __( 'URL to the original attachment file.' ),
  779. 'type' => 'string',
  780. 'format' => 'uri',
  781. 'context' => array( 'view', 'edit', 'embed' ),
  782. 'readonly' => true,
  783. );
  784. $schema['properties']['missing_image_sizes'] = array(
  785. 'description' => __( 'List of the missing image sizes of the attachment.' ),
  786. 'type' => 'array',
  787. 'items' => array( 'type' => 'string' ),
  788. 'context' => array( 'edit' ),
  789. 'readonly' => true,
  790. );
  791. unset( $schema['properties']['password'] );
  792. $this->schema = $schema;
  793. return $this->add_additional_fields_schema( $this->schema );
  794. }
  795. /**
  796. * Handles an upload via raw POST data.
  797. *
  798. * @since 4.7.0
  799. *
  800. * @param array $data Supplied file data.
  801. * @param array $headers HTTP headers from the request.
  802. * @return array|WP_Error Data from wp_handle_sideload().
  803. */
  804. protected function upload_from_data( $data, $headers ) {
  805. if ( empty( $data ) ) {
  806. return new WP_Error(
  807. 'rest_upload_no_data',
  808. __( 'No data supplied.' ),
  809. array( 'status' => 400 )
  810. );
  811. }
  812. if ( empty( $headers['content_type'] ) ) {
  813. return new WP_Error(
  814. 'rest_upload_no_content_type',
  815. __( 'No Content-Type supplied.' ),
  816. array( 'status' => 400 )
  817. );
  818. }
  819. if ( empty( $headers['content_disposition'] ) ) {
  820. return new WP_Error(
  821. 'rest_upload_no_content_disposition',
  822. __( 'No Content-Disposition supplied.' ),
  823. array( 'status' => 400 )
  824. );
  825. }
  826. $filename = self::get_filename_from_disposition( $headers['content_disposition'] );
  827. if ( empty( $filename ) ) {
  828. return new WP_Error(
  829. 'rest_upload_invalid_disposition',
  830. __( 'Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.' ),
  831. array( 'status' => 400 )
  832. );
  833. }
  834. if ( ! empty( $headers['content_md5'] ) ) {
  835. $content_md5 = array_shift( $headers['content_md5'] );
  836. $expected = trim( $content_md5 );
  837. $actual = md5( $data );
  838. if ( $expected !== $actual ) {
  839. return new WP_Error(
  840. 'rest_upload_hash_mismatch',
  841. __( 'Content hash did not match expected.' ),
  842. array( 'status' => 412 )
  843. );
  844. }
  845. }
  846. // Get the content-type.
  847. $type = array_shift( $headers['content_type'] );
  848. // Include filesystem functions to get access to wp_tempnam() and wp_handle_sideload().
  849. require_once ABSPATH . 'wp-admin/includes/file.php';
  850. // Save the file.
  851. $tmpfname = wp_tempnam( $filename );
  852. $fp = fopen( $tmpfname, 'w+' );
  853. if ( ! $fp ) {
  854. return new WP_Error(
  855. 'rest_upload_file_error',
  856. __( 'Could not open file handle.' ),
  857. array( 'status' => 500 )
  858. );
  859. }
  860. fwrite( $fp, $data );
  861. fclose( $fp );
  862. // Now, sideload it in.
  863. $file_data = array(
  864. 'error' => null,
  865. 'tmp_name' => $tmpfname,
  866. 'name' => $filename,
  867. 'type' => $type,
  868. );
  869. $size_check = self::check_upload_size( $file_data );
  870. if ( is_wp_error( $size_check ) ) {
  871. return $size_check;
  872. }
  873. $overrides = array(
  874. 'test_form' => false,
  875. );
  876. $sideloaded = wp_handle_sideload( $file_data, $overrides );
  877. if ( isset( $sideloaded['error'] ) ) {
  878. @unlink( $tmpfname );
  879. return new WP_Error(
  880. 'rest_upload_sideload_error',
  881. $sideloaded['error'],
  882. array( 'status' => 500 )
  883. );
  884. }
  885. return $sideloaded;
  886. }
  887. /**
  888. * Parses filename from a Content-Disposition header value.
  889. *
  890. * As per RFC6266:
  891. *
  892. * content-disposition = "Content-Disposition" ":"
  893. * disposition-type *( ";" disposition-parm )
  894. *
  895. * disposition-type = "inline" | "attachment" | disp-ext-type
  896. * ; case-insensitive
  897. * disp-ext-type = token
  898. *
  899. * disposition-parm = filename-parm | disp-ext-parm
  900. *
  901. * filename-parm = "filename" "=" value
  902. * | "filename*" "=" ext-value
  903. *
  904. * disp-ext-parm = token "=" value
  905. * | ext-token "=" ext-value
  906. * ext-token = <the characters in token, followed by "*">
  907. *
  908. * @since 4.7.0
  909. *
  910. * @link https://tools.ietf.org/html/rfc2388
  911. * @link https://tools.ietf.org/html/rfc6266
  912. *
  913. * @param string[] $disposition_header List of Content-Disposition header values.
  914. * @return string|null Filename if available, or null if not found.
  915. */
  916. public static function get_filename_from_disposition( $disposition_header ) {
  917. // Get the filename.
  918. $filename = null;
  919. foreach ( $disposition_header as $value ) {
  920. $value = trim( $value );
  921. if ( strpos( $value, ';' ) === false ) {
  922. continue;
  923. }
  924. list( $type, $attr_parts ) = explode( ';', $value, 2 );
  925. $attr_parts = explode( ';', $attr_parts );
  926. $attributes = array();
  927. foreach ( $attr_parts as $part ) {
  928. if ( strpos( $part, '=' ) === false ) {
  929. continue;
  930. }
  931. list( $key, $value ) = explode( '=', $part, 2 );
  932. $attributes[ trim( $key ) ] = trim( $value );
  933. }
  934. if ( empty( $attributes['filename'] ) ) {
  935. continue;
  936. }
  937. $filename = trim( $attributes['filename'] );
  938. // Unquote quoted filename, but after trimming.
  939. if ( substr( $filename, 0, 1 ) === '"' && substr( $filename, -1, 1 ) === '"' ) {
  940. $filename = substr( $filename, 1, -1 );
  941. }
  942. }
  943. return $filename;
  944. }
  945. /**
  946. * Retrieves the query params for collections of attachments.
  947. *
  948. * @since 4.7.0
  949. *
  950. * @return array Query parameters for the attachment collection as an array.
  951. */
  952. public function get_collection_params() {
  953. $params = parent::get_collection_params();
  954. $params['status']['default'] = 'inherit';
  955. $params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' );
  956. $media_types = $this->get_media_types();
  957. $params['media_type'] = array(
  958. 'default' => null,
  959. 'description' => __( 'Limit result set to attachments of a particular media type.' ),
  960. 'type' => 'string',
  961. 'enum' => array_keys( $media_types ),
  962. );
  963. $params['mime_type'] = array(
  964. 'default' => null,
  965. 'description' => __( 'Limit result set to attachments of a particular MIME type.' ),
  966. 'type' => 'string',
  967. );
  968. return $params;
  969. }
  970. /**
  971. * Handles an upload via multipart/form-data ($_FILES).
  972. *
  973. * @since 4.7.0
  974. *
  975. * @param array $files Data from the `$_FILES` superglobal.
  976. * @param array $headers HTTP headers from the request.
  977. * @return array|WP_Error Data from wp_handle_upload().
  978. */
  979. protected function upload_from_file( $files, $headers ) {
  980. if ( empty( $files ) ) {
  981. return new WP_Error(
  982. 'rest_upload_no_data',
  983. __( 'No data supplied.' ),
  984. array( 'status' => 400 )
  985. );
  986. }
  987. // Verify hash, if given.
  988. if ( ! empty( $headers['content_md5'] ) ) {
  989. $content_md5 = array_shift( $headers['content_md5'] );
  990. $expected = trim( $content_md5 );
  991. $actual = md5_file( $files['file']['tmp_name'] );
  992. if ( $expected !== $actual ) {
  993. return new WP_Error(
  994. 'rest_upload_hash_mismatch',
  995. __( 'Content hash did not match expected.' ),
  996. array( 'status' => 412 )
  997. );
  998. }
  999. }
  1000. // Pass off to WP to handle the actual upload.
  1001. $overrides = array(
  1002. 'test_form' => false,
  1003. );
  1004. // Bypasses is_uploaded_file() when running unit tests.
  1005. if ( defined( 'DIR_TESTDATA' ) && DIR_TESTDATA ) {
  1006. $overrides['action'] = 'wp_handle_mock_upload';
  1007. }
  1008. $size_check = self::check_upload_size( $files['file'] );
  1009. if ( is_wp_error( $size_check ) ) {
  1010. return $size_check;
  1011. }
  1012. // Include filesystem functions to get access to wp_handle_upload().
  1013. require_once ABSPATH . 'wp-admin/includes/file.php';
  1014. $file = wp_handle_upload( $files['file'], $overrides );
  1015. if ( isset( $file['error'] ) ) {
  1016. return new WP_Error(
  1017. 'rest_upload_unknown_error',
  1018. $file['error'],
  1019. array( 'status' => 500 )
  1020. );
  1021. }
  1022. return $file;
  1023. }
  1024. /**
  1025. * Retrieves the supported media types.
  1026. *
  1027. * Media types are considered the MIME type category.
  1028. *
  1029. * @since 4.7.0
  1030. *
  1031. * @return array Array of supported media types.
  1032. */
  1033. protected function get_media_types() {
  1034. $media_types = array();
  1035. foreach ( get_allowed_mime_types() as $mime_type ) {
  1036. $parts = explode( '/', $mime_type );
  1037. if ( ! isset( $media_types[ $parts[0] ] ) ) {
  1038. $media_types[ $parts[0] ] = array();
  1039. }
  1040. $media_types[ $parts[0] ][] = $mime_type;
  1041. }
  1042. return $media_types;
  1043. }
  1044. /**
  1045. * Determine if uploaded file exceeds space quota on multisite.
  1046. *
  1047. * Replicates check_upload_size().
  1048. *
  1049. * @since 4.9.8
  1050. *
  1051. * @param array $file $_FILES array for a given file.
  1052. * @return true|WP_Error True if can upload, error for errors.
  1053. */
  1054. protected function check_upload_size( $file ) {
  1055. if ( ! is_multisite() ) {
  1056. return true;
  1057. }
  1058. if ( get_site_option( 'upload_space_check_disabled' ) ) {
  1059. return true;
  1060. }
  1061. $space_left = get_upload_space_available();
  1062. $file_size = filesize( $file['tmp_name'] );
  1063. if ( $space_left < $file_size ) {
  1064. return new WP_Error(
  1065. 'rest_upload_limited_space',
  1066. /* translators: %s: Required disk space in kilobytes. */
  1067. sprintf( __( 'Not enough space to upload. %s KB needed.' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) ),
  1068. array( 'status' => 400 )
  1069. );
  1070. }
  1071. if ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
  1072. return new WP_Error(
  1073. 'rest_upload_file_too_big',
  1074. /* translators: %s: Maximum allowed file size in kilobytes. */
  1075. sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) ),
  1076. array( 'status' => 400 )
  1077. );
  1078. }
  1079. // Include multisite admin functions to get access to upload_is_user_over_quota().
  1080. require_once ABSPATH . 'wp-admin/includes/ms.php';
  1081. if ( upload_is_user_over_quota( false ) ) {
  1082. return new WP_Error(
  1083. 'rest_upload_user_quota_exceeded',
  1084. __( 'You have used your space quota. Please delete files before uploading.' ),
  1085. array( 'status' => 400 )
  1086. );
  1087. }
  1088. return true;
  1089. }
  1090. /**
  1091. * Gets the request args for the edit item route.
  1092. *
  1093. * @since 5.5.0
  1094. *
  1095. * @return array
  1096. */
  1097. protected function get_edit_media_item_args() {
  1098. return array(
  1099. 'src' => array(
  1100. 'description' => __( 'URL to the edited image file.' ),
  1101. 'type' => 'string',
  1102. 'format' => 'uri',
  1103. 'required' => true,
  1104. ),
  1105. 'modifiers' => array(
  1106. 'description' => __( 'Array of image edits.' ),
  1107. 'type' => 'array',
  1108. 'minItems' => 1,
  1109. 'items' => array(
  1110. 'description' => __( 'Image edit.' ),
  1111. 'type' => 'object',
  1112. 'required' => array(
  1113. 'type',
  1114. 'args',
  1115. ),
  1116. 'oneOf' => array(
  1117. array(
  1118. 'title' => __( 'Rotation' ),
  1119. 'properties' => array(
  1120. 'type' => array(
  1121. 'description' => __( 'Rotation type.' ),
  1122. 'type' => 'string',
  1123. 'enum' => array( 'rotate' ),
  1124. ),
  1125. 'args' => array(
  1126. 'description' => __( 'Rotation arguments.' ),
  1127. 'type' => 'object',
  1128. 'required' => array(
  1129. 'angle',
  1130. ),
  1131. 'properties' => array(
  1132. 'angle' => array(
  1133. 'description' => __( 'Angle to rotate clockwise in degrees.' ),
  1134. 'type' => 'number',
  1135. ),
  1136. ),
  1137. ),
  1138. ),
  1139. ),
  1140. array(
  1141. 'title' => __( 'Crop' ),
  1142. 'properties' => array(
  1143. 'type' => array(
  1144. 'description' => __( 'Crop type.' ),
  1145. 'type' => 'string',
  1146. 'enum' => array( 'crop' ),
  1147. ),
  1148. 'args' => array(
  1149. 'description' => __( 'Crop arguments.' ),
  1150. 'type' => 'object',
  1151. 'required' => array(
  1152. 'left',
  1153. 'top',
  1154. 'width',
  1155. 'height',
  1156. ),
  1157. 'properties' => array(
  1158. 'left' => array(
  1159. 'description' => __( 'Horizontal position from the left to begin the crop as a percentage of the image width.' ),
  1160. 'type' => 'number',
  1161. ),
  1162. 'top' => array(
  1163. 'description' => __( 'Vertical position from the top to begin the crop as a percentage of the image height.' ),
  1164. 'type' => 'number',
  1165. ),
  1166. 'width' => array(
  1167. 'description' => __( 'Width of the crop as a percentage of the image width.' ),
  1168. 'type' => 'number',
  1169. ),
  1170. 'height' => array(
  1171. 'description' => __( 'Height of the crop as a percentage of the image height.' ),
  1172. 'type' => 'number',
  1173. ),
  1174. ),
  1175. ),
  1176. ),
  1177. ),
  1178. ),
  1179. ),
  1180. ),
  1181. 'rotation' => array(
  1182. 'description' => __( 'The amount to rotate the image clockwise in degrees. DEPRECATED: Use `modifiers` instead.' ),
  1183. 'type' => 'integer',
  1184. 'minimum' => 0,
  1185. 'exclusiveMinimum' => true,
  1186. 'maximum' => 360,
  1187. 'exclusiveMaximum' => true,
  1188. ),
  1189. 'x' => array(
  1190. 'description' => __( 'As a percentage of the image, the x position to start the crop from. DEPRECATED: Use `modifiers` instead.' ),
  1191. 'type' => 'number',
  1192. 'minimum' => 0,
  1193. 'maximum' => 100,
  1194. ),
  1195. 'y' => array(
  1196. 'description' => __( 'As a percentage of the image, the y position to start the crop from. DEPRECATED: Use `modifiers` instead.' ),
  1197. 'type' => 'number',
  1198. 'minimum' => 0,
  1199. 'maximum' => 100,
  1200. ),
  1201. 'width' => array(
  1202. 'description' => __( 'As a percentage of the image, the width to crop the image to. DEPRECATED: Use `modifiers` instead.' ),
  1203. 'type' => 'number',
  1204. 'minimum' => 0,
  1205. 'maximum' => 100,
  1206. ),
  1207. 'height' => array(
  1208. 'description' => __( 'As a percentage of the image, the height to crop the image to. DEPRECATED: Use `modifiers` instead.' ),
  1209. 'type' => 'number',
  1210. 'minimum' => 0,
  1211. 'maximum' => 100,
  1212. ),
  1213. );
  1214. }
  1215. }