diff --git a/classes/Tools/Storage/StorageTool.php b/classes/Tools/Storage/StorageTool.php index 23ebc96f..cc5b8d63 100755 --- a/classes/Tools/Storage/StorageTool.php +++ b/classes/Tools/Storage/StorageTool.php @@ -2152,9 +2152,12 @@ public function filterContent( $content ) private function generateSrcSet( $id, $sizeName ) { - if ( $this->allSizes == null ) { + if ( $this->allSizes === null ) { $this->allSizes = ilab_get_image_sizes(); } + if ( !is_string( $sizeName ) ) { + return ''; + } if ( $sizeName !== 'full' && !isset( $this->allSizes[$sizeName] ) ) { return ''; } @@ -3764,13 +3767,23 @@ public function importImageAttachmentFromStorage( $fileInfo, $thumbs = array() ) * @return array|bool * @throws StorageException */ - public function importExistingAttachmentFromStorage( $postId, $fileInfo, $thumbs = array() ) + public function importExistingAttachmentFromStorage( + $postId, + $fileInfo, + $thumbs = array(), + $scaled = null + ) { if ( !$this->client || !$this->client->enabled() ) { return null; } if ( $fileInfo->mimeType() && strpos( $fileInfo->mimeType(), 'image/' ) === 0 ) { - return $this->importExistingImageAttachmentFromStorage( $postId, $fileInfo, $thumbs ); + return $this->importExistingImageAttachmentFromStorage( + $postId, + $fileInfo, + $thumbs, + $scaled + ); } $url = $this->client->url( $fileInfo->key() ); $s3Info = [ @@ -3810,11 +3823,20 @@ public function importExistingAttachmentFromStorage( $postId, $fileInfo, $thumbs * @return array|bool * @throws StorageException */ - public function importExistingImageAttachmentFromStorage( $postId, $fileInfo, $thumbs = array() ) + public function importExistingImageAttachmentFromStorage( + $postId, + $fileInfo, + $thumbs = array(), + $scaled = null + ) { if ( !$this->client || !$this->client->enabled() ) { return null; } + $originalFileInfo = $fileInfo; + if ( !empty($scaled) ) { + $fileInfo = $this->client->info( $scaled ); + } if ( !is_array( $fileInfo->size() ) ) { return null; } @@ -3893,6 +3915,25 @@ public function importExistingImageAttachmentFromStorage( $postId, $fileInfo, $t } } + + if ( !empty($scaled) ) { + $meta['file'] = $scaled; + $url = $this->client->url( $originalFileInfo->key() ); + $s3Info = [ + 'url' => $url, + 'mime-type' => $originalFileInfo->mimeType(), + 'bucket' => $this->client->bucket(), + 'privacy' => StorageGlobals::privacy( $originalFileInfo->mimeType() ), + 'key' => $originalFileInfo->key(), + 'v' => MEDIA_CLOUD_INFO_VERSION, + 'options' => [ + 'params' => [], + ], + ]; + $meta['original_image_s3'] = $s3Info; + $meta['original_image'] = pathinfo( $originalFileInfo->key(), PATHINFO_BASENAME ); + } + update_post_meta( $postId, '_wp_attachment_metadata', $meta ); $thumbUrl = image_downsize( $postId, [ 128, 128 ] ); if ( is_array( $thumbUrl ) ) { @@ -3914,7 +3955,7 @@ public function importExistingImageAttachmentFromStorage( $postId, $fileInfo, $t * @return array|bool * @throws StorageException */ - public function importAttachmentFromStorage( $fileInfo, $thumbs = array() ) + public function importAttachmentFromStorage( $fileInfo, $thumbs = array(), $scaled = null ) { if ( !$this->client || !$this->client->enabled() ) { return null; @@ -4013,10 +4054,31 @@ public function getFileList( $directoryKeys = array( '' ), $skipThumbnails = fal } } else { - $fileList[$file] = [ - 'key' => $file, - 'thumbs' => [], - ]; + + if ( preg_match( '/.*-scaled\\.[aA-zZ]+/', $file, $scaledMatch ) ) { + $sourceFile = str_replace( '-scaled', '', $file ); + + if ( isset( $fileList[$sourceFile] ) ) { + $fileList[$sourceFile]['scaled'] = $file; + } else { + + if ( isset( $unmatchedFileList[$sourceFile] ) ) { + $unmatchedFileList[$sourceFile]['scaled'] = $file; + } else { + $unmatchedFileList[$sourceFile] = [ + 'scaled' => $file, + ]; + } + + } + + } else { + $fileList[$file] = [ + 'key' => $file, + 'thumbs' => [], + ]; + } + } } @@ -4031,9 +4093,14 @@ public function getFileList( $directoryKeys = array( '' ), $skipThumbnails = fal } foreach ( $unmatchedFileList as $key => $thumbs ) { + if ( isset( $fileList[$key] ) ) { + if ( isset( $thumbs['scaled'] ) ) { + $fileList[$key]['scaled'] = $thumbs['scaled']; + } $fileList[$key]['thumbs'] = array_merge( $fileList[$key]['thumbs'], $thumbs['thumbs'] ); } + } return array_values( $fileList ); } @@ -4132,7 +4199,7 @@ public function providerHelp() //endregion //region Importing From Cloud - private function doImportFile( $key, $thumbs ) + private function doImportFile( $key, $thumbs, $scaled = null ) { global $wpdb ; $dir = wp_upload_dir(); @@ -4149,6 +4216,24 @@ private function doImportFile( $key, $thumbs ) $postId = $wpdb->get_var( $query ); } + + if ( empty($postId) ) { + $query = $wpdb->prepare( "select post_id from {$wpdb->postmeta} where meta_key='_wp_attached_file' and meta_value = %s", $key ); + $results = $wpdb->get_results( $query, ARRAY_A ); + if ( count( $results ) === 1 ) { + $postId = $results[0]['post_id']; + } + + if ( empty($postId) ) { + $query = $wpdb->prepare( "select post_id from {$wpdb->postmeta} where meta_key='_wp_attachment_metadata' and meta_value LIKE %s", '%' . $key . '%' ); + $results = $wpdb->get_results( $query, ARRAY_A ); + if ( count( $results ) === 1 ) { + $postId = $results[0]['post_id']; + } + } + + } + $destDir = pathinfo( $destFile, PATHINFO_DIRNAME ); if ( !file_exists( $destDir ) ) { @mkdir( $destDir, 0777, true ); @@ -4191,6 +4276,23 @@ private function doImportFile( $key, $thumbs ) } } + + if ( !empty($scaled) ) { + $scaledFile = $base . $scaled; + + if ( !file_exists( $scaledFile ) ) { + $url = $this->client()->presignedUrl( $scaled ); + $client = new Client(); + $response = $client->get( $url, [ + 'save_to' => $scaledFile, + ] ); + if ( $response->getStatusCode() != 200 ) { + $scaled = null; + } + } + + } + require_once ABSPATH . 'wp-admin/includes/image.php'; if ( empty($postId) ) { @@ -4284,7 +4386,7 @@ private function doImportFile( $key, $thumbs ) return true; } - private function doImportDynamicFile( $key, $thumbs ) + private function doImportDynamicFile( $key, $thumbs, $scaled = null ) { global $wpdb ; $dir = wp_upload_dir(); @@ -4293,10 +4395,39 @@ private function doImportDynamicFile( $key, $thumbs ) $query = $wpdb->prepare( "select ID from {$wpdb->posts} where (guid = %s) or (guid = %s)", $desturl, $info->url() ); $postId = $wpdb->get_var( $query ); + if ( empty($postId) ) { + $query = $wpdb->prepare( "select ID from {$wpdb->posts} where guid = %s", $this->client()->url( $key ) ); + $postId = $wpdb->get_var( $query ); + } + + + if ( empty($postId) ) { + $query = $wpdb->prepare( "select post_id from {$wpdb->postmeta} where meta_key='_wp_attached_file' and meta_value = %s", $key ); + $results = $wpdb->get_results( $query, ARRAY_A ); + if ( count( $results ) === 1 ) { + $postId = $results[0]['post_id']; + } + + if ( empty($postId) ) { + $query = $wpdb->prepare( "select post_id from {$wpdb->postmeta} where meta_key='_wp_attachment_metadata' and meta_value LIKE %s", '%' . $key . '%' ); + $results = $wpdb->get_results( $query, ARRAY_A ); + if ( count( $results ) === 1 ) { + $postId = $results[0]['post_id']; + } + } + + } + + if ( !empty($postId) ) { - $this->importExistingAttachmentFromStorage( $postId, $info, $thumbs ); + $this->importExistingAttachmentFromStorage( + $postId, + $info, + $thumbs, + $scaled + ); } else { - $this->importAttachmentFromStorage( $info, $thumbs ); + $this->importAttachmentFromStorage( $info, $thumbs, $scaled ); } return true; @@ -4306,7 +4437,8 @@ public function importFileFromStorage( $key, $thumbs, $importOnly, - $preservePaths + $preservePaths, + $scaled = null ) { $oldPreserve = $this->preserveFilePaths; @@ -4314,9 +4446,9 @@ public function importFileFromStorage( try { if ( $importOnly ) { - $success = $this->doImportDynamicFile( $key, $thumbs ); + $success = $this->doImportDynamicFile( $key, $thumbs, $scaled ); } else { - $success = $this->doImportFile( $key, $thumbs ); + $success = $this->doImportFile( $key, $thumbs, $scaled ); } } catch ( \Exception $ex ) { diff --git a/ilab-media-tools.php b/ilab-media-tools.php index 6f215b8c..e7eaa600 100755 --- a/ilab-media-tools.php +++ b/ilab-media-tools.php @@ -5,7 +5,7 @@ Plugin URI: https://github.com/interfacelab/ilab-media-tools Description: Automatically upload media to Amazon S3 and integrate with Imgix, a real-time image processing CDN. Boosts site performance and simplifies workflows. Author: interfacelab -Version: 3.3.0 +Version: 3.3.1 Author URI: http://interfacelab.io */ // Copyright (c) 2016 Interfacelab LLC. All rights reserved. @@ -93,7 +93,7 @@ } // Version Defines -define( 'MEDIA_CLOUD_VERSION', '3.3.0' ); +define( 'MEDIA_CLOUD_VERSION', '3.3.1' ); define( 'MEDIA_CLOUD_INFO_VERSION', '1.0.0' ); // Directory defines define( 'ILAB_TOOLS_DIR', dirname( __FILE__ ) ); diff --git a/readme.txt b/readme.txt index 20b27630..7d383294 100755 --- a/readme.txt +++ b/readme.txt @@ -108,6 +108,11 @@ No, I'm just one very enthusiastic customer. == Changelog == += 3.3.1 = + +* Fix for illegal offset error warning +* Fix for importing from cloud storage (Premium) + = 3.3.0 = * Wizards? Wizards! WIZARDS!