← All changes
|
jetpack_vendor/automattic/jetpack-import/src/endpoints/class-attachment.php
+39
-19
12.0.3
→
16.3
View file →
| @@ -6,8 +6,16 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Import\Endpoints; |
| 9 | 9 | |
| 10 | +use WP_Error; | |
| 11 | +use WP_REST_Request; | |
| 12 | +use WP_REST_Response; | |
| 13 | + | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 10 | 18 | /** |
| 11 | 19 | * Class Attachment |
| 12 | 20 | */ |
| 13 | 21 | class Attachment extends \WP_REST_Attachments_Controller { |
| @@ -12,11 +20,16 @@ | ||
| 12 | 20 | */ |
| 13 | 21 | class Attachment extends \WP_REST_Attachments_Controller { |
| 14 | 22 | |
| 15 | 23 | /** |
| 24 | + * Base class | |
| 25 | + */ | |
| 26 | + use Import; | |
| 27 | + | |
| 28 | + /** | |
| 16 | 29 | * The Import ID add a new item to the schema. |
| 17 | 30 | */ |
| 18 | - use Import; | |
| 31 | + use Import_ID; | |
| 19 | 32 | |
| 20 | 33 | /** |
| 21 | 34 | * Whether the controller supports batching. Default false. |
| 22 | 35 | * |
| @@ -95,8 +108,10 @@ | ||
| 95 | 108 | * @param WP_REST_Request $request Full details about the request. |
| 96 | 109 | * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. |
| 97 | 110 | */ |
| 98 | 111 | public function create_item( $request ) { |
| 112 | + // Set the WP_IMPORTING constant to prevent sync notifications | |
| 113 | + $this->set_importing(); | |
| 99 | 114 | $file_info = $this->get_file_info( $request ); |
| 100 | 115 | $attachment = $this->get_attachment_by_file_info( $file_info ); |
| 101 | 116 | if ( $attachment ) { |
| 102 | 117 | $response = $this->prepare_attachment_for_response( $attachment, $request ); |
| @@ -104,9 +119,9 @@ | ||
| 104 | 119 | if ( \is_wp_error( $response ) ) { |
| 105 | 120 | return $response; |
| 106 | 121 | } |
| 107 | 122 | |
| 108 | - return new \WP_Error( | |
| 123 | + return new WP_Error( | |
| 109 | 124 | 'attachment_exists', |
| 110 | 125 | __( 'The attachment already exists.', 'jetpack-import' ), |
| 111 | 126 | array( |
| 112 | 127 | 'status' => 409, |
| @@ -116,8 +131,10 @@ | ||
| 116 | 131 | ); |
| 117 | 132 | } |
| 118 | 133 | |
| 119 | 134 | $this->set_upload_dir( $request ); |
| 135 | + // Disable scaled image generation. | |
| 136 | + add_filter( 'big_image_size_threshold', '__return_false' ); | |
| 120 | 137 | return parent::create_item( $request ); |
| 121 | 138 | } |
| 122 | 139 | |
| 123 | 140 | /** |
| @@ -140,9 +157,8 @@ | ||
| 140 | 157 | * @param array $schema Schema array. |
| 141 | 158 | * @return array Modified Schema array. |
| 142 | 159 | */ |
| 143 | 160 | public function add_additional_fields_schema( $schema ) { |
| 144 | - | |
| 145 | 161 | // Validate the upload_date, used for placing the uploaded file in the correct upload directory. |
| 146 | 162 | $schema['properties']['upload_date'] = array( |
| 147 | 163 | 'description' => __( 'The date for the upload directory of the attachment.', 'jetpack-import' ), |
| 148 | 164 | 'type' => array( 'string', 'null' ), |
| @@ -171,9 +187,9 @@ | ||
| 171 | 187 | |
| 172 | 188 | /** |
| 173 | 189 | * Add a filter that rewrites the upload path. |
| 174 | 190 | * |
| 175 | - * @param \WP_REST_Request $request Full details about the request. | |
| 191 | + * @param WP_REST_Request $request Full details about the request. | |
| 176 | 192 | * |
| 177 | 193 | * @return void |
| 178 | 194 | * @throws \Exception If the date is invalid. |
| 179 | 195 | */ |
| @@ -200,9 +216,9 @@ | ||
| 200 | 216 | /** |
| 201 | 217 | * Prepares a single attachment for create or update. This function overrides the parent function |
| 202 | 218 | * |
| 203 | 219 | * @param WP_REST_Request $request Request object. |
| 204 | - * @return stdClass|WP_Error Post object. | |
| 220 | + * @return \stdClass|WP_Error Post object. | |
| 205 | 221 | */ |
| 206 | 222 | protected function prepare_item_for_database( $request ) { |
| 207 | 223 | $prepared_attachment = parent::prepare_item_for_database( $request ); |
| 208 | 224 | // date_gmt is equal to the date by default, so we need to override it. |
| @@ -248,16 +264,20 @@ | ||
| 248 | 264 | * @return mixed An associative array containing metadata for the attachment, or false if no attachment was found. |
| 249 | 265 | */ |
| 250 | 266 | protected function get_attachment_by_file_info( $fileinfo ) { |
| 251 | 267 | // Make sure all required variables are set and not empty |
| 252 | - if ( empty( $fileinfo['filename'] ) || empty( $fileinfo['mime_type'] ) || empty( $fileinfo['post_date_gmt'] ) ) { | |
| 268 | + if ( empty( $fileinfo['filename'] ) || empty( $fileinfo['mime_type'] ) ) { | |
| 253 | 269 | return false; |
| 254 | 270 | } |
| 271 | + $original_filename = $fileinfo['filename']; | |
| 272 | + $mime_type = $fileinfo['mime_type']; | |
| 273 | + $post_date_gmt = $fileinfo['post_date_gmt']; | |
| 274 | + // From WordPress 5.3, we introduced the scaled image feature, so we'll also need to check for the scaled filename. | |
| 275 | + // https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/ | |
| 276 | + $extension_pos = strrpos( $original_filename, '.' ); | |
| 277 | + $scaled_filename = substr( $original_filename, 0, $extension_pos ) . '-scaled' . substr( $original_filename, $extension_pos ); | |
| 278 | + $filename_check_array = array( $original_filename, $scaled_filename ); | |
| 255 | 279 | |
| 256 | - $filename = $fileinfo['filename']; | |
| 257 | - $mime_type = $fileinfo['mime_type']; | |
| 258 | - $post_date_gmt = $fileinfo['post_date_gmt']; | |
| 259 | - | |
| 260 | 280 | $args = array( |
| 261 | 281 | 'post_type' => 'attachment', |
| 262 | 282 | 'post_mime_type' => $mime_type, |
| 263 | 283 | 'date_query' => array( |
| @@ -267,18 +287,20 @@ | ||
| 267 | 287 | 'inclusive' => true, |
| 268 | 288 | 'column' => 'post_date_gmt', |
| 269 | 289 | ), |
| 270 | 290 | ), |
| 271 | - 'meta_query' => array( | |
| 272 | - array( | |
| 273 | - 'key' => '_wp_attached_file', | |
| 274 | - 'value' => preg_quote( $filename, '/' ), | |
| 275 | - 'compare' => 'REGEXP', | |
| 276 | - ), | |
| 277 | - ), | |
| 278 | 291 | 'posts_per_page' => 1, |
| 279 | 292 | ); |
| 280 | 293 | |
| 294 | + $args['meta_query'] = array( 'relation' => 'OR' ); | |
| 295 | + foreach ( $filename_check_array as $filename ) { | |
| 296 | + $args['meta_query'][] = array( | |
| 297 | + 'key' => '_wp_attached_file', | |
| 298 | + 'value' => preg_quote( $filename, '/' ), | |
| 299 | + 'compare' => 'REGEXP', | |
| 300 | + ); | |
| 301 | + } | |
| 302 | + | |
| 281 | 303 | $attachments = \get_posts( $args ); |
| 282 | 304 | |
| 283 | 305 | if ( ! empty( $attachments ) ) { |
| 284 | 306 | // Return the first attachment data found |
| @@ -283,9 +305,8 @@ | ||
| 283 | 305 | if ( ! empty( $attachments ) ) { |
| 284 | 306 | // Return the first attachment data found |
| 285 | 307 | return $attachments[0]; |
| 286 | 308 | } |
| 287 | - | |
| 288 | 309 | return false; |
| 289 | 310 | } |
| 290 | 311 | |
| 291 | 312 | /** |
| @@ -306,6 +327,5 @@ | ||
| 306 | 327 | } |
| 307 | 328 | |
| 308 | 329 | return (array) $response->get_data(); |
| 309 | 330 | } |
| 310 | - | |
| 311 | 331 | } |