| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Http\Endpoints; |
| 6 |
|
| 7 |
use Metricool\Http\Metricool\MetricoolApi; |
| 8 |
use Metricool\Interfaces\SingleEndpointInterface; |
| 9 |
use Metricool\Traits\HasAllowlistControl; |
| 10 |
use Metricool\Traits\HasRestAccess; |
| 11 |
|
| 12 |
class ConnectedBrandsEndpoint implements SingleEndpointInterface |
| 13 |
{ |
| 14 |
use HasRestAccess; |
| 15 |
use HasAllowlistControl; |
| 16 |
|
| 17 |
public const ROUTE = 'connected_brands'; |
| 18 |
|
| 19 |
public MetricoolApi $metricoolApi; |
| 20 |
|
| 21 |
public function __construct(MetricoolApi $metricoolApi) |
| 22 |
{ |
| 23 |
$this->metricoolApi = $metricoolApi; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @inheritDoc |
| 28 |
*/ |
| 29 |
public function enabled(): bool |
| 30 |
{ |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @inheritDoc |
| 36 |
*/ |
| 37 |
public function registerRoute(): string |
| 38 |
{ |
| 39 |
return self::ROUTE; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @inheritDoc |
| 44 |
*/ |
| 45 |
public function registerArguments(): array |
| 46 |
{ |
| 47 |
return [ |
| 48 |
'methods' => \WP_REST_Server::READABLE, |
| 49 |
'callback' => [$this, 'callback'], |
| 50 |
'middleware' => ['metricool:auth'], |
| 51 |
]; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Return the brands related to the user |
| 56 |
* |
| 57 |
* GET /wp-json/metricool/v1/connected_brands |
| 58 |
*/ |
| 59 |
public function callback(\WP_REST_Request $request): \WP_REST_Response |
| 60 |
{ |
| 61 |
try { |
| 62 |
$response = $this->metricoolApi->brands()->all(); |
| 63 |
} catch (\Throwable $e) { |
| 64 |
return $this->sendHttpErrorResponse(__('Failed to load brands data', 'metricool'), $e->getMessage(), $e->getCode()); |
| 65 |
} |
| 66 |
|
| 67 |
return $this->sendHttpResponse($response); |
| 68 |
} |
| 69 |
} |
| 70 |
|