使用 bitbucket RSS做个展示项目提交动态的WP模板页面 » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

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

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

/**
 * 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 '

repo: '. $repo .'

'; foreach ((array) $changesets as $item) { echo '

'; echo ''. basename($item->get_link()) . ''; echo '' . gmdate('Y-m-d H:i:s',strtotime($item->get_date('Y-m-d H:i:s') ) + 3600*8 ) .''; echo '' .$item->get_title() .''; echo '

'; } echo '
'; }

调用:

        

Tagged in : 模板,bitbucket,rss

All Comments (0)
Gravatar image
No Comments