WordPress.org

Make WordPress Core

Changeset 29029


Ignore:
Timestamp:
07/08/2014 05:47:53 PM (4 years ago)
Author:
wonderboymusic
Message:

When setting the poster image for a video shortcode, set that image as the featured image for that attachment (if found) in the background. This AJAX functionality could be used for audio as well.

Introduces attachment_url_to_postid() to attempt to turn URLs into post IDs.

Fixes #27891.

Location:
trunk/src
Files:
5 edited

Legend:

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

    r28580 r29029  
    6161    'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
    6262    'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
    63     'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed'
     63    'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail'
    6464);
    6565
  • trunk/src/wp-admin/includes/ajax-actions.php

    r28974 r29029  
    19481948
    19491949/**
     1950 * Ajax handler for setting the featured image for an attachment.
     1951 *
     1952 * @since 4.0.0
     1953 */
     1954function wp_ajax_set_attachment_thumbnail() {
     1955    if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) {
     1956        wp_send_json_error();
     1957    }
     1958
     1959    $thumbnail_id = (int) $_POST['thumbnail_id'];
     1960    if ( empty( $thumbnail_id ) ) {
     1961        wp_send_json_error();
     1962    }
     1963
     1964    $post_ids = array();
     1965    // For each URL, try to find its corresponding post ID.
     1966    foreach ( $_POST['urls'] as $url ) {
     1967        $post_id = attachment_url_to_postid( $url );
     1968        if ( ! empty( $post_id ) ) {
     1969            $post_ids[] = $post_id;
     1970        }
     1971    }
     1972
     1973    if ( empty( $post_ids ) ) {
     1974        wp_send_json_error();
     1975    }
     1976
     1977    $success = 0;
     1978    // For each found attachment, set its thumbnail.
     1979    foreach ( $post_ids as $post_id ) {
     1980        if ( ! current_user_can( 'edit_post', $post_id ) ) {
     1981            continue;
     1982        }
     1983
     1984        if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) {
     1985            $success++;
     1986        }
     1987    }
     1988
     1989    if ( 0 === $success ) {
     1990        wp_send_json_error();
     1991    } else {
     1992        wp_send_json_success();
     1993    }
     1994
     1995    wp_send_json_error();
     1996}
     1997
     1998/**
    19501999 * Ajax handler for date formatting.
    19512000 *
  • trunk/src/wp-admin/includes/media.php

    r28871 r29029  
    26432643        }
    26442644
     2645        $thumb_id = get_post_thumbnail_id( $attachment_id );
     2646        if ( ! empty( $thumb_id ) ) {
     2647            $attr['poster'] = wp_get_attachment_url( $thumb_id );
     2648        }
     2649
    26452650        echo wp_video_shortcode( $attr );
    26462651
  • trunk/src/wp-includes/js/media-audiovideo.js

    r28993 r29029  
    693693        renderSelectPosterImageToolbar: function() {
    694694            this.setPrimaryButton( l10n.videoSelectPosterImageTitle, function( controller, state ) {
    695                 var attachment = state.get( 'selection' ).single();
     695                var urls = [], attachment = state.get( 'selection' ).single();
    696696
    697697                controller.media.set( 'poster', attachment.get( 'url' ) );
    698698                state.trigger( 'set-poster-image', controller.media.toJSON() );
     699
     700                _.each( wp.media.view.settings.embedExts, function (ext) {
     701                    if ( controller.media.get( ext ) ) {
     702                        urls.push( controller.media.get( ext ) );
     703                    }
     704                } );
     705
     706                wp.ajax.send( 'set-attachment-thumbnail', {
     707                    data : {
     708                        urls: urls,
     709                        thumbnail_id: attachment.get( 'id' )
     710                    }
     711                } );
    699712            } );
    700713        },
  • trunk/src/wp-includes/media.php

    r28976 r29029  
    32193219    }
    32203220}
     3221
     3222/**
     3223 * Try to convert an attachment URL into a post ID.
     3224 *
     3225 * @since 4.0.0
     3226 *
     3227 * @global wpdb $wpdb WordPress database access abstraction object.
     3228 * @param string $url The URL to resolve.
     3229 * @return int The found post_id.
     3230 */
     3231function attachment_url_to_postid( $url ) {
     3232    global $wpdb;
     3233
     3234    $dir = wp_upload_dir();
     3235    $path = ltrim( $url, $dir['baseurl'] . '/' );
     3236
     3237    $sql = $wpdb->prepare(
     3238        "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
     3239        $path
     3240    );
     3241    $post_id = $wpdb->get_var( $sql );
     3242    if ( ! empty( $post_id ) ) {
     3243        return (int) $post_id;
     3244    }
     3245}
Note: See TracChangeset for help on using the changeset viewer.