Skip to content

Commit

Permalink
Fix for importing, fix for illegal offset error
Browse files Browse the repository at this point in the history
  • Loading branch information
jawngee committed Nov 20, 2019
1 parent 4568da0 commit 0d1a607
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 18 deletions.
164 changes: 148 additions & 16 deletions classes/Tools/Storage/StorageTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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;
Expand Down Expand Up @@ -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' => [],
];
}

}

}
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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();
Expand All @@ -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 );
Expand Down Expand Up @@ -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) ) {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -4306,17 +4437,18 @@ public function importFileFromStorage(
$key,
$thumbs,
$importOnly,
$preservePaths
$preservePaths,
$scaled = null
)
{
$oldPreserve = $this->preserveFilePaths;
$this->preserveFilePaths = ( $preservePaths ? 'preserve' : 'replace' );
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 ) {
Expand Down
4 changes: 2 additions & 2 deletions ilab-media-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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__ ) );
Expand Down
5 changes: 5 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 0d1a607

Please sign in to comment.