Skip to content

Commit

Permalink
Upload media files to the static files filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jan 2, 2025
1 parent 802a6f0 commit 875c89a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ private function convert_markdown_to_blocks() {
$this->append_content( '</p>' );
$this->pop_block();
}
// @TODO: Find a way to plug in the attachment ID here
$image_block = <<<BLOCK
<!-- wp:image -->
<figure class="wp-block-image size-full">
Expand Down
35 changes: 33 additions & 2 deletions packages/playground/data-liberation-static-files-editor/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}

if ( ! defined( 'WP_STATIC_MEDIA_DIR' ) ) {
define( 'WP_STATIC_MEDIA_DIR', WP_STATIC_PAGES_DIR . '/media' );
define( 'WP_STATIC_MEDIA_DIR', 'media' );
}

if( ! defined( 'WP_LOCAL_FILE_POST_TYPE' )) {
Expand Down Expand Up @@ -136,7 +136,6 @@ static public function initialize() {
// Register hooks
register_activation_hook( __FILE__, array(self::class, 'import_static_pages') );


add_action('init', function() {
self::get_fs();
self::register_post_type();
Expand All @@ -148,6 +147,36 @@ static public function initialize() {
}
});

// Handle media uploads
add_filter('wp_handle_upload', function($upload) {
try {
if(!self::acquire_synchronization_lock()) {
return $upload;
}

$file = $upload['file'];

$main_fs = self::get_fs();
$local_fs = new WP_Local_Filesystem(dirname($file));

$filename = basename($file);
$target_path = wp_join_paths(WP_STATIC_MEDIA_DIR, $filename);
$local_fs->copy($filename, $target_path, [
'to_fs' => $main_fs,
]);

// Update attachment URL to point to static path
$upload['url'] = rest_url('static-files-editor/v1/download-file?path=' . urlencode($target_path));

// Set local_file_path metadata for the attachment
update_post_meta($upload['attachment_id'], 'local_file_path', $target_path);

return $upload;
} finally {
self::release_synchronization_lock();
}
});

// Register the admin page
add_action('admin_menu', function() {
add_menu_page(
Expand Down Expand Up @@ -581,6 +610,8 @@ static private function unwordpressify_static_assets_urls($content) {
/**
* Convert references to files served via path to the
* corresponding download_file_endpoint references.
*
* @TODO: Plug in the attachment IDs into image blocks
*/
static private function wordpressify_static_assets_urls($content) {
$site_url = WP_URL::parse(get_site_url());
Expand Down

0 comments on commit 875c89a

Please sign in to comment.