Skip to content

Commit 7021c3a

Browse files
Refactor api limit reached block logic.
1 parent b3dc61b commit 7021c3a

File tree

3 files changed

+60
-54
lines changed

3 files changed

+60
-54
lines changed

includes/Classifai/Features/APIUsageTracking.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,48 @@ public function feature_setup() {
156156

157157
add_action( self::FORCE_CRON_HOOK, [ $this, 'run_usage_force_refresh' ] );
158158
add_action( self::CRON_HOOK, [ $this, 'run_usage_refresh' ] );
159+
160+
add_filter( 'classifai_pre_fetch_feature_response', [ $this, 'pre_fetch_feature_response' ], 10, 2 );
159161
}
160162

163+
/**
164+
* Pre-fetch the feature response to check if the feature is allowed to run based on the usage tracking data.
165+
*
166+
* @param mixed $response Response to return.
167+
* @param \Classifai\Providers\Provider $provider_instance provider used.
168+
*
169+
* @return mixed Response to return.
170+
*/
171+
public function pre_fetch_feature_response( $response, $provider_instance ) {
172+
$usage_tracking_provider = $this->get_feature_provider_instance();
173+
$provider_ids = [];
174+
175+
if ( ! empty( $usage_tracking_provider ) && $usage_tracking_provider instanceof UsageTrackingProvider ) {
176+
$provider_ids = $usage_tracking_provider->get_provider_ids();
177+
}
178+
179+
if (
180+
empty( $provider_ids )
181+
|| empty( $provider_instance::ID )
182+
|| ! in_array( $provider_instance::ID, $provider_ids, true )
183+
) {
184+
return $response;
185+
}
186+
187+
$limit_reached = get_option( self::HARD_LIMIT_REACHED_KEY, false );
188+
189+
if ( $limit_reached ) {
190+
return new WP_Error(
191+
'classifai_hard_limit_reached',
192+
__( 'Usage has reached the configured hard limit. Re-enable in ClassifAI / Pricing / Usage settings.', 'classifai' ),
193+
[
194+
'status' => 403,
195+
]
196+
);
197+
}
198+
199+
return $response;
200+
}
161201

162202
/**
163203
* Register any needed endpoints.

includes/Classifai/Features/Feature.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,26 @@ public function run( ...$args ) {
13991399
$provider_id = $settings['provider'];
14001400
$provider_instance = $this->get_feature_provider_instance( $provider_id );
14011401

1402+
/**
1403+
* Filter the response before the feature api endpoint is called.
1404+
*
1405+
* @since x.x.x
1406+
*
1407+
* @hook classifai_pre_fetch_feature_response
1408+
*
1409+
* @param mixed $response Response to return.
1410+
* @param \Classifai\Providers\Provider $provider_instance provider used.
1411+
* @param mixed $args Arguments used by the feature.
1412+
* @param \Classifai\Features\Feature $this Current feature class.
1413+
*
1414+
* @return mixed Response to return.
1415+
*/
1416+
$pre_fetch_response = apply_filters( 'classifai_pre_fetch_feature_response', null, $provider_instance, $args, $this );
1417+
1418+
if ( ! is_null( $pre_fetch_response ) ) {
1419+
return $pre_fetch_response;
1420+
}
1421+
14021422
if ( ! is_callable( [ $provider_instance, 'rest_endpoint_callback' ] ) ) {
14031423
return new WP_Error( 'invalid_route', esc_html__( 'The selected provider does not have a valid callback in place.', 'classifai' ) );
14041424
}

includes/Classifai/Providers/OpenAI/APIRequest.php

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Classifai\Providers\OpenAI;
44

5-
use Classifai\Features\APIUsageTracking;
6-
use Classifai\Providers\OpenAI\UsageTracking;
75
use Classifai\Providers\Provider;
86
use WP_Error;
97

@@ -75,13 +73,6 @@ public function __construct( string $api_key = '', string $feature = '', ?Provid
7573
* @return array|WP_Error
7674
*/
7775
public function get( string $url, array $options = [] ) {
78-
if ( ! $this->is_request_allowed() ) {
79-
return new WP_Error(
80-
'classifai_hard_limit',
81-
__( 'Usage has reached the configured hard limit. Re-enable in ClassifAI / Pricing / Usage settings.', 'classifai' )
82-
);
83-
}
84-
8576
/**
8677
* Filter the URL for the get request.
8778
*
@@ -142,13 +133,6 @@ public function get( string $url, array $options = [] ) {
142133
* @return array|WP_Error
143134
*/
144135
public function post( string $url = '', array $options = [] ) {
145-
if ( ! $this->is_request_allowed() ) {
146-
return new WP_Error(
147-
'classifai_hard_limit',
148-
__( 'Usage has reached the configured hard limit. Re-enable in ClassifAI / Pricing / Usage settings.', 'classifai' )
149-
);
150-
}
151-
152136
$options = wp_parse_args(
153137
$options,
154138
[
@@ -216,13 +200,6 @@ public function post( string $url = '', array $options = [] ) {
216200
* @return array|WP_Error
217201
*/
218202
public function post_form( string $url = '', array $body = [] ) {
219-
if ( ! $this->is_request_allowed() ) {
220-
return new WP_Error(
221-
'classifai_hard_limit',
222-
__( 'Usage has reached the configured hard limit. Re-enable in ClassifAI / Pricing / Usage settings.', 'classifai' )
223-
);
224-
}
225-
226203
/**
227204
* Filter the URL for the post form request.
228205
*
@@ -391,35 +368,4 @@ public function get_api_key() {
391368

392369
return $this->api_key ?? '';
393370
}
394-
395-
/**
396-
* Whether API requests are allowed (not blocked by hard usage limit).
397-
*
398-
* @return bool
399-
*/
400-
private function is_request_allowed(): bool {
401-
if ( empty( $this->provider::ID ) || ! in_array( $this->provider::ID, UsageTracking::get_provider_ids(), true ) ) {
402-
return true;
403-
}
404-
405-
/**
406-
* Filter whether API requests are allowed.
407-
* When the hard usage limit is reached and not overridden, this is set to false.
408-
*
409-
* @since x.x.x
410-
* @hook classifai_can_make_request
411-
* @param bool $allowed Whether the request is allowed. Default true.
412-
* @param string $feature The feature name.
413-
* @param Provider $provider The provider instance.
414-
*
415-
* @return bool
416-
*/
417-
$allowed = (bool) apply_filters( 'classifai_can_make_request', true, $this->feature, $this->provider );
418-
419-
if ( ! $allowed ) {
420-
return false;
421-
}
422-
423-
return ! (bool) get_option( APIUsageTracking::HARD_LIMIT_REACHED_KEY, false );
424-
}
425371
}

0 commit comments

Comments
 (0)