Press This:

- Strip slashes while running side_load_images(), add slashes after.
- Simplify and clean up side_load_images().
- Add another arg to media_sideload_image() to return the uploaded image src only, and fix it to always return WP_Error on errors.
Fixes #31660.

git-svn-id: https://develop.svn.wordpress.org/trunk@31799 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz
2015-03-17 00:46:01 +00:00
parent b91de55ede
commit 820fe85082
2 changed files with 29 additions and 46 deletions
+9 -2
View File
@@ -829,9 +829,10 @@ function wp_media_upload_handler() {
* @param string $file The URL of the image to download
* @param int $post_id The post ID the media is to be associated with
* @param string $desc Optional. Description of the image
* @param string $return Optional. What to return: an image tag (default) or only the src.
* @return string|WP_Error Populated HTML img tag on success
*/
function media_sideload_image( $file, $post_id, $desc = null ) {
function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) {
if ( ! empty( $file ) ) {
// Set variables for storage, fix file filename for query strings.
preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
@@ -860,9 +861,15 @@ function media_sideload_image( $file, $post_id, $desc = null ) {
// Finally check to make sure the file has been saved, then return the HTML.
if ( ! empty( $src ) ) {
if ( $return === 'src' ) {
return $src;
}
$alt = isset( $desc ) ? esc_attr( $desc ) : '';
$html = "<img src='$src' alt='$alt' />";
return $html;
} else {
return new WP_Error( 'image_sideload_failed' );
}
}
@@ -3071,4 +3078,4 @@ function wp_media_attach_action( $parent_id, $action = 'attach' ) {
wp_redirect( $location );
exit;
}
}
}