*/ private $dirty = array(); /** * Constructor. * * @param PriceLog $log Price log. */ public function __construct( PriceLog $log ) { $this->log = $log; } /** * Adds the hooks. */ public function register() { add_action( 'woocommerce_after_product_object_save', array( $this, 'on_save' ), 20 ); add_action( 'added_post_meta', array( $this, 'on_meta' ), 10, 3 ); add_action( 'updated_post_meta', array( $this, 'on_meta' ), 10, 3 ); add_action( 'deleted_post_meta', array( $this, 'on_meta' ), 10, 3 ); add_action( 'transition_post_status', array( $this, 'on_status' ), 10, 3 ); add_action( 'shutdown', array( $this, 'flush' ) ); add_action( self::SNAPSHOT_HOOK, array( $this, 'snapshot' ) ); } /** * A product was saved through WooCommerce. * * @param mixed $product Saved product. */ public function on_save( $product ) { if ( ! $product instanceof \WC_Product ) { return; } if ( $product->is_type( 'variable' ) ) { // A variable product's own price is the range of its variations. // They are saved on their own, except when only the parent's // status changed, which on_status() catches. return; } $this->record( $product ); unset( $this->dirty[ $product->get_id() ] ); /** This action is documented in src/Recorder.php */ do_action( 'eucheck_recorded', (int) $product->get_id() ); } /** * Price meta changed, maybe by a plugin that bypasses the product object. * * @param mixed $meta_id Meta ID or IDs. * @param int $object_id Post ID. * @param mixed $meta_key Meta key. */ public function on_meta( $meta_id, $object_id, $meta_key ) { if ( ! is_string( $meta_key ) || ! in_array( $meta_key, self::PRICE_KEYS, true ) ) { return; } $type = get_post_type( (int) $object_id ); if ( 'product' === $type || 'product_variation' === $type ) { $this->dirty[ (int) $object_id ] = true; } } /** * A product was published or stopped being public. * * @param string $new_status New status. * @param string $old_status Old status. * @param \WP_Post $post Post. */ public function on_status( $new_status, $old_status, $post ) { if ( $new_status === $old_status || ! $post instanceof \WP_Post ) { return; } if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; } if ( 'product' === $post->post_type || 'product_variation' === $post->post_type ) { $this->dirty[ (int) $post->ID ] = true; } } /** * Records the products marked during the request. */ public function flush() { $ids = array_keys( $this->dirty ); $this->dirty = array(); foreach ( $ids as $id ) { $product = wc_get_product( $id ); if ( ! $product ) { $this->log->close( $id, time() ); continue; } if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); if ( $child ) { $this->record( $child ); /** This action is documented below. */ do_action( 'eucheck_recorded', (int) $child_id ); } } continue; } $this->record( $product ); /** * Fires after a product's or variation's price, sale or status * changed and the log is up to date with it. * * @param int $id Product or variation ID. */ do_action( 'eucheck_recorded', (int) $id ); } } /** * Writes one product's current price to the log. * * @param \WC_Product $product Product or variation. * @param int|null $now Unix time, now by default. * @return bool Whether a row was written. */ public function record( \WC_Product $product, $now = null ) { $id = $product->get_id(); if ( ! $id || $product->is_type( array( 'variable', 'grouped' ) ) ) { return false; } $now = null === $now ? time() : (int) $now; $stored = self::stored( $product ); $price = $stored['_price']; if ( ! self::is_live( $product ) || '' === $price || ! is_numeric( $price ) ) { $this->log->close( $id, $now ); return false; } $sale = $stored['_sale_price']; $regular = $stored['_regular_price']; $written = $this->log->write( $id, $product->get_parent_id(), '' === $regular ? null : $regular, '' === $sale ? null : $sale, $price, PriceLog::currency(), $now ); if ( $written && ! get_option( 'eucheck_log_started' ) ) { update_option( 'eucheck_log_started', $now, false ); } return $written; } /** * A saved product's price fields as stored, in the shop's base currency. * * Read from the meta cache, not through get_post_meta(): currency * switchers filter that to the visitor's currency on the front end * (WooCommerce Multilingual converts while a product loads, so even the * edit context can hold converted amounts), and a stock change during a * checkout in another currency would otherwise log converted prices. * A product with changes not saved yet, or with no stored price, keeps * its object's values. * * @param \WC_Product $product Product or variation. * @return array{_price: string, _sale_price: string, _regular_price: string} */ private static function stored( \WC_Product $product ) { $meta = array(); if ( ! $product->get_changes() ) { $cache = update_meta_cache( 'post', array( $product->get_id() ) ); $meta = is_array( $cache ) && isset( $cache[ $product->get_id() ] ) ? $cache[ $product->get_id() ] : array(); } if ( ! isset( $meta['_price'][0] ) ) { return array( '_price' => (string) $product->get_price( 'edit' ), '_sale_price' => (string) $product->get_sale_price( 'edit' ), '_regular_price' => (string) $product->get_regular_price( 'edit' ), ); } $fields = array(); foreach ( array( '_price', '_sale_price', '_regular_price' ) as $key ) { $fields[ $key ] = isset( $meta[ $key ][0] ) ? (string) maybe_unserialize( $meta[ $key ][0] ) : ''; } return $fields; } /** * Records a batch of products, the first prices of a new log, in ID * order from after $after, so products changing status meanwhile never * shift the batches. * * @param int $after Last product ID already recorded. */ public function snapshot( $after = 0 ) { $after = max( 0, (int) $after ); $where = static function ( $sql ) use ( $after ) { global $wpdb; return $sql . $wpdb->prepare( " AND {$wpdb->posts}.ID > %d", $after ); }; add_filter( 'posts_where', $where ); $ids = get_posts( array( 'post_type' => array( 'product', 'product_variation' ), 'post_status' => 'publish', 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC', 'posts_per_page' => self::SNAPSHOT_BATCH, 'no_found_rows' => true, 'suppress_filters' => false, ) ); remove_filter( 'posts_where', $where ); foreach ( $ids as $id ) { $product = wc_get_product( $id ); if ( $product ) { $this->record( $product ); } } if ( count( $ids ) === self::SNAPSHOT_BATCH ) { Install::queue( (int) end( $ids ) ); } } /** * Whether shoppers can buy the product at its price now. * * @param \WC_Product $product Product or variation. * @return bool */ public static function is_live( \WC_Product $product ) { if ( 'publish' !== $product->get_status( 'edit' ) ) { return false; } $parent_id = $product->get_parent_id(); if ( $product->is_type( 'variation' ) && $parent_id ) { return 'publish' === get_post_status( $parent_id ); } return true; } }