Skip to content

Instantly share code, notes, and snippets.

@williameliel
Last active April 7, 2017 20:05
Show Gist options
  • Save williameliel/af49e60e7e41de5836af88ec647b1496 to your computer and use it in GitHub Desktop.
Save williameliel/af49e60e7e41de5836af88ec647b1496 to your computer and use it in GitHub Desktop.
Use this with the delicious brains S3 offset plugin. Will create the metadata for all the assets in the Wordpress site
<?php
include('wp/wp-load.php');
class S3{
private $bucket = 'bucket-cdn';
private $directories = 'content/uploads/';
private $sites = '';
private $post_id = null;
private $blog;
public function __construct($post_id = null, $blog = 1){
if (empty($post_id)){
return false;
}
$this->post_id = $post_id;
$this->blog = $blog;
$this->init();
}
public function init(){
$this->metadata = $this->get_metadata();
}
public function get_metadata(){
$metadata = get_metadata('post', $this->post_id, '_wp_attached_file', true);
$directory = $this->directories;
if($this->blog > 1){
$directory = $directory . $this->sites . $this->blog.'/';
}
$meta_arr = array(
'bucket'=> $this->bucket,
'key' => $directory.$metadata,
'region' => '',
);
return $meta_arr;
}
}
$blogs = array(1);
foreach ($blogs as $blog) {
//restore_current_blog();
//switch_to_blog($blog);
echo '<h1>'.get_bloginfo('name').'</h1>';
$the_query = new WP_Query(array( 'post_type'=>'attachment', 'post_status' => 'inherit', 'posts_per_page' => -1) );
if ( $the_query->have_posts() ){
while ( $the_query->have_posts() ){
$the_query->the_post();
$meta = new S3($post->ID, $blog);
delete_post_meta( $post->ID, 'amazonS3_info' );
add_post_meta( $post->ID, 'amazonS3_info', $meta->metadata );
print_r($meta->metadata);
echo '<br>';
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment