REST API: Ensure attachments are uploaded to the post's year/month folder.
If organizing uploads into month- and year-based folders, uploading an attachment to an existing post should store the file in `wp-content/uploads/<year>/<month>` based on the post's publish date. This is in line with the behavior in classic editor / the media modal. Props swissspidy, adamsilverstein, timothyblynjacobs, skithund, sergeybiryukov, patricia70. Fixes #61189. Built from https://develop.svn.wordpress.org/trunk@58130 git-svn-id: http://core.svn.wordpress.org/trunk@57595 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
8839c33306
commit
960c3631e4
|
@ -1089,7 +1089,7 @@ function _wp_handle_upload( &$file, $overrides, $time, $action ) {
|
|||
* @param array|false $overrides Optional. An associative array of names => values
|
||||
* to override default variables. Default false.
|
||||
* See _wp_handle_upload() for accepted values.
|
||||
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @return array See _wp_handle_upload() for return value.
|
||||
*/
|
||||
function wp_handle_upload( &$file, $overrides = false, $time = null ) {
|
||||
|
@ -1120,7 +1120,7 @@ function wp_handle_upload( &$file, $overrides = false, $time = null ) {
|
|||
* @param array|false $overrides Optional. An associative array of names => values
|
||||
* to override default variables. Default false.
|
||||
* See _wp_handle_upload() for accepted values.
|
||||
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @return array See _wp_handle_upload() for return value.
|
||||
*/
|
||||
function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
|
||||
|
|
|
@ -2344,10 +2344,10 @@ function wp_get_upload_dir() {
|
|||
* @since 2.0.0
|
||||
* @uses _wp_upload_dir()
|
||||
*
|
||||
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @param bool $create_dir Optional. Whether to check and create the uploads directory.
|
||||
* Default true for backward compatibility.
|
||||
* @param bool $refresh_cache Optional. Whether to refresh the cache. Default false.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @param bool $create_dir Optional. Whether to check and create the uploads directory.
|
||||
* Default true for backward compatibility.
|
||||
* @param bool $refresh_cache Optional. Whether to refresh the cache. Default false.
|
||||
* @return array {
|
||||
* Array of information about the upload directory.
|
||||
*
|
||||
|
@ -2419,7 +2419,7 @@ function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false
|
|||
* @since 4.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @return array See wp_upload_dir()
|
||||
*/
|
||||
function _wp_upload_dir( $time = null ) {
|
||||
|
@ -2871,7 +2871,7 @@ function _wp_check_existing_file_names( $filename, $files ) {
|
|||
* @param string $name Filename.
|
||||
* @param null|string $deprecated Never used. Set to null.
|
||||
* @param string $bits File content
|
||||
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @return array {
|
||||
* Information about the newly-uploaded file.
|
||||
*
|
||||
|
|
|
@ -254,10 +254,21 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||
$files = $request->get_file_params();
|
||||
$headers = $request->get_headers();
|
||||
|
||||
$time = null;
|
||||
|
||||
// Matches logic in media_handle_upload().
|
||||
if ( ! empty( $request['post'] ) ) {
|
||||
$post = get_post( $request['post'] );
|
||||
// The post date doesn't usually matter for pages, so don't backdate this upload.
|
||||
if ( $post && 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
|
||||
$time = $post->post_date;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $files ) ) {
|
||||
$file = $this->upload_from_file( $files, $headers );
|
||||
$file = $this->upload_from_file( $files, $headers, $time );
|
||||
} else {
|
||||
$file = $this->upload_from_data( $request->get_body(), $headers );
|
||||
$file = $this->upload_from_data( $request->get_body(), $headers, $time );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $file ) ) {
|
||||
|
@ -1035,12 +1046,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||
* Handles an upload via raw POST data.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @since 6.6.0 Added the `$time` parameter.
|
||||
*
|
||||
* @param string $data Supplied file data.
|
||||
* @param array $headers HTTP headers from the request.
|
||||
* @param string $data Supplied file data.
|
||||
* @param array $headers HTTP headers from the request.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @return array|WP_Error Data from wp_handle_sideload().
|
||||
*/
|
||||
protected function upload_from_data( $data, $headers ) {
|
||||
protected function upload_from_data( $data, $headers, $time = null ) {
|
||||
if ( empty( $data ) ) {
|
||||
return new WP_Error(
|
||||
'rest_upload_no_data',
|
||||
|
@ -1128,7 +1141,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||
'test_form' => false,
|
||||
);
|
||||
|
||||
$sideloaded = wp_handle_sideload( $file_data, $overrides );
|
||||
$sideloaded = wp_handle_sideload( $file_data, $overrides, $time );
|
||||
|
||||
if ( isset( $sideloaded['error'] ) ) {
|
||||
@unlink( $tmpfname );
|
||||
|
@ -1246,12 +1259,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||
* Handles an upload via multipart/form-data ($_FILES).
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @since 6.6.0 Added the `$time` parameter.
|
||||
*
|
||||
* @param array $files Data from the `$_FILES` superglobal.
|
||||
* @param array $headers HTTP headers from the request.
|
||||
* @param array $files Data from the `$_FILES` superglobal.
|
||||
* @param array $headers HTTP headers from the request.
|
||||
* @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.
|
||||
* @return array|WP_Error Data from wp_handle_upload().
|
||||
*/
|
||||
protected function upload_from_file( $files, $headers ) {
|
||||
protected function upload_from_file( $files, $headers, $time = null ) {
|
||||
if ( empty( $files ) ) {
|
||||
return new WP_Error(
|
||||
'rest_upload_no_data',
|
||||
|
@ -1293,7 +1308,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||
// Include filesystem functions to get access to wp_handle_upload().
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
|
||||
$file = wp_handle_upload( $files['file'], $overrides );
|
||||
$file = wp_handle_upload( $files['file'], $overrides, $time );
|
||||
|
||||
if ( isset( $file['error'] ) ) {
|
||||
return new WP_Error(
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.6-alpha-58129';
|
||||
$wp_version = '6.6-alpha-58130';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue