PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Http/Endpoints/CredentialsEndpoint.php +82 -0 2.0.0 → 2.1.1 View file →
@@ -1,0 +1,82 @@
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\Support\Validation\Validator;
10 +use Metricool\Traits\HasAllowlistControl;
11 +use Metricool\Traits\HasRestAccess;
12 +use Throwable;
13 +
14 +class CredentialsEndpoint implements SingleEndpointInterface
15 +{
16 + use HasRestAccess;
17 + use HasAllowlistControl;
18 +
19 + public const ROUTE = 'credentials';
20 +
21 + public MetricoolApi $metricoolApi;
22 +
23 + public function __construct(MetricoolApi $metricoolApi)
24 + {
25 + $this->metricoolApi = $metricoolApi;
26 + }
27 +
28 + /**
29 + * @inheritDoc
30 + */
31 + public function registerRoute(): string
32 + {
33 + return self::ROUTE;
34 + }
35 +
36 + /**
37 + * @inheritDoc
38 + */
39 + public function enabled(): bool
40 + {
41 + return true;
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 + * Update the password
58 + *
59 + * POST /wp-json/metricool/v1/credentials
60 + * {
61 + * "password": "current-password",
62 + * "newPassword": "new-password"
63 + * }
64 + */
65 + public function callback(\WP_REST_Request $request): \WP_REST_Response
66 + {
67 + $validated = Validator::validate($request->get_params(), [
68 + 'password' => 'required|string',
69 + 'newPassword' => 'required|string|confirm:password',
70 + ]);
71 +
72 + // Update the user password
73 + try {
74 + $this->metricoolApi->userCredentials()
75 + ->updatePassword($validated['password'], $validated['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 +}