get_name(); case 'sku': return $product->get_sku(); case 'status': return $product->get_status(); case 'productType': return $product->get_type(); case 'category': return $this->read_categories( $product ); case 'price': return $product->get_regular_price(); case 'shortDescription': return $product->get_short_description(); case 'fullDescription': return $product->get_description(); case 'tags': return $this->read_tags( $product ); case 'slug': return $product->get_slug(); } return null; } public function write( \WC_Product $product, string $field_id, $value ): array { $track_change = in_array( $field_id, self::EDITABLE_FIELDS, true ) && ! in_array( $field_id, self::SELF_TRACKED_CHANGE_FIELDS, true ); $before = $track_change ? $this->read_raw( $product, $field_id ) : null; $result = $this->perform_write( $product, $field_id, $value ); if ( $track_change && ! empty( $result['saved'] ) && ! array_key_exists( 'changed', $result ) ) { $result['changed'] = $before !== $this->read_raw( $product, $field_id ); } return $result; } /** * Read a field's RAW stored value, for change detection only. * * read() uses WooCommerce's default 'view' context, which passes the value * through the `woocommerce_product_get_{prop}` filters. A third-party filter * that normalizes, rewrites or truncates a value can collapse two DIFFERENT * stored values to the same displayed string, so a before/after diff built * on read() would report a genuine edit as unchanged — FieldRegistry would * then skip the modified-timestamp bump, and a second editor's stale * optimistic-concurrency token would still match, letting it silently * overwrite this edit. (The same filters can also make an UNCHANGED value * read differently across the two calls, manufacturing a false conflict.) * The 'edit' context returns the unfiltered stored value, which is what * "did this actually change?" has to be answered against. * * Only the fields that use the generic before/after diff in write() need a * raw reader; SELF_TRACKED_CHANGE_FIELDS compare their own values, and * anything else falls through to read(). * * @param \WC_Product $product The product being written. * @param string $field_id The editor field id. * @return mixed The unfiltered stored value. */ private function read_raw( \WC_Product $product, string $field_id ) { switch ( $field_id ) { case 'title': return $product->get_name( 'edit' ); case 'sku': return $product->get_sku( 'edit' ); case 'status': return $product->get_status( 'edit' ); case 'shortDescription': return $product->get_short_description( 'edit' ); case 'fullDescription': return $product->get_description( 'edit' ); case 'slug': return $product->get_slug( 'edit' ); } return $this->read( $product, $field_id ); } private function perform_write( \WC_Product $product, string $field_id, $value ): array { switch ( $field_id ) { case 'title': $product->set_name( \sanitize_text_field( (string) $value ) ); return $this->saved(); case 'sku': try { $product->set_sku( \sanitize_text_field( (string) $value ) ); } catch ( \WC_Data_Exception $e ) { return $this->not_saved( $e->getMessage() ); } return $this->saved(); case 'status': return $this->write_status( $product, $value ); case 'productType': return $this->not_saved( 'Product type is read-only.' ); case 'category': return $this->write_categories( $product, $value ); case 'price': return $this->write_price( $product, $value ); case 'shortDescription': $product->set_short_description( \AIPT\Utils\DescriptionSanitizer::clean( (string) $value ) ); return $this->saved(); case 'fullDescription': $product->set_description( \AIPT\Utils\DescriptionSanitizer::clean( (string) $value ) ); return $this->saved(); case 'tags': return $this->write_tags( $product, $value ); case 'slug': $product->set_slug( \sanitize_title( (string) $value ) ); return $this->saved(); } return $this->not_saved( 'Unsupported field.' ); } private function read_categories( \WC_Product $product ): array { $categories = array(); foreach ( (array) $product->get_category_ids() as $term_id ) { $term = \get_term( (int) $term_id, 'product_cat' ); if ( ! $term || ( \function_exists( 'is_wp_error' ) && \is_wp_error( $term ) ) ) { continue; } $categories[] = array( 'id' => (int) $term->term_id, 'name' => (string) $term->name, 'slug' => (string) $term->slug, ); } return $categories; } private function read_tags( \WC_Product $product ): array { $terms = \get_the_terms( $product->get_id(), 'product_tag' ); if ( ! \is_array( $terms ) ) { return array(); } return array_map( static function ( $term ) { return (string) $term->name; }, $terms ); } private function write_status( \WC_Product $product, $value ): array { $status = \sanitize_text_field( (string) $value ); if ( ! in_array( $status, array( 'publish', 'draft', 'pending', 'private' ), true ) ) { return $this->not_saved( 'Invalid status.' ); } $product->set_status( $status ); return $this->saved(); } private function write_price( \WC_Product $product, $value ): array { if ( $product->is_type( 'variable' ) || $product->is_type( 'grouped' ) ) { return $this->not_saved( 'Price is not directly editable for variable or grouped products.' ); } $regular_before = (string) $product->get_regular_price( 'edit' ); $sale_before = (string) $product->get_sale_price( 'edit' ); if ( '' === trim( (string) $value ) ) { $product->set_regular_price( '' ); $product->set_sale_price( '' ); return $this->price_result( $regular_before, $sale_before, $product ); } $normalized = $this->normalize_price( $value ); if ( null === $normalized ) { return $this->not_saved( 'Price must be a non-negative number.' ); } $regular_price = (float) $normalized; $product->set_regular_price( $normalized ); $sale_price = $product->get_sale_price(); if ( '' !== $sale_price && is_numeric( $sale_price ) && (float) $sale_price >= $regular_price ) { $product->set_sale_price( '' ); } return $this->price_result( $regular_before, $sale_before, $product ); } /** * Normalize a drafted price to the stored dot form, or null when invalid. * * Accepts plain digits with an optional decimal part written with a dot or, * on a comma-decimal store, a comma. Exponent, hex, sign and grouping forms * are rejected, in step with validatePrice() in the editor client. * * @param mixed $value Drafted price. * @return string|null Normalized price, or null when it is not a valid price. */ private function normalize_price( $value ): ?string { if ( is_int( $value ) || is_float( $value ) ) { $value = (string) $value; } if ( ! is_string( $value ) ) { return null; } $text = trim( $value ); if ( function_exists( 'wc_get_price_decimal_separator' ) && ',' === wc_get_price_decimal_separator() ) { $text = preg_replace( '/,/', '.', $text, 1 ); } return 1 === preg_match( '/^(?:\d+(?:\.\d+)?|\.\d+)$/', $text ) ? $text : null; } private function price_result( string $regular_before, string $sale_before, \WC_Product $product ): array { $result = $this->saved(); $result['changed'] = $regular_before !== (string) $product->get_regular_price( 'edit' ) || $sale_before !== (string) $product->get_sale_price( 'edit' ); return $result; } private function write_tags( \WC_Product $product, $value ): array { $tags = array(); foreach ( (array) $value as $tag ) { $tag = \sanitize_text_field( (string) $tag ); if ( '' !== $tag && ! in_array( $tag, $tags, true ) ) { $tags[] = $tag; } } $before = $this->read_tags( $product ); $result = \wp_set_object_terms( (int) $product->get_id(), $tags, 'product_tag', false ); \clean_object_term_cache( (int) $product->get_id(), 'product' ); if ( \function_exists( 'is_wp_error' ) && \is_wp_error( $result ) ) { return $this->not_saved( $result->get_error_message() ); } if ( false === $result ) { return $this->not_saved( 'Failed to write tags.' ); } $saved = $this->saved(); $saved['changed'] = $this->name_lists_differ( $before, $tags ); return $saved; } /** * Order-insensitive term-name comparison for the tags "changed" check. * * @param array $a One term-name list. * @param array $b The other term-name list. * @return bool True when the two lists describe different tag sets. */ private function name_lists_differ( array $a, array $b ): bool { $a = array_values( array_unique( array_map( 'strval', $a ) ) ); $b = array_values( array_unique( array_map( 'strval', $b ) ) ); sort( $a ); sort( $b ); return $a !== $b; } private function write_categories( \WC_Product $product, $value ): array { $items = (array) $value; $candidates = array(); $ids = array(); foreach ( $items as $item ) { $id = is_array( $item ) ? ( isset( $item['id'] ) ? (int) $item['id'] : 0 ) : (int) $item; if ( $id <= 0 ) { continue; } $candidates[ $id ] = true; if ( $this->is_product_category( $id ) ) { $ids[ $id ] = true; } } if ( array() === $ids && array() !== $items ) { return $this->not_saved( 'No valid product categories.' ); } $before_ids = array_values( array_unique( array_map( 'intval', (array) $product->get_category_ids() ) ) ); $new_ids = array_keys( $ids ); $product->set_category_ids( $new_ids ); $changed = $this->id_lists_differ( $before_ids, $new_ids ); if ( count( $ids ) < count( $candidates ) ) { return array( 'saved' => false, 'partial' => true, 'persisted' => true, 'changed' => $changed, 'message' => 'One or more selected categories no longer exist and were not applied.', ); } $result = $this->saved(); $result['changed'] = $changed; return $result; } /** * Order-insensitive id-set comparison for the category "changed" check: a * client resubmitting the same category set is not a real change even if * set_category_ids() happens to receive the ids in a different order. */ private function id_lists_differ( array $a, array $b ): bool { sort( $a ); sort( $b ); return $a !== $b; } private function is_product_category( int $term_id ): bool { $term = \get_term( $term_id, 'product_cat' ); return $term && ! ( \function_exists( 'is_wp_error' ) && \is_wp_error( $term ) ); } private function saved(): array { return array( 'saved' => true, 'message' => null ); } private function not_saved( string $message ): array { return array( 'saved' => false, 'message' => $message ); } }