Skip to content

Commit

Permalink
Merge pull request #131 from 10up/hotfix/upload-path-month-date
Browse files Browse the repository at this point in the history
Fix the upload date in image metadata on back date posts
  • Loading branch information
jeffpaul authored Jan 25, 2021
2 parents 789cfaf + a92e468 commit 249afec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
10 changes: 6 additions & 4 deletions includes/class-windows-azure-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,16 @@ static public function file_exists( $relative_path ) {
*
* @static
* @access public
*
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
*
* @return array
*/
static public function wp_upload_dir() {
static public function wp_upload_dir( $time = null ) {
static $wp_upload_dir = array();

$blog_id = get_current_blog_id();
if ( empty( $wp_upload_dir[ $blog_id ] ) ) {
if ( empty( $wp_upload_dir[ $blog_id ] ) || ! is_null( $time ) ) {
$dir = $upload_path = trim( get_option( 'upload_path' ) );
if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
$dir = WP_CONTENT_DIR . '/uploads';
Expand All @@ -615,12 +618,11 @@ static public function wp_upload_dir() {

$dir = rtrim( $dir, DIRECTORY_SEPARATOR );

$wp_upload_dir[ $blog_id ] = call_user_func_array( 'wp_upload_dir', func_get_args() );
$wp_upload_dir[ $blog_id ] = call_user_func_array( 'wp_upload_dir', array( $time ) );
$wp_upload_dir[ $blog_id ]['reldir'] = substr( $wp_upload_dir[ $blog_id ]['basedir'], strlen( $dir ) );
$wp_upload_dir[ $blog_id ]['uploads'] = $dir;
}

return $wp_upload_dir[ $blog_id ];
}

}
19 changes: 14 additions & 5 deletions windows-azure-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,18 @@ function windows_azure_storage_wp_get_attachment_metadata( $data, $post_id ) {
*/
function windows_azure_storage_wp_generate_attachment_metadata( $data, $post_id ) {
$default_azure_storage_account_container_name = \Windows_Azure_Helper::get_default_container();


$post = get_post( wp_get_post_parent_id( $post_id ) );
$time = null;
if ( $post ) {
// The post date doesn't usually matter for pages, so don't backdate this upload.
if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
$time = $post->post_date;
}
}

// Get upload directory.
$upload_dir = \Windows_Azure_Helper::wp_upload_dir();
$upload_dir = \Windows_Azure_Helper::wp_upload_dir( $time );

$upload_file_name = get_post_meta( $post_id, '_wp_attached_file', true );

Expand Down Expand Up @@ -522,7 +531,7 @@ function windows_azure_storage_wp_generate_attachment_metadata( $data, $post_id
*/
function windows_azure_storage_delete_local_files( $data, $attachment_id ) {
$upload_file_name = get_attached_file( $attachment_id, true );

// Use core function introduced in 4.9.7 for deleting local files if available
if ( function_exists( 'wp_delete_attachment_files') ) {
$deleted = wp_delete_attachment_files( $attachment_id, $data, array(), $upload_file_name );
Expand All @@ -535,14 +544,14 @@ function windows_azure_storage_delete_local_files( $data, $attachment_id ) {
// Get upload directory.
$upload_dir = Windows_Azure_Helper::wp_upload_dir();
$subdir = ltrim( $upload_dir['reldir'] . $upload_dir['subdir'], DIRECTORY_SEPARATOR );

$relative_file_name = DIRECTORY_SEPARATOR === $subdir
? basename( $upload_file_name )
: str_replace( $upload_dir['uploads'] . DIRECTORY_SEPARATOR, '', $upload_file_name );

// Delete local file
Windows_Azure_Helper::unlink_file( $relative_file_name );

$file_upload_dir = strpos( $relative_file_name, '/' ) !== false
? substr( $relative_file_name, 0, strrpos( $relative_file_name, '/' ) )
: '';
Expand Down

0 comments on commit 249afec

Please sign in to comment.