ContentMarkService content type. Only AI-capable fields * appear here; a saved field absent from this map is never content-marked. * seoTitle/metaDescription/focusKeyword collapse to seo_meta and short/full * description to description, mirroring the bulk generators' provenance so the * AI-status column and ai_processed filter treat editor applies identically. */ private const FIELD_TO_CONTENT_TYPE = array( 'title' => 'title', 'shortDescription' => 'description', 'fullDescription' => 'description', 'tags' => 'tags', 'faq' => 'faq', 'seoTitle' => 'seo_meta', 'metaDescription' => 'seo_meta', 'focusKeyword' => 'seo_meta', 'altText' => 'image_alt', ); /** * Editor field id -> SnapshotStore field_type. Maps the ProductEditor * field vocabulary to the snapshot field_type vocabulary used by * SnapshotStore::capture() and handle_before_apply(). */ private const EDITOR_FIELD_TO_SNAPSHOT_TYPE = array( 'title' => 'title', 'shortDescription' => 'short_description', 'fullDescription' => 'description', 'tags' => 'tags', 'faq' => 'faq', 'seoTitle' => 'seo_meta_title', 'metaDescription' => 'seo_meta_description', 'altText' => 'image_alt', ); /** * Saved means every requested field for a product persisted. Partial means at * least one field persisted and at least one failed. Rollup counters count only * full product saves in savedCount, conflicts in conflictCount, and both error * plus partial product outcomes in errorCount. */ private $field_registry; private $product_resolver; private $permission_checker; private $content_marker; private $locker; public function __construct( FieldRegistry $field_registry, ?callable $product_resolver = null, ?callable $permission_checker = null, ?callable $content_marker = null, ?callable $locker = null ) { $this->field_registry = $field_registry; $this->product_resolver = $product_resolver; $this->permission_checker = $permission_checker; $this->content_marker = $content_marker; $this->locker = $locker; } public function save_patches( array $patches, bool $force, int $user_id ): array { $response = array( 'results' => array(), 'savedCount' => 0, 'conflictCount' => 0, 'errorCount' => 0, ); $any_persisted = false; $batch_conflicts = $this->batch_alt_text_conflicts( $patches ); foreach ( $patches as $patch ) { $patch_array = is_array( $patch ) ? $patch : array(); $product_id = isset( $patch_array['id'] ) ? (int) $patch_array['id'] : 0; $result = $this->save_patch( $patch_array, $force, $user_id, $batch_conflicts[ $product_id ] ?? array() ); $response['results'][] = $result; if ( 'saved' === $result['status'] ) { ++$response['savedCount']; } elseif ( 'conflict' === $result['status'] ) { ++$response['conflictCount']; } else { ++$response['errorCount']; } if ( in_array( $result['status'], array( 'saved', 'partial' ), true ) ) { $any_persisted = true; } } if ( $any_persisted ) { \AIPT\Utils\ProductFilter\ProductFilterBuilder::flush_cache(); } return $response; } private function save_patch( array $patch, bool $force, int $user_id, array $batch_conflicts = array() ): array { $product_id = isset( $patch['id'] ) ? (int) $patch['id'] : 0; try { if ( ! $this->can_edit( $product_id, $user_id ) ) { return $this->product_error( $product_id, 'Permission denied' ); } $release = $this->acquire_lock( $product_id ); if ( null === $release ) { return $this->product_error( $product_id, 'Could not acquire a save lock. Please try again.' ); } if ( false === $release ) { $busy_product = $this->resolve_product( $product_id ); return array( 'id' => $product_id, 'status' => 'conflict', 'token' => is_object( $busy_product ) ? $this->token_for_product( $busy_product ) : '', 'fields' => array(), ); } try { $product = $this->resolve_product( $product_id ); if ( ! is_object( $product ) ) { return $this->product_error( $product_id, 'Product not found' ); } $current_token = $this->token_for_product( $product ); $incoming_token = isset( $patch['token'] ) ? (string) $patch['token'] : ''; if ( ! $force && ( '' === $incoming_token || '' === $current_token || $incoming_token !== $current_token ) ) { return array( 'id' => $product_id, 'status' => 'conflict', 'token' => $current_token, 'fields' => array(), ); } $fields = isset( $patch['fields'] ) && is_array( $patch['fields'] ) ? $patch['fields'] : array(); list( $fields, $batch_pre_results ) = $this->apply_batch_alt_text_conflicts( $fields, $batch_conflicts ); list( $safe_fields, $pre_results ) = $this->filter_alt_text_permissions( $fields, $user_id ); list( $safe_fields, $status_pre_results ) = $this->filter_status_permission( $safe_fields, $user_id ); $pre_results = array_merge( $batch_pre_results, $pre_results, $status_pre_results ); $ai_field_ids = isset( $patch['aiFields'] ) && is_array( $patch['aiFields'] ) ? $patch['aiFields'] : array(); $staged = array(); try { $staged = $this->stage_snapshots( $product_id, $safe_fields, $ai_field_ids, $user_id ); } catch ( \Throwable $capture_error ) { } try { do_action( \AIPT\Api\Rollback\ContentApplyDispatcher::HOOK, array( 'product_id' => $product_id, 'source' => 'product_editor', ) ); } catch ( \Throwable $latch_error ) { } try { $write_results = ! empty( $safe_fields ) ? $this->field_registry->write_fields( $product, $safe_fields ) : array(); } catch ( \Throwable $write_error ) { try { $this->commit_staged_snapshots( $staged, array() ); } catch ( \Throwable $rollback_error ) { } throw $write_error; } try { $this->commit_staged_snapshots( $staged, $write_results ); } catch ( \Throwable $capture_error ) { } $this->mark_ai_content( $product_id, $patch, $write_results ); $field_results = $this->merge_pre_results( $this->map_field_results( $write_results ), $pre_results ); $field_results = $this->with_saved_values( $product, $field_results ); $status = $this->status_for_field_results( $field_results ); $result = array( 'id' => $product_id, 'status' => $status, 'fields' => $field_results, ); if ( in_array( $status, array( 'saved', 'partial' ), true ) && $this->any_field_changed( $write_results ) ) { ProductEditorConcurrencyToken::bump_revision( $product_id ); $result['token'] = $this->fresh_token( $product_id, $product ); } return $result; } finally { $release(); } } catch ( \Throwable $throwable ) { return $this->product_error( $product_id, 'An unexpected error occurred while saving.' ); } } /** * Acquire the per-product save lock. * * @return callable|false|null Release callback on success, false when another * save already holds the lock (busy), or null when * the lock backend is unavailable (fail closed). */ private function acquire_lock( int $product_id ) { if ( null !== $this->locker ) { return call_user_func( $this->locker, $product_id ); } return \AIPT\Api\Helpers\ProductWriteLock::acquire( $product_id ); } private function resolve_product( int $product_id ) { if ( null !== $this->product_resolver ) { return call_user_func( $this->product_resolver, $product_id ); } return \wc_get_product( $product_id ); } private function can_edit( int $post_id, int $user_id ): bool { if ( null !== $this->permission_checker ) { return (bool) call_user_func( $this->permission_checker, 'edit_post', $post_id, $user_id ); } return (bool) \user_can( $user_id, 'edit_post', $post_id ); } private function token_for_product( $product ): string { return ProductEditorConcurrencyToken::for_product( $product ); } private function fresh_token( int $product_id, $product ): string { $modified = (string) \get_post_field( 'post_modified', $product_id ); return '' !== $modified ? ProductEditorConcurrencyToken::for_product_id( $product_id, $modified ) : ProductEditorConcurrencyToken::for_product( $product ); } private function filter_status_permission( array $fields, int $user_id ): array { $pre_results = array(); $status = ( isset( $fields['status'] ) && is_scalar( $fields['status'] ) ) ? (string) $fields['status'] : ''; if ( ! in_array( $status, array( 'publish', 'private' ), true ) || $this->can_publish( $user_id ) ) { return array( $fields, $pre_results ); } unset( $fields['status'] ); $pre_results[] = array( 'fieldId' => 'status', 'saved' => false, 'message' => 'You are not allowed to publish products.', ); return array( $fields, $pre_results ); } private function can_publish( int $user_id ): bool { if ( null !== $this->permission_checker ) { return (bool) call_user_func( $this->permission_checker, 'publish_products', 0, $user_id ); } return (bool) \user_can( $user_id, 'publish_products' ); } private function filter_alt_text_permissions( array $fields, int $user_id ): array { $pre_results = array(); if ( ! isset( $fields['altText'] ) || ! is_array( $fields['altText'] ) ) { return array( $fields, $pre_results ); } $allowed_entries = array(); foreach ( $fields['altText'] as $entry ) { if ( ! is_array( $entry ) || ! isset( $entry['attachmentId'] ) ) { $allowed_entries[] = $entry; continue; } $attachment_id = (int) $entry['attachmentId']; if ( $this->can_edit( $attachment_id, $user_id ) ) { $allowed_entries[] = $entry; continue; } $pre_results[] = array( 'fieldId' => 'altText', 'saved' => false, 'partial' => true, 'message' => 'Permission denied for attachment ID ' . $attachment_id . '.', 'attachments' => array( array( 'attachmentId' => $attachment_id, 'status' => 'denied', 'message' => 'Permission denied for attachment ID ' . $attachment_id . '.', ), ), ); } if ( empty( $allowed_entries ) ) { unset( $fields['altText'] ); } else { $fields['altText'] = $allowed_entries; } return array( $fields, $pre_results ); } private function map_field_results( array $write_results ): array { $mapped = array(); foreach ( $write_results as $field_id => $result ) { $item = array( 'fieldId' => (string) $field_id, 'saved' => ! empty( $result['saved'] ), ); if ( isset( $result['message'] ) && null !== $result['message'] && '' !== $result['message'] ) { $item['message'] = (string) $result['message']; } foreach ( array( 'partial', 'persisted', 'attachments' ) as $extra_key ) { if ( array_key_exists( $extra_key, $result ) ) { $item[ $extra_key ] = $result[ $extra_key ]; } } $mapped[] = $item; } return $mapped; } private function with_saved_values( \WC_Product $product, array $field_results ): array { foreach ( $field_results as &$result ) { $persisted_value = ! empty( $result['persisted'] ) && ! isset( $result['attachments'] ); if ( empty( $result['saved'] ) && ! $persisted_value ) { continue; } try { $field_id = $result['fieldId']; $values = $this->field_registry->read_fields( $product, array( $field_id ) ); if ( array_key_exists( $field_id, $values ) ) { $result['value'] = $values[ $field_id ]; } } catch ( \Throwable $read_error ) { } } unset( $result ); return $field_results; } /** * True when at least one persisted field write reports a genuine value * change. An adapter that omits 'changed' entirely (not yet updated with * the signal) is treated as changed whenever it persisted, preserving prior * behavior for those adapters — only an explicit 'changed' => false (a * detected no-op, e.g. CoreFieldAdapter resubmitting an already-current * value) withholds the token bump for that field. */ private function any_field_changed( array $write_results ): bool { foreach ( $write_results as $result ) { if ( empty( $result['saved'] ) && empty( $result['persisted'] ) ) { continue; } if ( ! array_key_exists( 'changed', $result ) || ! empty( $result['changed'] ) ) { return true; } } return false; } private function status_for_field_results( array $field_results ): string { $total = count( $field_results ); if ( 0 === $total ) { return 'error'; } $saved = 0; $persisted = 0; foreach ( $field_results as $result ) { if ( ! empty( $result['saved'] ) ) { ++$saved; ++$persisted; continue; } if ( ! empty( $result['persisted'] ) ) { ++$persisted; } } if ( $saved === $total ) { return 'saved'; } return $persisted > 0 ? 'partial' : 'error'; } private function batch_alt_text_conflicts( array $patches ): array { $by_attachment = array(); foreach ( $patches as $patch ) { if ( ! is_array( $patch ) || ! isset( $patch['fields']['altText'] ) || ! is_array( $patch['fields']['altText'] ) ) { continue; } $product_id = isset( $patch['id'] ) ? (int) $patch['id'] : 0; foreach ( $patch['fields']['altText'] as $entry ) { if ( ! is_array( $entry ) || ! isset( $entry['attachmentId'] ) ) { continue; } $attachment_id = (int) $entry['attachmentId']; $alt = \sanitize_text_field( isset( $entry['alt'] ) ? (string) $entry['alt'] : '' ); if ( ! isset( $by_attachment[ $attachment_id ] ) ) { $by_attachment[ $attachment_id ] = array(); } $by_attachment[ $attachment_id ][ $product_id ] = $alt; } } $conflicts = array(); foreach ( $by_attachment as $attachment_id => $product_alts ) { if ( count( array_unique( array_values( $product_alts ), SORT_REGULAR ) ) <= 1 ) { continue; } foreach ( array_keys( $product_alts ) as $product_id ) { if ( ! isset( $conflicts[ $product_id ] ) ) { $conflicts[ $product_id ] = array(); } $conflicts[ $product_id ][ $attachment_id ] = true; } } return $conflicts; } private function apply_batch_alt_text_conflicts( array $fields, array $batch_conflicts ): array { $pre_results = array(); if ( empty( $batch_conflicts ) || ! isset( $fields['altText'] ) || ! is_array( $fields['altText'] ) ) { return array( $fields, $pre_results ); } $allowed_entries = array(); $attachments = array(); foreach ( $fields['altText'] as $entry ) { if ( ! is_array( $entry ) || ! isset( $entry['attachmentId'] ) ) { $allowed_entries[] = $entry; continue; } $attachment_id = (int) $entry['attachmentId']; if ( empty( $batch_conflicts[ $attachment_id ] ) ) { $allowed_entries[] = $entry; continue; } $attachments[] = array( 'attachmentId' => $attachment_id, 'status' => 'conflict', 'message' => 'Conflicting alt text values for the same attachment in this batch.', ); } if ( empty( $allowed_entries ) ) { unset( $fields['altText'] ); } else { $fields['altText'] = $allowed_entries; } if ( ! empty( $attachments ) ) { $pre_results[] = array( 'fieldId' => 'altText', 'saved' => false, 'partial' => true, 'message' => 'Conflicting alt text values for the same attachment in this batch.', 'attachments' => $attachments, ); } return array( $fields, $pre_results ); } private function merge_pre_results( array $field_results, array $pre_results ): array { $alt_pre = array(); $merged = $field_results; foreach ( $pre_results as $pre_result ) { if ( isset( $pre_result['fieldId'] ) && 'altText' === $pre_result['fieldId'] ) { $alt_pre[] = $pre_result; continue; } $merged[] = $pre_result; } if ( empty( $alt_pre ) ) { return $merged; } $alt_index = null; foreach ( $merged as $index => $result ) { if ( isset( $result['fieldId'] ) && 'altText' === $result['fieldId'] ) { $alt_index = $index; break; } } $combined = null === $alt_index ? array( 'fieldId' => 'altText', 'saved' => false, 'partial' => true, 'attachments' => array() ) : $merged[ $alt_index ]; foreach ( $alt_pre as $pre_result ) { $combined = $this->combine_alt_text_result( $combined, $pre_result ); } if ( null === $alt_index ) { $merged[] = $combined; } else { $merged[ $alt_index ] = $combined; } return $merged; } private function combine_alt_text_result( array $base, array $extra ): array { $base_saved = ! empty( $base['saved'] ); $base['saved'] = false; $base['partial'] = true; $base['persisted'] = ! empty( $base['persisted'] ) || $base_saved || ! empty( $extra['persisted'] ); $base['message'] = isset( $base['message'] ) && '' !== (string) $base['message'] ? $base['message'] : ( $extra['message'] ?? 'Some attachment alt text entries could not be saved.' ); $base['attachments'] = array_merge( isset( $base['attachments'] ) && is_array( $base['attachments'] ) ? $base['attachments'] : array(), isset( $extra['attachments'] ) && is_array( $extra['attachments'] ) ? $extra['attachments'] : array() ); return $base; } /** * Content-mark the AI-authored fields a patch actually persisted. * * A field is marked only when it is BOTH declared AI-sourced by the client * (patch['aiFields'], populated when the value came from an accepted AI * suggestion) AND confirmed saved by write_fields(). Manual edits and failed * writes are never marked. Several fields collapsing to one content type * (seoTitle + metaDescription -> seo_meta) mark that type once. Best-effort * PER content type: a failing marker is logged and skipped, never affecting * the save result nor the product's other content types. */ private function mark_ai_content( int $product_id, array $patch, array $write_results ): void { if ( $product_id <= 0 || empty( $patch['aiFields'] ) || ! is_array( $patch['aiFields'] ) ) { return; } $ai_fields = array(); foreach ( $patch['aiFields'] as $field_id ) { if ( is_string( $field_id ) ) { $ai_fields[ $field_id ] = true; } } $content_types = array(); foreach ( $write_results as $field_id => $result ) { $field_id = (string) $field_id; if ( ( empty( $result['saved'] ) && empty( $result['persisted'] ) ) || ! isset( $ai_fields[ $field_id ] ) ) { continue; } $content_type = self::FIELD_TO_CONTENT_TYPE[ $field_id ] ?? null; if ( null !== $content_type ) { $content_types[ $content_type ] = true; } } foreach ( array_keys( $content_types ) as $content_type ) { try { $this->call_content_marker( $product_id, $content_type, array( 'gen' => \AIPT\Core\ContentMarking\MetaKeys::GEN_EDITOR ) ); } catch ( \Throwable $mark_error ) { } } } private function call_content_marker( int $product_id, string $content_type, array $meta ): bool { if ( null !== $this->content_marker ) { return (bool) call_user_func( $this->content_marker, $product_id, $content_type, $meta ); } return \AIPT\Core\ContentMarking\ContentMarkService::mark( $product_id, $content_type, $meta ); } /** * Stage pre-write field values into the snapshot store as real * status='pending' rows, immediately before write_fields() runs. * * Restricted to AI-sourced fields (present in $ai_field_ids, populated * from patch['aiFields'] — the same signal mark_ai_content() already uses) * — a manual grid edit is not an "AI overwrote this" event and must never * create a source=product_editor Content History entry or consume a * retention slot meant for AI content. The INSERT happens HERE, before the * write, not deferred to commit time: a snapshot inserted after the write * would order its row later than a concurrent generator's own capture for * the same field, letting the generator's intermediate value get pinned as * the group's "original" instead of this read's genuinely-earlier one. * The caller finalizes each staged id via commit_staged_snapshots() once * write_fields()'s result is known — commit() on success, rollback() on * failure, never leaving a failed write's staged row visible. * * @param int $product_id Product id. * @param array $fields Field map (field_id => new_value) about to be written. * @param array $ai_field_ids patch['aiFields'] — field ids the client marked AI-sourced. * @param int $user_id User performing the save. * @return array */ private function stage_snapshots( int $product_id, array $fields, array $ai_field_ids, int $user_id ): array { $store = new \AIPT\Core\ContentSnapshots\SnapshotStore(); $staged = array(); $ai_fields = array_flip( array_filter( $ai_field_ids, 'is_string' ) ); foreach ( $fields as $field_id => $new_value ) { if ( ! isset( $ai_fields[ $field_id ] ) ) { continue; } $snapshot_type = self::EDITOR_FIELD_TO_SNAPSHOT_TYPE[ $field_id ] ?? null; if ( null === $snapshot_type ) { continue; } if ( 'altText' === $field_id && is_array( $new_value ) ) { $entries = array(); foreach ( $new_value as $entry ) { if ( ! is_array( $entry ) || ! isset( $entry['attachmentId'] ) ) { continue; } $attachment_id = (int) $entry['attachmentId']; $new_alt = isset( $entry['alt'] ) ? (string) $entry['alt'] : ''; $current_alt = $store->readCurrentFieldValue( $product_id, 'image_alt', $attachment_id ); if ( null === $current_alt || $current_alt === $new_alt ) { continue; } try { $snapshot_id = $store->stage( $product_id, 'image_alt', $current_alt, array( 'source' => 'product_editor', 'applied_by' => $user_id, 'subject_id' => $attachment_id, ) ); if ( null !== $snapshot_id ) { $entries[] = array( 'attachment_id' => $attachment_id, 'snapshot_id' => $snapshot_id, ); } } catch ( \Throwable $stage_error ) { error_log( sprintf( '[AIPT ProductEditor] alt-text snapshot staging failed for product #%d attachment #%d: %s', $product_id, $attachment_id, $stage_error->getMessage() ) ); } } if ( ! empty( $entries ) ) { $staged[ $field_id ] = array( 'snapshot_id' => null, 'entries' => $entries, ); } continue; } $current_value = $store->readCurrentFieldValue( $product_id, $snapshot_type ); if ( null === $current_value || ! $this->is_field_value_changed( $field_id, $current_value, $new_value ) ) { continue; } $capture_opts = array( 'source' => 'product_editor', 'applied_by' => $user_id, ); $plugin_slug = $this->resolve_seo_plugin_slug( $snapshot_type ); if ( null !== $plugin_slug ) { $capture_opts['plugin_slug'] = $plugin_slug; } try { $snapshot_id = $store->stage( $product_id, $snapshot_type, $current_value, $capture_opts ); if ( null !== $snapshot_id ) { $staged[ $field_id ] = array( 'snapshot_id' => $snapshot_id, 'entries' => null, ); } } catch ( \Throwable $stage_error ) { error_log( sprintf( '[AIPT ProductEditor] snapshot staging failed for product #%d field %s: %s', $product_id, $field_id, $stage_error->getMessage() ) ); } } return $staged; } /** * Resolve the active SEO plugin slug for a staged snapshot's plugin_slug. * * readCurrentFieldValue() already detects the active SEO plugin * internally to read the current value for seo_meta_title/ * seo_meta_description, but does not surface it to the caller. Without * a plugin_slug, SnapshotStore's restorable-detection treats the row as * empty/'standalone' and permanently refuses to restore it (fail-closed * — see SnapshotStore::mapRow()), so a captured SEO snapshot from this * flow was silently unrestorable. 'standalone' (never empty string) * matches that same fail-closed vocabulary when no plugin is detected. * * @param string $snapshot_type Snapshot field_type. * @return string|null Null for non-SEO types — omit the opts key entirely. */ private function resolve_seo_plugin_slug( string $snapshot_type ): ?string { if ( ! in_array( $snapshot_type, array( 'seo_meta_title', 'seo_meta_description' ), true ) ) { return null; } $detected = \AIPT\Api\BulkGenerator\SeoMeta\Core\SeoPluginDetector::detect(); return ( is_array( $detected ) && ! empty( $detected['plugin_slug'] ) ) ? (string) $detected['plugin_slug'] : 'standalone'; } /** * Commit or roll back every snapshot stage_snapshots() staged, based on * whether each field's write actually succeeded. * * A field whose write failed (saved=false, persisted=false) is rolled * back (the staged pending row is deleted) — it must never record a * history entry implying the value changed when it did not. A field that * did succeed is committed (pending → available). * * @param array $staged Staged snapshot map from stage_snapshots(). * @param array $write_results Raw write results from FieldRegistry. * @return void */ private function commit_staged_snapshots( array $staged, array $write_results ): void { if ( empty( $staged ) ) { return; } $store = new \AIPT\Core\ContentSnapshots\SnapshotStore(); foreach ( $staged as $field_id => $entry ) { $result = $write_results[ $field_id ] ?? null; $field_landed = null !== $result && ( ! empty( $result['saved'] ) || ! empty( $result['persisted'] ) ); if ( null !== $entry['entries'] ) { $saved_attachment_ids = array(); foreach ( (array) ( $result['attachments'] ?? array() ) as $outcome ) { if ( is_array( $outcome ) && 'saved' === ( $outcome['status'] ?? '' ) ) { $saved_attachment_ids[ (int) ( $outcome['attachmentId'] ?? 0 ) ] = true; } } foreach ( $entry['entries'] as $alt_entry ) { $snapshot_id = (int) $alt_entry['snapshot_id']; if ( isset( $saved_attachment_ids[ (int) $alt_entry['attachment_id'] ] ) ) { $store->commit( $snapshot_id ); \AIPT\Api\ProductEditor\ProductEditorConcurrencyToken::bump_revision( (int) $alt_entry['attachment_id'] ); } else { $store->rollback( $snapshot_id ); } } continue; } if ( null !== $entry['snapshot_id'] ) { if ( $field_landed ) { $store->commit( $entry['snapshot_id'] ); } else { $store->rollback( $entry['snapshot_id'] ); } } } } /** * Whether the new field value differs from the current live value. * * @param string $field_id Editor field id. * @param string $current_value Current live value (may be JSON). * @param mixed $new_value Incoming value from the patch. * @return bool */ private function is_field_value_changed( string $field_id, string $current_value, $new_value ): bool { switch ( $field_id ) { case 'tags': $new_tags = is_array( $new_value ) ? array_values( array_map( 'strval', $new_value ) ) : array(); $new_json = \wp_json_encode( $new_tags ); return false !== $new_json && $current_value !== $new_json; case 'faq': return is_array( $new_value ) && $current_value !== \wp_json_encode( $this->sanitize_faq( $new_value ) ); case 'seoTitle': case 'metaDescription': if ( null === $current_value ) { return false; } return $current_value !== (string) $new_value; default: return $current_value !== (string) $new_value; } } /** * Sanitize FAQ entries for comparison with the stored JSON. * * @param array $entries Raw FAQ entries. * @return array Sanitized FAQ entries. */ private function sanitize_faq( array $entries ): array { $sanitized = array(); foreach ( $entries as $entry ) { if ( ! is_array( $entry ) ) { continue; } $question = \sanitize_text_field( isset( $entry['question'] ) ? (string) $entry['question'] : '' ); $answer = \wp_kses_post( isset( $entry['answer'] ) ? (string) $entry['answer'] : '' ); if ( '' === trim( $question ) && '' === trim( $answer ) ) { continue; } $sanitized[] = array( 'question' => $question, 'answer' => $answer, ); } return $sanitized; } private function product_error( int $product_id, string $message ): array { return array( 'id' => $product_id, 'status' => 'error', 'fields' => array(), 'message' => $message, ); } }