Skip to content

Instantly share code, notes, and snippets.

@retgef
Last active June 19, 2018 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retgef/201101de662982f5785f to your computer and use it in GitHub Desktop.
Save retgef/201101de662982f5785f to your computer and use it in GitHub Desktop.
Converts existing attachment meta for use with https://wordpress.org/plugins/amazon-s3-and-cloudfront/
<?php
/*
Plugin Name: Coderrr S3 Conversion
Description: Converts previously uploaded attachments to work with amazon-s3-and-cloudfront plugin
Version: 0.1
Author: Brian Fegter
License: GPLv2+
*/
/**
* Converts previous attachment metadata to work with amcn-s3-uploads plugin
* Usage when logged in as an admin:
* http://example.com/wp-admin/?s3=1&bucket=mybucket
*/
class Coderrr_S3_Conversion{
/**
* Obscure Query Variable
* @var string
*/
private $key = 's3';
/**
* Password
* @var string
*/
private $pass = '1';
/**
* Constructor Method
*/
public function __construct(){
#Set a high priority so all admin assets load
add_action('admin_init', array($this, 'migrate_data'), 9999999);
}
/**
* Verifies the authority of the user as well as the authenticity of the request
* Checks if:
* - Is wp-admin
* - User is logged in
* - User is an administrator
* - Query variable is set
* - Query variable value is correct
* @return bool
*/
protected function verify_request(){
return
is_admin()
&& current_user_can('manage_options')
&& isset($_GET[$this->key])
&& $_GET[$this->key] === $this->pass
&& isset($_GET['bucket'])
&& $_GET['bucket']
? true : false;
}
/**
* Migration
* @return null - exits page load
*/
public function migrate_data(){
#Check again since this is a public method
if(!$this->verify_request()){
wp_die("Required params: {$this->key}, bucket");
}
set_time_limit(0);
show_message('<pre>');
global $wpdb;
$sql = "
SELECT p.ID, pm.meta_value path
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta pm
ON p.ID = pm.post_id
WHERE p.post_type = 'attachment'
AND pm.meta_key = '_wp_attached_file'
";
$attachments = $wpdb->get_results($sql);
$count = count($attachments);
show_message("<h2>$count Attachments</h2>");
$errors = 0;
$error = array();
foreach($attachments as $attachment){
$s3_info = array();
$s3_info['bucket'] = $_GET['bucket'];
$s3_info['key'] = trim("/content/uploads/$attachment->path", '/');
if(update_post_meta($attachment->ID, 'amazonS3_info', $s3_info))
show_message("$count - #$attachment->ID - $attachment->path converted correctly");
else{
$errors++;
$error[] = $attachment->ID.' - '.$attachment->path;
show_message("<span style='font-weight:bold; color:red;'>$count - #$attachment->ID - $attachment->path was not converted</span>");
}
$count--;
}
show_message("<h2>$errors Errors</h2><span style='color:red;'");
print_r($error);
echo '</span></pre>';
exit;
}
}
new Coderrr_S3_Conversion;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment