| | 103 | $retry_ref = $request->get_header( 'X-WP-RetryUploadRef' ); |
| | 104 | |
| | 105 | if ( $retry_ref ) { |
| | 106 | // Include upload ref functions. |
| | 107 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| | 108 | $id = _wp_get_upload_ref_attachment_id( $retry_ref ); |
| | 109 | |
| | 110 | if ( ! $id ) { |
| | 111 | return new WP_Error( 'upload_reference_not_found', __( 'Upload failed. Please try again.' ), array( 'status' => 500 ) ); |
| | 112 | } |
| | 113 | |
| | 114 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| | 115 | wp_update_image_subsizes( $id ); |
| | 116 | _wp_clear_upload_ref( $retry_ref ); |
| | 117 | } else { |
| | 118 | $id = $this->insert_attachment( $request ); |
| | 119 | |
| | 120 | if ( is_wp_error( $id ) ) { |
| | 121 | return $id; |
| | 122 | } |
| | 123 | } |
| | 124 | |
| | 125 | $attachment = get_post( $id ); |
| | 126 | |
| | 127 | if ( isset( $request['alt_text'] ) ) { |
| | 128 | update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); |
| | 129 | } |
| | 130 | |
| | 131 | $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); |
| | 132 | |
| | 133 | if ( is_wp_error( $fields_update ) ) { |
| | 134 | return $fields_update; |
| | 135 | } |
| | 136 | |
| | 137 | $request->set_param( 'context', 'edit' ); |
| | 138 | |
| | 139 | /** |
| | 140 | * Fires after a single attachment is completely created or updated via the REST API. |
| | 141 | * |
| | 142 | * @since 5.0.0 |
| | 143 | * |
| | 144 | * @param WP_Post $attachment Inserted or updated attachment object. |
| | 145 | * @param WP_REST_Request $request Request object. |
| | 146 | * @param bool $creating True when creating an attachment, false when updating. |
| | 147 | */ |
| | 148 | do_action( 'rest_after_insert_attachment', $attachment, $request, true ); |
| | 149 | |
| | 150 | $response = $this->prepare_item_for_response( $attachment, $request ); |
| | 151 | $response = rest_ensure_response( $response ); |
| | 152 | $response->set_status( 201 ); |
| | 153 | $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
| | 154 | |
| | 155 | return $response; |
| | 156 | } |
| | 157 | |
| | 158 | /** |
| | 159 | * Inserts the attachment in the database. |
| | 160 | * |
| | 161 | * @since 5.3.0 |
| | 162 | * |
| | 163 | * @param WP_REST_Request $request |
| | 164 | * |
| | 165 | * @return int|WP_Error |
| | 166 | */ |
| | 167 | protected function insert_attachment( $request ) { |
| | 226 | if ( $request->get_header( 'X-WP-UploadRef' ) ) { |
| | 227 | if ( ! wp_attachment_is_image( $id ) ) { |
| | 228 | return new WP_Error( 'invalid_upload_ref', __( 'Upload references can only be used for image attachments.' ), array( 'status' => 400 ) ); |
| | 229 | } |
| | 230 | |
| | 231 | // Include upload ref functions. |
| | 232 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| | 233 | $set_ref = _wp_set_upload_ref( $request->get_header( 'X-WP-UploadRef' ), $id ); |
| | 234 | |
| | 235 | if ( is_wp_error( $set_ref ) ) { |
| | 236 | // TODO: cleanup attachment data. |
| | 237 | return $set_ref; |
| | 238 | } |
| | 239 | } |
| | 240 | |
| 180 | | if ( isset( $request['alt_text'] ) ) { |
| 181 | | update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); |
| 182 | | } |
| 183 | | |
| 184 | | $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); |
| 185 | | |
| 186 | | if ( is_wp_error( $fields_update ) ) { |
| 187 | | return $fields_update; |
| | 259 | if ( $request->get_header( 'X-WP-UploadRef' ) ) { |
| | 260 | // Include upload ref functions. |
| | 261 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| | 262 | _wp_clear_upload_ref( $request->get_header( 'X-WP-UploadRef' ) ); |
| 190 | | $request->set_param( 'context', 'edit' ); |
| 191 | | |
| 192 | | /** |
| 193 | | * Fires after a single attachment is completely created or updated via the REST API. |
| 194 | | * |
| 195 | | * @since 5.0.0 |
| 196 | | * |
| 197 | | * @param WP_Post $attachment Inserted or updated attachment object. |
| 198 | | * @param WP_REST_Request $request Request object. |
| 199 | | * @param bool $creating True when creating an attachment, false when updating. |
| 200 | | */ |
| 201 | | do_action( 'rest_after_insert_attachment', $attachment, $request, true ); |
| 202 | | |
| 203 | | $response = $this->prepare_item_for_response( $attachment, $request ); |
| 204 | | $response = rest_ensure_response( $response ); |
| 205 | | $response->set_status( 201 ); |
| 206 | | $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
| 207 | | |
| 208 | | return $response; |
| | 265 | return $id; |