WordPress 短代码应返回按日期排序的 Instagram、YouTube 和 Twitch 帖子

问题描述 投票:0回答:1

我正在尝试制作一个短代码,将我的 Twitch、YouTube 和 Instagram 帖子全部集中到一个地方,并按发布日期对它们进行排序,但由于某种原因它不起作用。我将注释掉凭据,但我可以确保问题出在其他地方,因为我已经单独测试了它们并让它们单独工作。

<?php
/*
Plugin Name:  Social media crawler
Description:  
Version:      1.0
Author:       
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Domain Path:  /languages
*/



function fetch_youtube_videos($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts(array(
        'channel_id' => '***', // Default channel ID (if any)
        'max_results' => 5, // Default number of videos to show
        'date' => '2010-01-01', // Default starting date in yyyy-mm-dd format
    ), $atts);

    $api_key = '***';
    $publishedAfter = $atts['date'] . 'T00:00:00Z';
    $api_url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' . $atts['channel_id'] .
        '&maxResults=' . $atts['max_results'] . '&publishedAfter=' . $publishedAfter .
        '&order=date&type=video&key=' . $api_key;

    // Make the API request
    $response = wp_remote_get($api_url);

    // Check for error in the response
    if (is_wp_error($response)) {
        return []; // Return an empty array in case of error
    }

    // Decode the response
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body);

    // Check if data items exist
    if (!isset($data->items)) {
        return []; // Return an empty array if no videos are found
    }

    // Initialize the posts array
    $posts = [];

    // Loop through each video and append to the posts array
    foreach ($data->items as $item) {
        $title = $item->snippet->title;
        $video_id = $item->id->videoId;
        $video_url = 'https://www.youtube.com/watch?v=' . $video_id;
        $embed_url = 'https://www.youtube.com/embed/' . $video_id; // URL for embedding
        $publishedAt = $item->snippet->publishedAt; // Video publication date

        // Construct the HTML content for the video
        $html = '<div class="youtube-video">' .
            '<iframe width="560" height="315" src="' . esc_url($embed_url) . '" title="' . esc_attr($title) . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>' .
            '<p><a href="' . esc_url($video_url) . '" target="_blank">' . esc_html($title) . '</a></p>' .
            '</div>';

        // Add to the posts array
        $posts[] = [
            'date' => $publishedAt,
            'html' => $html
        ];
    }

    // Return the array of posts
    return $posts;
}




function fetch_instagram_posts($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts([
        'limit' => 10, // Default number of Instagram posts to show
    ], $atts);

    $fields = "id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username";
    $token = "*";
    $limit = $atts['limit'];

    $json_feed_url = "https://graph.instagram.com/me/media?fields={$fields}&access_token={$token}&limit={$limit}";
    $json_feed = @file_get_contents($json_feed_url);
    if ($json_feed === FALSE) {
        return []; // Return an empty array in case of error
    }

    $contents = json_decode($json_feed, true);

    $posts = [];
    foreach ($contents["data"] as $post) {
        $username = isset($post["username"]) ? $post["username"] : "";
        $caption = isset($post["caption"]) ? $post["caption"] : "";
        $media_url = isset($post["media_url"]) ? $post["media_url"] : "";
        $permalink = isset($post["permalink"]) ? $post["permalink"] : "";
        $media_type = isset($post["media_type"]) ? $post["media_type"] : "";
        $timestamp = isset($post["timestamp"]) ? $post["timestamp"] : "";

        // Construct the HTML content for the post
        $html = "<div class='ig_post_container'><div>";
        if ($media_type == "VIDEO") {
            $html .= "<video controls style='width:100%; display: block !important;'><source src='{$media_url}' type='video/mp4'>Your browser does not support the video tag.</video>";
        } else {
            $html .= "<img src='{$media_url}' />";
        }
        $html .= "</div><div class='ig_post_details'><div><strong>@{$username}</strong> {$caption}</div><div class='ig_view_link'><a href='{$permalink}' target='_blank'>View on Instagram</a></div></div></div>";

        // Add to the posts array
        $posts[] = [
            'date' => $timestamp,
            'html' => $html
        ];
    }

    // Return the array of posts
    return $posts;
}


