Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9de09e7
Add usage tracking feature for OpenAI.
rahulsprajapati Feb 25, 2026
b1bcb83
Add openai_usage_tracking credentials masking.
rahulsprajapati Feb 25, 2026
8e0c53c
Update refresh interval filter
rahulsprajapati Feb 25, 2026
9fdca45
Block api request for usage hard limit reached.
rahulsprajapati Feb 25, 2026
8c8d39b
Update usage limit notice to be dismissible for soft alerts only.
rahulsprajapati Feb 25, 2026
c36dd16
Remove filter from setting description.
rahulsprajapati Feb 25, 2026
382ce1d
Fix phpstan errors
rahulsprajapati Feb 25, 2026
5cc47ed
Fix js linting errors
rahulsprajapati Feb 25, 2026
dc855d5
Fix vip 5s timeout issue and return 0 for float return function.
rahulsprajapati Feb 25, 2026
c5828a4
Update includes/Classifai/Services/ServicesManager.php
rahulsprajapati Feb 26, 2026
6fd3d94
Update includes/Classifai/Services/ServicesManager.php
rahulsprajapati Feb 26, 2026
61e9d98
Update includes/Classifai/Features/OpenAIUsage.php
rahulsprajapati Feb 26, 2026
899cb1f
Update includes/Classifai/Features/OpenAIUsage.php
rahulsprajapati Feb 26, 2026
7781634
Update includes/Classifai/Features/OpenAIUsage.php
rahulsprajapati Feb 26, 2026
8e17890
Update includes/Classifai/Features/OpenAIUsage.php
rahulsprajapati Feb 26, 2026
fc46262
Apply typo suggestions from code review
rahulsprajapati Feb 26, 2026
32a8832
Fix PR feedbacks.
rahulsprajapati Feb 26, 2026
53a9873
Update dashboard widget markup.
rahulsprajapati Feb 26, 2026
26cc07f
Fix lint error for translators comment.
rahulsprajapati Feb 26, 2026
58355f5
Refactor api usage provider and feature class.
rahulsprajapati Mar 10, 2026
b3dc61b
Fix notification issue.
rahulsprajapati Mar 10, 2026
7021c3a
Refactor api limit reached block logic.
rahulsprajapati Mar 10, 2026
8481066
Fix months usage cached data reset issue when year changes.
rahulsprajapati Mar 10, 2026
1051332
Fix "Spell Checker / Spell-check" flagged issue
rahulsprajapati Mar 10, 2026
369e25d
Fix phpstan issues.
rahulsprajapati Mar 10, 2026
0491d71
Code cleanup
rahulsprajapati Mar 10, 2026
84de627
Fix minor typo and update messages.
rahulsprajapati Mar 11, 2026
bc48019
Apply suggestions from code review
rahulsprajapati Mar 19, 2026
26cd80b
Update soft threshold notice non dismissible.
rahulsprajapati Mar 19, 2026
016cf45
Update force referesh endpoint permission.
rahulsprajapati Mar 19, 2026
456fb71
Unschedule cron job if cron interval is changed.
rahulsprajapati Mar 19, 2026
e492723
Remove redundant condition.
rahulsprajapati Mar 19, 2026
b06872c
Move scope period label+key common code to reusable method.
rahulsprajapati Mar 19, 2026
32ff803
Update hard thresold value greater then soft limit.
rahulsprajapati Mar 19, 2026
a31fc5c
Update page value to string type.
rahulsprajapati Mar 19, 2026
6773148
Sanitization project_id and refresh interval.
rahulsprajapati Mar 19, 2026
741857e
Update provider name to "OpenAI"
rahulsprajapati Mar 19, 2026
0997a35
Add default admin user role for api usage feature.
rahulsprajapati Mar 19, 2026
eba0991
Update text according to feedback.
rahulsprajapati Mar 19, 2026
20736c6
Fix phpstan issues.
rahulsprajapati Mar 19, 2026
d044673
Update widget style.
rahulsprajapati Mar 19, 2026
1ed1caf
Refactor "Updatin..." label logic.
rahulsprajapati Mar 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions includes/Classifai/Admin/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Classifai\Features\DescriptiveTextGenerator;
use Classifai\Features\Classification;
use Classifai\Features\APIUsageTracking;
use Classifai\Providers\UsageTrackingProvider;
use function Classifai\should_use_legacy_settings_panel;

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -51,6 +53,7 @@ public function maybe_render_notices() {
$this->v3_migration_completed_notice();
$this->render_embeddings_notice();
$this->render_legacy_settings_deprecation_notice();
$this->render_api_threshold_notice();
$this->render_notices();
}

