Move generic method to parent class for reusability.

This commit is contained in:
Vedanshu Jain 2023-02-22 19:42:47 +05:30
parent 6d162a9950
commit f3cb5424d7
2 changed files with 24 additions and 28 deletions

View File

@ -325,6 +325,29 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
return $result;
}
/**
* With HPOS, few internal meta keys such as _billing_address_index, _shipping_address_index are not considered internal anymore (since most internal keys were flattened into dedicated columns).
*
* This function helps in filtering out any remaining internal meta keys with HPOS is enabled.
*
* @param array $meta_data Order meta data.
*
* @return array Filtered order meta data.
*/
private function filter_internal_meta_keys( $meta_data ) {
if ( ! OrderUtil::custom_orders_table_usage_is_enabled() ) {
return $meta_data;
}
$cpt_hidden_keys = ( new \WC_Order_Data_Store_CPT() )->get_internal_meta_keys();
$meta_data = array_filter(
$meta_data,
function ( $meta ) use ( $cpt_hidden_keys ) {
return ! in_array( $meta->key, $cpt_hidden_keys, true );
}
);
return array_values( $meta_data );
}
/**
* Get formatted item data.
*
@ -369,6 +392,7 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
case 'meta_data':
$meta_data = $order->get_meta_data();
$data['meta_data'] = $this->get_meta_data_for_response( $this->request, $meta_data );
$data['meta_data'] = $this->filter_internal_meta_keys( $data['meta_data'] );
break;
case 'line_items':
$data['line_items'] = $order->get_items( 'line_item' );

View File

@ -235,34 +235,6 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
}
}
/**
* Get formatted item data.
*
* @param WC_Order $order WC_Data instance.
* @return array
*/
protected function get_formatted_item_data( $order ) {
$item_data = parent::get_formatted_item_data( $order );
$cpt_hidden_keys = array();
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$cpt_hidden_keys = ( new \WC_Order_Data_Store_CPT() )->get_internal_meta_keys();
}
// XXX: This might be removed once we finalize the design for internal keys vs meta vs props in COT.
if ( ! empty( $item_data['meta_data'] ) ) {
$item_data['meta_data'] = array_filter(
$item_data['meta_data'],
function ( $meta ) use ( $cpt_hidden_keys ) {
return ! in_array( $meta->key, $cpt_hidden_keys, true );
}
);
$item_data['meta_data'] = array_values( $item_data['meta_data'] );
}
return $item_data;
}
/**
* Prepare objects query.
*