-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfragment-thumbnail.php
60 lines (48 loc) · 1.5 KB
/
fragment-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
<?php
// After save
function zw_bunny_save_thumbnail($post_ID)
{
if (!is_int($post_ID)) {
return;
}
if (get_post_type($post_ID) !== 'fragment') {
return;
}
$url = get_field('fragment_url', $post_ID, false);
$video = zw_bunny_get_video_from_url($url);
if (!$video) {
return;
}
if (!$video->isAvailable()) {
return;
}
update_field('fragment_duur', $video->getDuration(), $post_ID);
$poster = $video->getThumbnail();
$thumbnail_id = get_post_thumbnail_id($post_ID);
if ($thumbnail_id != 0) {
// This fragment already has a thumbnail
return;
}
$tempPath = download_url($poster);
if ($tempPath instanceof WP_Error) {
error_log('Error downloading file: ' . $tempPath->get_error_message());
return;
}
$file = [
'name' => basename($poster),
'tmp_name' => $tempPath,
];
$post_array = [];
$parent = get_post($post_ID);
$post_array['post_date'] = $parent->post_date;
$post_array['post_date_gmt'] = $parent->post_date_gmt;
$post_array['meta_input'] = [];
$post_array['meta_input']['bunny_poster_url'] = $poster;
$thumbnail_id = media_handle_sideload($file, $post_ID, null, $post_array);
if ($thumbnail_id instanceof WP_Error) {
error_log('Error uploading file: ' . $thumbnail_id->get_error_message());
return;
}
set_post_thumbnail($post_ID, $thumbnail_id);
}
add_action('acf/save_post', 'zw_bunny_save_thumbnail');