Skip to content

Commit

Permalink
* Media Cloud will now import setting from Leopard Offload if you are…
Browse files Browse the repository at this point in the history
… upgrading. This only works for S3 and S3

  compatible cloud storage.  If you are using Google, you will have to set that up manually.
* When upgrading from Leopard Offload, your links will be migrated as needed, but you can manually run a background
  task to do them all in one go.
* If using Leopard Offload with WooCommerce, after migrating to Media Cloud you must run the command line
  tool `wp mediacloud:integrations fixLeopardWooLinks` to replace the weird link shortcode that Leopard Offload uses.
  This tool is only in the premium version.
  • Loading branch information
jawngee committed Nov 30, 2022
1 parent fd35ab5 commit 61d2e1a
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 323 deletions.
201 changes: 0 additions & 201 deletions classes/Tools/Storage/StorageGlobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,212 +385,11 @@ public static function alternativeFormatTypes() {
}
//endregion

//region Migration

public static function migrateFromOtherPlugin() {
$beenHereBefore = get_option('mcloud-other-plugins-migrated');
if (!empty($beenHereBefore)) {
return;
}

$migratedFrom = null;
$migrated = false;
$statelessVersion = get_option('wp-stateless-current-version');
if (!empty($statelessVersion)) {
$migrated = static::migrateStatelessSettings();
$migratedFrom = 'WP-Stateless';
} else {
$migrated = static::migrateOffloadSettings();
$migratedFrom = 'WP Offload Media';
}

if (!empty($migrated)) {
Environment::UpdateOption('mcloud-tool-enabled-storage', true);
if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('info', "Media Cloud noticed you were using {$migratedFrom} and has migrated your settings automatically. Everything should be working as before, but make sure to double check your Cloud Storage settings.", true, 'mcloud-migrated-other-plugin', 'forever');
}
update_option('mcloud-other-plugins-did-migrate', $migratedFrom);
}

update_option('mcloud-other-plugins-migrated', true);
}

//endregion

//region Stateless Migrations

private static function migrateStatelessSettings() {
$jsonConfigData = get_option('sm_key_json');
if (empty($jsonConfigData)) {
return false;
}

$bucket = get_option('sm_bucket');
if (empty($bucket)) {
return false;
}

$mode = get_option('sm_mode', 'cdn');
if ($mode === 'disabled') {
return false;
}

Environment::UpdateOption('mcloud-storage-provider', 'google');
Environment::UpdateOption('mcloud-storage-google-credentials', $jsonConfigData);
Environment::UpdateOption('mcloud-storage-google-bucket', $bucket);

$deleteUploads = ($mode === 'stateless');
if (!empty($deleteUploads)) {
Environment::UpdateOption("mcloud-storage-delete-uploads", true);
}

$cdn = get_option('sm_custom_domain');
if (!empty($cdn)) {
Environment::UpdateOption("mcloud-storage-cdn-base", $cdn);
}

$uploadDir = trim(get_option('sm_root_dir'), '/');
if (!empty($uploadDir)) {
Environment::UpdateOption("mcloud-storage-prefix", $uploadDir);
}

$cacheControl = get_option('sm_cache_control');
if (!empty($cacheControl)) {
Environment::UpdateOption("mcloud-storage-cache-control", $cacheControl);
}

return true;
}

//endregion

//region Maintenance

public static function reloadSettings() {
self::$instance = new StorageGlobals();
}

//endregion

//region Offload Migrations

private static function migrateOffloadMiscSettings($offloadConfig) {
$deleteUploads = arrayPath($offloadConfig, 'remove-local-file', false);
$cdn = arrayPath($offloadConfig, 'cloudfront', null);
$prefix = arrayPath($offloadConfig, 'object-prefix', null);
$usePrefix = arrayPath($offloadConfig, 'enable-object-prefix', false);

if (!empty($deleteUploads)) {
Environment::UpdateOption("mcloud-storage-delete-uploads", true);
}

if (!empty($cdn)) {
Environment::UpdateOption("mcloud-storage-cdn-base", 'https://'.$cdn);
}

if (!empty($prefix) && !empty($usePrefix)) {
$prefix = rtrim($prefix, '/');

$useYearMonth = arrayPath($offloadConfig, 'use-yearmonth-folders', false);
if (!empty($useYearMonth)) {
$prefix = trailingslashit($prefix).'@{date:Y/m}';
}

$useVersioning = arrayPath($offloadConfig, 'object-versioning', false);
if ($useVersioning) {
$prefix = trailingslashit($prefix).'@{versioning}';
}

Environment::UpdateOption("mcloud-storage-prefix", $prefix);
}
}

