44
55namespace FrostyMedia \WpRestCop \RestApi ;
66
7- use FrostyMedia \ WpRestCop \ RestApi \ Rules \ IpRules ;
7+ use Dwnload \ WpSettingsApi \ Api \ Options ;
88use FrostyMedia \WpRestCop \RestApi \Rules \IpRulesInterface ;
99use FrostyMedia \WpRestCop \ServiceProvider ;
10+ use FrostyMedia \WpRestCop \Settings \Settings ;
1011use Psr \Container \ContainerInterface ;
1112use TheFrosty \WpUtilities \Plugin \AbstractContainerProvider ;
1213use TheFrosty \WpUtilities \Plugin \HttpFoundationRequestInterface ;
2223use function esc_html__ ;
2324use function filter_var ;
2425use function get_current_user_id ;
25- use function get_option ;
2626use function is_user_logged_in ;
2727use function rest_convert_error_to_response ;
2828use function sprintf ;
2929use function update_option ;
3030use const FILTER_VALIDATE_BOOLEAN ;
31- use const MINUTE_IN_SECONDS ;
3231
3332/**
3433 * RateLimit class.
@@ -39,10 +38,6 @@ class Officer extends AbstractContainerProvider implements HttpFoundationRequest
3938
4039 use HttpFoundationRequestTrait;
4140
42- public const string OPTION = 'wp_rest_cop_settings ' ;
43- protected const int DEFAULT_INTERVAL = MINUTE_IN_SECONDS ;
44- protected const int DEFAULT_LIMIT = MINUTE_IN_SECONDS ;
45-
4641 /**
4742 * Officer constructor.
4843 * @param ContainerInterface|null $container
@@ -51,21 +46,12 @@ class Officer extends AbstractContainerProvider implements HttpFoundationRequest
5146 */
5247 public function __construct (
5348 ?ContainerInterface $ container = null ,
54- protected int $ interval = self ::DEFAULT_INTERVAL ,
55- protected int $ limit = self ::DEFAULT_LIMIT
49+ protected int $ interval = Settings ::DEFAULT_INTERVAL ,
50+ protected int $ limit = Settings ::DEFAULT_LIMIT
5651 ) {
5752 parent ::__construct ($ container );
5853 }
5954
60- /**
61- * Get the option array.
62- * @return array
63- */
64- public static function getSettings (): array
65- {
66- return get_option (self ::OPTION , []);
67- }
68-
6955 /**
7056 * Add class hooks.
7157 */
@@ -123,12 +109,12 @@ public function getInterval(): int
123109
124110 /**
125111 * Set the number of seconds per interval.
126- * @param int $interval Seconds per interval.
112+ * @param int|string $interval Seconds per interval.
127113 * @return $this
128114 */
129- public function setInterval (int $ interval ): static
115+ public function setInterval (int | string $ interval ): static
130116 {
131- $ this ->interval = $ interval ;
117+ $ this ->interval = ( int ) $ interval ;
132118 return $ this ;
133119 }
134120
@@ -143,12 +129,12 @@ public function getLimit(): int
143129
144130 /**
145131 * Set the number of requests allowed per interval.
146- * @param int $limit Number of requests.
132+ * @param int|string $limit Number of requests.
147133 * @return $this
148134 */
149- public function setLimit (int $ limit ): static
135+ public function setLimit (int | string $ limit ): static
150136 {
151- $ this ->limit = $ limit ;
137+ $ this ->limit = ( int ) $ limit ;
152138 return $ this ;
153139 }
154140
@@ -157,24 +143,19 @@ public function setLimit(int $limit): static
157143 */
158144 protected function initializeSettings (): void
159145 {
160- $ settings = self :: getSettings ( );
146+ $ settings = Options:: getOptions (Settings:: SETTINGS );
161147
162148 if (empty ($ settings )) {
163149 $ settings = [
164- 'interval ' => self ::DEFAULT_INTERVAL ,
165- 'limit ' => self ::DEFAULT_LIMIT ,
166- 'rules ' => [
167- IpRulesInterface::ALLOW => [
168- '127.0.0.1 ' ,
169- '::1 ' ,
170- ],
171- IpRulesInterface::DENY => [],
172- ],
150+ Settings::SETTING_INTERVAL => Settings::DEFAULT_INTERVAL ,
151+ Settings::SETTING_LIMIT => Settings::DEFAULT_LIMIT ,
152+ Settings::SETTING_ALLOW_RULES => ['127.0.0.1 ' , '::1 ' ],
153+ Settings::SETTING_DENY_RULES => [],
173154 ];
174- update_option (self :: OPTION , $ settings );
155+ update_option (Settings:: SETTINGS , $ settings );
175156 }
176157
177- $ this ->setInterval ($ settings [' interval ' ])->setLimit ($ settings [' limit ' ]);
158+ $ this ->setInterval ($ settings [Settings:: SETTING_INTERVAL ])->setLimit ($ settings [Settings:: SETTING_LIMIT ]);
178159 }
179160
180161 /**
@@ -183,12 +164,11 @@ protected function initializeSettings(): void
183164 */
184165 protected function initializeIpRules (): void
185166 {
186- $ settings = self ::getSettings ();
187- /** @var IpRules $rules */
167+ /** @var \FrostyMedia\WpRestCop\RestApi\Rules\IpRules $rules */
188168 $ rules = $ this ->getContainer ()->get (ServiceProvider::IP_RULES );
189169 $ rules
190- ->allow ($ settings [ ' rules ' ][IpRulesInterface:: ALLOW ] ?? '' )
191- ->deny ($ settings [ ' rules ' ][IpRulesInterface:: DENY ] ?? '' );
170+ ->allow (Options:: getOption (Settings:: SETTING_ALLOW_RULES , Settings:: SETTINGS ) )
171+ ->deny (Options:: getOption (Settings:: SETTING_DENY_RULES , Settings:: SETTINGS ) );
192172 }
193173
194174 /**
@@ -199,7 +179,7 @@ protected function initializeIpRules(): void
199179 */
200180 protected function checkIpRules (WP_Error |bool |null $ error ): WP_Error |bool |null
201181 {
202- /** @var IpRules $rules */
182+ /** @var \FrostyMedia\WpRestCop\RestApi\Rules\ IpRules $rules */
203183 $ rules = $ this ->getContainer ()->get (ServiceProvider::IP_RULES );
204184 if (!$ rules ->check ($ this ->getIpAddress ())) {
205185 return $ this ->getForbiddenError ();
@@ -268,11 +248,15 @@ protected function maybeThrottleRequest(
268248 protected function checkRouteIpRules (mixed $ response , WP_REST_Request $ request ): mixed
269249 {
270250 $ ips = [];
271- if (!empty ($ request ->get_attributes ()[' ips ' ])) {
272- $ ips = $ request ->get_attributes ()[' ips ' ];
251+ if (!empty ($ request ->get_attributes ()[IpRulesInterface:: IPS ])) {
252+ $ ips = $ request ->get_attributes ()[IpRulesInterface:: IPS ];
273253 }
274254
275- $ rules = $ ips instanceof IpRulesInterface ? $ ips : new IpRules ($ ips );
255+ /** @var \FrostyMedia\WpRestCop\RestApi\Rules\IpRules $rules */
256+ $ rules = $ this ->getContainer ()->get (ServiceProvider::IP_RULES );
257+ if (!$ ips instanceof IpRulesInterface) {
258+ $ rules ->allow ($ ips [IpRulesInterface::ALLOW ] ?? '' )->deny ($ ips [IpRulesInterface::DENY ] ?? '' );
259+ }
276260
277261 if (!$ rules ->check ($ this ->getIpAddress ())) {
278262 $ response = $ this ->getForbiddenError ();
0 commit comments