Removed/reverted more unnecessary changes.

This commit is contained in:
vedanshujain 2020-09-16 21:24:12 +05:30
parent 12f69c2d2d
commit bb1e2f44ae
6 changed files with 51 additions and 120 deletions

View File

@ -392,19 +392,6 @@ abstract class WC_Data {
}
}
/**
* Helper function to create and set WC_Meta_data object from std objects. Automatically ignores internal keys.
*
* @param array $raw_data Array of objects of raw data to set.
*/
public function set_meta_data_from_raw_data( $raw_data ) {
if ( null === $this->meta_data ) {
$this->meta_data = array();
}
$meta_data = $this->data_store->filter_raw_data( $this, $raw_data );
$this->set_from_raw_meta_data( $meta_data );
}
/**
* Add meta data.
*
@ -535,7 +522,7 @@ abstract class WC_Data {
/**
* Helper method to compute meta cache key. Different from WP Meta cache key.
*
* @since 4.4.0
* @since 4.7.0
*
* @return string
*/
@ -550,6 +537,8 @@ abstract class WC_Data {
/**
* Generate cache key from id and group.
*
* @since 4.7.0
*
* @param int|string $id Object ID.
* @param string $cache_group Group name use to store cache. Whole group cache can be invalidated in one go.
*
@ -562,6 +551,8 @@ abstract class WC_Data {
/**
* Prime caches for raw meta data. This includes meta_id column as well, which is not included by default in WP meta data.
*
* @since 4.7.0
*
* @param array $raw_meta_data_collection Array of objects of { object_id => array( meta_row_1, meta_row_2, ... }.
* @param string $cache_group Name of cache group.
*/
@ -610,7 +601,15 @@ abstract class WC_Data {
}
if ( $raw_meta_data ) {
$this->set_from_raw_meta_data( $raw_meta_data );
foreach ( $raw_meta_data as $meta ) {
$this->meta_data[] = new WC_Meta_Data(
array(
'id' => (int) $meta->meta_id,
'key' => $meta->meta_key,
'value' => maybe_unserialize( $meta->meta_value ),
)
);
}
if ( ! $cache_loaded && ! empty( $this->cache_group ) ) {
wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group );
@ -618,26 +617,6 @@ abstract class WC_Data {
}
}
/**
* Helper function to create WC_Meta_data object from std objects.
*
* @param array $raw_meta_data Array of object of raw meta data.
*/
private function set_from_raw_meta_data( $raw_meta_data ) {
if ( ! is_array( $raw_meta_data ) ) {
return;
}
foreach ( $raw_meta_data as $meta ) {
$this->meta_data[] = new WC_Meta_Data(
array(
'id' => (int) $meta->meta_id,
'key' => $meta->meta_key,
'value' => maybe_unserialize( $meta->meta_value ),
)
);
}
}
/**
* Update Meta Data in the database.
*

View File

@ -18,41 +18,17 @@ class WC_Order_Factory {
/**
* Get order.
*
* @param mixed $order (default: false) Order ID to get.
*
* @param mixed $order_id (default: false) Order ID to get.
* @return WC_Order|bool
*/
public static function get_order( $order = false ) {
$order_id = self::get_order_id( $order );
public static function get_order( $order_id = false ) {
$order_id = self::get_order_id( $order_id );
if ( ! $order_id ) {
return false;
}
$order_type = WC_Data_Store::load( 'order' )->get_order_type( $order_id );
$classname = self::get_class_name_for_order( $order_id, $order_type );
if ( ! $classname ) {
return false;
}
try {
return new $classname( $order_id );
} catch ( Exception $e ) {
wc_caught_exception( $e, __FUNCTION__, array( $order_id ) );
return false;
}
}
/**
* Helper method to fetch class name for an order.
*
* @param int $order_id Order ID.
* @param string $order_type Order type.
*
* @return bool|mixed|void
*/
private static function get_class_name_for_order( $order_id, $order_type ) {
$order_type = WC_Data_Store::load( 'order' )->get_order_type( $order_id );
$order_type_data = wc_get_order_type( $order_type );
if ( $order_type_data ) {
$classname = $order_type_data['class_name'];
@ -67,17 +43,21 @@ class WC_Order_Factory {
return false;
}
return $classname;
try {
return new $classname( $order_id );
} catch ( Exception $e ) {
wc_caught_exception( $e, __FUNCTION__, array( $order_id ) );
return false;
}
}
/**
* Helper method to fetch class for order item.
* Get order item.
*
* @param int $item_id Order Item ID.
*
* @return bool|mixed|string|void Class name.
* @param int $item_id Order item ID to get.
* @return WC_Order_Item|false if not found
*/
public static function get_order_item_class( $item_id = 0 ) {
public static function get_order_item( $item_id = 0 ) {
if ( is_numeric( $item_id ) ) {
$item_type = WC_Data_Store::load( 'order-item' )->get_order_item_type( $item_id );
$id = $item_id;
@ -91,9 +71,9 @@ class WC_Order_Factory {
$item_type = false;
$id = false;
}
$classname = false;
if ( $id && $item_type ) {
$classname = false;
switch ( $item_type ) {
case 'line_item':
case 'product':
@ -112,25 +92,15 @@ class WC_Order_Factory {
$classname = 'WC_Order_Item_Tax';
break;
}
$classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id );
}
return $classname;
}
/**
* Get order item.
*
* @param int $item_id Order item ID to get.
* @return WC_Order_Item|false if not found
*/
public static function get_order_item( $item_id = 0 ) {
$classname = self::get_order_item_class( $item_id );
if ( $classname && class_exists( $classname ) ) {
try {
return new $classname( $item_id );
} catch ( Exception $e ) {
return false;
if ( $classname && class_exists( $classname ) ) {
try {
return new $classname( $id );
} catch ( Exception $e ) {
return false;
}
}
}
return false;

View File

@ -64,18 +64,10 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
$this->set_id( $item->get_id() );
} elseif ( is_numeric( $item ) && $item > 0 ) {
$this->set_id( $item );
} elseif ( is_object( $item ) && isset( $item->order_item_id ) ) {
// TODO: Could this cause issue with child classes. Should be fine as long as they are calling parent::__construct.
$this->set_id( $item->order_item_id );
} else {
$this->set_object_read( true );
}
if ( isset( $item->metadata ) && is_array( $item->metadata ) ) {
$meta_cache_key = $this->get_meta_cache_key();
wp_cache_set( $meta_cache_key, $item->metadata, $this->cache_group );
}
$type = 'line_item' === $this->get_type() ? 'product' : $this->get_type();
$this->data_store = WC_Data_Store::load( 'order-item-' . $type );
if ( $this->get_id() > 0 ) {

View File

@ -100,29 +100,6 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
public function read( &$order ) {
$order->set_defaults();
$post_object = get_post( $order->get_id() );
$this->read_from_post( $order, $post_object );
$order->read_meta_data();
$order->set_object_read( true );
/**
* In older versions, discounts may have been stored differently.
* Update them now so if the object is saved, the correct values are
* stored. @todo When meta is flattened, handle this during migration.
*/
if ( version_compare( $order->get_version( 'edit' ), '2.3.7', '<' ) && $order->get_prices_include_tax( 'edit' ) ) {
$order->set_discount_total( (float) get_post_meta( $order->get_id(), '_cart_discount', true ) - (float) get_post_meta( $order->get_id(), '_cart_discount_tax', true ) );
}
}
/**
* Helper function to read data from post_object.
*
* @param WC_Order $order Order object.
* @param WP_Post $post_object Post object.
*
* @throws Exception When passed order object is invalid.
*/
private function read_from_post( $order, $post_object ) {
if ( ! $order->get_id() || ! $post_object || ! in_array( $post_object->post_type, wc_get_order_types(), true ) ) {
throw new Exception( __( 'Invalid order.', 'woocommerce' ) );
}
@ -137,6 +114,17 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
);
$this->read_order_data( $order, $post_object );
$order->read_meta_data();
$order->set_object_read( true );
/**
* In older versions, discounts may have been stored differently.
* Update them now so if the object is saved, the correct values are
* stored. @todo When meta is flattened, handle this during migration.
*/
if ( version_compare( $order->get_version( 'edit' ), '2.3.7', '<' ) && $order->get_prices_include_tax( 'edit' ) ) {
$order->set_discount_total( (float) get_post_meta( $order->get_id(), '_cart_discount', true ) - (float) get_post_meta( $order->get_id(), '_cart_discount_tax', true ) );
}
}
/**

View File

@ -100,6 +100,8 @@ class WC_Data_Store_WP {
/**
* Helper method to filter internal meta keys from all meta data rows for the object.
*
* @since 4.7.0
*
* @param WC_Data $object WC_Data object.
* @param array $raw_meta_data Array of std object of meta data to be filtered.
*

View File

@ -930,7 +930,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
$cache_keys_mapping[ $order_id ] = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'refunds' . $order_id;
}
$non_cached_ids = array();
$cache_values = wp_cache_get_multiple( array_keys( $cache_keys_mapping ), 'orders' );
$cache_values = wp_cache_get_multiple( array_values( $cache_keys_mapping ), 'orders' );
foreach ( $order_ids as $order_id ) {
if ( false === $cache_values[ $cache_keys_mapping[ $order_id ] ] ) {
$non_cached_ids[] = $order_id;
@ -1049,7 +1049,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
foreach ( $order_ids as $order_id ) {
$cache_keys_mapping[ $order_id ] = WC_Order::generate_meta_cache_key( $order_id, 'orders' );
}
$cache_values = wp_cache_get_multiple( $cache_keys_mapping, 'orders' );
$cache_values = wp_cache_get_multiple( array_values( $cache_keys_mapping ), 'orders' );
$non_cached_ids = array();
foreach ( $order_ids as $order_id ) {
if ( false === $cache_values[ $cache_keys_mapping[ $order_id ] ] ) {