-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-thumbnail.php
66 lines (54 loc) · 1.79 KB
/
video-thumbnail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
add_action( 'save_post', 'extract_thumbnail_from_embed_block', 10, 3 );
function extract_thumbnail_from_embed_block( $post_id, $post, $update ) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if (wp_is_post_revision($post)) {
return;
}
if (wp_is_post_autosave($post)) {
return;
}
if ( has_post_thumbnail( $post ) ) {
return;
}
$content = $post->post_content; // Obtenir le contenu du post
// Vérifier si le bloc Embed est présent
if ( ! has_block( 'core/embed', $content ) ) {
return;
}
// Extraire l'URL du bloc Embed
$embed_block = parse_blocks( $content );
$embed_url = '';
foreach ( $embed_block as $block ) {
if ( $block['blockName'] === 'core/embed' && $block['attrs']['type'] === 'video' && isset( $block['attrs']['url'] )) {
$embed_url = $block['attrs']['url'];
break;
}
}
if ( ! $embed_url ) {
return;
}
// Récupérer la miniature de la vidéo
$thumbnail_url = get_video_thumbnail( $embed_url );
// Mettre à jour la miniature du post
if ( $thumbnail_url ) {
$attachment_id = media_sideload_image( $thumbnail_url . "#.jpg", $post_id, '', 'id' );
if ( ! is_wp_error( $attachment_id ) ) {
set_post_thumbnail( $post_id, $attachment_id );
}
}
}
function get_video_thumbnail( $url ) {
$oembed = _wp_oembed_get_object();
$data = $oembed->get_data( $url );
if ( ! $data && ! isset( $data->thumbnail_url )) {
return '';
}
$thumbnail_url = $data->thumbnail_url;
return $thumbnail_url;
}