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