使用 bitbucket RSS做个展示项目提交动态的WP模板页面

接上次《使用 bitbucket REST API做个展示项目提交动态的WP模板页面》一文。
上次调用其API需要用户名和密码认证,这次调用rss实现起来比上次简单。不过WP默认的WP_SimplePie_File类不支持关闭sslverify选项,因此得自己写一个。

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * added ssl un-verify support
 */

require_once ABSPATH . WPINC . '/class-feed.php';

class ihacklog_SimplePie_File extends SimplePie_File {

    function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
        $this->url = $url;
        $this->timeout = $timeout;
        $this->redirects = $redirects;
        $this->headers = $headers;
        $this->useragent = $useragent;

        $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;

        if ( preg_match('/^http(s)?:\/\//i', $url) ) {
            $args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects,'sslverify' => FALSE,);

            if ( !empty($this->headers) )
                $args['headers'] = $this->headers;

            if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
                $args['user-agent'] = $this->useragent;

            $res = wp_remote_request($url, $args);

            if ( is_wp_error($res) ) {
                $this->error = 'WP HTTP Error: ' . $res->get_error_message();
                $this->success = false;
            } else {
                $this->headers = wp_remote_retrieve_headers( $res );
                $this->body = wp_remote_retrieve_body( $res );
                $this->status_code = wp_remote_retrieve_response_code( $res );
            }
        } else {
            if ( ! $this->body = file_get_contents($url) ) {
                $this->error = 'file_get_contents could not read the file';
                $this->success = false;
            }
        }
    }
}

function get_bitbucket_feed_changesets($feed_url ='',$limit = 10)
{
    global $wp_version;
    $changesets = array();
    $url_path = parse_url( $feed_url, PHP_URL_PATH);
    $path_part = explode('/',ltrim($url_path,'/') );
    //var_dump($url_part);
    $user = $path_part[0];
    $repo = $path_part[1];
    is_dir( WP_CONTENT_DIR .'/cache' ) || mkdir ( WP_CONTENT_DIR .'/cache/',0777, TRUE);
    $bitbucket_cache_file = WP_CONTENT_DIR .'/cache/'. md5($feed_url);

    if( file_exists( $bitbucket_cache_file ) )
    {
    if( time() - filemtime($bitbucket_cache_file) > 3600* 1 )
    {
        unlink($bitbucket_cache_file);
    }
    else
    {
        $changesets = unserialize( base64_decode(file_get_contents($bitbucket_cache_file) ) );
    }
    }
   
    if( !is_array($changesets) || count($changesets) < 1 )
    {
        $feed = new SimplePie();
        $feed->set_feed_url( $feed_url );    //RSS 地址
        $feed->set_file_class('ihacklog_SimplePie_File');
        $feed->enable_cache(true);
        $feed->set_cache_class('WP_Feed_Cache');
        $feed->set_cache_location(WP_CONTENT_DIR . '/cache');
        $feed->set_cache_duration(5);
        $feed->set_useragent('WordPress/' . $wp_version . '; ihacklog-WP-bitbucket-page-template-v1.0');
        $feed->init();
        $feed->handle_content_type();
        $changesets = $feed->get_items( 0, $limit );
        file_put_contents($bitbucket_cache_file,base64_encode(serialize($changesets) ) );
    }
        //var_dump($items);
        echo '<div class="repo">  <h2>repo: '. $repo .'</h2>';
       
        foreach ((array) $changesets as $item)
        {
            echo '<p class="bitbucket-change">';
            echo '<span class="bitbucket-node"><a href="'. $item->get_link() .'" title="by '. $user .'" target="_blank">'. basename($item->get_link()) . '</a></span>';
            echo '<span class="bitbucket-utctime">' . gmdate('Y-m-d H:i:s',strtotime($item->get_date('Y-m-d H:i:s') ) + 3600*8 ) .'</span>';
            echo '<span class="bitbucket-message">' .$item->get_title()  .'</span>';
            echo '</p>';
        }
        echo '</div>';
}

调用:

1
2
3
        <?php
        get_bitbucket_feed_changesets('https://bitbucket.org/ihacklog/REPO SLUG/rss?token=TOKEN VALUE', 10);
        ?>
更多
2 Responses Post a comment

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS