Make WordPress Core

Changeset 37035


Ignore:
Timestamp:
03/18/2016 08:04:19 PM (10 years ago)
Author:
azaozz
Message:

Media: fix erroneously inserting a rel attribute in get_image_send_to_editor(). Reverts most of [34259] and [34260] and adds a unit test.

Props joemcgill, azaozz.
Fixes #36084.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r36472 r37035  
    25762576        }
    25772577
    2578         $rel = '';
    25792578        $url = empty( $attachment['url'] ) ? '' : $attachment['url'];
    2580         if ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url ) {
    2581                 $rel = 'attachment wp-att-' . $id;
    2582         }
     2579        $rel = ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url );
    25832580
    25842581        remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );
     
    26012598        } else {
    26022599                $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
     2600                $rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized
     2601
    26032602                if ( ! empty( $url ) ) {
    2604                         $html = '<a href="' . esc_url( $url ) . '"' . 'rel="' . esc_attr( $rel ) . '">' . $html . '</a>';
     2603                        $html = '<a href="' . esc_url( $url ) . '"' . $rel . '">' . $html . '</a>';
    26052604                }
    26062605        }
  • trunk/src/wp-admin/includes/media.php

    r36879 r37035  
    112112 * @param string       $align   Image CSS alignment property.
    113113 * @param string       $url     Optional. Image src URL. Default empty.
    114  * @param string       $rel     Optional. Image rel attribute. Default empty.
     114 * @param bool|string  $rel     Optional. Value for rel attribute or whether to add a dafault value. Default false.
    115115 * @param string|array $size    Optional. Image size. Accepts any valid image size, or an array of width
    116116 *                              and height values in pixels (in that order). Default 'medium'.
     
    118118 * @return string The HTML output to insert into the editor.
    119119 */
    120 function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = '', $size = 'medium', $alt = '' ) {
    121 
    122         $html = get_image_tag($id, $alt, '', $align, $size);
    123 
    124         if ( ! $rel ) {
    125                 $rel = ' rel="attachment wp-att-' . esc_attr( $id ) . '"';
     120function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
     121
     122        $html = get_image_tag( $id, $alt, '', $align, $size );
     123
     124        if ( $rel ) {
     125                if ( is_string( $rel ) ) {
     126                        $rel = ' rel="' . esc_attr( $rel ) . '"';
     127                } else {
     128                        $rel = ' rel="attachment wp-att-' . intval( $id ) . '"';
     129                }
    126130        } else {
    127                 $rel = ' rel="' . esc_attr( $rel ) . '"';
     131                $rel = '';
    128132        }
    129133
    130134        if ( $url )
    131                 $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
     135                $html = '<a href="' . esc_attr( $url ) . '"' . $rel . '>' . $html . '</a>';
    132136
    133137        /**
     
    11671171                $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
    11681172                $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
    1169                 $rel = ( $url == get_attachment_link($attachment_id) );
     1173                $rel = ( strpos( $url, 'attachment_id') || $url === get_attachment_link( $attachment_id ) );
    11701174
    11711175                return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  • trunk/tests/phpunit/tests/media.php

    r37034 r37035  
    15401540                $this->assertSame( $expected, $actual );
    15411541        }
     1542
     1543        /**
     1544         * @ticket 36084
     1545         */
     1546        function test_get_image_send_to_editor_defaults() {
     1547                $id      = self::$large_id;
     1548                $caption = '';
     1549                $title   = 'A test title value.';
     1550                $align   = 'left';
     1551
     1552                // Calculate attachment data (default is medium).
     1553                $attachment = wp_get_attachment_image_src( $id, 'medium' );
     1554
     1555                $html = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
     1556                $expected = sprintf( $html, $attachment[0], $attachment[1], $attachment[2], $align, $id );
     1557
     1558                $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
     1559
     1560                $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
     1561        }
     1562
     1563        /**
     1564         * @ticket 36084
     1565         */
     1566        function test_get_image_send_to_editor_defaults_with_optional_params() {
     1567                $id      = self::$large_id;
     1568                $caption = 'A test caption.';
     1569                $title   = 'A test title value.';
     1570                $align   = 'left';
     1571                $url     = get_permalink( $id );
     1572                $rel     = true;
     1573                $size    = 'thumbnail';
     1574                $alt     = 'An example alt value.';
     1575
     1576                // Calculate attachment data.
     1577                $attachment = wp_get_attachment_image_src( $id, $size );
     1578
     1579                $html = '<a href="%1$s" rel="%2$s"><img src="%3$s" alt="%4$s" width="%5$d" height="%6$d" class="size-%8$s wp-image-%9$d" /></a>';
     1580                $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
     1581
     1582                $expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption );
     1583
     1584                $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
     1585        }
     1586
     1587        /**
     1588         * @ticket 36084
     1589         */
     1590        function test_get_image_send_to_editor_defaults_no_caption_no_rel() {
     1591                $id      = self::$large_id;
     1592                $caption = '';
     1593                $title   = 'A test title value.';
     1594                $align   = 'left';
     1595                $url     = get_permalink( $id );
     1596                $rel     = '';
     1597                $size    = 'thumbnail';
     1598                $alt     = 'An example alt value.';
     1599
     1600                // Calculate attachment data.
     1601                $attachment = wp_get_attachment_image_src( $id, $size );
     1602
     1603                $html = '<a href="%1$s"><img src="%2$s" alt="%3$s" width="%4$d" height="%5$d" class="align%6$s size-%7$s wp-image-%8$d" /></a>';
     1604
     1605                $expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id );
     1606
     1607                $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
     1608        }
    15421609}
Note: See TracChangeset for help on using the changeset viewer.