Skip to content

Commit

Permalink
Store updated media files when they're, e.g., rotated
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jan 2, 2025
1 parent 11638c6 commit a11d520
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions packages/playground/data-liberation-static-files-editor/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ static public function initialize() {
});

// Handle media uploads
add_filter('wp_handle_upload', function($upload) {
add_filter('wp_generate_attachment_metadata', function($metadata, $attachment_id) {
try {
if(!self::acquire_synchronization_lock()) {
return $upload;
return $metadata;
}
$file_path= $upload['file'];


$file_path = get_attached_file($attachment_id);
if(!$file_path) {
return $metadata;
}

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

Expand All @@ -168,6 +172,9 @@ static public function initialize() {

$file_path = self::resize_to_max_dimensions_if_files_is_an_image($file_path);

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

// Copy the file to the static media directory
$local_fs->copy($file_name, $target_path, [
'to_fs' => $main_fs,
Expand All @@ -176,17 +183,55 @@ static public function initialize() {
// Force pull after every write operation
self::$client->force_pull();

// Update attachment URL to point to static path
$upload['url'] = rest_url('static-files-editor/v1/download-file?path=' . urlencode($target_path));
return $metadata;
} finally {
self::release_synchronization_lock();
}
}, 10, 2);

// Set local_file_path metadata for the attachment
update_post_meta($upload['attachment_id'], 'local_file_path', $target_path);
// Handle attachment updates (e.g. image rotations)
add_action('wp_update_attachment_metadata', function($metadata, $attachment_id) {
try {
if(!self::acquire_synchronization_lock()) {
return $metadata;
}

$file_path = get_attached_file($attachment_id);
if(!$file_path) {
return $metadata;
}

return $upload;
$local_file_path = get_post_meta($attachment_id, 'local_file_path', true);
if(!$local_file_path) {
return $metadata;
}

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

$file_name = basename($file_path);
// Copy the updated file to the static media directory
$local_fs->copy($file_name, $local_file_path, [
'to_fs' => $main_fs,
]);

// Force pull after every write operation
self::$client->force_pull();

return $metadata;
} finally {
self::release_synchronization_lock();
}
});
}, 10, 2);

// Rewrite attachment URLs to use the static files download endpoint
add_filter('wp_get_attachment_url', function($url, $attachment_id) {
$local_file_path = get_post_meta($attachment_id, 'local_file_path', true);
if ($local_file_path) {
return rest_url('static-files-editor/v1/download-file?path=' . urlencode($local_file_path));
}
return $url;
}, 10, 2);

// Register the admin page
add_action('admin_menu', function() {
Expand Down

0 comments on commit a11d520

Please sign in to comment.