Expand Down Expand Up @@ -390,6 +393,84 @@ public function ajax_maybe_dismiss_notice() {
update_user_meta( get_current_user_id(), "classifai_dismissed_{$notice_id}", true );
}

/**
* Render a notice when AI usage exceeds the soft or hard threshold limit.
*/
public function render_api_threshold_notice() {

// Ensure the feature exists.
if ( ! class_exists( 'Classifai\Features\APIUsageTracking' ) ) {
return;
}

$feature_instance = new APIUsageTracking();

// Don't show the notice if the feature is not enabled.
if ( ! $feature_instance->is_feature_enabled() ) {
return;
}

// Don't show the notice if the provider is not UsageTrackingProvider.
$provider = $feature_instance->get_feature_provider_instance();

if ( ! $provider instanceof UsageTrackingProvider ) {
return;
}

$settings = $feature_instance->get_settings( $provider::ID );

if ( empty( $settings['soft_threshold_enabled'] ) && empty( $settings['hard_threshold_enabled'] ) ) {
return;
}

$hard_threshold_reached = get_option( $feature_instance::HARD_LIMIT_REACHED_KEY, false );

if ( $hard_threshold_reached ) {
$scope = isset( $settings['hard_threshold_scope'] ) ? $settings['hard_threshold_scope'] : 'current_month';
} else {
$scope = isset( $settings['soft_threshold_scope'] ) ? $settings['soft_threshold_scope'] : 'current_month';
}

$usage_data = $feature_instance->get_usage_data();
$amount = $feature_instance->get_amount_for_scope( $usage_data, $scope );
$threshold = $hard_threshold_reached ? (float) $settings['hard_threshold_amount'] : (float) $settings['soft_threshold_amount'];

if ( $amount < $threshold ) {
return;
}

$key = 'api_threshold_reached';
$settings_url = admin_url( 'tools.php?page=classifai#/usage_tracking/api_usage_tracking' );
$classes = [
'notice',
];

if ( $hard_threshold_reached ) {
$classes[] = 'notice-error';
$message = sprintf(
/* translators: 1: amount with currency, 2: link to settings */
__( 'AI Features are currently disabled due to exceeding your hard threshold of $%1$s for this period. <a href="%2$s">Re-enable it from the pricing page</a>.', 'classifai' ),
esc_html( number_format_i18n( $threshold, 2 ) . ' ' . strtoupper( $usage_data['currency'] ?? 'USD' ) ),
esc_url( $settings_url )
);
} else {
$classes[] = 'notice-warning';
$message = sprintf(
/* translators: 1: amount with currency, 2: link to settings */
__( 'AI usage has exceeded your soft threshold of $%1$s for this period. <a href="%2$s">Configure alerts</a>.', 'classifai' ),
esc_html( number_format_i18n( $threshold, 2 ) . ' ' . strtoupper( $usage_data['currency'] ?? 'USD' ) ),
esc_url( $settings_url )
);
}
?>
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" data-notice="<?php echo esc_attr( $key ); ?>">
<p>
<?php echo wp_kses_post( $message ); ?>
</p>
</div>
<?php
}

/**
* Render any saved notices to display.
*/
Expand Down
Loading
Loading