| @@ -1068,9 +1068,19 @@ | ||
| 1068 | 1068 | |
| 1069 | 1069 | return $updated_url; |
| 1070 | 1070 | } |
| 1071 | 1071 | |
| 1072 | - $upload = $this->fetch_remote_file( $url, $post ); | |
| 1072 | + $upload_dir = wp_upload_dir( $post['upload_date'] ); | |
| 1073 | + if ( ! ( $upload_dir && false === $upload_dir['error'] ) ) { | |
| 1074 | + return new WP_Error( 'upload_dir_error', $upload_dir['error'] ); | |
| 1075 | + } | |
| 1076 | + | |
| 1077 | + // Move the file to the uploads dir. | |
| 1078 | + $file_name = basename( parse_url( $url, PHP_URL_PATH ) ); | |
| 1079 | + $file_name = wp_unique_filename( $upload_dir['path'], $file_name ); | |
| 1080 | + $dest_file = $upload_dir['path'] . "/$file_name"; | |
| 1081 | + $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir ); | |
| 1082 | + | |
| 1073 | 1083 | if ( is_wp_error( $upload ) ) { |
| 1074 | 1084 | return $upload; |
| 1075 | 1085 | } |
| 1076 | 1086 | |
| @@ -1080,8 +1090,9 @@ | ||
| 1080 | 1090 | } else { |
| 1081 | 1091 | return new WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type', 'elementor' ) ); |
| 1082 | 1092 | } |
| 1083 | 1093 | |
| 1094 | + $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed? | |
| 1084 | 1095 | $post['guid'] = $upload['url']; |
| 1085 | 1096 | |
| 1086 | 1097 | // As per wp-admin/includes/upload.php. |
| 1087 | 1098 | $post_id = wp_insert_attachment( $post, $upload['file'] ); |
| @@ -1093,9 +1104,9 @@ | ||
| 1093 | 1104 | |
| 1094 | 1105 | // error_log('Metadata: ' . print_r($metadata, true)); |
| 1095 | 1106 | |
| 1096 | 1107 | // For gutenberg pages |
| 1097 | - $metadata = $this->import_sizes($sizes, $metadata, $upload, $post_id); | |
| 1108 | + $metadata = $this->import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id); | |
| 1098 | 1109 | |
| 1099 | 1110 | // error_log('Metadata: ' . print_r($metadata, true)); |
| 1100 | 1111 | wp_update_attachment_metadata( $post_id, $metadata ); |
| 1101 | 1112 | |
| @@ -1118,35 +1129,8 @@ | ||
| 1118 | 1129 | |
| 1119 | 1130 | return $parts['dirname'] . '/' . $name; |
| 1120 | 1131 | } |
| 1121 | 1132 | |
| 1122 | - private function import_size($remote_url, $destination_path) { | |
| 1123 | - // Include the file for the download_url function | |
| 1124 | - if(!function_exists('download_url')) { | |
| 1125 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 1126 | - } | |
| 1127 | - | |
| 1128 | - // Download file to temp dir | |
| 1129 | - $temp_file = download_url($remote_url); | |
| 1130 | - | |
| 1131 | - // Check for download errors | |
| 1132 | - if(is_wp_error($temp_file)) { | |
| 1133 | - return $temp_file; | |
| 1134 | - } | |
| 1135 | - | |
| 1136 | - // Move temp file to destination path | |
| 1137 | - $result = copy($temp_file, $destination_path); | |
| 1138 | - | |
| 1139 | - // Remove temp file | |
| 1140 | - @unlink($temp_file); | |
| 1141 | - | |
| 1142 | - if (!$result) { | |
| 1143 | - return false; | |
| 1144 | - } | |
| 1145 | - | |
| 1146 | - return true; | |
| 1147 | - } | |
| 1148 | - | |
| 1149 | 1133 | /** |
| 1150 | 1134 | * Attempt to download a remote file attachment |
| 1151 | 1135 | * |
| 1152 | 1136 | * @param string $url URL of item to fetch |
| @@ -1153,16 +1137,12 @@ | ||
| 1153 | 1137 | * @param array $post Attachment details |
| 1154 | 1138 | * |
| 1155 | 1139 | * @return array|WP_Error Local file location details on success, WP_Error otherwise |
| 1156 | 1140 | */ |
| 1157 | - private function fetch_remote_file( $url, $post ) { | |
| 1158 | - // Extract the file name from the URL. | |
| 1159 | - $file_name = basename( parse_url( $url, PHP_URL_PATH ) ); | |
| 1141 | + private function fetch_remote_file( $url, $new_file, $uploads ) { | |
| 1142 | + // Extract the file name from the new_file. | |
| 1143 | + $file_name = basename( $new_file ); | |
| 1160 | 1144 | |
| 1161 | - if ( ! $file_name ) { | |
| 1162 | - $file_name = md5( $url ); | |
| 1163 | - } | |
| 1164 | - | |
| 1165 | 1145 | // Include the file for the download_url function |
| 1166 | 1146 | if(!function_exists('wp_tempnam')) { |
| 1167 | 1147 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 1168 | 1148 | } |
| @@ -1172,17 +1152,24 @@ | ||
| 1172 | 1152 | return new WP_Error( 'import_no_file', esc_html__( 'Could not create temporary file.', 'elementor' ) ); |
| 1173 | 1153 | } |
| 1174 | 1154 | |
| 1175 | 1155 | // Fetch the remote URL and write it to the placeholder file. |
| 1176 | - $remote_response = wp_safe_remote_get( $url, [ | |
| 1177 | - 'timeout' => 300, | |
| 1178 | - 'stream' => true, | |
| 1179 | - 'filename' => $tmp_file_name, | |
| 1180 | - 'headers' => [ | |
| 1181 | - 'Accept-Encoding' => 'identity', | |
| 1182 | - ] | |
| 1183 | - ] ); | |
| 1156 | + $attempt = 0; | |
| 1157 | + $retry_count = 3; | |
| 1158 | + $remote_response = null; | |
| 1159 | + do { | |
| 1160 | + $remote_response = wp_safe_remote_get( $url, [ | |
| 1161 | + 'timeout' => 300, | |
| 1162 | + 'stream' => true, | |
| 1163 | + 'filename' => $tmp_file_name, | |
| 1164 | + 'headers' => [ | |
| 1165 | + 'Accept-Encoding' => 'identity', | |
| 1166 | + ] | |
| 1167 | + ] ); | |
| 1168 | + $attempt++; | |
| 1169 | + } while (is_wp_error( $remote_response ) && $attempt < $retry_count); | |
| 1184 | 1170 | |
| 1171 | + | |
| 1185 | 1172 | if ( is_wp_error( $remote_response ) ) { |
| 1186 | 1173 | @unlink( $tmp_file_name ); |
| 1187 | 1174 | |
| 1188 | 1175 | return new WP_Error( 'import_file_error', sprintf( /* translators: 1: WordPress error message, 2: WordPress error code. */ esc_html__( 'Request failed due to an error: %1$s (%2$s)', 'elementor' ), esc_html( $remote_response->get_error_message() ), esc_html( $remote_response->get_error_code() ) ) ); |
| @@ -1261,16 +1248,8 @@ | ||
| 1261 | 1248 | if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) { |
| 1262 | 1249 | return new WP_Error( 'import_file_error', esc_html__( 'Sorry, this file type is not permitted for security reasons.', 'elementor' ) ); |
| 1263 | 1250 | } |
| 1264 | 1251 | |
| 1265 | - $uploads = wp_upload_dir( $post['upload_date'] ); | |
| 1266 | - if ( ! ( $uploads && false === $uploads['error'] ) ) { | |
| 1267 | - return new WP_Error( 'upload_dir_error', $uploads['error'] ); | |
| 1268 | - } | |
| 1269 | - | |
| 1270 | - // Move the file to the uploads dir. | |
| 1271 | - $file_name = wp_unique_filename( $uploads['path'], $file_name ); | |
| 1272 | - $new_file = $uploads['path'] . "/$file_name"; | |
| 1273 | 1252 | $move_new_file = copy( $tmp_file_name, $new_file ); |
| 1274 | 1253 | |
| 1275 | 1254 | if ( ! $move_new_file ) { |
| 1276 | 1255 | @unlink( $tmp_file_name ); |
| @@ -1291,9 +1270,8 @@ | ||
| 1291 | 1270 | ]; |
| 1292 | 1271 | |
| 1293 | 1272 | // Keep track of the old and new urls so we can substitute them later. |
| 1294 | 1273 | $this->url_remap[ $url ] = $upload['url']; |
| 1295 | - $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed? | |
| 1296 | 1274 | // Keep track of the destination if the remote url is redirected somewhere else. |
| 1297 | 1275 | if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] !== $url ) { |
| 1298 | 1276 | $this->url_remap[ $headers['x-final-location'] ] = $upload['url']; |
| 1299 | 1277 | } |
| @@ -1378,9 +1356,9 @@ | ||
| 1378 | 1356 | 'resized' => false, |
| 1379 | 1357 | ); |
| 1380 | 1358 | } |
| 1381 | 1359 | |
| 1382 | - public function import_sizes($sizes, $metadata, $upload, $post_id) { | |
| 1360 | + public function import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id) { | |
| 1383 | 1361 | if (!empty($sizes)) { |
| 1384 | 1362 | do_action( 'templately_import.finalize_gutenberg_attachment', $post_id ); |
| 1385 | 1363 | |
| 1386 | 1364 | $size_dimensions = $this->extract_sizes($sizes); |
| @@ -1388,9 +1366,9 @@ | ||
| 1388 | 1366 | |
| 1389 | 1367 | foreach ($size_dimensions as $size_dimension => $__url) { |
| 1390 | 1368 | if (!$this->size_exists_in_metadata($size_dimension, $metadata)) { |
| 1391 | 1369 | $unique_destination_file = $this->create_unique_destination_file($upload['file'], $size_dimension); |
| 1392 | - $missing_size = $this->import_size($__url, $unique_destination_file); | |
| 1370 | + $missing_size = $this->fetch_remote_file($__url, $unique_destination_file, $upload_dir); | |
| 1393 | 1371 | |
| 1394 | 1372 | if($missing_size && !is_wp_error($missing_size)) { |
| 1395 | 1373 | $metadata['sizes'][$size_dimension] = $this->create_size_array($unique_destination_file, $size_dimension); |
| 1396 | 1374 | do_action( 'templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension ); |