function fetch_twitch_videos($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts([
        'date' => '1970-01-01', // Use a very old date as default to ensure all clips are shown unless specified
    ], $atts);

    $clientID = '*';
    $clientSecret = '*';
    $tokenUrl = 'https://id.twitch.tv/oauth2/token';

    // Parameters for the token request
    $params = [
        'client_id' => $clientID,
        'client_secret' => $clientSecret,
        'grant_type' => 'client_credentials',
    ];

    // Initialize cURL session for the token request
    $ch = curl_init($tokenUrl);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute the token request
    $response = curl_exec($ch);
    if (curl_errno($ch) || ($httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) !== 200) {
        curl_close($ch);
        return []; // Return an empty array in case of error
    }

    curl_close($ch);
    $tokenData = json_decode($response, true);
    if (!isset($tokenData['access_token'])) {
        return []; // Return an empty array if token not obtained
    }

    // Use the obtained access token to make the actual API request
    $accessToken = $tokenData['access_token'];
    $broadcasterUserId = '*'; // Example broadcaster's user ID
    $videoClipsUrl = "https://api.twitch.tv/helix/clips?broadcaster_id=$broadcasterUserId";

    $headers = [
        'Authorization: Bearer ' . $accessToken,
        'Client-Id: ' . $clientID,
    ];

    // Initialize cURL session for the API request
    $ch = curl_init($videoClipsUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute the API request
    $response = curl_exec($ch);
    if (curl_errno($ch) || ($httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) !== 200) {
        curl_close($ch);
        return []; // Return an empty array if the request failed
    }

    curl_close($ch);
    $videoClipsData = json_decode($response, true);

    if (!isset($videoClipsData['data']) || empty($videoClipsData['data'])) {
        return []; // Return an empty array if no videos are found
    }

    // Prepare posts array
    $posts = array_map(function ($clip) {
        $clipDate = $clip['created_at'];
        $html = '<iframe src="' . $clip['embed_url'] . '&parent=*" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>';
        return [
            'date' => $clipDate,
            'html' => $html,
        ];
    }, $videoClipsData['data']);

    // Filter posts based on the 'date' attribute
    $minDateTimestamp = strtotime($atts['date']);
    $filteredPosts = array_filter($posts, function ($post) use ($minDateTimestamp) {
        return strtotime($post['date']) >= $minDateTimestamp;
    });

    return array_values($filteredPosts); // Re-index the array and return
}



function social_wall_shortcode($atts) {
    ob_start();

    // Assuming fetching functions have been modified to return arrays
    $youtubePosts = fetch_youtube_videos($atts);
    $instagramPosts = my_instagram_feed_shortcode($atts);
    $twitchPosts = display_twitch_videos($atts);

    // Aggregate posts
    $allPosts = array_merge($youtubePosts, $instagramPosts, $twitchPosts);

    // Sort posts by date
    usort($allPosts, function($a, $b) {
        return strtotime($a['date']) <=> strtotime($b['date']);
    });

    // Generate HTML output
    echo '<div class="social-wall-container">';
    foreach ($allPosts as $post) {
        echo $post['html']; // Each post's HTML content
    }
    echo '</div>';

    $content = ob_get_clean();
    return $content;
}

// Register the modified shortcode with WordPress
add_shortcode('social_wall', 'social_wall_shortcode');

php wordpress youtube-api shortcode social-media
1个回答
0
投票

你能成功吗?我是个菜鸟,正在寻找一种将帖子和个人资料嵌入到不同博客帖子中的方法

© www.soinside.com 2019 - 2024. All rights reserved.