PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Http / Endpoints / CredentialsEndpoint.php

CredentialsEndpoint.php in Metricool – Social media and site statistics 2.0.2, at app/Http/Endpoints/CredentialsEndpoint.php

83 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 use Throwable;
12
13 class CredentialsEndpoint implements SingleEndpointInterface
14 {
15 use HasRestAccess;
16 use HasAllowlistControl;
17
18 public const ROUTE = 'credentials';
19
20 public MetricoolApi $metricoolApi;
21
22 public function __construct(MetricoolApi $metricoolApi)
23 {
24 $this->metricoolApi = $metricoolApi;
25 }
26
27 /**
28 * @inheritDoc
29 */
30 public function registerRoute(): string
31 {
32 return self::ROUTE;
33 }
34
35 /**
36 * Only enable this endpoint if the user has access to the admin area and
37 * the user has saved a user token.
38 */
39 public function enabled(): bool
40 {
41 return $this->adminAccessAllowed();
42 }
43
44 /**
45 * @inheritDoc
46 */
47 public function registerArguments(): array
48 {
49 return [
50 'methods' => \WP_REST_Server::EDITABLE,
51 'callback' => [$this, 'callback'],
52 'middleware' => ['metricool:auth'],
53 ];
54 }
55
56 /**
57 * Method will dynamically request the requested statistic. If the metric
58 * is filterable and filters are provided, it will apply them before
59 * retrieving the data.
60 * @example /wp-json/metricool/v1/analytics
61 */
62 public function callback(\WP_REST_Request $request): \WP_REST_Response
63 {
64 $password = (string) $request->get_param('password');
65 $newPassword = (string) $request->get_param('newPassword');
66
67 // Validation
68 if (empty($password) || empty($newPassword)) {
69 return $this->sendHttpErrorResponse(__('Validation failed', 'metricool'));
70 }
71
72 // Update the user password
73 try {
74 $this->metricoolApi->userCredentials()
75 ->updatePassword($password, $newPassword);
76 } catch (Throwable $e) {
77 return $this->sendHttpErrorResponse(__('Something went wrong.', 'metricool'), $e->getMessage(), $e->getCode());
78 }
79
80 return $this->sendHttpResponse(['success' => true]);
81 }
82 }
83