PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / rest / RestApiResponsePresenter.php

RestApiResponsePresenter.php in 404 Solution trunk, at includes/rest/RestApiResponsePresenter.php

176 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Shapes REST API responses for the abj404/v1 namespace.
9 */
10 final class ABJ_404_Solution_RestApiResponsePresenter {
11
12 /**
13 * @param array<int|string, mixed> $rows
14 * @return \WP_REST_Response
15 */
16 public function paginated(array $rows, int $total, int $page, int $perPage): \WP_REST_Response {
17 return new \WP_REST_Response(array(
18 'items' => array_values($rows),
19 'total' => intval($total),
20 'page' => $page,
21 'per_page' => $perPage,
22 'total_pages' => max(1, (int)ceil(intval($total) / $perPage)),
23 ), 200);
24 }
25
26 /**
27 * @param int|string $status
28 * @return \WP_REST_Response
29 */
30 public function redirectCreated(int $id, string $from, string $to, int $code, $status): \WP_REST_Response {
31 return new \WP_REST_Response(array(
32 'id' => $id,
33 'from' => $from,
34 'to' => $to,
35 'code' => $code,
36 'status' => (string)$status,
37 ), 201);
38 }
39
40 /**
41 * @param int|string $status
42 * @return \WP_REST_Response
43 */
44 public function redirectUpdated(int $id, string $from, string $to, int $code, $status): \WP_REST_Response {
45 return new \WP_REST_Response(array(
46 'id' => $id,
47 'from' => $from,
48 'to' => $to,
49 'code' => $code,
50 'status' => (string)$status,
51 ), 200);
52 }
53
54 public function redirectTrashed(int $id): \WP_REST_Response {
55 return new \WP_REST_Response(array('trashed' => true, 'id' => $id), 200);
56 }
57
58 public function capturedPromoted(int $id, string $from, string $to, int $code): \WP_REST_Response {
59 return new \WP_REST_Response(array(
60 'id' => $id,
61 'from' => $from,
62 'to' => $to,
63 'code' => $code,
64 ), 200);
65 }
66
67 /**
68 * @param array<string, mixed> $data
69 * @return \WP_REST_Response
70 */
71 public function stats(array $data): \WP_REST_Response {
72 $redirects = isset($data['redirects']) && is_array($data['redirects']) ? $data['redirects'] : array();
73 $captured = isset($data['captured']) && is_array($data['captured']) ? $data['captured'] : array();
74
75 return new \WP_REST_Response(array(
76 'redirects' => array(
77 'auto_301' => $this->scalarInt($redirects, 'auto301'),
78 'auto_302' => $this->scalarInt($redirects, 'auto302'),
79 'manual_301' => $this->scalarInt($redirects, 'manual301'),
80 'manual_302' => $this->scalarInt($redirects, 'manual302'),
81 'trashed' => $this->scalarInt($redirects, 'trashed'),
82 ),
83 'captured' => array(
84 'captured' => $this->scalarInt($captured, 'captured'),
85 'ignored' => $this->scalarInt($captured, 'ignored'),
86 'trashed' => $this->scalarInt($captured, 'trashed'),
87 ),
88 ), 200);
89 }
90
91 /**
92 * @param array<string, mixed> $source
93 */
94 private function scalarInt(array $source, string $key): int {
95 $value = $source[$key] ?? 0;
96 return intval(is_scalar($value) ? $value : 0);
97 }
98
99 /**
100 * @param array<string, mixed> $redirect
101 */
102 public function testStoredMatch(string $normalizedUrl, array $redirect): \WP_REST_Response {
103 $finalDest = isset($redirect['final_dest']) && is_string($redirect['final_dest']) ? $redirect['final_dest'] : '';
104
105 return new \WP_REST_Response(array(
106 'matched' => true,
107 'type' => 'stored',
108 'redirect_id' => intval(is_scalar($redirect['id'] ?? 0) ? ($redirect['id'] ?? 0) : 0),
109 'from' => $normalizedUrl,
110 'to' => $finalDest,
111 'code' => intval(is_scalar($redirect['code'] ?? 301) ? ($redirect['code'] ?? 301) : 301),
112 'status' => intval(is_scalar($redirect['status'] ?? 0) ? ($redirect['status'] ?? 0) : 0),
113 ), 200);
114 }
115
116 /**
117 * @param array<string, mixed> $redirect
118 */
119 public function testRegexMatch(string $normalizedUrl, array $redirect): \WP_REST_Response {
120 $id = is_scalar($redirect['id'] ?? null) ? intval($redirect['id']) : 0;
121 $dest = is_scalar($redirect['final_dest'] ?? null) ? (string)$redirect['final_dest'] : '';
122 $code = is_scalar($redirect['code'] ?? null) ? intval($redirect['code']) : 301;
123
124 return new \WP_REST_Response(array(
125 'matched' => true,
126 'type' => 'regex',
127 'redirect_id' => $id,
128 'from' => $normalizedUrl,
129 'to' => $dest,
130 'code' => $code,
131 ), 200);
132 }
133
134 public function testNoMatch(string $normalizedUrl): \WP_REST_Response {
135 return new \WP_REST_Response(array(
136 'matched' => false,
137 'url' => $normalizedUrl,
138 ), 200);
139 }
140
141 public function createFailed(): \WP_Error {
142 return new \WP_Error('create_failed', __('Failed to create redirect.', '404-solution'), array('status' => 500));
143 }
144
145 public function updateFailed(string $errorCode): \WP_Error {
146 return new \WP_Error('update_failed', $this->messageForRedirectUpdateError($errorCode), array('status' => 500));
147 }
148
149 public function trashFailed(string $error): \WP_Error {
150 return new \WP_Error('trash_failed', $error, array('status' => 500));
151 }
152
153 public function capturedNotFound(): \WP_Error {
154 return new \WP_Error('not_found', __('Captured 404 not found.', '404-solution'), array('status' => 404));
155 }
156
157 public function capturedBadRecord(): \WP_Error {
158 return new \WP_Error('bad_record', __('The captured 404 record has no URL.', '404-solution'), array('status' => 500));
159 }
160
161 public function statsError(\Throwable $e): \WP_Error {
162 return new \WP_Error('stats_error', $e->getMessage(), array('status' => 500));
163 }
164
165 private function messageForRedirectUpdateError(string $errorCode): string {
166 if ($errorCode === 'bad_update_request') {
167 return __('Error: Bad data passed for update redirect request.', '404-solution');
168 }
169
170 return sprintf(
171 __('Error: Unable to update redirect data. Repository result: %s', '404-solution'),
172 esc_html($errorCode)
173 );
174 }
175 }
176