private static function migrateOffload($provider, $key, $secret) {
$offloadConfig = get_option('tantan_wordpress_s3');
$bucket = arrayPath($offloadConfig, 'bucket', null);
$region = arrayPath($offloadConfig, 'region', 'auto');
if (empty($bucket)) {
return false;
}

Environment::UpdateOption('mcloud-storage-provider', $provider);
Environment::UpdateOption("mcloud-storage-s3-access-key", $key);
Environment::UpdateOption("mcloud-storage-s3-secret", $secret);
Environment::UpdateOption("mcloud-storage-s3-bucket", $bucket);

if ($provider === 'do') {
Environment::UpdateOption("mcloud-storage-s3-endpoint", "https://{$region}.digitaloceanspaces.com");
} else {
Environment::UpdateOption("mcloud-storage-s3-region", $region);
}

static::migrateOffloadMiscSettings($offloadConfig);

return true;
}

private static function migrateOffloadFromConfig($data) {
$provider = arrayPath($data, 'provider', null);
if (empty($provider) || !in_array($provider, ['aws', 'do', 'gcp'])) {
return false;
}

$providerMap = ['aws' => 's3', 'do' => 'do', 'gcp' => 'google'];
$provider = $providerMap[$provider];

if ($provider !== 'google') {
$key = arrayPath($data, 'access-key-id');
$secret = arrayPath($data, 'secret-access-key');

return static::migrateOffload($provider, $key, $secret);
}

$keyPath = arrayPath($data, 'key-file-path', null);
if (!empty($keyPath) && file_exists($keyPath)) {
$googleConfig = file_get_contents($keyPath);
} else {
$googleConfigData = arrayPath($data, 'key-file', null);
if (empty($googleConfigData) || !is_array($googleConfigData)) {
return false;
}

$googleConfig = json_encode($googleConfigData, JSON_PRETTY_PRINT);
}

if (empty($googleConfig)) {
return false;
}

$offloadConfig = get_option('tantan_wordpress_s3');
$bucket = arrayPath($offloadConfig, 'bucket', null);
if (empty($bucket)) {
return false;
}

Environment::UpdateOption('mcloud-storage-provider', 'google');
Environment::UpdateOption('mcloud-storage-google-credentials', $googleConfig);
Environment::UpdateOption('mcloud-storage-google-bucket', $bucket);

static::migrateOffloadMiscSettings($offloadConfig);

return true;
}

public static function migrateOffloadSettings() {
$migrated = false;
if (defined('AS3CF_SETTINGS')) {
$data = unserialize(constant('AS3CF_SETTINGS'));
if (!empty($data)) {
$migrated = static::migrateOffloadFromConfig($data);
}
} else {
$offloadConfig = get_option('tantan_wordpress_s3');
if (!empty($offloadConfig)) {
$migrated = static::migrateOffloadFromConfig($offloadConfig);
}
}

return $migrated;
}
//endregion
}
135 changes: 135 additions & 0 deletions classes/Tools/Storage/StorageTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2094,9 +2094,16 @@ public function getAttachmentURL( $url, $post_id )
$new_url = $this->importOffloadMetadata( $post_id, $meta, $offloadS3Info );
} else {
$statelessInfo = get_post_meta( $post_id, 'sm_cloud', true );

if ( !empty($statelessInfo) ) {
$new_url = $this->importStatelessMetadata( $post_id, $meta, $statelessInfo );
} else {
$leopardInfo = get_post_meta( $post_id, '_nou_leopard_wom_amazonS3_info', true );
if ( !empty($leopardInfo) ) {
$new_url = $this->importLeopardMetadata( $post_id, $meta, $leopardInfo );
}
}

}

}
Expand Down Expand Up @@ -4914,6 +4921,127 @@ private function getOffloadS3URL(
return null;
}

