>> */ private $memo = array(); /** * Table name with the site prefix. * * @return string */ public static function table() { global $wpdb; return $wpdb->prefix . 'eucheck_price_log'; } /** * Records the product's price as of $now, unless it is unchanged. * * @param int $product_id Product or variation ID. * @param int $parent_id Parent product ID, 0 for none. * @param string|null $regular Regular price. * @param string|null $sale Sale price, null when there is none. * @param string $price Price in force. * @param string $currency Currency code. * @param int $now Unix time. * @return bool Whether a row was written. */ public function write( $product_id, $parent_id, $regular, $sale, $price, $currency, $now ) { global $wpdb; $table = self::table(); $open = $this->open_row( $product_id ); $values = array( 'regular_price' => self::decimal( $regular ), 'sale_price' => self::decimal( $sale ), 'price' => self::decimal( $price ), 'currency' => substr( (string) $currency, 0, 3 ), ); if ( $open && self::same( $open, $values ) ) { return false; } $time = gmdate( 'Y-m-d H:i:s', $now ); if ( $open && $open['valid_from'] === $time ) { // Two changes within one second: keep the last one only. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin table; the cache is cleared below. $wpdb->update( $table, $values, array( 'id' => (int) $open['id'] ) ); } else { $this->close( $product_id, $now, false ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Plugin table. $wpdb->insert( $table, array_merge( array( 'product_id' => (int) $product_id, 'parent_id' => (int) $parent_id, 'valid_from' => $time, ), $values ) ); } $this->forget( $product_id ); return true; } /** * Ends the row in force: the product stopped being sold at a price. * * @param int $product_id Product or variation ID. * @param int $now Unix time. * @param bool $forget Whether to clear the cache. */ public function close( $product_id, $now, $forget = true ) { global $wpdb; $table = self::table(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin table; the cache is cleared below. $wpdb->query( $wpdb->prepare( 'UPDATE %i SET valid_to = %s WHERE product_id = %d AND valid_to IS NULL', $table, gmdate( 'Y-m-d H:i:s', $now ), $product_id ) ); if ( $forget ) { $this->forget( $product_id ); } } /** * The shop's base currency, the one the log is kept in. Read from the * option rather than get_woocommerce_currency(), which a currency * switcher changes to the visitor's currency while the stored prices * stay in the base one. * * @return string */ public static function currency() { return (string) get_option( 'woocommerce_currency' ); } /** * Rows for one product, oldest first, in the shape PriorPrice reads. * * @param int $product_id Product or variation ID. * @return array> */ public function rows( $product_id ) { $product_id = (int) $product_id; if ( isset( $this->memo[ $product_id ] ) ) { return $this->memo[ $product_id ]; } $raw = wp_cache_get( 'rows_' . $product_id, self::CACHE_GROUP ); if ( ! is_array( $raw ) ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Plugin table, cached right below. $raw = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT regular_price, sale_price, price, currency, valid_from, valid_to FROM %i WHERE product_id = %d ORDER BY valid_from DESC, id DESC LIMIT %d', self::table(), $product_id, self::MAX_ROWS ), ARRAY_A ); wp_cache_set( 'rows_' . $product_id, $raw, self::CACHE_GROUP, HOUR_IN_SECONDS ); } // Rows in another currency are not comparable: a shop that changed // its base currency starts over. $rows = array(); $currency = self::currency(); foreach ( array_reverse( $raw ) as $row ) { if ( '' !== $row['currency'] && $row['currency'] !== $currency ) { continue; } $rows[] = array( 'regular' => $row['regular_price'], 'sale' => $row['sale_price'], 'price' => $row['price'], 'from' => self::timestamp( $row['valid_from'] ), 'to' => null === $row['valid_to'] ? null : self::timestamp( $row['valid_to'] ), ); } $this->memo[ $product_id ] = $rows; return $rows; } /** * Reads the rows of several products in one query into the cache, for * product lists and variable products. * * @param array $product_ids Product or variation IDs. */ public function prime( array $product_ids ) { $missing = array(); foreach ( array_unique( array_map( 'intval', $product_ids ) ) as $product_id ) { if ( $product_id > 0 && ! isset( $this->memo[ $product_id ] ) && ! is_array( wp_cache_get( 'rows_' . $product_id, self::CACHE_GROUP ) ) ) { $missing[] = $product_id; } } if ( ! $missing ) { return; } global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Plugin table, cached right below. $found = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT product_id, regular_price, sale_price, price, currency, valid_from, valid_to FROM %i WHERE product_id IN (' . implode( ',', array_fill( 0, count( $missing ), '%d' ) ) . ') ORDER BY product_id, valid_from DESC, id DESC', array_merge( array( self::table() ), $missing ) ), ARRAY_A ); $grouped = array_fill_keys( $missing, array() ); foreach ( $found as $row ) { $product_id = (int) $row['product_id']; unset( $row['product_id'] ); if ( count( $grouped[ $product_id ] ) < self::MAX_ROWS ) { $grouped[ $product_id ][] = $row; } } foreach ( $grouped as $product_id => $raw ) { wp_cache_set( 'rows_' . $product_id, $raw, self::CACHE_GROUP, HOUR_IN_SECONDS ); } } /** * When the log started, as Unix time, or null when it is empty. * * @return int|null */ public function started() { $started = get_option( 'eucheck_log_started' ); return $started ? (int) $started : null; } /** * Number of products and variations with a price in force. * * @return int */ public function tracked() { $count = wp_cache_get( 'tracked', self::CACHE_GROUP ); if ( false === $count ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Plugin table, cached right below. $count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE valid_to IS NULL', self::table() ) ); wp_cache_set( 'tracked', $count, self::CACHE_GROUP, 10 * MINUTE_IN_SECONDS ); } return (int) $count; } /** * Clears the cached rows of one product. * * @param int $product_id Product or variation ID. */ public function forget( $product_id ) { unset( $this->memo[ (int) $product_id ] ); wp_cache_delete( 'rows_' . (int) $product_id, self::CACHE_GROUP ); wp_cache_delete( 'tracked', self::CACHE_GROUP ); } /** * The row in force for one product. * * @param int $product_id Product or variation ID. * @return array|null */ private function open_row( $product_id ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin table; a write must see the stored row, not a cached one. $row = $wpdb->get_row( $wpdb->prepare( 'SELECT id, regular_price, sale_price, price, currency, valid_from FROM %i WHERE product_id = %d AND valid_to IS NULL ORDER BY id DESC LIMIT 1', self::table(), $product_id ), ARRAY_A ); return $row ? $row : null; } /** * Whether a stored row holds the same prices. * * @param array $row Stored row. * @param array $values New values. * @return bool */ private static function same( array $row, array $values ) { foreach ( $values as $key => $value ) { $stored = $row[ $key ]; if ( null === $value || null === $stored ) { if ( $value !== $stored ) { return false; } continue; } if ( 'currency' === $key ? $value !== $stored : 0 !== self::compare( $value, $stored ) ) { return false; } } return true; } /** * Compares two decimal strings to four places. * * @param string $a First value. * @param string $b Second value. * @return int */ private static function compare( $a, $b ) { $a = round( (float) $a, 4 ); $b = round( (float) $b, 4 ); return $a < $b ? -1 : ( $a > $b ? 1 : 0 ); } /** * A price as a four place decimal string, or null. * * @param mixed $value Price. * @return string|null */ private static function decimal( $value ) { if ( null === $value || '' === $value || ! is_numeric( $value ) ) { return null; } return number_format( (float) $value, 4, '.', '' ); } /** * A UTC MySQL datetime as Unix time. * * @param string $datetime UTC datetime. * @return int */ private static function timestamp( $datetime ) { $time = strtotime( $datetime . ' UTC' ); return false === $time ? 0 : $time; } }