Created
November 27, 2019 13:37
-
-
Save matgargano/89de9cfdcc6fce69f0e8afa00ce7157d to your computer and use it in GitHub Desktop.
Sideload Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace VHC; | |
class Sideload { | |
public static function get( $image_url, $post_id = null ) { | |
$tmp = \download_url( $image_url ); | |
if ( ! $post_id ) { | |
$post_id = md5( time() ); | |
} | |
// Set variables for storage | |
preg_match( '/[^\?]+\.(jpe?g|jpe|gif|pdf|png)\b/i', $image_url, $matches ); | |
$file_array['name'] = basename( $matches[0] ); | |
$file_array['tmp_name'] = $tmp; | |
// If error storing temporarily, unlink | |
if ( is_wp_error( $tmp ) ) { | |
@unlink( $file_array['tmp_name'] ); | |
$file_array['tmp_name'] = ''; | |
} | |
$file_parts = pathinfo( $file_array['tmp_name'] ); | |
$new_file_array['tmp_name'] = sprintf( '%s/%s.%s.%s', $file_parts['dirname'], basename( get_permalink( $post_id ) ), $post_id, $file_parts['extension'] ); | |
$file_parts = pathinfo( $file_array['name'] ); | |
$new_file_array['name'] = sprintf( '%s.%s', $post_id, $file_parts['extension'] ); | |
rename( $file_array['tmp_name'], $new_file_array['tmp_name'] ); | |
$file_array = $new_file_array; | |
// do the validation and storage stuff | |
$file_array['name'] = basename( $matches[0] ); | |
$id = media_handle_sideload( $file_array, $post_id = null ); | |
// If error storing permanently, unlink | |
if ( is_wp_error( $id ) ) { | |
@unlink( $file_array['tmp_name'] ); | |
return $id; | |
} | |
return $id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment