= $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 = '
'; } else { $html = '
'; } $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 .= '
'; if ( null !== $stop ) { $html .= self::get_dismiss_script( 'aipt-spend-limit-notice', self::STOP_DISMISS_ACTION, 'stop' ); } return $html; } /** * The dismissible warning from WARNING_PERCENT of the limit. * * @param int $spent This month's estimated spend in USD micros. * @param int $limit The limit in USD micros. * @return string */ private static function warning_html( int $spent, int $limit ): string { $nonce = esc_attr( wp_create_nonce( self::DISMISS_ACTION ) ); $month = esc_attr( self::current_month() ); $text = sprintf( /* translators: 1: this plugin's estimated AI spend this month in US dollars, such as $16.20. 2: the share of the limit, such as 80. 3: the monthly spend limit in US dollars, such as $20.00. */ __( 'This plugin\'s estimated AI spend this month (%1$s) has reached %2$d%% of your monthly spend limit (%3$s). At the limit, AI requests stop until the month changes or the limit is raised.', 'ai-product-tools' ), self::format_usd( $spent ), self::WARNING_PERCENT, self::format_usd( $limit ) ); $html = '
'; $html .= '

' . esc_html( $text ) . '

'; $html .= '

' . esc_html__( 'Change the limit', 'ai-product-tools' ) . '

'; $html .= '
'; $html .= self::get_dismiss_script( 'aipt-spend-limit-warning', self::DISMISS_ACTION, 'month' ); return $html; } /** * An amount in US dollars with cents, such as $20.00. * * @param int $micros USD micros. * @return string */ private static function format_usd( int $micros ): string { return '$' . number_format_i18n( $micros / 1000000, 2 ); } /** * The month the limit counts in, as UsageTracker keys it (UTC). * * @return string */ private static function current_month(): string { return gmdate( 'Y_m' ); } /** * Settings > AI connection, where the limit is set. * * @return string */ private static function settings_url(): string { return admin_url( 'admin.php?page=aipt-settings&tab=api' ); } /** * Whether the current admin screen belongs to this plugin. Mirrors * {@see SchemaUpgradeNotice::is_plugin_page()}. * * @return bool */ private static function is_plugin_page(): bool { if ( ! is_admin() ) { return false; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Page slug read only, no state change. $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; if ( '' === $page ) { return false; } if ( 'ai-product-tools' === $page ) { return true; } return 0 === strpos( $page, 'aipt-' ); } /** * Whether the current admin screen is this site's WordPress Dashboard. * Mirrors {@see NewProductsNotice::is_products_list()}. * * @return bool */ private static function is_dashboard(): bool { if ( ! function_exists( 'get_current_screen' ) ) { return false; } $screen = get_current_screen(); return is_object( $screen ) && self::DASHBOARD_SCREEN_ID === $screen->id; } /** * Inline script wiring the core dismiss button of a notice to its AJAX * handler, which gets the notice's nonce and one of its data values. * Mirrors {@see SchemaUpgradeNotice::get_dismiss_script()}. * * @param string $class_name The notice's class. * @param string $action The AJAX action. * @param string $field The data attribute (without "data-") the handler reads, posted under the same name. * @return string */ private static function get_dismiss_script( string $class_name, string $action, string $field ): string { return ''; } }