| 1 |
<?php |
| 2 |
/** |
| 3 |
* WCPOS sync read surface. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Sync |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Sync; |
| 9 |
|
| 10 |
// phpcs:disable Squiz.Commenting, Generic.Commenting -- Ported lab documentation is preserved verbatim. |
| 11 |
|
| 12 |
/** |
| 13 |
* Adapts a WordPress TERM (category / brand — taxonomy product_cat / product_brand) |
| 14 |
* to the small WC_Data meta contract that Pos_Uuid::ensure_uuid and |
| 15 |
* prune_duplicate_uuid_meta duck-type on (get_meta_data / update_meta_data / |
| 16 |
* save_meta_data / delete_meta_data_by_mid). WP_Term is NOT a WC_Data object and |
| 17 |
* get_term_meta returns bare scalar values with no meta-id, so the uuid stamping |
| 18 |
* logic cannot be reused on a term directly — this thin adapter over wp_termmeta |
| 19 |
* lets the audited ensure_uuid/dedup path stamp terms unchanged, exactly as |
| 20 |
* WC_Customer (which IS WC_Data) let it stamp customers. |
| 21 |
* |
| 22 |
* get_meta_data surfaces the per-row meta_id (queried from wp_termmeta), which |
| 23 |
* prune_duplicate_uuid_meta needs to delete duplicate uuid metas by mid. |
| 24 |
* update_meta_data has WC_Data's replace-or-add semantics (a single key holds one |
| 25 |
* value), applied on save via update_term_meta — so re-keying a collided term |
| 26 |
* REPLACES its uuid rather than leaving a second one behind. |
| 27 |
*/ |
| 28 |
final class Term_Meta_Adapter { |
| 29 |
/** |
| 30 |
* @var array<string,mixed> key => value, applied (replace-or-add) on save |
| 31 |
*/ |
| 32 |
private array $pending = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
private $term_id; |
| 38 |
|
| 39 |
public function __construct( int $term_id ) { |
| 40 |
$this->term_id = $term_id; |
| 41 |
} |
| 42 |
|
| 43 |
public function get_id(): int { |
| 44 |
return $this->term_id; |
| 45 |
} |
| 46 |
|
| 47 |
/** The term's _woocommerce_pos_uuid metas as objects carrying ->id (meta_id), |
| 48 |
* ->key, ->value — meta_id ASC so first-valid-wins matches the bulk read. */ |
| 49 |
public function get_meta_data(): array { |
| 50 |
global $wpdb; |
| 51 |
if ( ! isset( $wpdb ) ) { |
| 52 |
return array(); |
| 53 |
} |
| 54 |
$rows = $wpdb->get_results( |
| 55 |
$wpdb->prepare( |
| 56 |
"SELECT meta_id, meta_value FROM {$wpdb->termmeta} WHERE term_id = %d AND meta_key = %s ORDER BY meta_id ASC", |
| 57 |
$this->term_id, |
| 58 |
Pos_Uuid::META_KEY |
| 59 |
), |
| 60 |
ARRAY_A |
| 61 |
); |
| 62 |
$out = array(); |
| 63 |
foreach ( (array) $rows as $row ) { |
| 64 |
if ( ! \is_array( $row ) ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
$out[] = (object) array( |
| 68 |
'id' => (int) ( $row['meta_id'] ?? 0 ), |
| 69 |
'key' => Pos_Uuid::META_KEY, |
| 70 |
'value' => $row['meta_value'] ?? '', |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
return $out; |
| 75 |
} |
| 76 |
|
| 77 |
public function update_meta_data( string $key, $value ): void { |
| 78 |
$this->pending[ $key ] = $value; // replace-or-add (matches WC_Data) |
| 79 |
} |
| 80 |
|
| 81 |
public function save_meta_data(): void { |
| 82 |
if ( \function_exists( 'update_term_meta' ) ) { |
| 83 |
foreach ( $this->pending as $key => $value ) { |
| 84 |
// update_term_meta() rewrites EVERY matching row to the new value |
| 85 |
// without collapsing extras — a collision re-key on a term with |
| 86 |
// duplicate uuid rows would keep the duplicates. Enforce the |
| 87 |
// one-row contract: delete all rows for the key, then add one. |
| 88 |
if ( \function_exists( 'get_term_meta' ) && \function_exists( 'delete_term_meta' ) |
| 89 |
&& \count( (array) get_term_meta( $this->term_id, $key, false ) ) > 1 ) { |
| 90 |
delete_term_meta( $this->term_id, $key ); |
| 91 |
add_term_meta( $this->term_id, $key, $value, true ); |
| 92 |
continue; |
| 93 |
} |
| 94 |
update_term_meta( $this->term_id, $key, $value ); |
| 95 |
} |
| 96 |
} |
| 97 |
$this->pending = array(); |
| 98 |
} |
| 99 |
|
| 100 |
public function delete_meta_data_by_mid( $mid ): void { |
| 101 |
if ( $mid && \function_exists( 'delete_metadata_by_mid' ) ) { |
| 102 |
delete_metadata_by_mid( 'term', $mid ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|