Changeset 3328458
- Timestamp:
- 07/15/2025 06:36:37 PM (8 months ago)
- Location:
- airdriemedia-posts-for-bluesky/trunk
- Files:
-
- 3 edited
-
airdriemedia-posts-for-bluesky.php (modified) (6 diffs)
-
assets/screenshot-3.png (modified) (previous)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
airdriemedia-posts-for-bluesky/trunk/airdriemedia-posts-for-bluesky.php
r3320669 r3328458 4 4 * Plugin Name: Airdrie Media Posts for Bluesky 5 5 * Plugin URI: https://www.airdriemedia.com/airdriemedia-posts-for-bluesky 6 * Version: 1. 26 * Version: 1.3 7 7 * Description: Displays the most recent Bluesky posts 8 8 * Author: Airdrie Media … … 62 62 //@ini_set('display_errors', 0); 63 63 if ( !defined( 'AMBLUESKY_PLUGIN_VERSION' ) ) { 64 define( 'AMBLUESKY_PLUGIN_VERSION', '1. 2' );64 define( 'AMBLUESKY_PLUGIN_VERSION', '1.3' ); 65 65 } 66 66 if ( !class_exists( 'amBluesky' ) ) { … … 87 87 $this->options = get_option( 'ambluesky_options' ); 88 88 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 ); 89 96 } 90 97 … … 179 186 } 180 187 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 181 267 public function amBluesky_main( $atts = [] ) { 182 268 // set default values … … 471 557 'settings_section' 472 558 ); 559 add_settings_field( 560 'cb_postExcerptName', 561 'Posting to Bluesky', 562 array($this, 'cb_postExcerpt_callback'), 563 'ambluesky', 564 'settings_section' 565 ); 473 566 } 474 567 … … 664 757 } 665 758 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 666 781 } 667 782 -
airdriemedia-posts-for-bluesky/trunk/readme.txt
r3320669 r3328458 1 1 === Airdrie Media Posts for Bluesky === 2 Contributors: brygs 2 Contributors: brygs, freemius 3 3 Tags: bluesky 4 4 Donate link: https://paypal.me/airdriemedia 5 5 Requires at least: 5.7 6 6 Tested up to: 6.7 7 Stable tag: 1. 27 Stable tag: 1.3 8 8 Requires PHP: 7.4 9 9 License: GPL v2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html 11 11 12 Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode. 12 Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode. 13 13 14 14 == Description == 15 Airdrie Media Posts for Bluesky allows you to embed your Bluesky feed into your Wordpress site via a shortcode. 15 Airdrie 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). 16 16 17 17 == Installation == … … 31 31 32 32 == Changelog == 33 34 Version 1.3: 35 1) Allows posting to Bluesky the excerpt of any new WordPress posts. 36 33 37 Version 1.2: 34 38 1) Fixed bug that would allow some overwriting of stylesheets.
Note: See TracChangeset
for help on using the changeset viewer.