observe_ids( (int) $product_id, 0, $this->source() ); } } /** * @param int $variation_id Variation ID. * @return void */ public function on_variation( $variation_id ) { if ( ! function_exists( 'wc_get_product' ) ) { return; } if ( ! ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) ) { return; } $product = wc_get_product( (int) $variation_id ); if ( ! $product ) { return; } ( new PriceRevisionWriter() )->observe_product( $product, $this->source() ); } /** * @param \WC_Product $product Product object. * @return void */ public function on_product_object( $product ) { if ( ! $product || ! is_object( $product ) ) { return; } // Avoid double-write when classic hooks already fired; still catch REST-only paths. if ( ! ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) ) { return; } ( new PriceRevisionWriter() )->observe_product( $product, $this->source() ); } /** * @return string */ private function source() { if ( defined( 'WP_CLI' ) && WP_CLI ) { return 'cli'; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return 'rest'; } return 'admin'; } }