LoginSignup
0
0

BlueSkyへ記事リンクをポストするワードプレスプラグインを作りました[開発者用]。

Last updated at Posted at 2024-01-04

BlueSkyへ記事リンクをポストするワードプレスプラグインを作りました[開発者用]。

ご使用にあたって下記の条件を必須とします。
1.ワードプレスプラグイン作成方法及びコンポーザーの使用ができる人(コマンドからコンポーザーが叩けるひと)。
2.phpコードが理解できる人。
3.php8.2のサーバー環境であること。

出来れば使用する場合、こちらの記事にリンクして頂けたら幸いです。
また、コードの可変は行っても構いません。実際、入力欄をワードプレスに作りdbへデータ(APIキーなど)を持たせるなどを行えば配布しやすい事は明確ですがそれをやってしまうと商売にならないですからね、ここで留めています。

※APP_PASSWORDに関してこちらの記事を参考してください🙇。
blueskyの自動投稿を作ってみたくなり即興で作りました。 #PHP - Qiita

bluesky-wp/bluesky-wp.php
<?php
/*
Plugin Name: BlueSky-Wp
Description: BlueSkyへ新規投稿 post
Version: 1.0
Author: @taoka_toshiaki
Author URI: https://taoka-toshiaki.com
*/
if (!defined('ABSPATH')) exit;
require dirname( __FILE__ ) . 'bluesky.php';

function blueskypost($new_status, $old_status, $post)
{
    
    if ($new_status == 'publish' && $old_status != 'publish') {
        try {
            //アイキャッチ画像URL取得
            $imageUrl = get_the_post_thumbnail_url(
                $post->ID, // 投稿IDまたは投稿オブジェクトを指定(省略可)
                'medium' // 画像サイズを指定
            );
            //ノーイメージ画像URL取得
            $noImageUrl = home_url( '/' ).'img/noimage.png';
            $imageUrl = $imageUrl?$imageUrl:$noImageUrl;
            //URLをディレクトリパスに変更する
            $imageUri = str_replace(home_url('/'),get_home_path(),$imageUrl);
            //Blueskyへポストする
            (new bluesky)->webPost(
                get_the_title($post->ID),
                get_permalink($post->ID),
                get_the_title($post->ID),
                get_the_title($post->ID),
                $imageUri,
            );
        } catch (\Throwable $th) {
            //throw $th;
        }
    }
}
add_action('transition_post_status', 'blueskypost', 10, 3);
bluesky-wp/config.php
<?php
define('USER_NAME','xxxxx.bsky.social');
define('APP_PASSWORD','xxxx-xxxx-xxxx-xxxx');
bluesky-wp/bluesky.php
<?php
require dirname( __FILE__ ) .'/vendor/autoload.php';
require dirname( __FILE__ ) .'/config.php';
use \potibm\Bluesky\BlueskyApi;
use \potibm\Bluesky\BlueskyPostService;
use \potibm\Bluesky\Feed\Post;

class bluesky
{
    private $api = null;
    private $postService = null;

    public function __construct()
    {
        $this->api = new BlueskyApi(USER_NAME, APP_PASSWORD);
        $this->postService = new BlueskyPostService($this->api);
    }

    /**
     * 簡単なテキスト投稿
     * @param $text
     * @return object
     */
    public function post($text)
    {
        $post = Post::create($text);
        $response = $this->api->createRecord($post);
        return $response;
    }

    /**
     * link付き投稿
     * @param $text
     * @param $url
     * @param $title
     * @param $description
     * @param $optionalimage|null
     * @return object
     */
    public function webPost($text,$url,$title,$description,$optionalimage=null)
    {
        $post = Post::create($text);
        $post = $this->postService->addWebsiteCard(
            $post, 
            $url, 
            $title, 
            $description,
            $optionalimage,
        );
        $response = $this->api->createRecord($post);
        return $response;
    }

    /**
     * 画像投稿
     * @param $text
     * @param $imgLink
     * @param $altText
     * @return object
     */
    public function imagePost($text, $imgLink, $altText)
    {
        $post = Post::create($text);
        $post = $this->postService->addImage(
            $post,
            $imgLink,
            $altText
        );
        $response = $this->api->createRecord($post);
        return $response;
    }
}
if($argv[0]){
    try {
        //var_dump((new bluesky)->post('これはテスト投稿ですよ'));
        //var_dump((new bluesky)->imagePost('これは画像テスト投稿ですよ','cron.png','クロン'));    
    } catch (\Throwable $th) {
        print $th->getMessage();
        //throw $th;
    }
}
bluesky-wp/composer.json
{
    "require": {
        "potibm/phluesky": "^0.3.0",
        "symfony/http-client": "^7.0",
        "nyholm/psr7": "^1.8"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true
        }
    }
}
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0