Permalink
Browse files

Delete files on S3 when deleting WP attachment. General cleanup.

  • Loading branch information...
bradt committed Sep 5, 2012
1 parent 3151c9f commit e777cd49a4b6999f999bd969241fb24cbbcece60
Showing with 65 additions and 28 deletions.
  1. +1 −1 wordpress-s3/admin-tab-head.html
  2. +64 −27 wordpress-s3/class-plugin.php
@@ -1,6 +1,6 @@
<?php
$dir = dirname(__FILE__);
$pluginRootURL = get_option('siteurl').substr($dir, strpos($dir, '/wp-content'));
$pluginRootURL = plugins_url( '', __FILE__ );
if (!ereg('media-upload.php', $_SERVER['REQUEST_URI'])): // WP < 2.5
?>
@@ -12,15 +12,16 @@ function TanTanWordPressS3Plugin() {
if (!file_exists(dirname(__FILE__).'/config.php')) {
add_action('admin_menu', array(&$this, 'settings'));
}
if (!$this->options['hideAmazonS3UploadTab']) {
if (!isset($this->options['hideAmazonS3UploadTab']) || !$this->options['hideAmazonS3UploadTab']) {
add_action('load-upload.php', array(&$this, 'addPhotosTab')); // WP < 2.5
// WP >= 2.5
add_action('media_buttons_context', array(&$this, 'media_buttons'));
add_action('media_buttons_context', array(&$this, 'media_buttons'));
add_action('media_upload_tabs', array(&$this, 'media_upload_tabs'));
add_action('media_upload_tantan-wordpress-s3', array(&$this, 'media_upload_content'));
}
add_action('activate_tantan/wordpress-s3.php', array(&$this, 'activate'));
if ($_GET['tantanActivate'] == 'wordpress-s3') {
if (isset($_GET['tantanActivate']) && $_GET['tantanActivate'] == 'wordpress-s3') {
$this->showConfigNotice();
}
$this->photos = array();
@@ -52,11 +53,11 @@ function settings() {
}
function addhooks() {
parent::addhooks();
if (!$_POST['disable_amazonS3']) {
if (!isset($_POST['disable_amazonS3']) || !$_POST['disable_amazonS3']) {
add_filter('wp_update_attachment_metadata', array(&$this, 'wp_update_attachment_metadata'), 9, 2);
//can't delete mirrored files just yet
add_filter('wp_get_attachment_metadata', array(&$this, 'wp_get_attachment_metadata'));
//add_filter('wp_delete_file', array(&$this, 'wp_delete_file'));
//add_filter('wp_get_attachment_metadata', array(&$this, 'wp_get_attachment_metadata'));
add_filter('delete_attachment', array(&$this, 'delete_attachment'));
}
}
function version_check() {
@@ -132,30 +133,47 @@ function admin() {
/*
Delete corresponding file from Amazon S3
Delete corresponding files from Amazon S3
*/
function wp_delete_file($file) {
return $file;
function delete_attachment( $post_id ) {
if (!$this->options) $this->options = get_option('tantan_wordpress_s3');
if (!$this->options['wp-uploads'] || !$this->options['bucket'] || !$this->options['secret']) {
return $file;
return;
}
if (is_array($this->meta)) {
require_once(dirname(__FILE__).'/lib.s3.php');
$this->s3 = new TanTanS3($this->options['key'], $this->options['secret']);
$this->s3->setOptions($this->options);
if (deleteObject($this->meta['bucket'], $this->meta['key'])) {
$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
$intermediate_sizes = array();
foreach ( get_intermediate_image_sizes() as $size ) {
if ( $intermediate = image_get_intermediate_size( $post_id, $size ) )
$intermediate_sizes[] = $intermediate;
}
if ( !( $amazon = get_post_meta( $post_id, 'amazonS3_info', true ) ) ) {
return;
}
$amazon_path = dirname( $amazon['key'] );
require_once(dirname(__FILE__).'/lib.s3.php');
$this->s3 = new TanTanS3($this->options['key'], $this->options['secret']);
$this->s3->setOptions($this->options);
// remove intermediate and backup images if there are any
foreach ( $intermediate_sizes as $intermediate ) {
$this->s3->deleteObject( $amazon['bucket'], path_join( $amazon_path, $intermediate['file'] ) );
}
if ( is_array($backup_sizes) ) {
foreach ( $backup_sizes as $size ) {
$this->s3->deleteObject( $amazon['bucket'], path_join( $amazon_path, $del_file ) );
}
$accessDomain = ($this->options['cloudfront'] != '' ? $this->options['cloudfront'] : ($this->options['virtual-host'] ? $this->meta['bucket'] : $this->meta['bucket'].'.s3.amazonaws.com'));
return $file;
//return 'http://'.$accessDomain.'/'.$amazon['key'];
}
return $file;
$this->s3->deleteObject( $amazon['bucket'], $amazon['key'] );
}
function wp_get_attachment_metadata($data=false, $postID=false) {
if (is_numeric($postID)) $this->meta = get_post_meta($postID, 'amazonS3_info', true);
return $data;
@@ -179,6 +197,8 @@ function wp_update_attachment_metadata($data, $postID) {
$type = get_post_mime_type($postID);
$data['file'] = get_attached_file($postID, true);
$acl = apply_filters( 'wps3_upload_acl', 'public-read', $this, $postID, $data, $type );
if (file_exists($data['file'])) {
$file = array(
@@ -193,7 +213,7 @@ function wp_update_attachment_metadata($data, $postID) {
$this->s3 = new TanTanS3($this->options['key'], $this->options['secret']);
$this->s3->setOptions($this->options);
if ($this->s3->putObjectStream($this->options['bucket'], $prefix.$file['name'], $file)) {
if ($this->s3->putObjectStream($this->options['bucket'], $prefix.$file['name'], $file, $acl)) {
if ($data['thumb']) {
$thumbpath = str_replace( basename( $data['file'] ), $data['thumb'], $data['file'] );
@@ -255,8 +275,7 @@ function upload_path($path='') {
}
function media_buttons($context) {
global $post_ID, $temp_ID;
$dir = dirname(__FILE__);
$pluginRootURL = get_option('siteurl').substr($dir, strpos($dir, '/wp-content'));
$pluginRootURL = plugins_url( '', __FILE__ );
$image_btn = $pluginRootURL.'/database.png';
$image_title = 'Amazon S3';
@@ -266,6 +285,10 @@ function media_buttons($context) {
$out = ' <a href="'.$media_upload_iframe_src.'&tab=tantan-wordpress-s3&TB_iframe=true&height=500&width=640" class="thickbox" title="'.$image_title.'"><img src="'.$image_btn.'" alt="'.$image_title.'" /></a>';
return $context.$out;
}
function media_upload_tabs( $tabs ) {
$tabs['tantan-wordpress-s3'] = 'Amazon S3';
return $tabs;
}
function media_upload_content() {
$this->upload_files_tantan_amazons3(); // process any uploaded files or new folders
@@ -315,12 +338,26 @@ function wp_upload_tabs ($array) {
}
}
function get_access_domain() {
if (!$this->options) $this->options = get_option('tantan_wordpress_s3');
if ( isset($this->options['cloudfront']) && $this->options['cloudfront'] ) {
return $this->options['cloudfront'];
}
elseif ( isset($this->options['virtual-host']) && $this->options['virtual-host'] ) {
return $this->options['bucket'];
}
else {
return $this->options['bucket'].'.s3.amazonaws.com';
}
}
function upload_tabs_scripts() {
//wp_enqueue_script('prototype');
if (!$this->options) $this->options = get_option('tantan_wordpress_s3');
$accessDomain = ($this->options['cloudfront'] != '' ? $this->options['cloudfront'] : ($this->options['virtual-host'] ? $this->options['bucket'] : $this->options['bucket'].'.s3.amazonaws.com'));
$accessDomain = $this->get_access_domain();
include(dirname(__FILE__).'/admin-tab-head.html');
}
function upload_files_upload() {
@@ -363,7 +400,7 @@ function tab() {
return;
}
$bucket = $this->options['bucket'];
$accessDomain = ($this->options['cloudfront'] != '' ? $this->options['cloudfront'] : ($this->options['virtual-host'] ? $this->options['bucket'] : $this->options['bucket'].'.s3.amazonaws.com'));
$accessDomain = $this->get_access_domain();
$prefix = $_GET['prefix'] ? $_GET['prefix'] : '';
list($prefixes, $keys, $meta, $privateKeys) = $this->getKeys($restrictPrefix.$prefix);

1 comment on commit e777cd4

@irfan-area47

This comment has been minimized.

Show comment
Hide comment
@irfan-area47

irfan-area47 Dec 16, 2012

thanks for plugin, but how can i save my file on S3 only, not to local storage ?

irfan-area47 commented on e777cd4 Dec 16, 2012

thanks for plugin, but how can i save my file on S3 only, not to local storage ?

Please sign in to comment.