| | 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 | $is_retry = true; |
| | 115 | } else { |
| | 116 | $id_and_file = $this->insert_attachment( $request ); |
| | 117 | |
| | 118 | if ( is_wp_error( $id_and_file ) ) { |
| | 119 | return $id_and_file; |
| | 120 | } |
| | 121 | |
| | 122 | list( $id, $file ) = $id_and_file; |
| | 123 | $is_retry = false; |
| | 124 | } |
| | 125 | |
| | 126 | if ( is_wp_error( $id ) ) { |
| | 127 | return $id; |
| | 128 | } |
| | 129 | |
| | 130 | $attachment = get_post( $id ); |
| | 131 | |
| | 132 | /** |
| | 133 | * Fires after a single attachment is created or updated via the REST API. |
| | 134 | * |
| | 135 | * @since 4.7.0 |
| | 136 | * |
| | 137 | * @param WP_Post $attachment Inserted or updated attachment |
| | 138 | * object. |
| | 139 | * @param WP_REST_Request $request The request sent to the API. |
| | 140 | * @param bool $creating True when creating an attachment, false when updating. |
| | 141 | */ |
| | 142 | do_action( 'rest_insert_attachment', $attachment, $request, true ); |
| | 143 | |
| | 144 | // Include admin function to get access to wp_generate_attachment_metadata(). |
| | 145 | require_once ABSPATH . 'wp-admin/includes/media.php'; |
| | 146 | |
| | 147 | if ( $is_retry ) { |
| | 148 | wp_update_image_subsizes( $id ); |
| | 149 | _wp_clear_upload_ref( $retry_ref ); |
| | 150 | } else { |
| | 151 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
| | 152 | |
| | 153 | if ( $request->get_header( 'X-WP-UploadRef' ) ) { |
| | 154 | // Include upload ref functions. |
| | 155 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| | 156 | _wp_clear_upload_ref( $request->get_header( 'X-WP-UploadRef' ) ); |
| | 157 | } |
| | 158 | } |
| | 159 | |
| | 160 | if ( isset( $request['alt_text'] ) ) { |
| | 161 | update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); |
| | 162 | } |
| | 163 | |
| | 164 | $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); |
| | 165 | |
| | 166 | if ( is_wp_error( $fields_update ) ) { |
| | 167 | return $fields_update; |
| | 168 | } |
| | 169 | |
| | 170 | $request->set_param( 'context', 'edit' ); |
| | 171 | |
| | 172 | /** |
| | 173 | * Fires after a single attachment is completely created or updated via the REST API. |
| | 174 | * |
| | 175 | * @since 5.0.0 |
| | 176 | * |
| | 177 | * @param WP_Post $attachment Inserted or updated attachment object. |
| | 178 | * @param WP_REST_Request $request Request object. |
| | 179 | * @param bool $creating True when creating an attachment, false when updating. |
| | 180 | */ |
| | 181 | do_action( 'rest_after_insert_attachment', $attachment, $request, true ); |
| | 182 | |
| | 183 | $response = $this->prepare_item_for_response( $attachment, $request ); |
| | 184 | $response = rest_ensure_response( $response ); |
| | 185 | $response->set_status( 201 ); |
| | 186 | $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
| | 187 | |
| | 188 | return $response; |
| | 189 | } |
| | 190 | |
| | 191 | /** |
| | 192 | * Inserts the attachment in the database. |
| | 193 | * |
| | 194 | * @since 5.3.0 |
| | 195 | * |
| | 196 | * @param WP_REST_Request $request |
| | 197 | * |
| | 198 | * @return array|WP_Error |
| | 199 | */ |
| | 200 | protected function insert_attachment( $request ) { |
| 161 | | $attachment = get_post( $id ); |
| 162 | | |
| 163 | | /** |
| 164 | | * Fires after a single attachment is created or updated via the REST API. |
| 165 | | * |
| 166 | | * @since 4.7.0 |
| 167 | | * |
| 168 | | * @param WP_Post $attachment Inserted or updated attachment |
| 169 | | * object. |
| 170 | | * @param WP_REST_Request $request The request sent to the API. |
| 171 | | * @param bool $creating True when creating an attachment, false when updating. |
| 172 | | */ |
| 173 | | do_action( 'rest_insert_attachment', $attachment, $request, true ); |
| 174 | | |
| 175 | | // Include admin function to get access to wp_generate_attachment_metadata(). |
| 176 | | require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 177 | | |
| 178 | | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
| 179 | | |
| 180 | | if ( isset( $request['alt_text'] ) ) { |
| 181 | | update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); |
| 182 | | } |
| | 259 | if ( $request->get_header( 'X-WP-UploadRef' ) ) { |
| | 260 | if ( ! wp_attachment_is_image( $id ) ) { |
| | 261 | return new WP_Error( 'invalid_upload_ref', __( 'Upload references can only be used for image attachments.' ), array( 'status' => 400 ) ); |
| | 262 | } |
| 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; |
| | 274 | return array( $id, $file ); |