1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13 14 15
16 class WC_Post_Data {
17
18 private $editing_term = null;
19
20 21 22
23 public function __construct() {
24 add_action( 'edit_term', array( $this, 'edit_term' ), 10, 3 );
25 add_action( 'edited_term', array( $this, 'edited_term' ), 10, 3 );
26 add_filter( 'update_order_item_metadata', array( $this, 'update_order_item_metadata' ), 10, 5 );
27 add_filter( 'update_post_metadata', array( $this, 'update_post_metadata' ), 10, 5 );
28 }
29
30 31 32 33 34 35
36 public function edit_term( $term_id, $tt_id, $taxonomy ) {
37 if ( strpos( $taxonomy, 'pa_' ) === 0 ) {
38 $this->editing_term = get_term_by( 'id', $term_id, $taxonomy );
39 } else {
40 $this->editing_term = null;
41 }
42 }
43
44 45 46 47 48 49
50 public function edited_term( $term_id, $tt_id, $taxonomy ) {
51 if ( ! is_null( $this->editing_term ) && strpos( $taxonomy, 'pa_' ) === 0 ) {
52 $edited_term = get_term_by( 'id', $term_id, $taxonomy );
53
54 if ( $edited_term->slug !== $this->editing_term->slug ) {
55 global $wpdb;
56
57 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND meta_value = %s;", $edited_term->slug, 'attribute_' . sanitize_title( $taxonomy ), $this->editing_term->slug ) );
58 }
59 } else {
60 $this->editing_term = null;
61 }
62 }
63
64 65 66 67 68 69 70 71 72 73
74 public function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
75 if ( ! empty( $meta_value ) && is_float( $meta_value ) ) {
76
77
78 $meta_value = wc_float_to_string( $meta_value );
79
80
81 update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value );
82
83
84 return true;
85 }
86 return $check;
87 }
88
89 90 91 92 93 94 95 96 97 98
99 public function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
100 if ( ! empty( $meta_value ) && is_float( $meta_value ) && in_array( get_post_type( $object_id ), array( 'shop_order', 'shop_coupon', 'product', 'product_variation' ) ) ) {
101
102
103 $meta_value = wc_float_to_string( $meta_value );
104
105
106 update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value );
107
108
109 return true;
110 }
111 return $check;
112 }
113
114 }
115
116 new WC_Post_Data();