get_id()` unconditionally, and * `WC_Product_Data_Store_CPT::update()` discards what `wp_update_post()` * returned. `wp_update_post()` returns `0` - not a WP_Error - both when the post * row is gone and when `$wpdb->update()` itself fails. So the value every caller * tests (`! is_wp_error( $saved ) && ! empty( $saved )`) is TRUE even when * nothing was written. * * Verified against WordPress 7.1.1 + WooCommerce 11.1.0 in an isolated install: * deleting the post row mid-save still produced `save() === 10`, the caller * predicate still said "saved", and no row existed afterwards. * * The signal used here is WordPress's own commit event, `wp_after_insert_post`, * which fires only on `wp_insert_post()`'s success path - after both of * `wp_update_post()`'s early `return 0` branches. Unlike comparing the stored * value it cannot false-negative on legitimate `kses`/`content_save_pre` * filtering, and unlike comparing `post_modified` it cannot false-negative on * two saves landing in the same second. * * ONE MEASURED CAVEAT, handled explicitly below: WooCommerce calls * `wp_update_post()` only when a POST-BACKED property changed. A save that * touches meta alone (price, SKU, stock) never reaches it, so the event cannot * fire even though the save succeeded. Confirmed in the same isolated install: * `set_regular_price()` alone -> event does not fire, save is fine. For that * case row existence is the right post-row signal, because no post-row write * was attempted at all. * * Neither signal covers META, though, and both are reached by saves that change * meta: the Product Editor edits `sku` and `price` and stamps `date_modified` * in the same save, so a green commit event there proves only that the POST ROW * landed. `metaChangesLanded()` therefore re-reads the meta this save actually * changed, straight from the database, and `landed` is false if any of it is * missing. */ final class ProductSaveVerifier { /** * Product properties WooCommerce persists on the post row itself. A change * to any of these is what makes its data store call `wp_update_post()`. * * @var string[] */ private const POST_BACKED_PROPS = array( 'name', 'slug', 'date_created', 'date_modified', 'status', 'description', 'short_description', 'parent_id', 'reviews_allowed', 'menu_order', 'post_password', ); /** * Save a product and report whether the write actually committed. * * @param \WC_Product $product Product to save. * @return array{result:mixed,landed:bool} `result` is whatever save() * returned (callers that still need it keep working unchanged); * `landed` is the trustworthy signal. */ public static function save( \WC_Product $product ): array { $product_id = (int) $product->get_id(); $changes = is_callable( array( $product, 'get_changes' ) ) ? (array) $product->get_changes() : array(); $touches_post_row = array() !== array_intersect( array_keys( $changes ), self::POST_BACKED_PROPS ); $committed = false; $listener = static function ( $post_id ) use ( &$committed, &$product_id ) { if ( 0 === $product_id || (int) $post_id === $product_id ) { $committed = true; } }; \add_action( 'wp_after_insert_post', $listener, 10, 1 ); try { $result = $product->save(); } finally { \remove_action( 'wp_after_insert_post', $listener, 10 ); } if ( \is_wp_error( $result ) || empty( $result ) ) { return array( 'result' => $result, 'landed' => false ); } if ( 0 === $product_id ) { $product_id = (int) $result; } $post_row_landed = $touches_post_row ? $committed : self::rowExists( $product_id ); if ( ! $post_row_landed ) { return array( 'result' => $result, 'landed' => false ); } return array( 'result' => $result, 'landed' => self::metaChangesLanded( $product, $product_id, $changes ), ); } /** * Meta keys for the meta-backed properties this verifier checks. * * Deliberately SMALL. A property is listed only when it has a stable 1:1 * meta key AND a getter that returns the post-save, WooCommerce-normalized * value - so the comparison below is normalized-against-stored, never * caller-input-against-stored. Comparing raw input would false-negative the * moment WooCommerce reformats it (`wc_format_decimal` turning 7.50 into * '7.5', SKU trimming, and so on), and a false "this save was lost" on the * core write path is worse than the gap it closes. * * Properties NOT listed are simply not verified, so they can never cause a * false negative: derived meta (`_price`), stock fields whose persistence * depends on `manage_stock`, and variable-product aggregates all have * conditional write rules that cannot be checked this cheaply. Extending * this map requires re-running tests/_evidence/backend-audit/probe-save-verifier.php * in the isolated install. * * @var array */ private const VERIFIED_META_PROPS = array( 'sku' => '_sku', 'regular_price' => '_regular_price', 'sale_price' => '_sale_price', ); /** * Confirm the meta-backed properties this save changed are really stored. * * @param \WC_Product $product Product, already saved - so its getters * return the normalized values WooCommerce * intended to persist. * @param int $product_id Product id. * @param array $changes Changes captured BEFORE the save. * @return bool False as soon as one changed value is absent from the database. */ private static function metaChangesLanded( \WC_Product $product, int $product_id, array $changes ): bool { foreach ( self::VERIFIED_META_PROPS as $prop => $meta_key ) { if ( ! array_key_exists( $prop, $changes ) ) { continue; } $getter = 'get_' . $prop; if ( ! is_callable( array( $product, $getter ) ) ) { continue; } if ( (string) self::readMeta( $product_id, $meta_key ) !== (string) $product->{$getter}( 'edit' ) ) { return false; } } return true; } /** * Cache-bypassing read of a single meta value. * * @param int $product_id Product id. * @param string $meta_key Meta key. * @return string|null Stored value, or null when no row exists. */ private static function readMeta( int $product_id, string $meta_key ) { global $wpdb; if ( $product_id <= 0 || ! isset( $wpdb ) || ! is_object( $wpdb ) || ! is_callable( array( $wpdb, 'get_var' ) ) ) { return null; } return $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s LIMIT 1", $product_id, $meta_key ) ); } /** * Cache-bypassing existence check for the product's post row. * * @param int $product_id Product id. * @return bool */ private static function rowExists( int $product_id ): bool { global $wpdb; if ( $product_id <= 0 || ! isset( $wpdb ) || ! is_object( $wpdb ) || ! is_callable( array( $wpdb, 'get_var' ) ) ) { return false; } return null !== $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID = %d", $product_id ) ); } }