Skip to content

Commit 7f91f64

Browse files
author
Akshay Paghdar
committed
Preview & Delete Option In File List Tree
1 parent 1783bf7 commit 7f91f64

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

assets/admin/css/style.css

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,29 @@ ul.tree-list a:hover {
2929
ul.tree-list li {
3030
text-align : justify;
3131
line-height: 25px;
32+
position: relative;
3233
}
3334

3435
.file-list {
3536
border : 1px solid #cccccc40;
3637
padding : 10px;
3738
border-radius: 5px;
3839
background : #fbfbfb;
39-
}
40+
}
41+
42+
li.tree-file:hover{
43+
background: #f1f1f1;
44+
}
45+
li.tree-file:hover .file-actions{display:inline-block;}
46+
.file-actions{
47+
position: absolute;
48+
top: 0;
49+
right: 0;
50+
display: none;
51+
}
52+
.file-actions a{
53+
font-size: 12px;
54+
padding: 0 5px;
55+
border-right: 1px solid #DDD;
56+
}
57+
.file-actions a:last-child{border:0;}

assets/admin/js/main.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,27 @@ function expandCollapseAll(oElID,oState) {
194194
oLnk.onclick();
195195
} } } }
196196

197-
jQuery(function ($){
198-
compactMenu('tree-list',true, '');
197+
jQuery(function ($) {
198+
compactMenu('tree-list', true, '');
199+
200+
jQuery(document).on('click', '.delete-tree-file', function () {
201+
var $thisele = jQuery(this);
202+
var zip = jQuery('#zip-file-url').val();
203+
var path = jQuery(this).attr('data-path');
204+
var file = jQuery(this).attr('data-file');
205+
if (confirm('Are you sure you want to remove this file?')) {
206+
jQuery.ajax({type: "post", dataType: "json",
207+
url: ajaxurl,
208+
data: {action: 'delete_tree_file', path: path, file: file, zip: zip},
209+
success: function (msg) {
210+
if (msg != 1) {
211+
return false;
212+
} else {
213+
$thisele.parent('div').parent('li').remove();
214+
}
215+
}
216+
});
217+
}
218+
return false;
219+
});
199220
});

includes/post-types/contentsView.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function isFolder($path)
5858
/**
5959
* Helper method to list folders in a list
6060
*/
61-
public function recursiveFileStructure($fileStructure)
61+
public function recursiveFileStructure($fileStructure, $url='')
6262
{
6363
$output = '';
6464
foreach ($fileStructure as $folder => $children) {
@@ -70,13 +70,22 @@ public function recursiveFileStructure($fileStructure)
7070
if ($this->isFolder($folder) && !empty($folder)) {
7171
$output .= '<strong>' . $folder . '/</strong>';
7272
} elseif ($folder && !empty($folder)) {
73-
$output .= '<li>' . $folder . '</li>';
73+
$file_path = str_replace(site_url('/'), '', esc_url($url));
74+
$file_url = $url.'/'.$children;
75+
$output .= '<li class="tree-file">';
76+
$output .= $folder;
77+
$output .= '<div class="file-actions">';
78+
$output .= '<a href="'.$file_url.'" target="_blank">'. __( 'Preview', 'drophtml' ) . '</a>';
79+
//$output .= '<a href="#">'. __( 'Edit', 'drophtml' ) . '</a>';
80+
$output .= '<a href="javascript:void(0)" class="delete-tree-file" data-path="'.$file_path.'" data-file="'.$children.'">'. __( 'Delete', 'drophtml' ) . '</a>';
81+
$output .= '</div>';
82+
$output .= '</li>';
7483
}
7584
}
7685

7786
if (is_array($children) && !empty($children)) {
7887
$output .= '<ul class="folder">';
79-
$output .= $this->recursiveFileStructure($children);
88+
$output .= $this->recursiveFileStructure($children, $url);
8089
$output .= '</ul>';
8190
}
8291

@@ -114,6 +123,7 @@ function showFileList($id)
114123
}
115124

116125
$file = get_post_meta($id, 'wp_custom_attachment', true);
126+
$drop_url = get_post_meta($id, 'drop_preview_url', true);
117127

118128
if ($file) {
119129

@@ -139,9 +149,10 @@ function showFileList($id)
139149
$html .= '<pre class="file-list">';
140150
$html .= '<ul id="tree-list" class="tree-list" role="tree" aria-labelledby="plugin-files-label">';
141151
if ($fileStructure) {
142-
$html .= $this->recursiveFileStructure($fileStructure);
152+
$html .= $this->recursiveFileStructure($fileStructure, $drop_url);
143153
}
144154
$html .= '</ul>';
155+
$html .= '<input type="hidden" id="zip-file-url" value="'.$path.'">';
145156
$html .= '</pre>';
146157
}
147158

includes/post-types/upload.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function __construct() {
2929
add_filter("manage_edit-{$this->post_type}_sortable_columns", [$this, 'register_sortable_columns']);
3030
add_filter('post_row_actions', [$this, 'action_row'], 10, 2);
3131
add_action('before_delete_post', [$this, 'delete_all_attached_media']);
32-
}
32+
33+
add_action('wp_ajax_nopriv_delete_tree_file', [$this, 'delete_tree_file']);
34+
add_action('wp_ajax_delete_tree_file', [$this, 'delete_tree_file']);
35+
}
3336

3437
/**
3538
* Helper function to check correct post type.
@@ -227,4 +230,26 @@ function delete_all_attached_media( $post_id ) {
227230
}
228231
}
229232

233+
function delete_tree_file() {
234+
$result = '0';
235+
if (isset($_POST['path']) && !empty($_POST['path'])) {
236+
$zipfile = $_POST['zip'];
237+
$path = $_POST['path'];
238+
$file = $_POST['file'];
239+
240+
$zip = new \ZipArchive();
241+
$zip->open($zipfile);
242+
$zip->deleteName($file);
243+
$zip->close();
244+
245+
$file_path = trailingslashit(ABSPATH) . $path . '/' . $file;
246+
if (file_exists($file_path)) {
247+
unlink($file_path);
248+
}
249+
$result = '1';
250+
}
251+
echo $result;
252+
exit;
253+
}
254+
230255
}

0 commit comments

Comments
 (0)