WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#32072 closed enhancement (fixed)

Move link code for clarity in wp_ajax_send_attachment_to_editor() in ajax-actions.php

Reported by: tychay Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version: 3.5
Component: Media Keywords:
Focuses: Cc:

Description

Code is called from the media library insert.

Lines 2411-2418 do nothing most of the time since $html is often overwritten:

	$rel = $url = '';
	$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
	if ( ! empty( $attachment['url'] ) ) {
		$url = $attachment['url'];
		if ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url )
			$rel = ' rel="attachment wp-att-' . $id . '"';
		$html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
	}

The code is only needed when an attachment is not an image or video. It'd be better to move it to there for more clarity (e.g. lines 24411-2431 should be replaced like so)

	$url = ( empty ($attachment['url'] ) ) ? '' : $attachment['url'];

	remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );

	if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
		$align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
		$size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
		$alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
		$caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
		$title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
		$html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt );
	} elseif ( 'video' === substr( $post->post_mime_type, 0, 5 ) || 'audio' === substr( $post->post_mime_type, 0, 5 )  ) {
		$html = stripslashes_deep( $_POST['html'] );
	} else {
		$rel ='';
		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
		if ( $url ) {
			if ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url )
				$rel = ' rel="attachment wp-att-' . $id . '"';
			$html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
		}
	}

Attachments (1)

32072.diff (1.6 KB) - added by wonderboymusic 6 years ago.

Download all attachments as: .zip

Change History (5)

#1 @tychay
6 years ago

The above code has an error, $rel needs to be assigned below $url:

<?php
$rel = ('…') ? ' rel="attachment wp-att-' . $id . '"' : '';

Else it will trigger a warning in the 'image' code.

e.g.

<?php
        $url = ( empty ($attachment['url'] ) ) ? '' : $attachment['url'];
        $rel = ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url ) ?  ' rel="attachment wp-att-' . $id . '"' : '';

        remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );

        if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
                $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
                $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
                $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
                $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
                $title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
                $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt );
        } elseif ( 'video' === substr( $post->post_mime_type, 0, 5 ) || 'audio' === substr( $post->post_mime_type, 0, 5 )  ) {
                $html = stripslashes_deep( $_POST['html'] );
        } else {
                $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
                if ( $url ) {
                        $html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
                }
        }
Last edited 6 years ago by tychay (previous) (diff)

#2 @johnbillion
6 years ago

  • Version changed from 4.1.2 to 3.5

@wonderboymusic
6 years ago

#3 @wonderboymusic
6 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 34260:

Media: In wp_ajax_send_attachment_to_editor(), the fallback logic for $html should be tucked into an else statement so it isn't run needlessly and overwritten.

Props tychay.
Fixes #32072.

#4 @SergeyBiryukov
6 years ago

  • Milestone changed from Awaiting Review to 4.4
Note: See TracTickets for help on using tickets.