[wp-trac] [WordPress Trac] #38907: Add `path_is_absolute` filter.
WordPress Trac
noreply at wordpress.org
Wed Nov 23 00:52:01 UTC 2016
#38907: Add `path_is_absolute` filter.
-----------------------------+-----------------------------
Reporter: tmatsuo | Owner:
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 4.6.1
Severity: normal | Keywords:
Focuses: |
-----------------------------+-----------------------------
so that plugin developers can easily add media functionalities with stream
wrappers.
The path_is_absolute function will be updated to:
{{{#!php
<?php
function path_is_absolute( $path ) {
/*
* First apply a filter and see the result
*/
if ( apply_filters('path_is_absolute', $path) == true )
return true;
/*
* This is definitive if true but fails if $path does not exist or
contains
* a symbolic link.
*/
if ( realpath($path) == $path )
return true;
if ( strlen($path) == 0 || $path[0] == '.' )
return false;
// Windows allows absolute paths like this.
if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
return true;
// A path starting with / or \ is absolute; anything else is relative.
return ( $path[0] == '/' || $path[0] == '\\' );
}
}}}
Background:
I'm developing a plugin for uploading media files to Google Cloud Storage,
using Google Cloud Storage stream wrapper (gs:// path).
It's easy to use the `upload_dir` to achieve it, but when it comes to
deleting media files, there is a problem.
In `wp-includes/post.php`, there are some code to delete related files
when you delete an uploaded media file:
{{{#!php
<?php
// Remove intermediate and backup images if there are any.
if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $size => $sizeinfo ) {
$intermediate_file = str_replace( basename( $file
), $sizeinfo['file'], $file );
/** This filter is documented in wp-
includes/functions.php */
$intermediate_file = apply_filters(
'wp_delete_file', $intermediate_file );
@ unlink( path_join( $uploadpath['basedir'],
$intermediate_file ) );
}
}
}}}
When we replace directories with `upload_dir` filter, the values of
`$uploadpath['basedir']` and $intermediate_file are:
{{{#!php
<?php
$uploadpath['basedir']; // gs://bucket_name/1/
$intermediate_file; // gs://bucket_name/1/2016/11/old-electrical-panel-
150x150.jpg
}}}
So the path_join now returns something like:
`gs://bucket_name/1/gs://bucket_name/1/2016/11/old-electrical-panel-
150x150.jpg` because the file path is not considered as absolute. If we
can customize the return value of `path_is_absolute`, then the return
value of `path_join` will be something like `gs://bucket_name/1/2016/11
/old-electrical-panel-150x150.jpg` and the media editor correctly unlink
the files.
I'd appreciate it if you could consider adding this filter.
Of course, let me know if there are better solutions.
Thanks,
--
Ticket URL: <https://core.trac.wordpress.org/ticket/38907>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list