寄稿しました。詳しくはリンク先の記事を参照して下さい。(ㆁᴗㆁ✿)
composerによるTwitterOAuthライブラリインストール
# pwd /var/www/html/wp/wp-content/plugins/wptweetpost
# curl -sS https://getcomposer.org/installer | php
# mv composer.phar /usr/local/bin/composer
# composer require abraham/twitteroauth
[root@localhost wptweetpost]# ls -laht 合計 16K drwxr-xr-x 6 hoge apache 117 6月 26 23:28 .. drwxr-xr-x 3 root root 85 6月 26 23:27 . -rw-r--r-- 1 root root 4.4K 6月 26 23:26 wptweetpost.php drwxr-xr-x 4 root root 57 6月 26 18:30 vendor -rw-r--r-- 1 root root 2.4K 6月 26 18:30 composer.lock -rw-r--r-- 1 root root 68 6月 26 18:30 composer.json
機能を作る
# vi /var/www/html/wp/wp-content/plugins/wptweetpost/wptweetpost.php
<?php
/**
* @package WPTweetPost
* @version 0.1.1
*/
/*
Plugin Name: WPTweetPost
Plugin URI: http://example.com/plugins/admin-yuu/
Description: Plugin development tutorial for making admin menu and page easily
Author: Kanehiro Yuu
Version: 0.1.1
Author URI: https://sys-guard.com/
*/
//TwitterOAuthライブラリの読み込み
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//実行するよ!
$sgTweetObj = new Wp_Tweet();
// デバッグモード =================================
$sgTweetObj->debugFlag = 1; // 0 = OFF, 1 = ON
// ===============================================
class Wp_Tweet
{
private $consumerKey = ''; // Twitter API consumerKey
private $consumerSecret = ''; // Twitter API consumerSecret
private $accessToken = ''; // Twitter API accessToken
private $accessTokenSecret = ''; // Twitter API accessTokenSecret
private $message = ''; // ツイート
private $connection = ''; // Twitter API インスタンス
public $debugFlag; // Twitter API ツイート投稿 デバッグフラグ
private $res = ''; // Twitter API ツイート投稿 デバッグ出力用変数
private $option_values; //フォームのレコード __construct()で配列に初期化しています。
private $show_text1 = ''; // フォームのレコード Twitter API Key consumerKey
private $show_text2 = ''; // フォームのレコード Twitter API Key consumerSecret
private $show_text3 = ''; // フォームのレコード Twitter API Key accessToken
private $show_text4 = ''; // フォームのレコード Twitter API Key accessTokenSecret
private $show_text5 = ''; // フォームのレコード ツイート
function __construct(){
$this->option_values = array();
$this->admin_init();
}
//管理画面にメニューを追加する =========================
public function admin_init(){
add_action('admin_menu',array($this,'tweet_admin_hook'));
}
public function tweet_admin_hook()
{
add_menu_page( 'Tweet Post', 'Tweet Post', 'manage_options', 'sg-wp-tweet-post', array($this,'tweet_post'));
}
//管理画面にメニューを追加する ここまで ===============
//Twitter投稿フォーム実装
public function tweet_post() {
//現在のユーザがmanage_options権限グループを持っているかチェックする
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//Submitボタンが押されたら実行
if ( isset($_POST['twitter_api_options'])) {
check_admin_referer('twitte_api_options_check'); //セキュリティ1 管理画面リファラチェック
$this->option_values = $_POST['twitter_api_options'];
update_option('twitter_api_options', $this->option_values);
?>
<div class="updated fade"><p><strong><?php echo "投稿完了したよ!"; ?></strong></p></div><?php
//認証情報4つ
$this->consumerKey = isset($this->option_values['text1']) ? $this->option_values['text1'] : null;
$this->consumerSecret = isset($this->option_values['text2']) ? $this->option_values['text2'] : null;
$this->accessToken = isset($this->option_values['text3']) ? $this->option_values['text3'] : null;
$this->accessTokenSecret = isset($this->option_values['text4']) ? $this->option_values['text4'] : null;
$this->message = isset($this->option_values['text5']) ? $this->option_values['text5'] : null;
//接続します。
$this->connection = new TwitterOAuth($this->consumerKey, $this->consumerSecret, $this->accessToken, $this->accessTokenSecret);
//ツイートします。
$this->res = $this->connection->post("statuses/update", array("status" => $this->message));
//デバッグモード時に出力 0 = OFF, 1 = ON
if($this->debugFlag == 1){
echo "</hr>";
var_dump($this->res);
echo "</hr>";
}else{}
//デバッグモード時に出力 ここまで
} ?>
<h1>Twitter API連携設定</h1>
<form action="" method="post">
<?php
wp_nonce_field('twitte_api_options_check'); //セキュリティ2
$this->option_values = get_option('twitter_api_options');//wp-optionsからデータ読み込み
$this->show_text1 = isset($this->option_values['text1']) ? $this->option_values['text1'] : null;
$this->show_text2 = isset($this->option_values['text2']) ? $this->option_values['text2'] : null;
$this->show_text3 = isset($this->option_values['text3']) ? $this->option_values['text3'] : null;
$this->show_text4 = isset($this->option_values['text4']) ? $this->option_values['text4'] : null;
$this->show_text5 = isset($this->option_values['text5']) ? $this->option_values['text5'] : null;
?>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="consumerKey">Twitter consumer_key</label>
</th>
<td><input type="text" id="consumerKey" class="regular-text" name="twitter_api_options[text1]" value="<?php echo $this->show_text1; ?>"></td>
</tr>
<tr>
<th scope="row">
<label for="consumerSecret">Twitter consumer_secret</label>
</th>
<td><input type="text" id="consumerSecret" class="regular-text" name="twitter_api_options[text2]" value="<?php echo $this->show_text2; ?>"></td>
</tr>
<tr>
<th scope="row">
<label for="accessToken">Twitter access_token</label>
</th>
<td><input type="text" id="accessToken" class="regular-text" name="twitter_api_options[text3]" value="<?php echo $this->show_text3; ?>"></td>
</tr>
<tr>
<th scope="row">
<label for="accessTokenSecret">Twitter access_token_secret</label>
</th>
<td><input type="text" id="accessTokenSecret" class="regular-text" name="twitter_api_options[text4]" value="<?php echo $this->show_text4; ?>"></td>
</tr>
<tr>
<th scope="row">
<label for="message">投稿メッセージ</label>
</th>
<td><input type="text" id="message" class="regular-text" name="twitter_api_options[text5]" value="<?php echo $this->show_text5; ?>"></td>
</tr>
</tbody>
</table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="Tweet Post" /></p>
</form>
<?php }
}//class Wp_Tweet
次回のネタはDB絡みかな~。


