Plugin Directory

Changeset 3328458


Ignore:
Timestamp:
07/15/2025 06:36:37 PM (8 months ago)
Author:
brygs
Message:

Version 1.3 adds ability to post to Bluesky using excerpt of WordPress post

Location:
airdriemedia-posts-for-bluesky/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • airdriemedia-posts-for-bluesky/trunk/airdriemedia-posts-for-bluesky.php

    r3320669 r3328458  
    44* Plugin Name:      Airdrie Media Posts for Bluesky
    55* Plugin URI:       https://www.airdriemedia.com/airdriemedia-posts-for-bluesky
    6 * Version:          1.2
     6* Version:          1.3
    77* Description:      Displays the most recent Bluesky posts
    88* Author:           Airdrie Media
     
    6262//@ini_set('display_errors', 0);
    6363if ( !defined( 'AMBLUESKY_PLUGIN_VERSION' ) ) {
    64     define( 'AMBLUESKY_PLUGIN_VERSION', '1.2' );
     64    define( 'AMBLUESKY_PLUGIN_VERSION', '1.3' );
    6565}
    6666if ( !class_exists( 'amBluesky' ) ) {
     
    8787            $this->options = get_option( 'ambluesky_options' );
    8888            add_action( 'wp_enqueue_scripts', [$this, 'ambluesky_enqueue_stylesheet'], 10 );
     89            //            add_action('publish_post', 'post_excerpt_to_bluesky', 10, 1);
     90            add_action(
     91                'publish_post',
     92                [$this, 'post_excerpt_to_bluesky'],
     93                10,
     94                1
     95            );
    8996        }
    9097
     
    179186        }
    180187
     188        function post_excerpt_to_bluesky( $post_id ) {
     189            if ( $this->getOption( "postExcerpt" ) == 1 ) {
     190                $this->authenticate();
     191                // Avoid firing on non-published posts
     192                $post = get_post( $post_id );
     193                if ( $post->post_status !== 'publish' ) {
     194                    return;
     195                }
     196                // Get and clean excerpt
     197                $excerpt = wp_strip_all_tags( get_the_excerpt( $post_id ) );
     198                $excerpt = trim( $excerpt );
     199                // Add permalink and calculate max excerpt length
     200                $link = get_permalink( $post_id );
     201                $link_len = strlen( $link ) + 1;
     202                // +1 for the space
     203                $max_total_length = 300;
     204                $max_excerpt_length = $max_total_length - $link_len;
     205                // Truncate excerpt if necessary
     206                if ( mb_strlen( $excerpt ) > $max_excerpt_length ) {
     207                    $excerpt = mb_substr( $excerpt, 0, $max_excerpt_length - 1 ) . '…';
     208                }
     209                $final_text = $excerpt . '\\n' . $link;
     210                // Bluesky credentials
     211                $handle = $this->username;
     212                // yourhandle.bsky.social
     213                $app_password = $this->app_password;
     214                // get from https://bsky.app/settings/app-passwords
     215                // Step 1: Log in to get access token
     216                $login_response = wp_remote_post( 'https://bsky.social/xrpc/com.atproto.server.createSession', [
     217                    'headers' => [
     218                        'Content-Type' => 'application/json',
     219                    ],
     220                    'body'    => json_encode( [
     221                        'identifier' => $handle,
     222                        'password'   => $app_password,
     223                    ] ),
     224                ] );
     225                if ( is_wp_error( $login_response ) ) {
     226                    error_log( 'Bluesky login error: ' . $login_response->get_error_message() );
     227                    return;
     228                }
     229                $login_body = json_decode( wp_remote_retrieve_body( $login_response ), true );
     230                if ( empty( $login_body['accessJwt'] ) || empty( $login_body['did'] ) ) {
     231                    error_log( 'Bluesky login failed: missing token or DID' );
     232                    return;
     233                }
     234                $token = $login_body['accessJwt'];
     235                $did = $login_body['did'];
     236                // Step 2: Create the post (record)
     237                $post_data = [
     238                    'repo'       => $did,
     239                    'collection' => 'app.bsky.feed.post',
     240                    'record'     => [
     241                        '$type'     => 'app.bsky.feed.post',
     242                        'text'      => $final_text,
     243                        'createdAt' => gmdate( 'c' ),
     244                    ],
     245                ];
     246                $post_response = wp_remote_post( 'https://bsky.social/xrpc/com.atproto.repo.createRecord', [
     247                    'headers' => [
     248                        'Content-Type'  => 'application/json',
     249                        'Authorization' => 'Bearer ' . $token,
     250                    ],
     251                    'body'    => json_encode( $post_data ),
     252                ] );
     253                if ( is_wp_error( $post_response ) ) {
     254                    error_log( 'Bluesky post failed: ' . $post_response->get_error_message() );
     255                    return;
     256                }
     257                $response_body = wp_remote_retrieve_body( $post_response );
     258                $response_data = json_decode( $response_body, true );
     259                if ( !empty( $response_data['uri'] ) ) {
     260                    error_log( 'Bluesky post successful: ' . $response_data['uri'] );
     261                } else {
     262                    error_log( 'Bluesky post may have failed: ' . $response_body );
     263                }
     264            }
     265        }
     266
    181267        public function amBluesky_main( $atts = [] ) {
    182268            // set default values
     
    471557                'settings_section'
    472558            );
     559            add_settings_field(
     560                'cb_postExcerptName',
     561                'Posting to Bluesky',
     562                array($this, 'cb_postExcerpt_callback'),
     563                'ambluesky',
     564                'settings_section'
     565            );
    473566        }
    474567
     
    664757        }
    665758
     759        public function cb_postExcerpt_callback() {
     760            $setVal = $this->getOption( 'postExcerpt' );
     761            $is_premium = function_exists( 'bp_fs' ) && bp_fs()->can_use_premium_code__premium_only();
     762            ?>
     763            <input type="checkbox" id="cb_postExcerpt" name="ambluesky_options[postExcerpt]" value="1"
     764            <?php
     765            checked( 1, $setVal );
     766            echo ( !$is_premium ? 'disabled' : '' );
     767            ?> />
     768            <label for="cb_postExcerpt"> Upon publishing a WordPress post here, create a Bluesky post using the excerpt content from the WordPress post.</label>
     769            <p>
     770            <?php
     771            if ( !$is_premium ) {
     772                ?>
     773                <em style="color: red;">Upgrade to Pro for this feature</em>
     774            <?php
     775            }
     776            ?>
     777            </p>
     778            <?php
     779        }
     780
    666781    }
    667782
  • airdriemedia-posts-for-bluesky/trunk/readme.txt

    r3320669 r3328458  
    11=== Airdrie Media Posts for Bluesky ===
    2 Contributors: brygs
     2Contributors: brygs, freemius
    33Tags: bluesky
    44Donate link: https://paypal.me/airdriemedia
    55Requires at least: 5.7
    66Tested up to: 6.7
    7 Stable tag: 1.2
     7Stable tag: 1.3
    88Requires PHP: 7.4
    99License: GPL v2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1111
    12 Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode.
     12Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode. 
    1313
    1414== Description ==
    15 Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode.
     15Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode. You can also create a Bluesky post from your WordPress posts (premium feature).
    1616
    1717== Installation ==
     
    3131
    3232== Changelog ==
     33
     34Version 1.3:
     351) Allows posting to Bluesky the excerpt of any new WordPress posts.
     36
    3337Version 1.2:
    34381) Fixed bug that would allow some overwriting of stylesheets.
Note: See TracChangeset for help on using the changeset viewer.