1 <?php
  2 
  3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  4 
  5 /**
  6  * Post Data
  7  *
  8  * Standardises certain post data on save.
  9  *
 10  * @class       WC_Post_Data
 11  * @version     2.1.0
 12  * @package     WooCommerce/Classes/Data
 13  * @category    Class
 14  * @author      WooThemes
 15  */
 16 class WC_Post_Data {
 17 
 18     private $editing_term = null;
 19 
 20     /**
 21      * Constructor
 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      * When editing a term, check for product attributes
 32      * @param  id $term_id
 33      * @param  id $tt_id
 34      * @param  string $taxonomy
 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      * When a term is edited, check for product attributes and update variations
 46      * @param  id $term_id
 47      * @param  id $tt_id
 48      * @param  string $taxonomy
 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      * Ensure floats are correctly converted to strings based on PHP locale
 66      * 
 67      * @param  null $check
 68      * @param  int $object_id
 69      * @param  string $meta_key
 70      * @param  mixed $meta_value
 71      * @param  mixed $prev_value
 72      * @return null|bool
 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             // Convert float to string
 78             $meta_value = wc_float_to_string( $meta_value );
 79 
 80             // Update meta value with new string
 81             update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value );
 82 
 83             // Return
 84             return true;
 85         }
 86         return $check;
 87     }
 88 
 89     /**
 90      * Ensure floats are correctly converted to strings based on PHP locale
 91      * 
 92      * @param  null $check
 93      * @param  int $object_id
 94      * @param  string $meta_key
 95      * @param  mixed $meta_value
 96      * @param  mixed $prev_value
 97      * @return null|bool
 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             // Convert float to string
103             $meta_value = wc_float_to_string( $meta_value );
104 
105             // Update meta value with new string
106             update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value );
107 
108             // Return
109             return true;
110         }
111         return $check;
112     }
113 
114 }
115 
116 new WC_Post_Data();
WooCommerce API documentation generated by ApiGen 2.8.0