Skip to content

Instantly share code, notes, and snippets.

@snoise
Created May 25, 2014 15:57
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/4e8e26332963d6674883 to your computer and use it in GitHub Desktop.
Save snoise/4e8e26332963d6674883 to your computer and use it in GitHub Desktop.
フィードを新規で記事に追加する #WordPressプラグイン
<?php
/*
Plugin Name: Paka3FeedPost
Plugin URI: http://www.paka3.com/wpplugin
Description: フィードを新規で記事に追加する
Author: Shoji ENDO
Version: 0.1
Author URI:http://www.paka3.com/
*/
$paka3FeedPost = new Paka3_feed_post;
class Paka3_feed_post {
function __construct( ) {
//メニューの追加
add_action( 'admin_menu' , array($this , 'adminAddMenu' ) );
}
//メニューの「paka3投稿」の設定(今回はサブメニュー)
function adminAddMenu ( ) {
add_submenu_page("edit.php", '別の投稿ページだよ', 'paka3投稿', 'edit_themes', 'paka3_post', array($this,'paka3_post_page'));
}
//paka3投稿のページ
function paka3_post_page () {
if(isset($_POST['newPost']) && check_admin_referer( get_bloginfo('url').'paka3post_new','paka3post' )){
//feedの設定を保存する
update_option('paka3_newPost', $_POST['newPost']);
//new_my_post
$html = $this->get_my_feed( $_POST['newPost']['feed_url'] , $_POST['newPost']['count'] );
$newPost = array ('title' => esc_html($_POST['newPost']['title']),
'content' => $html);
$res = $this->new_my_post( $newPost, $catID );
if($res){
//$data = get_post($res);
echo '<div class="updated fade"><p><strong>新規に投稿されました。';
echo '<br /><a href ="'.get_edit_post_link( $res ).'">【編集はこちらをクリックして、おこなってください。】</a>';
echo "</strong></p></div>";
}
}
//保存された値を取得
$newPost = get_option('paka3_newPost');
//ページに表示する内容
$wp_n = wp_nonce_field(get_bloginfo('url').'paka3post_new','paka3post');
//タイトル
$newPost['title'] = isset($newPost['title']) ? $newPost['title'] : null;
//FEED URL
$newPost['feed_url'] = isset($newPost['feed_url']) ? $newPost['feed_url'] : null;
//Feedの取得数
$newPost['count'] = isset($newPost['count']) ? $newPost['count'] : 1;
$countList = array( array(1,1,selected( $newPost['count'], 1, false )),
array(2,2,selected( $newPost['count'], 2, false )),
array(3,3,selected( $newPost['count'], 3, false )),
array(4,4,selected( $newPost['count'], 4, false )),
array(5,5,selected( $newPost['count'], 5, false ))
);
//Feedの表示形式
$newPost['kind'] = isset($newPost['kind']) ? $newPost['kind'] : 1;
$kindList = array( array(1,'簡略表示(写真+140文字)',checked( $newPost['kind'], 1, false ))
);
echo <<< EOS
<div class="wrap">
<h2>paka3 投稿ページ</h2>
<form method="post" action="">
{$wp_n}
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="titile">記事タイトル</label></th>
<td><input name="newPost[title]" type="text" value="{$newPost[title]}" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="feed_url">Feed URL</label></th>
<td><input name="newPost[feed_url]" type="text" value="{$newPost[feed_url]}" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="count">件数</label></th>
<td>
<select name="newPost[count]">
EOS;
foreach ( $countList as $c ){
echo "<option value='$c[0]' $c[2]>{$c[1]}";
}
echo <<< EOS
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="count">表示形式</label></th>
<td>
EOS;
foreach ( $kindList as $k ){
echo "<label><input type='radio' name='newPost[kind]' value='$k[0]' $k[2]>{$k[1]}</label><br />";
}
echo <<< EOS
</td>
</tr>
</table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="新しく記事を追加する" /></p>
</form>
</div>
EOS;
}
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;
}
}
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