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