function generate_youtube_thumbnail() { global $post; // Sprawdź, czy jesteśmy w edytorze postów if ( ! is_admin() || ! isset( $post->ID ) ) { return; } // Pobierz treść postu i znajdź link do YouTube $content = $post->post_content; preg_match_all('~(https?://www.youtube.com/watch\?v=)([a-zA-Z0-9-]+)~', $content, $matches); if ( empty($matches[2]) ) { return; } $videoId = $matches[2][0]; // Sprawdź, czy miniatura już istnieje $existingAttachment = get_posts(array( 'post_type' => 'attachment', 'post_parent' => $post->ID, 'meta_query' => array( array( 'key' => '_wp_attachment_image_alt', 'value' => 'Miniatura YouTube', 'compare' => 'LIKE' ) ) )); if (!empty($existingAttachment)) { // Miniatura już istnieje, użyj jej identyfikatora $attachmentId = $existingAttachment[0]->ID; set_post_thumbnail($post->ID, $attachmentId); return; } // Pobierz najlepszą dostępną miniaturę $youtubeImageSrc = "https://img.youtube.com/vi/$videoId/"; $possibleThumbnails = ['maxresdefault.jpg', 'hqdefault.jpg', 'mqdefault.jpg']; $youtubeImage = ''; foreach ($possibleThumbnails as $thumbnail) { $headers = get_headers($youtubeImageSrc . $thumbnail, 1); if ($headers && strpos($headers[0], '200 OK') !== false) { $youtubeImage = $thumbnail; break; } } if (!$youtubeImage) { error_log('No valid YouTube thumbnail found.'); return; } $thumbnailUrl = $youtubeImageSrc . $youtubeImage; // Pobierz obraz i informacje o wymiarach $response = wp_safe_remote_get( $thumbnailUrl ); if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { error_log( 'Błąd podczas pobierania miniatury: ' . $response->get_error_message() ); return; } $imageData = wp_remote_retrieve_body( $response ); // Sprawdź, czy pobrano dane if ( empty( $imageData ) ) { error_log( 'Nie udało się pobrać danych obrazu' ); return; } // Oblicz wymiary obrazu list($width, $height) = getimagesizefromstring($imageData); // Zapisz obraz w formacie WebP $uploadDir = wp_upload_dir(); $filename = wp_unique_filename( $uploadDir['path'], 'youtube-thumbnail-' . $post->ID . '.webp' ); $filePath = $uploadDir['path'] . '/' . $filename; // Utwórz obiekt GD $image = imagecreatefromstring($imageData); if (!$image) { error_log('Błąd podczas tworzenia obrazu GD'); return; } // Zapisz obraz w formacie WebP if (!imagewebp($image, $filePath)) { error_log('Błąd podczas zapisywania obrazu w formacie WebP'); imagedestroy($image); return; } imagedestroy($image); // Utwórz załącznik $attachment = array( 'post_mime_type' => 'image/webp', 'guid' => $uploadDir['url'] . '/' . $filename, 'post_parent' => $post->ID, 'post_title' => 'Miniatura YouTube', 'post_content' => '', 'post_status' => 'inherit' ); $attachmentId = wp_insert_attachment( $attachment, $filePath ); // Ustaw miniaturę jako obraz wyróżniony i dodaj metadane o wymiarach if ( ! is_wp_error( $attachmentId ) ) { set_post_thumbnail( $post->ID, $attachmentId ); update_post_meta( $attachmentId, '_wp_attachment_image_alt',  get_the_title() ); } else { error_log('Błąd podczas tworzenia załącznika: ' . $attachmentId->get_error_message() ); } } add_action( 'save_post', 'generate_youtube_thumbnail' );