= $limit ) { if ( $on_plugin_page ) { return self::reached_html( $spent, $limit, $can_change, null ); } $stop = self::stop_key( $spent, $limit ); if ( $stop === get_user_meta( get_current_user_id(), self::stop_dismiss_meta_key(), true ) ) { return ''; } return self::reached_html( $spent, $limit, $can_change, $stop ); } if ( ! $on_plugin_page || ! $can_change || $spent * 100 < $limit * self::WARNING_PERCENT ) { return ''; } if ( self::current_month() === get_user_meta( get_current_user_id(), self::dismiss_meta_key(), true ) ) { return ''; } return self::warning_html( $spent, $limit ); } /** * AJAX handler storing the dismissal of the month named in the request. * * Only the current month is stored: a warning left open past the turn of * the month belongs to the month before, and closing it must not hide the * next month's warning. * * @return void */ public static function handle_dismiss(): void { check_ajax_referer( self::DISMISS_ACTION, 'nonce' ); if ( ! current_user_can( 'manage_aipt_settings' ) ) { wp_die( -1 ); } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_ajax_referer() above verified the nonce. $month = isset( $_POST['month'] ) ? sanitize_text_field( wp_unslash( $_POST['month'] ) ) : ''; if ( self::current_month() !== $month ) { wp_die( '0' ); } else { update_user_meta( get_current_user_id(), self::dismiss_meta_key(), $month ); wp_die( '1' ); } } /** * Meta key of the dismissal on the current site. User meta is shared by * every site of a network, and each site has its own limit. * * @return string */ public static function dismiss_meta_key(): string { return self::DISMISS_META_PREFIX . get_current_blog_id(); } /** * AJAX handler storing that the user closed the Dashboard stop notice. * * Every user who sees the notice may close it. The stop named in the * request is stored only while it is the stop in effect: a notice left * open while the month or the limit changed belongs to the stop before, * and closing it must not hide the next one. * * @return void */ public static function handle_stop_dismiss(): void { check_ajax_referer( self::STOP_DISMISS_ACTION, 'nonce' ); if ( ! current_user_can( 'manage_aipt_settings' ) && ! CapabilityManager::hasToolSurfaceAccess() ) { wp_die( -1 ); } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_ajax_referer() above verified the nonce. $stop = isset( $_POST['stop'] ) ? sanitize_text_field( wp_unslash( $_POST['stop'] ) ) : ''; if ( self::current_stop_key() !== $stop ) { wp_die( '0' ); } else { update_user_meta( get_current_user_id(), self::stop_dismiss_meta_key(), $stop ); wp_die( '1' ); } } /** * Meta key of the closed Dashboard stop notice on the current site. * * @return string */ public static function stop_dismiss_meta_key(): string { return self::STOP_DISMISS_META_PREFIX . get_current_blog_id(); } /** * The stop in effect, as stop_key() names it, or null while AI requests * may go. * * @return string|null */ private static function current_stop_key(): ?string { $limit = SpendLimit::limitMicros(); if ( $limit <= 0 ) { return null; } $spent = UsageTracker::currentMonthSpendMicros(); if ( null !== $spent && $spent < $limit ) { return null; } return self::stop_key( $spent, $limit ); } /** * Names one stop: the UTC month, the limit in USD micros and whether the * spend was read. A closed Dashboard notice stays closed for that stop * only. A limit changed back to an earlier value within the month names * the earlier stop again. * * @param int|null $spent This month's estimated spend in USD micros, or null when it could not be read. * @param int $limit The limit in USD micros. * @return string */ private static function stop_key( ?int $spent, int $limit ): string { return self::current_month() . ':' . $limit . ':' . ( null === $spent ? 'unread' : 'reached' ); } /** * The notice once the limit is reached, or when this month's spend could * not be read (the limit then counts as reached). * * @param int|null $spent This month's estimated spend in USD micros, or null when it could not be read. * @param int $limit The limit in USD micros. * @param bool $can_change Whether the user may change the limit. * @param string|null $stop The stop the user can close the notice for, or null for a notice that stays. * @return string */ private static function reached_html( ?int $spent, int $limit, bool $can_change, ?string $stop ): string { if ( null === $spent ) { $text = sprintf( /* translators: %s: the monthly spend limit in US dollars, such as $20.00. */ __( 'AI requests are stopped. This month\'s estimated AI spend could not be read, so your monthly spend limit (%s) counts as reached. Requests go again once it can be read.', 'ai-product-tools' ), self::format_usd( $limit ) ); } else { $text = sprintf( /* translators: 1: this plugin's estimated AI spend this month in US dollars, such as $20.40. 2: the monthly spend limit in US dollars, such as $20.00. */ __( 'AI requests are stopped. This plugin\'s estimated AI spend this month (%1$s) has reached your monthly spend limit (%2$s). Requests go again when the month changes (00:00 UTC on the 1st) or when the limit is raised.', 'ai-product-tools' ), self::format_usd( $spent ), self::format_usd( $limit ) ); } if ( null === $stop ) { $html = '
' . esc_html( $text ) . '
'; if ( $can_change ) { $html .= '' . esc_html__( 'Change the limit', 'ai-product-tools' ) . '
'; } else { $html .= '' . esc_html__( 'A site administrator can raise the limit in Settings > AI connection.', 'ai-product-tools' ) . '
'; } $html .= '' . esc_html( $text ) . '
'; $html .= '' . esc_html__( 'Change the limit', 'ai-product-tools' ) . '
'; $html .= '