/**
* Generates a URL from Leopard Offload
*
* @param $provider
* @param $region
* @param $bucket
* @param $key
*
* @return string|null
*
* @throws StorageException
*/
private function getLeopardURL(
string $provider,
$region,
$bucket,
$key
)
{
$prefixFix = Environment::Option( 'mcloud-leopard-prefix-fix', null, false );
if ( !empty($prefixFix) ) {
$key = $prefixFix . '/' . $key;
}
if ( $provider === StorageToolSettings::driver() && $bucket === $this->client->bucket() ) {
return $this->client->url( $key );
}

if ( $provider === 's3' ) {
if ( empty($region) ) {
return "https://s3.amazonaws.com/{$bucket}/{$key}";
}
return "https://s3-{$region}.amazonaws.com/{$bucket}/{$key}";
}

if ( $provider === 'do' ) {
return "https://{$region}.digitaloceanspaces.com/{$bucket}/{$key}";
}
if ( $provider === 'google' ) {
return "https://storage.googleapis.com/{$bucket}/{$key}";
}
return null;
}

private function importLeopardMetadata( $postId, $meta, $leopardMetadata )
{
$provider = arrayPath( $leopardMetadata, 'provider', 'aws' );
$bucket = arrayPath( $leopardMetadata, 'bucket', null );
$region = arrayPath( $leopardMetadata, 'region', null );
$key = arrayPath( $leopardMetadata, 'key', null );
$prefix = Environment::Option( 'mcloud-leopard-prefix-fix', null, false );
if ( empty($provider) || empty($bucket) || empty($key) || !in_array( $provider, [
's3',
'aws',
'do',
'gcp'
] ) ) {
return null;
}
$providerMap = [
'aws' => 's3',
's3' => 's3',
'do' => 'do',
'gcp' => 'google',
];
$provider = $providerMap[$provider];
$mime = get_post_mime_type( $postId );
$hasMeta = !empty($meta);
if ( empty($meta) ) {
$meta = [];
}
$s3Info = [
'url' => $this->getLeopardURL(
$provider,
$region,
$bucket,
$key
),
'provider' => $provider,
'bucket' => $bucket,
'key' => ( !empty($prefix) ? $prefix . '/' . $key : $key ),
'privacy' => 'public-read',
'v' => MEDIA_CLOUD_INFO_VERSION,
'mime-type' => $mime,
];
if ( $provider !== 'google' ) {
$s3Info['region'] = $region;
}
$meta['s3'] = $s3Info;

if ( isset( $meta['sizes'] ) ) {
$baseKey = ltrim( pathinfo( '/' . $key, PATHINFO_DIRNAME ), '/' );
$newSizes = [];
foreach ( $meta['sizes'] as $size => $sizeData ) {
$sizeKey = trailingslashit( $baseKey ) . $sizeData['file'];
$sizeS3Info = $s3Info;
$sizeS3Info['url'] = $this->getLeopardURL(
$provider,
$region,
$bucket,
$sizeKey
);
$sizeS3Info['key'] = ( !empty($prefix) ? $prefix . '/' . $sizeKey : $sizeKey );
$sizeS3Info['mime-type'] = $sizeData['mime-type'];
$sizeData['s3'] = $sizeS3Info;
$newSizes[$size] = $sizeData;
}
$meta['sizes'] = $newSizes;
}


if ( $hasMeta ) {
update_post_meta( $postId, '_wp_attachment_metadata', $meta );
} else {
update_post_meta( $postId, 'ilab_s3_info', [
's3' => $s3Info,
] );
}

return $s3Info['url'];
}

/**
* Imports metadata from WP-Stateless
*
Expand Down Expand Up @@ -5148,9 +5276,16 @@ public function migratePostFromOtherPlugin( $postId )
$new_url = $this->importOffloadMetadata( $postId, $meta, $offloadS3Info );
} else {
$statelessInfo = get_post_meta( $postId, 'sm_cloud', true );

if ( !empty($statelessInfo) ) {
$new_url = $this->importStatelessMetadata( $postId, $meta, $statelessInfo );
} else {
$leopardInfo = get_post_meta( $postId, '_nou_leopard_wom_amazonS3_info', true );
if ( !empty($leopardInfo) ) {
$new_url = $this->importLeopardMetadata( $postId, $meta, $leopardInfo );
}
}

}

return !empty($new_url);
Expand Down
Loading

0 comments on commit 61d2e1a

Please sign in to comment.