@@ -434,4 +434,85 @@ private static function fetch_og_tags( string $url ): array|\WP_Error {
434434
435435 return $ og ;
436436 }
437+
438+ private function getAuthProvider (): ?BlueskyAuth {
439+ if ( ! class_exists ( '\DataMachine\Abilities\AuthAbilities ' ) ) {
440+ return null ;
441+ }
442+
443+ $ auth = new \DataMachine \Abilities \AuthAbilities ();
444+ $ provider = $ auth ->getProvider ( 'bluesky ' );
445+
446+ if ( ! $ provider instanceof BlueskyAuth ) {
447+ return null ;
448+ }
449+
450+ return $ provider ;
451+ }
452+
453+ public function checkPermission (): bool {
454+ return PermissionHelper::can_manage ();
455+ }
456+
457+ public function execute ( array $ input ): array |\WP_Error {
458+ $ auth = $ this ->getAuthProvider ();
459+ if ( ! $ auth ) {
460+ return new \WP_Error ( 'missing_auth ' , 'Bluesky auth provider not available ' , array ( 'status ' => 401 ) );
461+ }
462+
463+ $ session = $ auth ->get_session ();
464+ if ( empty ( $ session ['accessJwt ' ] ) ) {
465+ return new \WP_Error ( 'missing_auth ' , 'Bluesky session not available ' , array ( 'status ' => 401 ) );
466+ }
467+
468+ if ( empty ( $ input ['post_uri ' ] ) ) {
469+ return new \WP_Error ( 'missing_param ' , 'post_uri is required ' , array ( 'status ' => 400 ) );
470+ }
471+
472+ $ did = $ session ['did ' ];
473+
474+ // Parse rkey from AT URI: at://did:plc:xxx/app.bsky.feed.post/rkey
475+ $ post_uri = $ input ['post_uri ' ];
476+ $ uri_parts = explode ( '/ ' , $ post_uri );
477+ $ rkey = end ( $ uri_parts );
478+
479+ if ( empty ( $ rkey ) ) {
480+ return new \WP_Error ( 'api_error ' , 'Could not extract rkey from post URI: ' . $ post_uri , array ( 'status ' => 500 ) );
481+ }
482+
483+ $ response = wp_remote_post (
484+ 'https://bsky.social/xrpc/com.atproto.repo.deleteRecord ' ,
485+ array (
486+ 'timeout ' => 30 ,
487+ 'headers ' => array (
488+ 'Authorization ' => 'Bearer ' . $ session ['accessJwt ' ],
489+ 'Content-Type ' => 'application/json ' ,
490+ ),
491+ 'body ' => wp_json_encode ( array (
492+ 'repo ' => $ did ,
493+ 'collection ' => 'app.bsky.feed.post ' ,
494+ 'rkey ' => $ rkey ,
495+ ) ),
496+ )
497+ );
498+
499+ if ( is_wp_error ( $ response ) ) {
500+ return new \WP_Error ( 'api_error ' , $ response ->get_error_message (), array ( 'status ' => 500 ) );
501+ }
502+
503+ $ status_code = wp_remote_retrieve_response_code ( $ response );
504+
505+ if ( 200 === $ status_code || 204 === $ status_code ) {
506+ return array (
507+ 'success ' => true ,
508+ 'data ' => array (
509+ 'post_uri ' => $ input ['post_uri ' ],
510+ 'deleted ' => true ,
511+ ),
512+ );
513+ }
514+
515+ $ body = json_decode ( wp_remote_retrieve_body ( $ response ), true );
516+ return new \WP_Error ( 'api_error ' , $ body ['error ' ] ?? 'Failed to delete post ' , array ( 'status ' => 500 ) );
517+ }
437518}
0 commit comments