get_stats();
if ( empty( $stats['success'] ) ) {
return [];
}
if ( ! UpgradeHelper::is_usage_warning( $stats ) ) {
return [];
}
// Free-plan users without a payment method only. Paid users and free
// users with a card auto-charge past the free limit, so the banner does
// not apply; unknown card state stays silent (safe-by-default).
if ( ! UpgradeHelper::requires_payment_method( $stats ) ) {
return [];
}
// Respect dismissal only while merely nearing the limit. Once exhausted
// the banner is sticky, so a prior 80% dismissal must not hide it.
if ( ! UpgradeHelper::is_usage_exhausted( $stats ) && UpgradeHelper::is_banner_dismissed( $stats ) ) {
return [];
}
return $stats;
}
/**
* Build a concrete usage warning or an explicit exhausted-plan message.
*
* Only reached for free-plan users without a payment method. Both at the
* warning threshold and once exhausted, the message links to plans on the
* billing page. Counts reflect the latest cached subscription statistics.
*
* @since 1.1.0
* @since 1.3.0 Simplified to the free no-card flow: add-payment-method prompt at both thresholds.
* @since 1.8.0 Show concrete usage counts and explain when new submissions are not checked.
*
* @param array $stats Subscription stats.
*
* @return string HTML message.
*/
private static function get_banner_message( array $stats ): string {
$billing_url = esc_url( UpgradeHelper::get_billing_url( 'top_banner' ) );
$link_open = '';
$link_close = '';
if ( UpgradeHelper::is_usage_exhausted( $stats ) ) {
return sprintf(
/* translators: 1: opening anchor tag, 2: closing anchor tag. */
__( 'ActiveLayer free plan limit reached. New submissions aren\'t being checked for spam. %1$sSee plans%2$s to keep protecting your site.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ),
$link_open,
$link_close
);
}
$usage = sprintf(
/* translators: 1: percentage used, 2: total free checks, 3: checks remaining. */
__( 'You\'ve used %1$s%% of your %2$s free checks. %3$s left.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ),
number_format_i18n( (float) ( $stats['usage_percentage'] ?? 0 ) ),
number_format_i18n( (int) ( $stats['requests_limit'] ?? 0 ) ),
number_format_i18n( max( 0, (int) ( $stats['requests_remaining'] ?? 0 ) ) )
);
return $usage . ' ' . sprintf(
/* translators: 1: opening anchor tag, 2: closing anchor tag. */
__( '%1$sSee plans%2$s to keep protecting your site.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ),
$link_open,
$link_close
);
}
/**
* AJAX handler to dismiss the usage limit banner.
*
* @since 1.1.0
*/
public static function ajax_dismiss_banner(): void {
check_ajax_referer( 'activelayer_admin', 'nonce' );
if ( ! current_user_can( 'manage_activelayer' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Permission denied.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) ], 403 );
}
UpgradeHelper::dismiss_banner();
wp_send_json_success();
}
}