mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-16 19:46:21 +00:00
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. Built from https://develop.svn.wordpress.org/trunk@31799 git-svn-id: http://core.svn.wordpress.org/trunk@31781 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
bf34e504dc
commit
9e1d17ad36
@ -58,60 +58,40 @@ class WP_Press_This {
|
||||
* @access public
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param string $content Optional. Current expected markup for Press This. Default empty.
|
||||
* @param string $content Optional. Current expected markup for Press This. Expects slashed. Default empty.
|
||||
* @return string New markup with old image URLs replaced with the local attachment ones if swapped.
|
||||
*/
|
||||
public function side_load_images( $post_id, $content = '' ) {
|
||||
$new_content = $content;
|
||||
$content = wp_unslash( $content );
|
||||
|
||||
preg_match_all( '/<img [^>]+>/', $content, $matches );
|
||||
|
||||
if ( ! empty( $matches ) && current_user_can( 'upload_files' ) ) {
|
||||
foreach ( (array) $matches[0] as $key => $image ) {
|
||||
preg_match( '/src=["\']{1}([^"\']+)["\']{1}/', stripslashes( $image ), $url_matches );
|
||||
|
||||
if ( empty( $url_matches[1] ) ) {
|
||||
if ( preg_match_all( '/<img [^>]+>/', $content, $matches ) && current_user_can( 'upload_files' ) ) {
|
||||
foreach ( (array) $matches[0] as $image ) {
|
||||
// This is inserted from our JS so HTML attributes should always be in double quotes.
|
||||
if ( ! preg_match( '/src="([^"]+)"/', $image, $url_matches ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$image_url = $url_matches[1];
|
||||
$image_src = $url_matches[1];
|
||||
|
||||
// Don't try to sideload a file without a file extension, leads to WP upload error.
|
||||
if ( ! preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $image_url ) )
|
||||
continue;
|
||||
if ( ! preg_match( '/[^\?]+\.(?:jpe?g|jpe|gif|png)(?:\?|$)/i', $image_src ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// See if files exist in content - we don't want to upload non-used selected files.
|
||||
if ( false !== strpos( $new_content, htmlspecialchars( $image_url ) ) ) {
|
||||
// Sideload image, which gives us a new image src.
|
||||
$new_src = media_sideload_image( $image_src, $post_id, null, 'src' );
|
||||
|
||||
// Sideload image, which ives us a new image tag, strip the empty alt that comes with it.
|
||||
$upload = str_replace( ' alt=""', '', media_sideload_image( $image_url, $post_id ) );
|
||||
|
||||
// Preserve assigned class, id, width, height and alt attributes.
|
||||
if ( preg_match_all( '/(class|width|height|id|alt)=\\\?(\"|\')[^"\']+\\\?(\2)/', $image, $attr_matches )
|
||||
&& is_array( $attr_matches[0] )
|
||||
) {
|
||||
foreach ( $attr_matches[0] as $attr ) {
|
||||
$upload = str_replace( '<img', '<img ' . $attr, $upload );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Replace the POSTED content <img> with correct uploaded ones.
|
||||
* Regex contains fix for Magic Quotes.
|
||||
*/
|
||||
if ( ! is_wp_error( $upload ) ) {
|
||||
$new_content = str_replace( $image, $upload, $new_content );
|
||||
}
|
||||
if ( ! is_wp_error( $new_src ) ) {
|
||||
// Replace the POSTED content <img> with correct uploaded ones.
|
||||
// Need to do it in two steps so we don't replace links to the original image if any.
|
||||
$new_image = str_replace( $image_src, $new_src, $image );
|
||||
$content = str_replace( $image, $new_image, $content );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error handling for media_sideload, send original content back.
|
||||
if ( is_wp_error( $new_content ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return $new_content;
|
||||
// Edxpected slashed
|
||||
return wp_slash( $content );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -150,11 +130,7 @@ class WP_Press_This {
|
||||
}
|
||||
}
|
||||
|
||||
$new_content = $this->side_load_images( $post_id, $post['post_content'] );
|
||||
|
||||
if ( ! is_wp_error( $new_content ) ) {
|
||||
$post['post_content'] = $new_content;
|
||||
}
|
||||
$post['post_content'] = $this->side_load_images( $post_id, $post['post_content'] );
|
||||
|
||||
$updated = wp_update_post( $post, true );
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.2-beta1-31798';
|
||||
$wp_version = '4.2-beta1-31799';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
Loading…
x
Reference in New Issue
Block a user