Skip to content

Commit

Permalink
Support renaming and deleting files
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Dec 27, 2024
1 parent 18e73b8 commit 0da6447
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 68 deletions.
12 changes: 11 additions & 1 deletion packages/playground/components/src/FilePickerTree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
__experimentalTreeGridCell as TreeGridCell,
Button,
Spinner,
DropdownMenu,
} from '@wordpress/components';
import { Icon, chevronRight, chevronDown } from '@wordpress/icons';
import { Icon, chevronRight, chevronDown, more } from '@wordpress/icons';
import '@wordpress/components/build-style/style.css';
import css from './style.module.css';
import classNames from 'classnames';
Expand Down Expand Up @@ -277,6 +278,15 @@ const NodeRow: React.FC<{
</Button>
)}
</TreeGridCell>
<TreeGridCell>
{() => (
<DropdownMenu
icon={more}
label="More actions"
controls={[]}
/>
)}
</TreeGridCell>
</TreeGridRow>
{isExpanded &&
node.children &&
Expand Down
52 changes: 52 additions & 0 deletions packages/playground/data-liberation-static-files-editor/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ static public function initialize() {
return current_user_can('edit_posts');
},
));

register_rest_route('static-files-editor/v1', '/rename-file', array(
'methods' => 'POST',
'callback' => array(self::class, 'rename_file_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 @@ -201,6 +209,18 @@ static public function initialize() {
return $response;
}, 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);
}
}
});

// Update the file after post is saved
add_action('save_post_' . WP_LOCAL_FILE_POST_TYPE, function($post_id, $post, $update) {
self::save_post_data_to_local_file($post);
Expand Down Expand Up @@ -560,6 +580,38 @@ static public function create_directory_endpoint($request) {
return array('success' => true);
}

static public function rename_file_endpoint($request) {
$original_path = $request->get_param('originalPath');
$new_path = $request->get_param('newPath');

if (!$original_path || !$new_path) {
return new WP_Error('missing_path', 'Both original and new paths are required');
}

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

$fs = self::get_fs();
if (!$fs->rename($original_path, $new_path)) {
return new WP_Error('rename_failed', 'Failed to rename file');
}

if (!empty($existing_posts)) {
update_post_meta($existing_posts[0]->ID, 'local_file_path', $new_path);
wp_update_post(array(
'ID' => $existing_posts[0]->ID,
'post_title' => basename($new_path)
));
}

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

}

WP_Static_Files_Editor_Plugin::initialize();
Loading

0 comments on commit 0da6447

Please sign in to comment.