Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
* - registerAbility() Register one or more abilities once, guarded.
* - resolveProvider() Resolve + authenticate the platform provider.
* - resolveAccessToken() Resolve a valid OAuth access token server-side.
* - resolveCredentials() Resolve the provider and its access token together.
* - apiError() Build the standard api_error WP_Error.
*
* The three resolve* helpers delegate to CredentialResolver, which is also
* what static ability callbacks, handlers, and CLI commands call directly.
*
* @package DataMachineSocials
* @subpackage Abilities
* @since 0.6.0
*/

namespace DataMachineSocials\Abilities;

use DataMachine\Abilities\AuthAbilities;
use DataMachineSocials\Auth\CredentialResolver;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -79,25 +83,18 @@ protected function registerAbility( callable $register_callback, bool $require_w
/**
* Resolve and authenticate the platform auth provider.
*
* @param string $platform Provider key (e.g. 'threads').
* @param string $label Human-readable platform label for errors.
* @param bool $require_authenticated When true, enforce is_authenticated().
* @return object|\WP_Error The provider on success, WP_Error otherwise.
* Delegates to CredentialResolver, the single credential implementation.
*
* @template T of object
*
* @param string $platform Provider key (e.g. 'threads').
* @param string $label Human-readable platform label for errors.
* @param bool $require_authenticated When true, enforce is_authenticated().
* @param class-string<T>|null $expected_class Expected provider class, if any.
* @return ($expected_class is null ? object : T|\WP_Error) The provider on success, WP_Error otherwise.
*/
protected function resolveProvider( string $platform, string $label, bool $require_authenticated = true ) {
$auth = new AuthAbilities();
$provider = $auth->getProvider( $platform );

if ( ! $provider ) {
return new \WP_Error( 'missing_auth', $label . ' not authenticated', array( 'status' => 401 ) );
}

$is_authenticated = array( $provider, 'is_authenticated' );
if ( $require_authenticated && ! ( is_callable( $is_authenticated ) && call_user_func( $is_authenticated ) ) ) {
return new \WP_Error( 'missing_auth', $label . ' not authenticated', array( 'status' => 401 ) );
}

return $provider;
protected function resolveProvider( string $platform, string $label, bool $require_authenticated = true, ?string $expected_class = null ): object {
return CredentialResolver::provider( $platform, $label, $require_authenticated, $expected_class );
}

/**
Expand All @@ -112,17 +109,25 @@ protected function resolveProvider( string $platform, string $label, bool $requi
* @return string|\WP_Error Access token, or a missing_auth WP_Error.
*/
protected function resolveAccessToken( string $platform, string $label ): string|\WP_Error {
$provider = $this->resolveProvider( $platform, $label );
if ( is_wp_error( $provider ) ) {
return $provider;
}
$resolved = CredentialResolver::token( $platform, $label );

$token = method_exists( $provider, 'get_valid_access_token' ) ? $provider->get_valid_access_token() : null;
if ( ! is_string( $token ) || '' === trim( $token ) ) {
return new \WP_Error( 'missing_auth', 'Could not obtain a valid ' . $label . ' access token.', array( 'status' => 401 ) );
}
return is_wp_error( $resolved ) ? $resolved : $resolved['token'];
}

return trim( $token );
/**
* Resolve a platform's provider plus a valid access token.
*
* @template T of object
*
* @param string $platform Provider key.
* @param string $label Human-readable platform label for errors.
* @param bool $require_authenticated When true, enforce is_authenticated(). Pass
* false so an expired token can still refresh.
* @param class-string<T>|null $expected_class Expected provider class, if any.
* @return ($expected_class is null ? array{provider: object, token: string}|\WP_Error : array{provider: T, token: string}|\WP_Error)
*/
protected function resolveCredentials( string $platform, string $label, bool $require_authenticated = true, ?string $expected_class = null ): array|\WP_Error {
return CredentialResolver::token( $platform, $label, $require_authenticated, $expected_class );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function checkPermission(): bool {
}

public function execute( array $input ): array|\WP_Error {
$auth = $this->getAuthProvider();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Bluesky auth provider not available', array( 'status' => 401 ) );
$auth = $this->resolveProvider( 'bluesky', 'Bluesky', false, BlueskyAuth::class );
if ( is_wp_error( $auth ) ) {
return $auth;
}

$session = $auth->get_session();
Expand Down Expand Up @@ -119,19 +119,4 @@ public function execute( array $input ): array|\WP_Error {

return new \WP_Error( 'api_error', $result['error'] ?? 'Failed to delete post', array( 'status' => 500 ) );
}

private function getAuthProvider(): ?BlueskyAuth {
if ( ! class_exists( '\DataMachine\Abilities\AuthAbilities' ) ) {
return null;
}

$auth = new \DataMachine\Abilities\AuthAbilities();
$provider = $auth->getProvider( 'bluesky' );

if ( ! $provider instanceof BlueskyAuth ) {
return null;
}

return $provider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace DataMachineSocials\Abilities\Bluesky;

use DataMachine\Abilities\AuthAbilities;
use DataMachine\Core\HttpClient;
use DataMachineSocials\Abilities\AbstractSocialAbility;
use DataMachineSocials\PublishAuthorization;
use DataMachineSocials\Auth\CredentialResolver;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -138,11 +138,9 @@ public static function execute_publish( array $input ): array|\WP_Error {
return new \WP_Error( 'missing_param', 'A valid image URL is required for image publishing', array( 'status' => 400 ) );
}

$auth = new AuthAbilities();
$provider = $auth->getProvider( 'bluesky' );

if ( ! $provider || ! $provider->is_authenticated() ) {
return new \WP_Error( 'missing_auth', 'Bluesky not authenticated', array( 'status' => 401 ) );
$provider = CredentialResolver::provider( 'bluesky', 'Bluesky' );
if ( is_wp_error( $provider ) ) {
return $provider;
}

// Use the provider's session method which handles auth internally.
Expand Down Expand Up @@ -261,11 +259,9 @@ public static function execute_publish( array $input ): array|\WP_Error {
*/
public static function get_account( array $input ): array|\WP_Error {
$input;
$auth = new AuthAbilities();
$provider = $auth->getProvider( 'bluesky' );

if ( ! $provider || ! $provider->is_authenticated() ) {
return new \WP_Error( 'missing_auth', 'Bluesky not authenticated', array( 'status' => 401 ) );
$provider = CredentialResolver::provider( 'bluesky', 'Bluesky' );
if ( is_wp_error( $provider ) ) {
return $provider;
}

$details = $provider->get_account_details();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function checkPermission(): bool {
public function execute( array $input ): array|\WP_Error {
$action = $input['action'] ?? 'list';

$auth = $this->getAuthProvider();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Bluesky auth provider not available', array( 'status' => 401 ) );
$auth = $this->resolveProvider( 'bluesky', 'Bluesky', false, BlueskyAuth::class );
if ( is_wp_error( $auth ) ) {
return $auth;
}

$session = $auth->get_session();
Expand Down Expand Up @@ -231,15 +231,4 @@ private function getProfile( string $pds_url, string $access_token, string $acto
'data' => $data,
);
}

private function getAuthProvider(): ?BlueskyAuth {
$auth_abilities = new \DataMachine\Abilities\AuthAbilities();
$provider = $auth_abilities->getProvider( 'bluesky' );

if ( $provider instanceof BlueskyAuth ) {
return $provider;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public function checkPermission(): bool {
public function execute( array $input ): array|\WP_Error {
$action = $input['action'] ?? '';

$auth = $this->getAuthProvider();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Bluesky auth provider not available', array( 'status' => 401 ) );
$auth = $this->resolveProvider( 'bluesky', 'Bluesky', false, BlueskyAuth::class );
if ( is_wp_error( $auth ) ) {
return $auth;
}

$session = $auth->get_session();
Expand Down Expand Up @@ -104,21 +104,6 @@ public function execute( array $input ): array|\WP_Error {
}
}

private function getAuthProvider(): ?BlueskyAuth {
if ( ! class_exists( '\DataMachine\Abilities\AuthAbilities' ) ) {
return null;
}

$auth = new \DataMachine\Abilities\AuthAbilities();
$provider = $auth->getProvider( 'bluesky' );

if ( ! $provider instanceof BlueskyAuth ) {
return null;
}

return $provider;
}

private function deletePost( array $session, string $post_uri ): array|\WP_Error {
$pds_url = $session['pds_url'];
$did = $session['did'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use DataMachine\Engine\AI\Actions\PendingActionHelper;
use DataMachine\Engine\AI\Actions\SignPendingActionResolutionAbility;
use DataMachineSocials\Abilities\AbstractSocialAbility;
use DataMachineSocials\Handlers\Facebook\FacebookAuth;
use DataMachineSocials\Handlers\Facebook\FacebookGraphClient;
use DataMachineSocials\PublishAuthorization;
use DataMachineSocials\Auth\CredentialResolver;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -121,9 +123,9 @@ public function checkPermission(): bool {

/** @return array|\WP_Error */
public function execute( array $input ) {
$auth = FacebookGraphClient::auth();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Facebook auth provider not available', array( 'status' => 401 ) );
$auth = CredentialResolver::provider( 'facebook', 'Facebook', false, FacebookAuth::class );
if ( is_wp_error( $auth ) ) {
return $auth;
}

$promotion = FacebookAdsPolicy::validate( $input, FacebookAdsPolicy::limits(), $auth->get_ad_account_id(), time() );
Expand Down Expand Up @@ -368,10 +370,7 @@ private function uploadImage( FacebookGraphClient $graph, string $ad_account_id,
}

private static function instagramUserId(): string {
if ( ! class_exists( '\DataMachine\Abilities\AuthAbilities' ) ) {
return '';
}
$ig = ( new \DataMachine\Abilities\AuthAbilities() )->getProvider( 'instagram' );
return ( $ig && method_exists( $ig, 'get_user_id' ) ) ? (string) $ig->get_user_id() : '';
$ig = CredentialResolver::provider( 'instagram', 'Instagram', false );
return ( ! is_wp_error( $ig ) && method_exists( $ig, 'get_user_id' ) ) ? (string) $ig->get_user_id() : '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

use DataMachine\Abilities\PermissionHelper;
use DataMachineSocials\Abilities\AbstractSocialAbility;
use DataMachineSocials\Handlers\Facebook\FacebookAuth;
use DataMachineSocials\Handlers\Facebook\FacebookGraphClient;
use DataMachineSocials\PublishAuthorization;
use DataMachineSocials\Auth\CredentialResolver;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -109,11 +111,11 @@ public function listAdAccounts( array $input = array() ) {
if ( is_wp_error( $res ) ) {
return $res;
}
$auth = FacebookGraphClient::auth();
$auth = CredentialResolver::provider( 'facebook', 'Facebook', false, FacebookAuth::class );
return array(
'success' => true,
'accounts' => $res['data'] ?? array(),
'selected' => $auth ? $auth->get_ad_account_id() : '',
'selected' => is_wp_error( $auth ) ? '' : $auth->get_ad_account_id(),
);
}

Expand Down Expand Up @@ -340,8 +342,8 @@ public function report( array $input = array() ) {
);
}

$auth = FacebookGraphClient::auth();
$account = $auth ? $auth->get_ad_account_id() : '';
$auth = CredentialResolver::provider( 'facebook', 'Facebook', false, FacebookAuth::class );
$account = is_wp_error( $auth ) ? '' : $auth->get_ad_account_id();
if ( '' === $account ) {
return new \WP_Error( 'ads_not_configured', 'No ad account selected.' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use DataMachine\Abilities\PermissionHelper;
use DataMachine\Engine\AI\Actions\PendingActionHelper;
use DataMachineSocials\Abilities\AbstractSocialAbility;
use DataMachineSocials\Handlers\Facebook\FacebookAuth;
use DataMachineSocials\Handlers\Facebook\FacebookGraphClient;
use DataMachineSocials\PublishAuthorization;
use DataMachineSocials\Auth\CredentialResolver;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -90,8 +92,8 @@ public function checkPermission(): bool {

/** @return array|\WP_Error */
public function execute( array $input ) {
$auth = FacebookGraphClient::auth();
$act = $auth ? $auth->get_ad_account_id() : '';
$auth = CredentialResolver::provider( 'facebook', 'Facebook', false, FacebookAuth::class );
$act = is_wp_error( $auth ) ? '' : $auth->get_ad_account_id();
if ( '' === $act ) {
return new \WP_Error( 'ads_not_configured', 'No ad account selected.' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public function checkPermission(): bool {
}

public function execute( array $input ): array|\WP_Error {
$auth = $this->getAuthProvider();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Facebook auth provider not available', array( 'status' => 401 ) );
$auth = $this->resolveProvider( 'facebook', 'Facebook', false, FacebookAuth::class );
if ( is_wp_error( $auth ) ) {
return $auth;
}

// Facebook uses page_access_token for page operations.
Expand All @@ -102,21 +102,6 @@ public function execute( array $input ): array|\WP_Error {
return $this->replyToComment( $page_token, $comment_id, $message );
}

private function getAuthProvider(): ?FacebookAuth {
if ( ! class_exists( '\DataMachine\Abilities\AuthAbilities' ) ) {
return null;
}

$auth = new \DataMachine\Abilities\AuthAbilities();
$provider = $auth->getProvider( 'facebook' );

if ( ! $provider instanceof FacebookAuth ) {
return null;
}

return $provider;
}

private function replyToComment( string $page_token, string $comment_id, string $message ): array|\WP_Error {
$url = FacebookAuth::GRAPH_API_URL . '/' . rawurlencode( $comment_id ) . '/comments';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function checkPermission(): bool {
}

public function execute( array $input ): array|\WP_Error {
$auth = $this->getAuthProvider();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Facebook auth provider not available', array( 'status' => 401 ) );
$auth = $this->resolveProvider( 'facebook', 'Facebook', false, FacebookAuth::class );
if ( is_wp_error( $auth ) ) {
return $auth;
}

$access_token = $auth->get_page_access_token();
Expand Down Expand Up @@ -109,19 +109,4 @@ public function execute( array $input ): array|\WP_Error {

return $this->apiError( $result['error'] ?? 'Failed to delete post' );
}

private function getAuthProvider(): ?FacebookAuth {
if ( ! class_exists( '\DataMachine\Abilities\AuthAbilities' ) ) {
return null;
}

$auth = new \DataMachine\Abilities\AuthAbilities();
$provider = $auth->getProvider( 'facebook' );

if ( ! $provider instanceof FacebookAuth ) {
return null;
}

return $provider;
}
}
Loading
Loading