Make WordPress Core

Changeset 22768


Ignore:
Timestamp:
11/21/2012 04:02:20 PM (13 years ago)
Author:
koopersmith
Message:

Media: Backwards compatibility for the many send_to_editor filters. props nacin. see #22186, #21390.

Location:
trunk
Files:
4 edited

Legend:

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

    r22541 r22768  
    5555    'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
    5656    'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    57     'query-attachments', 'save-attachment', 'save-attachment-compat',
     57    'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
     58    'send-attachment-to-editor',
    5859);
    5960
  • trunk/wp-admin/includes/ajax-actions.php

    r22723 r22768  
    19081908    wp_send_json_success( $attachment );
    19091909}
     1910
     1911/**
     1912 * Generates the HTML to send an attachment to the editor.
     1913 * Backwards compatible with the media_send_to_editor filter and the chain
     1914 * of filters that follow.
     1915 *
     1916 * @since 3.5.0
     1917 */
     1918function wp_ajax_send_attachment_to_editor() {
     1919    check_ajax_referer( 'media-send-to-editor', 'nonce' );
     1920
     1921    $attachment = stripslashes_deep( $_POST['attachment'] );
     1922
     1923    $id = intval( $attachment['id'] );
     1924
     1925    if ( ! $post = get_post( $id ) )
     1926        wp_send_json_error();
     1927
     1928    if ( ! current_user_can( 'edit_post', $id ) )
     1929        wp_send_json_error();
     1930
     1931    if ( 'attachment' != $post->post_type )
     1932        wp_send_json_error();
     1933
     1934    $html = isset( $attachment['title'] ) ? $attachment['title'] : '';
     1935    if ( ! empty( $attachment['url'] ) ) {
     1936        $rel = '';
     1937        if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link( $id ) == $attachment['url'] )
     1938            $rel = ' rel="attachment wp-att-' . $id . '"';
     1939        $html = '<a href="' . esc_url( $attachment['url'] ) . '"' . $rel . '>' . $html . '</a>';
     1940    }
     1941
     1942    remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
     1943
     1944    if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
     1945        $url = $attachment['url'];
     1946        $align = isset( $attachment['image-align'] ) ? $attachment['image-align'] : 'none';
     1947        $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
     1948        $alt = isset( $attachment['image-alt'] ) ? $attachment['image-alt'] : '';
     1949        $caption = isset( $attachment['caption'] ) ? $attachment['caption'] : '';
     1950        $title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
     1951        $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt );
     1952    }
     1953
     1954    $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment );
     1955
     1956    wp_send_json_success( $html );
     1957}
     1958
     1959/**
     1960 * Generates the HTML to send a non-image embed link to the editor.
     1961 *
     1962 * Backwards compatible with the following filters:
     1963 * - file_send_to_editor_url
     1964 * - audio_send_to_editor_url
     1965 * - video_send_to_editor_url
     1966 *
     1967 * @since 3.5.0
     1968 */
     1969function wp_ajax_send_link_to_editor() {
     1970    check_ajax_referer( 'media-send-to-editor', 'nonce' );
     1971
     1972    if ( ! $src = stripslashes( $_POST['src'] ) )
     1973        wp_send_json_error();
     1974
     1975    if ( ! strpos( $src, '://' ) )
     1976        $src = 'http://' . $src;
     1977
     1978    if ( ! $src = esc_url_raw( $src ) )
     1979        wp_send_json_error();
     1980
     1981    if ( ! $title = trim( stripslashes( $_POST['title'] ) ) )
     1982        $title = wp_basename( $src );
     1983
     1984    $html = '';
     1985    if ( $title )
     1986        $html = '<a href="' . esc_url( $src ) . '">' . $title . '</a>';
     1987
     1988    // Figure out what filter to run:
     1989    $type = 'file';
     1990    if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
     1991        && ( 'audio' == $ext_type || 'video' == $ext_type ) )
     1992            $type = $ext_type;
     1993
     1994    $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $title );
     1995
     1996    wp_send_json_success( $html );
     1997}
  • trunk/wp-admin/js/media-upload.js

    r22761 r22768  
    110110
    111111    wp.media.string = {
     112        // Joins the `props` and `attachment` objects,
     113        // outputting the proper object format based on the
     114        // attachment's type.
     115        props: function( props, attachment ) {
     116            var link, linkUrl, size, sizes;
     117
     118            props = props ? _.clone( props ) : {};
     119
     120            if ( attachment && attachment.type )
     121                props.type = attachment.type;
     122
     123            if ( 'image' === props.type ) {
     124                props = _.defaults( props || {}, {
     125                    align:   getUserSetting( 'align', 'none' ),
     126                    size:    getUserSetting( 'imgsize', 'medium' ),
     127                    url:     '',
     128                    classes: []
     129                });
     130            }
     131
     132            // All attachment-specific settings follow.
     133            if ( ! attachment )
     134                return props;
     135
     136            link = props.link || getUserSetting( 'urlbutton', 'post' );
     137            if ( 'file' === link )
     138                linkUrl = attachment.url;
     139            else if ( 'post' === link )
     140                linkUrl = attachment.link;
     141            else if ( 'custom' === link )
     142                linkUrl = props.linkUrl;
     143            props.linkUrl = linkUrl || '';
     144
     145            // Format properties for images.
     146            if ( 'image' === attachment.type ) {
     147                props.classes.push( 'wp-image-' + attachment.id );
     148
     149                sizes = attachment.sizes;
     150                size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
     151
     152                _.extend( props, _.pick( attachment, 'align', 'caption' ), {
     153                    width:     size.width,
     154                    height:    size.height,
     155                    src:       size.url,
     156                    captionId: 'attachment_' + attachment.id
     157                });
     158
     159            // Format properties for non-images.
     160            } else {
     161                _.extend( props, {
     162                    title:   attachment.title || attachment.filename,
     163                    rel:     'attachment wp-att-' + attachment.id
     164                });
     165            }
     166
     167            return props;
     168        },
     169
    112170        link: function( props, attachment ) {
    113171            var options;
    114172
    115             props = _.defaults( props || {}, {
    116                 title:   '',
    117                 linkUrl: ''
    118             });
    119 
    120             if ( attachment ) {
    121                 _.extend( props, {
    122                     title:   attachment.title || attachment.filename,
    123                     linkUrl: linkToUrl( props, attachment ),
    124                     rel:     'attachment wp-att-' + attachment.id
    125                 });
    126             }
     173            props = wp.media.string.props( props, attachment );
    127174
    128175            options = {
     
    141188
    142189        image: function( props, attachment ) {
    143             var classes = [],
    144                 img = {},
    145                 options, sizes, size, shortcode, html;
    146 
    147             props = _.defaults( props || {}, {
    148                 align: getUserSetting( 'align', 'none' ),
    149                 size:  getUserSetting( 'imgsize', 'medium' ),
    150                 url:   ''
    151             });
    152 
    153             if ( attachment ) {
    154                 classes.push( 'wp-image-' + attachment.id );
    155 
    156                 sizes = attachment.sizes;
    157                 size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
    158 
    159                 _.extend( props, _.pick( attachment, 'align', 'caption' ), {
    160                     width:     size.width,
    161                     height:    size.height,
    162                     src:       size.url,
    163                     linkUrl:   linkToUrl( props, attachment ),
    164                     captionId: 'attachment_' + attachment.id
    165                 });
    166             }
     190            var img = {},
     191                options, classes, shortcode, html;
     192
     193            props = wp.media.string.props( props, attachment );
     194            classes = props.classes || [];
    167195
    168196            img.src = props.url;
     
    396424                    attachment = attachment.toJSON();
    397425
    398                     // If captions are disabled, clear the caption.
    399                     if ( ! wp.media.view.settings.captions )
    400                         delete attachment.caption;
    401 
    402                     if ( 'image' === attachment.type )
    403                         this.insert( wp.media.string.image( detail, attachment ) + ' ' );
    404                     else
    405                         this.insert( wp.media.string.link( detail, attachment ) + ' ' );
     426                    this.send.attachment( detail, attachment );
    406427                }, this );
    407428            }, this );
     
    422443                    });
    423444
    424                     this.insert( wp.media.string.link( embed ) );
     445                    this.send.link( embed );
    425446
    426447                } else if ( 'image' === embed.type ) {
     
    452473        },
    453474
     475        send: {
     476            attachment: function( props, attachment ) {
     477                var caption = attachment.caption,
     478                    options, html;
     479
     480                // If captions are disabled, clear the caption.
     481                if ( ! wp.media.view.settings.captions )
     482                    delete attachment.caption;
     483
     484                props = wp.media.string.props( props, attachment );
     485
     486                options = {
     487                    id: attachment.id
     488                };
     489
     490                if ( 'image' === attachment.type ) {
     491                    html = wp.media.string.image( props );
     492                    options['caption'] = caption;
     493
     494                    _.each({
     495                        align:   'image-align',
     496                        size:    'image-size',
     497                        alt:     'image-alt',
     498                        linkUrl: 'url'
     499                    }, function( option, prop ) {
     500                        if ( props[ prop ] )
     501                            options[ option ] = props[ prop ];
     502                    });
     503
     504                } else {
     505                    html = wp.media.string.link( props );
     506                    options.title = props.title;
     507                }
     508
     509                return media.post( 'send-attachment-to-editor', {
     510                    nonce:      wp.media.view.settings.nonce.sendToEditor,
     511                    attachment: options,
     512                    html:       html
     513                }).done( function( resp ) {
     514                    wp.media.editor.insert( resp );
     515                });
     516            },
     517
     518            link: function( embed ) {
     519                return media.post( 'send-link-to-editor', {
     520                    nonce: wp.media.view.settings.nonce.sendToEditor,
     521                    src:   embed.linkUrl,
     522                    title: embed.title,
     523                    html:  wp.media.string.link( embed )
     524                }).done( function( resp ) {
     525                    wp.media.editor.insert( resp );
     526                });
     527            }
     528        },
     529
    454530        init: function() {
    455531            $(document.body).on('click', '.insert-media', function( event ) {
  • trunk/wp-includes/media.php

    r22761 r22768  
    13291329        'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ),
    13301330        'captions'  => ! apply_filters( 'disable_captions', '' ),
     1331        'nonce'     => array(
     1332            'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ),
     1333        ),
    13311334    );
    13321335
Note: See TracChangeset for help on using the changeset viewer.