Skip to content

Commit

Permalink
Add S3 column to media library.
Browse files Browse the repository at this point in the history
Add "Import to S3" bulk action to media library
  • Loading branch information
jawngee committed Sep 11, 2017
1 parent 716a63f commit 4e933ec
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions classes/tools/s3/ilab-media-s3-tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,83 @@ private function hookupUI() {
});
</script>
<?php

} );

add_action('admin_head', function(){

if (get_current_screen()->base == 'upload') {
?>
<style>
th.column-s3, td.column-s3 {
width:60px !important;
max-width: 60px !important;
}
</style>
<?php
}
});
} );

add_action('admin_init',function(){
add_meta_box('ilab-s3-info-meta','S3 Info',[$this,'renderS3InfoMeta'], 'attachment', 'side', 'low');

add_action('edit_attachment', [$this, 'editAttachment']);

add_filter('manage_media_columns', function($cols) {
$cols["s3"] = 'S3';
return $cols;
});

add_action('manage_media_custom_column', function($column_name, $id) {
$meta = wp_get_attachment_metadata($id);
if (!empty($meta) && isset($meta['s3'])) {
echo "<a href='".$meta['s3']['url']."' target=_blank>View</a>";
}
}, 10, 2);

if ($this->enabled()) {
add_filter('bulk_actions-upload', function($actions){
$actions['ilab_s3_import'] = 'Import to S3';
return $actions;
});

add_filter('handle_bulk_actions-upload', function($redirect_to, $action_name, $post_ids) {
if ('ilab_s3_import' === $action_name) {
$posts_to_import = [];
if (count($post_ids) > 0) {
foreach($post_ids as $post_id) {
$meta = wp_get_attachment_metadata($post_id);
if (!empty($meta) && isset($meta['s3'])) {
continue;
}

$posts_to_import[] = $post_id;
}
}

if (count($posts_to_import) > 0) {
update_option('ilab_s3_import_status', true);
update_option('ilab_s3_import_total_count', count($posts_to_import));
update_option('ilab_s3_import_current', 1);
update_option('ilab_s3_import_should_cancel', false);

$process = new ILABS3ImportProcess();

for($i = 0; $i < count($posts_to_import); ++$i) {
$process->push_to_queue(['index' => $i, 'post' => $posts_to_import[$i]]);
}

$process->save();
$process->dispatch();

return 'admin.php?page=media-tools-s3-importer';
}
}

return $redirect_to;
}, 1000, 3);
}
});
}

Expand Down

0 comments on commit 4e933ec

Please sign in to comment.