Skip to content

Commit

Permalink
Use the correct paths in wp_handle_upload
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jan 2, 2025
1 parent 875c89a commit 0077659
Showing 1 changed file with 67 additions and 14 deletions.
81 changes: 67 additions & 14 deletions packages/playground/data-liberation-static-files-editor/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Plugin Name: Data Liberation – WordPress Static files editor
*
* @TODO: Page metadata editor in Gutenberg
* @TODO: Choose the local file storage format (MD, HTML, etc.) in Gutenberg page options.
* @TODO: HTML, XHTML, and Blocks renderers
* @TODO: Integrity check – is the database still in sync with the files?
* If not, what should we do?
* * Overwrite the database with the local files? This is a local files editor after all.
* * Display a warning in wp-admin and let the user decide what to do?
* @TODO: Consider tricky scenarios – moving a parent to trash and then restoring it.
* @TODO: Call resize_to_max_dimensions_if_files_is_an_image for the images dragged directly
* into the file picker
*/

use WordPress\Filesystem\WP_Local_Filesystem;
Expand All @@ -32,6 +33,10 @@
define( 'WP_AUTOSAVES_DIRECTORY', '.autosaves' );
}

if( ! defined( 'WP_STATIC_FILES_EDITOR_IMAGE_MAX_DIMENSION' )) {
define( 'WP_STATIC_FILES_EDITOR_IMAGE_MAX_DIMENSION', 1280 );
}

if(isset($_GET['dump'])) {
add_action('init', function() {
WP_Static_Files_Editor_Plugin::import_static_pages();
Expand Down Expand Up @@ -153,18 +158,24 @@ static public function initialize() {
if(!self::acquire_synchronization_lock()) {
return $upload;
}

$file = $upload['file'];
$file_path= $upload['file'];

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

$filename = basename($file);
$target_path = wp_join_paths(WP_STATIC_MEDIA_DIR, $filename);
$local_fs->copy($filename, $target_path, [
$file_name = basename($file_path);
$target_path = wp_join_paths(WP_STATIC_MEDIA_DIR, $file_name);

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

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

// 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));

Expand Down Expand Up @@ -407,6 +418,46 @@ function() {
}, 10, 1);
}

/**
* Resize image to a maximum width and height.
*
* @param string $image_path The path to the image file
* @return string The path to the resized image file
*/
static public function resize_to_max_dimensions_if_files_is_an_image($image_path) {
// Only resize if this is an image file
// getimagesize() returns false for non-images (and
// also image formats it can't handle)
$image_size = @getimagesize($image_path);
if ($image_size === false) {
return $image_path;
}

if ($image_size[0] > WP_STATIC_FILES_EDITOR_IMAGE_MAX_DIMENSION || $image_size[1] > WP_STATIC_FILES_EDITOR_IMAGE_MAX_DIMENSION) {
$editor = wp_get_image_editor($image_path);
if (is_wp_error($editor)) {
return $image_path;
}

$editor->resize(WP_STATIC_FILES_EDITOR_IMAGE_MAX_DIMENSION, WP_STATIC_FILES_EDITOR_IMAGE_MAX_DIMENSION, false);

// Try saving to original path first
$result = $editor->save($image_path);

// If saving fails (read-only), save to temp file
if (is_wp_error($result)) {
$temp_path = wp_tempnam(basename($image_path));
$result = $editor->save($temp_path);
if (is_wp_error($result)) {
return $image_path;
}
$image_path = $temp_path;
}
}

return $image_path;
}

static public function download_file_endpoint($request) {
$path = $request->get_param('path');
$fs = self::get_fs();
Expand Down Expand Up @@ -445,7 +496,7 @@ static public function download_file_endpoint($request) {
return new WP_Error('file_error', 'Could not read file');
}

static private $synchronizing = false;
static private $synchronizing = 0;
static private function acquire_synchronization_lock() {
// Skip if in maintenance mode
if (wp_is_maintenance_mode()) {
Expand All @@ -457,15 +508,15 @@ static private function acquire_synchronization_lock() {
}

// @TODO: Synchronize between threads
if(self::$synchronizing) {
if(self::$synchronizing > 0) {
return false;
}
self::$synchronizing = true;
++self::$synchronizing;
return true;
}

static private function release_synchronization_lock() {
self::$synchronizing = false;
self::$synchronizing = max(0, self::$synchronizing - 1);
}

static private function refresh_post_from_local_file($post) {
Expand Down Expand Up @@ -741,11 +792,13 @@ static public function import_static_pages() {
// Prevent ID conflicts
self::reset_db_data();

return self::do_import_static_pages();
return self::do_import_static_pages([
'from_filesystem' => self::get_fs(),
]);
}

static private function do_import_static_pages($options = array()) {
$fs = $options['filesystem'] ?? self::get_fs();
$fs = $options['from_filesystem'];
$importer = WP_Stream_Importer::create(
function () use ($fs, $options) {
return new WP_Filesystem_Entity_Reader(
Expand Down Expand Up @@ -987,7 +1040,7 @@ static public function create_files_endpoint($request) {
}

$result = self::do_import_static_pages(array(
'filesystem' => $uploaded_fs,
'from_filesystem' => $uploaded_fs,
'post_tree_options' => array(
'root_parent_id' => $parent_id,
'create_index_pages' => false,
Expand Down

0 comments on commit 0077659

Please sign in to comment.