Skip to content

Instantly share code, notes, and snippets.

@snoise
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoise/a0e38dc4ea3415448816 to your computer and use it in GitHub Desktop.
Save snoise/a0e38dc4ea3415448816 to your computer and use it in GitHub Desktop.
定期的(1日おきに)に、RSSを取得し、新しい記事を自動で投稿する。#WordPressプラグイン
<?php
/*
Plugin Name: Paka3_task_post_rss
Plugin URI: http://www.paka3.com/wpplugin
Description: ※定期的(1日おきに)に、RSSを取得し、新しい記事を自動で投稿する。
Author: Shoji ENDO
Version: 0.1
Author URI:http://www.paka3.com/
*/
$paka3_task_post_rss = new Paka3_task_post_rss ;
class Paka3_task_post_rss{
//インスタンス変数(設定)
private $t = "13:00:00" ;
private $title = "今日のオススメ記事" ;
private $url = "http://www.en3.jp/feed" ;
private $count = 3 ;
function __construct(){
//プラグインを有効化したとき
if(function_exists('register_activation_hook')) {
register_activation_hook (__FILE__ , array( $this , 'paka3_plugin_start' ) ) ;
}
//プラグインをストップしたとき
if(function_exists('register_deactivation_hook')) {
register_deactivation_hook (__FILE__ , array( $this , 'paka3_plugin_stop' ) ) ;
}
add_action( 'paka3_task_post_hook', array( $this , 'paka3_task_post_function' ) ) ;
}
//プラグインを有効化したときに呼ばれる関数
function paka3_plugin_start(){
//時差を求める
$p = ( current_time( 'timestamp' ) - time( ) ) / 3600;
//(今日から)毎日タスクを実行する時間を設定する
//管理>設定されたタイムゾーンでの時間を設定する(13:00)
$my_time = date( 'Y-m-d '.$this->t, current_time( 'timestamp' ) );
//時差を引いて、UNIX時間(UTC:秒)に合わせる
$task_time = strtotime( -1 * $p." hour", strtotime( $my_time ) );
wp_schedule_event( $task_time, 'daily', paka3_task_post_hook );
}
//プラグインをストップしたときに呼ばれる関数
function paka3_plugin_stop(){
wp_clear_scheduled_hook( 'paka3_task_post_hook' );
}
//実行する処理
function paka3_task_post_function() {
$url = $this->url;
$count = $this->count;
$title = $this->title;
$html = $this->get_my_feed( $url , $count );
$newPost = array ('title' => $title,
'content' => $html);
$res = $this->new_my_post( $newPost, $catID );
}
//新規投稿の関数
function new_my_post($post, $catID ) {
$my_post = array( );
$my_post[ 'post_title' ] = $post['title'];
$my_post[ 'post_content' ] = $post['content'];
$my_post[ 'post_status' ] = 'draft'; //下書き
$my_post[ 'post_author' ] = 1;
$my_post[ 'post_date' ] = date( 'Y-m-d H:i:s', current_time('timestamp') );
$my_post[ 'post_category' ] = array( $catID );
// データベースに投稿を追加
$res = wp_insert_post( $my_post );
if( $res != 0 ) {
return $res; //post_id
}else{
return false;
}
}
//RSS取得の関数
function get_my_feed ( $url, $count ) {
//ここからfeedを取得して行きます。
add_filter ( 'wp_feed_cache_transient_lifetime' , array( $this , 'return_1800' ) );
$feed = fetch_feed( $url );
remove_filter( 'wp_feed_cache_transient_lifetime' , array( $this , 'return_1800' ) );
//*表示数を設定
if ( ! is_wp_error( $feed ) ) {
$maxitems = $feed->get_item_quantity( $count );
$rss_items = $feed->get_items( 0, $maxitems );
}
//サイト名とサイトURLの取得
$title = $feed->get_title();
$site_url = $feed->get_permalink();
//表示数
$str="";
if ( $maxitems > 0){
foreach ( $rss_items as $item ){
$f_link = esc_url( $item->get_permalink() );
$f_date = sprintf( __( '%s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') );
$f_title = esc_html( $item->get_title() );
$f_content = mb_strimwidth(strip_tags($item->get_content()), 0, 140, "...","UTF-8");
if(preg_match_all('/<img.*?src=(["\'])(.+?)\1.*?>/i',$item->get_content(),$img_array)){
$site_img = $img_array[2][0];
}
if(!preg_match('/^http(s)?:\/\/[^\/\s]+(.*)$/',$site_img,$r)){
$site_img = $site_url.$site_img;
}
//itemのhtmlの生成
$str .= <<<EOS
<div class="feed_item">
<a href="{$f_link}" rel="nofollow" >
<div class=feed_item_img><img src = {$site_img} /></div>
<h4>{$f_title}</h4>
<p>{$f_content}</p>
</a></div>
EOS;
}
}
//全体の
$html =<<< EOS
<div class="feed_block">
<h2><a href="{$site_url}" rel="nofollow" >{$title}</a></h2>
{$str}
</div>
EOS;
return $html ;
}
function return_1800(){
return 1800;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment