45 || preg_match( '/[^A-Za-z0-9.:_\-\/]/', $value ) ) { $value = md5( $value ); } return $metric . ':' . $value; } /** * Adds one to each key, in the current minute. * * @param string[] $keys Counter keys. * @param int|null $now Unix time; now when null. */ public static function hit( array $keys, $now = null ) { $keys = array_values( array_unique( array_filter( $keys ) ) ); if ( empty( $keys ) ) { return; } $now = null === $now ? time() : (int) $now; $minute = intdiv( $now, 60 ); if ( self::uses_cache() ) { $hour = intdiv( $now, 3600 ); foreach ( $keys as $key ) { self::cache_incr( 'm:' . $key . ':' . $minute, 2 * HOUR_IN_SECONDS ); self::cache_incr( 'h:' . $key . ':' . $hour, 26 * HOUR_IN_SECONDS ); } return; } global $wpdb; $table = Schema::table( 'counters' ); // One short statement per counter (a checkout attempt touches four or five). foreach ( $keys as $key ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table; a counter cannot be cached. $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (ckey,bucket,n) VALUES (%s,%d,1) ON DUPLICATE KEY UPDATE n = n + 1', $table, $key, $minute ) ); } } /** * Adds one to a cache counter, creating it when needed. * * @param string $key Cache key. * @param int $ttl Seconds to keep it. */ private static function cache_incr( $key, $ttl ) { if ( false === wp_cache_incr( $key, 1, self::GROUP ) ) { if ( ! wp_cache_add( $key, 1, self::GROUP, $ttl ) ) { wp_cache_incr( $key, 1, self::GROUP ); } } } /** * Totals over time windows. * * @param array $wanted Name => array( counter key, window in seconds ). * @param int|null $now Unix time; now when null. * @return array Name => total. */ public static function sums( array $wanted, $now = null ) { $now = null === $now ? time() : (int) $now; $out = array(); $keys = array(); $far = 0; foreach ( $wanted as $name => $spec ) { $out[ $name ] = 0; if ( empty( $spec[0] ) ) { unset( $wanted[ $name ] ); continue; } $keys[ $spec[0] ] = true; $far = max( $far, (int) $spec[1] ); } if ( empty( $keys ) ) { return $out; } if ( self::uses_cache() ) { return self::cache_sums( $wanted, $out, $now ); } global $wpdb; $table = Schema::table( 'counters' ); $minute = intdiv( $now, 60 ); $oldest = $minute - (int) ceil( $far / 60 ); $key_list = array_keys( $keys ); $key_places = implode( ',', array_fill( 0, count( $key_list ), '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- One placeholder per key is built above. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ckey, bucket, n FROM %i WHERE ckey IN ({$key_places}) AND bucket > %d", array_merge( array( $table ), $key_list, array( $oldest ) ) ), ARRAY_A ); if ( empty( $rows ) ) { return $out; } foreach ( $wanted as $name => $spec ) { $from = $minute - (int) ceil( (int) $spec[1] / 60 ); foreach ( $rows as $row ) { if ( $row['ckey'] === $spec[0] && (int) $row['bucket'] > $from ) { $out[ $name ] += (int) $row['n']; } } } return $out; } /** * Totals from the object cache: minute buckets for windows up to an hour, hour buckets beyond. * * @param array $wanted Name => array( key, window ). * @param array $out Name => 0. * @param int $now Unix time. * @return array */ private static function cache_sums( array $wanted, array $out, $now ) { $minute = intdiv( $now, 60 ); $hour = intdiv( $now, 3600 ); $fetch = array(); $plan = array(); foreach ( $wanted as $name => $spec ) { $plan[ $name ] = array(); if ( (int) $spec[1] <= 3600 ) { $count = (int) ceil( (int) $spec[1] / 60 ); for ( $i = 0; $i < $count; $i++ ) { $plan[ $name ][] = 'm:' . $spec[0] . ':' . ( $minute - $i ); } } else { $count = (int) ceil( (int) $spec[1] / 3600 ); for ( $i = 0; $i <= $count; $i++ ) { $plan[ $name ][] = 'h:' . $spec[0] . ':' . ( $hour - $i ); } } foreach ( $plan[ $name ] as $cache_key ) { $fetch[ $cache_key ] = true; } } $found = wp_cache_get_multiple( array_keys( $fetch ), self::GROUP ); foreach ( $plan as $name => $cache_keys ) { foreach ( $cache_keys as $cache_key ) { if ( ! empty( $found[ $cache_key ] ) ) { $out[ $name ] += (int) $found[ $cache_key ]; } } } return $out; } /** * One total. * * @param string $key Counter key. * @param int $window Seconds. * @param int|null $now Unix time; now when null. * @return int */ public static function sum( $key, $window, $now = null ) { $sums = self::sums( array( 'x' => array( $key, (int) $window ) ), $now ); return $sums['x']; } /** * Removes buckets older than the longest window (24 hours). Runs hourly. */ public static function prune() { global $wpdb; $table = Schema::table( 'counters' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE bucket < %d', $table, intdiv( time(), 60 ) - 1500 ) ); } /** * Empties every counter (reset command and tests). */ public static function clear() { global $wpdb; $table = Schema::table( 'counters' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i', $table ) ); if ( function_exists( 'wp_cache_flush_group' ) && function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) { wp_cache_flush_group( self::GROUP ); } } }