50, 'pro' => 1000, 'premium' => 4000, 'agency' => 4000, ); /** * Cooldown period between requests for free-plan users (seconds). * * @var int */ const FREE_COOLDOWN = 0; /** * Transient prefix used for per-request cooldowns. * * @var string */ const COOLDOWN_PREFIX = 'schema_ai_cooldown_'; /** * Check whether a user is allowed to make another request right now. * * @param int $user_id WordPress user ID. * @return bool True if the request is allowed; false otherwise. */ public function check( $user_id ) { $plan = $this->get_plan( $user_id ); // Check monthly limit. if ( $this->remaining( $user_id ) <= 0 ) { return false; } // Check per-request cooldown for free users only. if ( 'free' === $plan && $this->is_in_cooldown( $user_id ) ) { return false; } return true; } /** * Consume one request from the user's monthly quota. * * @param int $user_id WordPress user ID. */ public function consume( $user_id ) { global $wpdb; $table = esc_sql( $wpdb->prefix . 'schema_ai_usage' ); $today = current_time( 'Y-m-d' ); // Upsert: insert a new row for today or increment the existing one. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $existing = $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is sanitized via esc_sql() and not user input. "SELECT id FROM {$table} WHERE user_id = %d AND date = %s", $user_id, $today ) ); if ( $existing ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; write/DDL operation, caching not applicable. $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is sanitized via esc_sql() and not user input. "UPDATE {$table} SET ai_generations = ai_generations + 1 WHERE id = %d", $existing ) ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; write operation (INSERT), caching not applicable. $wpdb->insert( $table, array( 'user_id' => $user_id, 'date' => $today, 'ai_generations' => 1, ), array( '%d', '%s', '%d' ) ); } // Set cooldown for free users. $plan = $this->get_plan( $user_id ); if ( 'free' === $plan ) { set_transient( self::COOLDOWN_PREFIX . $user_id, time(), self::FREE_COOLDOWN ); } } /** * Get the user's monthly usage count. * * @param int $user_id WordPress user ID. * @return int Number of AI generations this month. */ public function get_monthly_usage( $user_id ) { global $wpdb; $table = esc_sql( $wpdb->prefix . 'schema_ai_usage' ); $month_start = gmdate( 'Y-m-01' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $count = (int) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is sanitized via esc_sql() and not user input. "SELECT COALESCE( SUM( ai_generations ), 0 ) FROM {$table} WHERE user_id = %d AND date >= %s", $user_id, $month_start ) ); return $count; } /** * Get the number of requests remaining for this month. * * @param int $user_id WordPress user ID. * @return int Remaining requests. */ public function remaining( $user_id ) { $limit = $this->get_limit( $user_id ); $used = $this->get_monthly_usage( $user_id ); return max( 0, $limit - $used ); } /** * Get the monthly limit for a user based on their plan. * * @param int $user_id WordPress user ID. * @return int Monthly generation limit. */ public function get_limit( $user_id ) { $plan = $this->get_plan( $user_id ); if ( isset( self::MONTHLY_LIMITS[ $plan ] ) ) { return self::MONTHLY_LIMITS[ $plan ]; } return self::MONTHLY_LIMITS['free']; } /** * Get the ISO 8601 datetime when the monthly counter resets. * * @param int $user_id WordPress user ID. * @return string ISO 8601 datetime string for the 1st of next month. */ public function get_reset_time( $user_id ) { return date_i18n( 'c', strtotime( 'first day of next month midnight' ) ); } /** * Retrieve the user's plan from user meta. * * Defaults to 'free' when no plan meta is stored. * * @param int $user_id WordPress user ID. * @return string One of: free, pro, premium, agency. */ private function get_plan( $user_id ) { $plan = get_user_meta( $user_id, 'schema_ai_plan', true ); if ( empty( $plan ) || ! isset( self::MONTHLY_LIMITS[ $plan ] ) ) { return 'free'; } return $plan; } /** * Check whether the user is currently in a per-request cooldown. * * Only applies to free-plan users. * * @param int $user_id WordPress user ID. * @return bool True if the cooldown is active. */ private function is_in_cooldown( $user_id ) { $transient = get_transient( self::COOLDOWN_PREFIX . $user_id ); return false !== $transient; } }