public function get_videos($limit = 6) { $cache_key = 'youtube_videos_' . $limit; $cached = Cache::get($cache_key); if ($cached) { return $cached; } if (empty($this->channel_id) || empty($this->api_key)) { return $this->get_mock_data(); } // Get channel info $channel_url = $this->api_url . 'channels'; $channel_params = [ 'id' => $this->channel_id, 'key' => $this->api_key, 'part' => 'contentDetails' ]; $channel_response = wp_remote_get($channel_url . '?' . http_build_query($channel_params)); if (is_wp_error($channel_response)) { return $this->get_mock_data(); } $channel_data = json_decode(wp_remote_retrieve_body($channel_response), true); $playlist_id = $channel_data['items'][0]['contentDetails']['relatedPlaylists']['uploads'] ?? ''; if (empty($playlist_id)) { return $this->get_mock_data(); } // Get videos $video_url = $this->api_url . 'playlistItems'; $video_params = [ 'playlistId' => $playlist_id, 'key' => $this->api_key, 'part' => 'snippet,contentDetails', 'maxResults' => $limit ]; $video_response = wp_remote_get($video_url . '?' . http_build_query($video_params)); if (is_wp_error($video_response)) { return $this->get_mock_data(); } $video_data = json_decode(wp_remote_retrieve_body($video_response), true); if (!isset($video_data['items']) || empty($video_data['items'])) { return $this->get_mock_data(); } // Get video statistics $video_ids = array_column(array_column($video_data['items'], 'contentDetails'), 'videoId'); $stats_url = $this->api_url . 'videos'; $stats_params = [ 'id' => implode(',', $video_ids), 'key' => $this->api_key, 'part' => 'statistics' ]; $stats_response = wp_remote_get($stats_url . '?' . http_build_query($stats_params)); $stats_data = json_decode(wp_remote_retrieve_body($stats_response), true); $stats_map = []; foreach ($stats_data['items'] ?? [] as $item) { $stats_map[$item['id']] = $item['statistics']; } $videos = []; foreach ($video_data['items'] as $item) { $video_id = $item['contentDetails']['videoId']; $stat = $stats_map[$video_id] ?? ['viewCount' => 0, 'likeCount' => 0]; // 🔥 FIX: Get best thumbnail $thumbnails = $item['snippet']['thumbnails'] ?? []; $thumbnail_url = $thumbnails['maxres']['url'] ?? $thumbnails['high']['url'] ?? $thumbnails['medium']['url'] ?? $thumbnails['default']['url'] ?? ''; $videos[] = [ 'id' => $video_id, 'platform' => 'youtube', 'type' => 'video', // 🔥 SEMUA YOUTUBE ADALAH VIDEO 'title' => $item['snippet']['title'] ?? '', 'message' => $item['snippet']['description'] ?? '', 'image' => $thumbnail_url, // Thumbnail 'video_url' => 'https://www.youtube.com/embed/' . $video_id, // 🔥 EMBED URL 'url' => 'https://www.youtube.com/watch?v=' . $video_id, 'created_time' => $item['snippet']['publishedAt'] ?? date('Y-m-d H:i:s'), 'views' => $stat['viewCount'] ?? 0, 'likes' => $stat['likeCount'] ?? 0, 'comments' => $stat['commentCount'] ?? 0, 'platform_icon' => 'fab fa-youtube', 'platform_color' => '#FF0000', 'is_video' => true, // 🔥 FLAG VIDEO 'video_id' => $video_id ]; } Cache::set($cache_key, $videos, HOUR_IN_SECONDS); return $videos; } private function get_mock_data() { return [ [ 'id' => 'yt_mock_1', 'platform' => 'youtube', 'type' => 'video', 'title' => 'ZELLO Social Feed - Demo Video', 'message' => 'Tutorial cara menggunakan ZELLO Social Feed plugin', 'image' => 'https://via.placeholder.com/480x360/FF0000/FFFFFF?text=YouTube+Video', 'video_url' => 'https://www.youtube.com/embed/dQw4w9WgXcQ', 'url' => '#', 'created_time' => date('Y-m-d H:i:s'), 'views' => 1250, 'likes' => 87, 'comments' => 23, 'platform_icon' => 'fab fa-youtube', 'platform_color' => '#FF0000', 'is_video' => true, 'video_id' => 'dQw4w9WgXcQ' ] ]; }