Skip to content

Commit

Permalink
Enable deleting file subtrees
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Dec 27, 2024
1 parent 7a4a0dc commit e55b94a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
63 changes: 53 additions & 10 deletions packages/playground/data-liberation-static-files-editor/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ static public function initialize() {
return current_user_can('edit_posts');
},
));

register_rest_route('static-files-editor/v1', '/delete-path', array(
'methods' => 'POST',
'callback' => array(self::class, 'delete_path_endpoint'),
'permission_callback' => function() {
return current_user_can('edit_posts');
},
));
});

// @TODO: the_content and rest_prepare_local_file filters run twice for REST API requests.
Expand Down Expand Up @@ -219,16 +227,18 @@ static public function initialize() {
}, 10, 3);

// Delete the associated file when a post is deleted
add_action('before_delete_post', function($post_id) {
$post = get_post($post_id);
if ($post && $post->post_type === WP_LOCAL_FILE_POST_TYPE) {
$fs = self::get_fs();
$path = get_post_meta($post_id, 'local_file_path', true);
if ($path && $fs->is_file($path)) {
$fs->rm($path);
}
}
});
// @TODO: Rethink this. We have a separate endpoint for deleting an entire path.
// Do we need a separate hook at all?
// add_action('before_delete_post', function($post_id) {
// $post = get_post($post_id);
// if ($post && $post->post_type === WP_LOCAL_FILE_POST_TYPE) {
// $fs = self::get_fs();
// $path = get_post_meta($post_id, 'local_file_path', true);
// if ($path && $fs->is_file($path)) {
// $fs->rm($path);
// }
// }
// });

// Update the file after post is saved
add_action('save_post_' . WP_LOCAL_FILE_POST_TYPE, function($post_id, $post, $update) {
Expand Down Expand Up @@ -724,6 +734,39 @@ static private function process_node($node, $parent_path, $fs, $request) {
return $created_files;
}

static public function delete_path_endpoint($request) {
$path = $request->get_param('path');
if (!$path) {
return new WP_Error('missing_path', 'File path is required');
}

// Find and delete associated post
$existing_posts = get_posts(array(
'post_type' => WP_LOCAL_FILE_POST_TYPE,
'meta_key' => 'local_file_path',
'meta_value' => $path,
'posts_per_page' => 1
));

if (!empty($existing_posts)) {
wp_delete_post($existing_posts[0]->ID, true);
}

// Delete the actual file
$fs = self::get_fs();
if($fs->is_dir($path)) {
if (!$fs->rmdir($path, ['recursive' => true])) {
return new WP_Error('delete_failed', 'Failed to delete directory');
}
} else {
if (!$fs->rm($path)) {
return new WP_Error('delete_failed', 'Failed to delete file');
}
}

return array('success' => true);
}

}

WP_Static_Files_Editor_Plugin::initialize();
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,16 @@ function ConnectedFilePickerTree() {
);

const handleNodeDeleted = async (path: string) => {
const { post_id } = (await apiFetch({
path: '/static-files-editor/v1/get-or-create-post-for-file',
method: 'POST',
data: { path },
})) as { post_id: string };
console.log({
post_id,
deleteUrl: `/wp/v2/${WP_LOCAL_FILE_POST_TYPE}/${post_id}`,
});

await apiFetch({
// ?force=true to skip the trash and delete the file immediately
path: `/wp/v2/${WP_LOCAL_FILE_POST_TYPE}/${post_id}?force=true`,
headers: {
'X-HTTP-Method-Override': 'DELETE',
},
});
await refreshFileTree();
try {
await apiFetch({
path: '/static-files-editor/v1/delete-path',
method: 'POST',
data: { path },
});
await refreshFileTree();
} catch (error) {
console.error('Failed to delete file:', error);
}
};

const handleFileClick = async (filePath: string, node: FileNode) => {
Expand Down

0 comments on commit e55b94a

Please sign in to comment.