Skip to content

Instantly share code, notes, and snippets.

@1naveengiri
Created March 9, 2017 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1naveengiri/38be87277710cde063c7b9f369fbe65a to your computer and use it in GitHub Desktop.
Save 1naveengiri/38be87277710cde063c7b9f369fbe65a to your computer and use it in GitHub Desktop.
New S3 converter
<?php
function get_attachments_s3_array() {
global $wpdb;
$table_name = $wpdb->prefix . 'rtamazon_s3_media';
$query = "Select * from ".$table_name." where s3_url IS NULL AND media_id=83";
$attachment_data = $wpdb->get_results($query,OBJECT);
$final_attachment_data = array();
if(!empty($attachment_data)){
foreach ($attachment_data as $key => $value) {
$single_attachment_data['blog_id'] = 1;
$single_attachment_data['media_id'] = $value->media_id;
$url_host = parse_url( $value->wp_url );
$single_attachment_data['obj_key'] = $value->obj_key;
$single_attachment_data['wp_url'] = $value->wp_url;
$bucket = rtamazon_s3_get_bucket_name();
$rtawss3_client = new RTAWSS3_Client();
$key = $value->obj_key;
$s3_url = $rtawss3_client->rtawss3_get_object_url( $bucket, $key );
$single_attachment_data['s3_url'] = $s3_url;
$single_attachment_data['bucket'] = $bucket;
$single_attachment_data['parent_id'] = null;
$single_attachment_data['media_size'] = null;
$single_attachment_data['status'] = 'moved';
//set parent attachment data.
$attachment_data_array[$value->id] = $single_attachment_data;
}
if(!empty($attachment_data_array)){
//set child attachment data.
$final_attachment_data = add_child_attchments( $attachment_data_array );
}
}
return $final_attachment_data;
}
/**
* Render child attachment data based on given attachment array.
*
* @param array $attachment_data parent attachment data to be store in s3 DB table.
*/
function add_child_attchments( $attachment_data ) {
if ( ! empty( $attachment_data ) ) {
foreach ( $attachment_data as $parent_key => $parent_value ) {
$child_attachment_array = array();
if ( ! empty( $parent_value['media_id'] ) ) {
$attachment_meta_data = wp_get_attachment_metadata( $parent_value['media_id'] );
if ( ! empty( $attachment_meta_data['sizes'] ) ) {
foreach ( $attachment_meta_data['sizes'] as $_meta_key => $_meta_value ) {
$child_attachment_array = $parent_value;
$child_attachment_array['media_id'] = null;
$child_attachment_array['media_size'] = $_meta_key;
$child_url_array = explode( '/', $parent_value['wp_url'] );
array_pop( $child_url_array );
array_push( $child_url_array, $_meta_value['file'] );
$child_url = implode( '/',$child_url_array );
$child_attachment_array['wp_url'] = $child_url;
$url_host = parse_url( $child_url );
$child_attachment_array['obj_key'] = $url_host['path'];
$bucket = rtamazon_s3_get_bucket_name();
$rtawss3_client = new RTAWSS3_Client();
$key = $url_host['path'];
$s3_url = $rtawss3_client->rtawss3_get_object_url( $bucket, $key );
$child_attachment_array['s3_url'] = $s3_url;
$child_attachment_array['parent_id'] = $parent_value['media_id'];
$attachment_data[] = $child_attachment_array;
}
}
}
}
}
return $attachment_data;
}
/**
* Function insert data in amazon s3 table and set /?do=now in your URL to fire this script.
*/
function save_amazons3_attachment_data() {
if(isset($_GET['do']) && $_GET['do'] == 'now'){
global $wpdb;
$table_name = $wpdb->prefix . 'rtamazon_s3_media';
$attachment_data = get_attachments_s3_array();
if(!empty($attachment_data)){
foreach ( $attachment_data as $key => $value ) {
$format = array( '%d','%d','%s','%s','%s','%s','%d','%s','%s' );
$wpdb->update( $table_name, $value, array( 'ID' => $key ),$format , array( '%d' ) );
}
}
}
}
add_action( 'init','save_amazons3_attachment_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment