Skip to content

[Data Liberation] Edit static files directly in a git repository #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
4da97f0
Remount NODEFS and other mounts after hotswapPhpRuntime is called
adamziel Dec 23, 2024
9456b22
Preserve the /internal directory when hotSwapPhpRuntime is called
adamziel Dec 23, 2024
fc74f35
Test with 4 mounts
adamziel Dec 23, 2024
199bc5e
Local files browser in the post editor
adamziel Dec 23, 2024
e46e7e9
Add Editor UI and hooks to synchronize WordPress pages to local files
adamziel Dec 24, 2024
7b5b125
Implement Git sparse checkout in PHP
adamziel Dec 25, 2024
e821059
Plug in WP_Git_Filesystem to directly browse and edit the static file…
adamziel Dec 26, 2024
4d10e75
Prototype git push
adamziel Dec 27, 2024
45d7a88
Put all pack-related methods in WP_Git_Pack_Processor
adamziel Dec 27, 2024
0d8f88f
WP_Git_Filesystem that can read and write files from a Git repo
adamziel Dec 27, 2024
cefacc2
Tweak new file creation and metadata loading
adamziel Dec 27, 2024
18e73b8
Add specific exception for renaming directories
adamziel Dec 27, 2024
0da6447
Support renaming and deleting files
adamziel Dec 27, 2024
88b57b7
Tweak the user experience of the file picker. Enable drag&drop
adamziel Dec 27, 2024
74e671a
Replace recursive props with a context
adamziel Dec 27, 2024
7a4a0dc
Enable file uploading by drag&drop
adamziel Dec 27, 2024
e55b94a
Enable deleting file subtrees
adamziel Dec 27, 2024
718bbe6
Dsplay directories before files, use a more useful default commit
adamziel Dec 27, 2024
d9c0ead
Display a visually consistent selection in the Local Files tab without a
adamziel Dec 27, 2024
f945ec3
Use the vertical three dots icon for "more"
adamziel Dec 27, 2024
e5679d2
CSS tweaks
adamziel Dec 27, 2024
1fbfbfc
CSS tweaks
adamziel Dec 27, 2024
7e5e96a
Restrict the local files tab to the list view sidebar
adamziel Dec 28, 2024
6951723
WP_Git_Client: Use AsyncHttp\Client for HTTP requests
adamziel Dec 29, 2024
8b764c1
Fix disappearing editor, add post loading overlay
adamziel Dec 29, 2024
fdf9393
Add WP_Git_Cached_Index that stores objects in a local directory
adamziel Dec 29, 2024
63ed39a
Expand WP_Git_Cached_Index to support more operations and commits
adamziel Dec 30, 2024
59884ed
Streaming git read, ability to compute objects newly added in a commit
adamziel Dec 30, 2024
6200a4a
Implement git pull and push
adamziel Dec 30, 2024
d56adb5
Rename Git_Cached_Index to Git_Repository ane enable remote management
adamziel Dec 30, 2024
724e177
Support user.name and user.email git settings and auto push mode in t…
adamziel Dec 30, 2024
c07add9
Use serialize_blocks to store custom blocks in markdown
adamziel Dec 30, 2024
decd81e
Resolve minor papercuts to enable using the plugin with git
adamziel Dec 30, 2024
96f52a3
Store auto-saves on the disk
adamziel Dec 31, 2024
270737d
Improve Blocks/Markdown conversion
adamziel Dec 31, 2024
7bef2bc
Implement reparent, squash, amend
adamziel Dec 31, 2024
e368495
Send autosaves as commit amends
adamziel Dec 31, 2024
1676afa
Drag and drop from the editor to desktop
adamziel Jan 1, 2025
d33b84f
Preview static files
adamziel Jan 1, 2025
620755d
Only force pull at most once every 10 minutes, exclude .autosaves fro…
adamziel Jan 1, 2025
8594402
Reduce waiting in the UI – server provides each page's ID, the editor…
adamziel Jan 1, 2025
6956b58
Import attachments on boot and on upload
adamziel Jan 1, 2025
b2755d7
Suport UTF-8 filenames in download_file_endpoint
adamziel Jan 1, 2025
a9202bb
Rewrite WordPress URLs to relative URLs when serializing to markdown
adamziel Jan 2, 2025
802a6f0
Preserve images as blocks
adamziel Jan 2, 2025
875c89a
Upload media files to the static files filesystem
adamziel Jan 2, 2025
0077659
Use the correct paths in wp_handle_upload
adamziel Jan 2, 2025
11638c6
Delete files when backspace is pressed
adamziel Jan 2, 2025
a11d520
Store updated media files when they're, e.g., rotated
adamziel Jan 2, 2025
34e08ff
Media files preview
adamziel Jan 2, 2025
86ab57d
Derive WP_Block_Markup_Processor from WP_HTML_Tag_Processor, not WP_H…
adamziel Jan 2, 2025
6be33e3
Preserve the correct last href in Blocks -> Markdown conversion
adamziel Jan 2, 2025
1f27630
Don't explicitly force pull after writes
adamziel Jan 3, 2025
2ad968b
Rudimentary commit diffing and text diffing
adamziel Jan 3, 2025
12c3fb2
Clarify a TODO comment
adamziel Jan 3, 2025
b13b833
Implement three way merge
adamziel Jan 3, 2025
65c73bb
Support for the HTML format and changing file extensions
adamziel Jan 4, 2025
6357abd
Start implementing a git server
adamziel Jan 5, 2025
dd73f79
Parse Git Client messages
adamziel Jan 5, 2025
bb3928e
Add unit tests
adamziel Jan 5, 2025
b28b46a
Functional fetch support
adamziel Jan 6, 2025
4d90846
Implement git push in WP_Git_Server
adamziel Jan 6, 2025
32af23d
Remove debug comment
adamziel Jan 6, 2025
122fe94
Add TODO
adamziel Jan 6, 2025
149f8b4
git fetch WordPress pages
adamziel Jan 6, 2025
75f7601
Add readme for the git repo plugin
adamziel Jan 6, 2025
9b09454
Add a todo
adamziel Jan 6, 2025
8dd84b3
git push to update content
adamziel Jan 6, 2025
2d5da28
When fetching, fetch all the relevant objects without leaving out any
adamziel Jan 7, 2025
d4afa36
Comment out the failure in find_objects_added_in
adamziel Jan 7, 2025
bbea7ae
Handle content deletions in the git endpoint
adamziel Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enable deleting file subtrees
  • Loading branch information
adamziel committed Dec 27, 2024
commit e55b94a717e25bd07c161a3167d864ed123c20ca
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
Loading