Make WordPress Core


Ignore:
Timestamp:
07/08/2014 05:47:53 PM (11 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 *
Note: See TracChangeset for help on using the changeset viewer.