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.


git-svn-id: https://develop.svn.wordpress.org/trunk@29029 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2014-07-08 17:47:53 +00:00
parent f0bde0c945
commit c6428ff1ba
5 changed files with 94 additions and 2 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ $core_actions_post = array(
'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed'
'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail'
);
// Register core Ajax calls.
+49
View File
@@ -1946,6 +1946,55 @@ function wp_ajax_set_post_thumbnail() {
wp_die( 0 );
}
/**
* Ajax handler for setting the featured image for an attachment.
*
* @since 4.0.0
*/
function wp_ajax_set_attachment_thumbnail() {
if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) {
wp_send_json_error();
}
$thumbnail_id = (int) $_POST['thumbnail_id'];
if ( empty( $thumbnail_id ) ) {
wp_send_json_error();
}
$post_ids = array();
// For each URL, try to find its corresponding post ID.
foreach ( $_POST['urls'] as $url ) {
$post_id = attachment_url_to_postid( $url );
if ( ! empty( $post_id ) ) {
$post_ids[] = $post_id;
}
}
if ( empty( $post_ids ) ) {
wp_send_json_error();
}
$success = 0;
// For each found attachment, set its thumbnail.
foreach ( $post_ids as $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
continue;
}
if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) {
$success++;
}
}
if ( 0 === $success ) {
wp_send_json_error();
} else {
wp_send_json_success();
}
wp_send_json_error();
}
/**
* Ajax handler for date formatting.
*
+5
View File
@@ -2642,6 +2642,11 @@ function edit_form_image_editor( $post ) {
$attr['height'] = $h;
}
$thumb_id = get_post_thumbnail_id( $attachment_id );
if ( ! empty( $thumb_id ) ) {
$attr['poster'] = wp_get_attachment_url( $thumb_id );
}
echo wp_video_shortcode( $attr );
endif; ?>