| @@ -1,0 +1,37 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Http\Middleware; | |
| 6 | + | |
| 7 | +use Metricool\Http\Metricool\MetricoolClient; | |
| 8 | +use Metricool\Interfaces\MiddlewareInterface; | |
| 9 | +use Metricool\Traits\HasRestAccess; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Checks if the client is authenticated with Metricool API. | |
| 13 | + */ | |
| 14 | +class IsMetricoolAuthenticated implements MiddlewareInterface | |
| 15 | +{ | |
| 16 | + use HasRestAccess; | |
| 17 | + | |
| 18 | + private MetricoolClient $client; | |
| 19 | + | |
| 20 | + public function __construct(MetricoolClient $client) | |
| 21 | + { | |
| 22 | + $this->client = $client; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function handle(\WP_REST_Request $request, callable $next) | |
| 26 | + { | |
| 27 | + if (!$this->client->hasAuthentication()) { | |
| 28 | + return $this->sendHttpErrorResponse( | |
| 29 | + __('Unauthorized. Please log-in into the Metricool plugin.', 'metricool'), | |
| 30 | + null, | |
| 31 | + 401 | |
| 32 | + ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + return $next($request); | |
| 36 | + } | |
| 37 | +} | |