PluginProbe
Metricool – Social media and site statistics / trunk
Metricool – Social media and site statistics vtrunk
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Features / UserSettings / UserSettingsEndpoint.php

UserSettingsEndpoint.php in Metricool – Social media and site statistics trunk, at app/Features/UserSettings/UserSettingsEndpoint.php

107 lines 3.3 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\Features\UserSettings;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 use Metricool\Features\UserSettings\Exceptions\StorageSubmitException;
12 use Metricool\Features\UserSettings\Exceptions\ValidationFailedExceptions;
13 use Metricool\Traits\HasAllowlistControl;
14 use Metricool\Traits\HasRestAccess;
15
16 class UserSettingsEndpoint
17 {
18 use HasRestAccess;
19 use HasAllowlistControl;
20
21 private UserSettingsService $service;
22
23 public function __construct(UserSettingsService $service)
24 {
25 $this->service = $service;
26 }
27
28 public function register(): void
29 {
30 add_filter('metricool_rest_routes', [$this, 'addRoutes']);
31 }
32
33 /**
34 * Add the task routes to the REST API.
35 */
36 public function addRoutes(array $routes): array
37 {
38 $routeParameters = [
39 'methods' => \WP_REST_Server::READABLE . ', ' . \WP_REST_Server::EDITABLE,
40 'callback' => [$this, 'callback'],
41 ];
42
43 $routes['user_settings'] = $routeParameters;
44 $routes['user_settings/(?P<section>[^/]+)'] = $routeParameters;
45
46 return $routes;
47 }
48
49 /**
50 * Handle the REST API request, routing to the appropriate method to the
51 * correct method. Only allows GET, POST, PUT, and PATCH.
52 */
53 public function callback(\WP_REST_Request $request): \WP_REST_Response
54 {
55 switch ($request->get_method()) {
56 case \WP_REST_Server::READABLE:
57 return $this->getUserSettings($request);
58 case 'POST':
59 case 'PUT':
60 case 'PATCH':
61 return $this->storeUserSettings($request);
62 default:
63 return $this->sendHttpResponse([], false, 'Method not allowed', 405);
64 }
65 }
66
67 /**
68 * Retrieve user settings, optionally filtered by section. Sends an error
69 * response if something goes wrong.
70 */
71 protected function getUserSettings(\WP_REST_Request $request): \WP_REST_Response
72 {
73 try {
74 $settings = $this->service->getSettingsResponse(
75 $request->get_param('section')
76 );
77 } catch (\Exception $e) {
78 return $this->sendHttpErrorResponse(__('Failed to retrieve settings', 'metricool'), $e->getMessage());
79 }
80
81 return $this->sendHttpResponse($settings);
82 }
83
84 /**
85 * Store user settings. Sends an error response if something goes wrong.
86 */
87 protected function storeUserSettings(\WP_REST_Request $request): \WP_REST_Response
88 {
89 try {
90 $settings = $request->get_params();
91 $updatedSettings = $this->service->storeSettings($settings, $request);
92 } catch (ValidationFailedExceptions $e) {
93 return $this->sendHttpResponse($e->getErrors(), false, __('Validation failed', 'metricool'), 422);
94 } catch (StorageSubmitException $e) {
95 return $this->sendHttpErrorResponse($e->getMessage(), $e->getErrors(), 502);
96 } catch (\Exception $e) {
97 return $this->sendHttpErrorResponse(__('An unknown error occurred. Failed to update the settings!', 'metricool'), $e->getMessage());
98 }
99
100 $response = new UserSettingsResponse($updatedSettings);
101
102 return $this->sendHttpResponse(
103 $response->parse()->get()
104 );
105 }
106 }
107