Written by Anonymous
function get_rss_feed_tag( $atts ) {
include_once(ABSPATH . WPINC . '/feed.php');
extract(shortcode_atts(
array(
'url' => 'https://ja.wordpress.org/feed/', //取得するRSSフィードURL
'count' => '5', //取得する数
'img' => NO_IMAGE_RSS, //画像が取得できなかった場合のイメージ
'target' => '_blank', //ブラウザの開き方(target属性)
'cache_minute' => '60', //キャッシュ時間(分)
'desc' => '1', //説明文表示 1 or 0
'date' => '1', //日付表示 1 or 0
'type' => '', //表示タイプ
'bold' => 0, //タイトルを太字にするか
'arrow' => 0, //矢印を出すか
'class' => null, //拡張クラス
'site' => 0, //サイト名を表示するか
),
$atts,
'rss'
));
$feed_url = $url;
$feed_count = $count;
$img_url = $img;
$feed_contents = '';
//Cache処理(かなり簡易的なもの)
$transient_id = 'ree_feed_'.md5($feed_url.'_'.$count.'_'.$img_url.'_'.$target.'_'.$desc.'_'.$date.'_'.$type.'_'.$bold.'_'.$arrow.'_'.$class.'_'.$site);
$feed_contents = get_transient( $transient_id );
//キャッシュが存在しない場合URLから取得
if (!$feed_contents) {
// WordPress 6.9対応: SimplePieの内部キャッシュを無効化するためのコールバック関数
$feed_options_callback = function($feed) {
// SimplePieの内部キャッシュを無効化(独自のtransientキャッシュを使用するため)
$feed->enable_cache(false);
// 日付順ソートを無効化(RSS配信側の順序を尊重するため)
$feed->enable_order_by_date(false);
};
// フィード取得前にアクションを追加
add_action('wp_feed_options', $feed_options_callback);
$rss = fetch_feed( $feed_url );
// フィード取得後にアクションを削除(他のフィード取得に影響しないようにする)
remove_action('wp_feed_options', $feed_options_callback);
if ( !is_wp_error( $rss ) ) {
// サイト名取得(無条件)
$site_title = trim( (string) $rss->get_title() );
if ( empty($site_title) ) {
$site_title = parse_url($feed_url, PHP_URL_HOST);
}
$maxitems = $rss->get_item_quantity( $feed_count );
$rss_items = $rss->get_items( 0, $maxitems );
ob_start(); // ループ全体の出力をバッファリング
foreach ( $rss_items as $item ) :
$first_img = '';
// get_content() が null を返す場合があるため、確実に文字列にしてから利用
$item_content = $item->get_content();
if ( !is_string( $item_content ) ) {
$item_content = '';
}
if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $item_content, $matches )) {
$first_img = $matches[1];
}
if ( !empty( $first_img ) ) :
$feed_img = esc_attr( $first_img );
else:
$feed_img = $img_url;
endif;
$feed_url = $item->get_permalink();
$feed_title = $item->get_title() ?? '';
$feed_title = str_replace(["\r\n", "\r", "\n"], '', $feed_title);
$feed_date = $item->get_date(get_site_date_format());
// 上で文字列化した $item_content を再利用
$feed_text = get_content_excerpt(strip_tags($item_content), get_entry_card_excerpt_max_length());
// テンプレートに渡す引数をまとめる
$template_args = array(
'url' => $feed_url,
'title' => $feed_title,
'target' => $target,
'img' => $feed_img,
'site' => $site,
'site_title' => $site_title,
'desc' => $desc,
'date' => $date,
'text' => $feed_text,
'date_str' => $feed_date,
'cache_minute' => $cache_minute,
);
// カード部分のテンプレートを呼び出す
cocoon_template_part('tmp/rss-card', null, $template_args);
endforeach;
$feed_content = ob_get_clean();
} else {
$feed_content = '<p>' . __( 'RSSフィードを取得できません', THEME_NAME ) . '</p>';
}
$atts = array(
'type' => $type,
'bold' => $bold,
'arrow' => $arrow,
'class' => $class,
);
$card_class = get_additional_widget_entry_cards_classes($atts);
$feed_contents = '<div class="rss-entry-cards widget-entry-cards'.$card_class.' no-icon">' . $feed_content . '</div>';
set_transient($transient_id, $feed_contents, 60 * intval($cache_minute));
}
return apply_filters( 'get_rss_feed_tag', $feed_contents);
}