rebase on oct-10-2023
This commit is contained in:
@@ -106,6 +106,23 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an order exists by id.
|
||||
*
|
||||
* @since 8.0.0
|
||||
*
|
||||
* @param int $order_id The order id to check.
|
||||
* @return bool True if an order exists with the given name.
|
||||
*/
|
||||
public function order_exists( $order_id ) : bool {
|
||||
if ( ! $order_id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$post_object = get_post( $order_id );
|
||||
return ! is_null( $post_object ) && in_array( $post_object->post_type, wc_get_order_types(), true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to read an order from the database.
|
||||
*
|
||||
@@ -120,7 +137,8 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
throw new Exception( __( 'Invalid order.', 'woocommerce' ) );
|
||||
}
|
||||
|
||||
$order->set_props(
|
||||
$this->set_order_props(
|
||||
$order,
|
||||
array(
|
||||
'parent_id' => $post_object->post_parent,
|
||||
'date_created' => $this->string_to_timestamp( $post_object->post_date_gmt ),
|
||||
@@ -143,6 +161,43 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the properties of an object and log the first error found while doing so.
|
||||
*
|
||||
* @param $order WC_Order $order Order object.
|
||||
* @param array $props The properties to set.
|
||||
*/
|
||||
private function set_order_props( &$order, array $props ) {
|
||||
$errors = $order->set_props( $props );
|
||||
|
||||
if ( ! $errors instanceof WP_Error ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$order_id = $order->get_id();
|
||||
$logger = WC()->call_function( 'wc_get_logger' );
|
||||
|
||||
foreach ( $errors->get_error_codes() as $error_code ) {
|
||||
$property_name = $errors->get_error_data( $error_code )['property_name'] ?? '';
|
||||
$error_message = $errors->get_error_message( $error_code );
|
||||
$logger->warning(
|
||||
sprintf(
|
||||
/* translators: %1$s = order ID, %2$s = order id, %3$s = error message. */
|
||||
__( 'Error when setting property \'%1$s\' for order %2$d: %3$s', 'woocommerce' ),
|
||||
$property_name,
|
||||
$order_id,
|
||||
$error_message
|
||||
),
|
||||
array(
|
||||
'error_code' => $error_code,
|
||||
'error_message' => $error_message,
|
||||
'order_id' => $order_id,
|
||||
'property_name' => $property_name,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to update an order in the database.
|
||||
*
|
||||
@@ -204,7 +259,8 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
$args = wp_parse_args(
|
||||
$args,
|
||||
array(
|
||||
'force_delete' => false,
|
||||
'force_delete' => false,
|
||||
'suppress_filters' => false,
|
||||
)
|
||||
);
|
||||
|
||||
@@ -212,14 +268,60 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
return;
|
||||
}
|
||||
|
||||
$do_filters = ! $args['suppress_filters'];
|
||||
|
||||
if ( $args['force_delete'] ) {
|
||||
if ( $do_filters ) {
|
||||
/**
|
||||
* Fires immediately before an order is deleted from the database.
|
||||
*
|
||||
* @since 8.0.0
|
||||
*
|
||||
* @param int $order_id ID of the order about to be deleted.
|
||||
* @param WC_Order $order Instance of the order that is about to be deleted.
|
||||
*/
|
||||
do_action( 'woocommerce_before_delete_order', $id, $order );
|
||||
}
|
||||
|
||||
wp_delete_post( $id );
|
||||
$order->set_id( 0 );
|
||||
do_action( 'woocommerce_delete_order', $id );
|
||||
|
||||
if ( $do_filters ) {
|
||||
/**
|
||||
* Fires immediately after an order is deleted.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @param int $order_id ID of the order that has been deleted.
|
||||
*/
|
||||
do_action( 'woocommerce_delete_order', $id );
|
||||
}
|
||||
} else {
|
||||
if ( $do_filters ) {
|
||||
/**
|
||||
* Fires immediately before an order is trashed.
|
||||
*
|
||||
* @since 8.0.0
|
||||
*
|
||||
* @param int $order_id ID of the order about to be trashed.
|
||||
* @param WC_Order $order Instance of the order that is about to be trashed.
|
||||
*/
|
||||
do_action( 'woocommerce_before_trash_order', $id, $order );
|
||||
}
|
||||
|
||||
wp_trash_post( $id );
|
||||
$order->set_status( 'trash' );
|
||||
do_action( 'woocommerce_trash_order', $id );
|
||||
|
||||
if ( $do_filters ) {
|
||||
/**
|
||||
* Fires immediately after an order is trashed.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @param int $order_id ID of the order that has been trashed.
|
||||
*/
|
||||
do_action( 'woocommerce_trash_order', $id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +404,8 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
protected function read_order_data( &$order, $post_object ) {
|
||||
$id = $order->get_id();
|
||||
|
||||
$order->set_props(
|
||||
$this->set_order_props(
|
||||
$order,
|
||||
array(
|
||||
'currency' => get_post_meta( $id, '_order_currency', true ),
|
||||
'discount_total' => get_post_meta( $id, '_cart_discount', true ),
|
||||
@@ -599,11 +702,11 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to update order metadata from intialized order object.
|
||||
* Helper method to update order metadata from initialized order object.
|
||||
*
|
||||
* @param WC_Abstract_Order $order Order object.
|
||||
*/
|
||||
private function update_order_meta_from_object( $order ) {
|
||||
protected function update_order_meta_from_object( $order ) {
|
||||
if ( is_null( $order->get_meta() ) ) {
|
||||
return;
|
||||
}
|
||||
@@ -612,13 +715,24 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
|
||||
foreach ( $order->get_meta_data() as $meta_data ) {
|
||||
if ( isset( $existing_meta_data[ $meta_data->key ] ) ) {
|
||||
if ( $existing_meta_data[ $meta_data->key ] === $meta_data->value ) {
|
||||
// We don't know if the meta is single or array, so we assume it to be an array.
|
||||
$meta_value = is_array( $meta_data->value ) ? $meta_data->value : array( $meta_data->value );
|
||||
|
||||
if ( $existing_meta_data[ $meta_data->key ] === $meta_value ) {
|
||||
unset( $existing_meta_data[ $meta_data->key ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
unset( $existing_meta_data[ $meta_data->key ] );
|
||||
delete_post_meta( $order->get_id(), $meta_data->key );
|
||||
if ( is_array( $existing_meta_data[ $meta_data->key ] ) ) {
|
||||
$value_index = array_search( $meta_data->value, $existing_meta_data[ $meta_data->key ], true );
|
||||
if ( false !== $value_index ) {
|
||||
unset( $existing_meta_data[ $meta_data->key ][ $value_index ] );
|
||||
if ( 0 === count( $existing_meta_data[ $meta_data->key ] ) ) {
|
||||
unset( $existing_meta_data[ $meta_data->key ] );
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
add_post_meta( $order->get_id(), $meta_data->key, $meta_data->value, false );
|
||||
}
|
||||
@@ -632,7 +746,11 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||
);
|
||||
|
||||
foreach ( $keys_to_delete as $meta_key ) {
|
||||
delete_post_meta( $order->get_id(), $meta_key );
|
||||
if ( isset( $existing_meta_data[ $meta_key ] ) ) {
|
||||
foreach ( $existing_meta_data[ $meta_key ] as $meta_value ) {
|
||||
delete_post_meta( $order->get_id(), $meta_key, maybe_unserialize( $meta_value ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->update_post_meta( $order );
|
||||
|
||||
@@ -532,16 +532,16 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
|
||||
);
|
||||
|
||||
$query_for_tentative_usages = $this->get_tentative_usage_query( $coupon->get_id() );
|
||||
$db_timestamp = $wpdb->get_var( 'SELECT UNIX_TIMESTAMP() FROM DUAL' );
|
||||
$db_timestamp = $wpdb->get_var( 'SELECT UNIX_TIMESTAMP() FROM ' . $wpdb->posts . ' LIMIT 1' );
|
||||
|
||||
$coupon_usage_key = '_coupon_held_' . ( (int) $db_timestamp + $held_time ) . '_' . wp_generate_password( 6, false );
|
||||
|
||||
$insert_statement = $wpdb->prepare(
|
||||
"
|
||||
INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value )
|
||||
SELECT %d, %s, %s FROM DUAL
|
||||
SELECT %d, %s, %s FROM $wpdb->posts
|
||||
WHERE ( $query_for_usages ) + ( $query_for_tentative_usages ) < %d
|
||||
",
|
||||
LIMIT 1",
|
||||
$coupon->get_id(),
|
||||
$coupon_usage_key,
|
||||
'',
|
||||
@@ -629,15 +629,15 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
|
||||
); // WPCS: unprepared SQL ok.
|
||||
|
||||
$query_for_tentative_usages = $this->get_tentative_usage_query_for_user( $coupon->get_id(), $user_aliases );
|
||||
$db_timestamp = $wpdb->get_var( 'SELECT UNIX_TIMESTAMP() FROM DUAL' );
|
||||
$db_timestamp = $wpdb->get_var( 'SELECT UNIX_TIMESTAMP() FROM ' . $wpdb->posts . ' LIMIT 1' );
|
||||
|
||||
$coupon_used_by_meta_key = '_maybe_used_by_' . ( (int) $db_timestamp + $held_time ) . '_' . wp_generate_password( 6, false );
|
||||
$insert_statement = $wpdb->prepare(
|
||||
"
|
||||
INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value )
|
||||
SELECT %d, %s, %s FROM DUAL
|
||||
SELECT %d, %s, %s FROM $wpdb->posts
|
||||
WHERE ( $query_for_usages ) + ( $query_for_tentative_usages ) < %d
|
||||
",
|
||||
LIMIT 1",
|
||||
$coupon->get_id(),
|
||||
$coupon_used_by_meta_key,
|
||||
$user_alias,
|
||||
|
||||
@@ -538,7 +538,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
||||
|
||||
$unpaid_orders = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
// @codingStandardsIgnoreStart
|
||||
// @codingStandardsIgnoreStart
|
||||
"SELECT posts.ID
|
||||
FROM {$wpdb->posts} AS posts
|
||||
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
|
||||
@@ -577,6 +577,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
||||
'_shipping_address_index',
|
||||
'_billing_last_name',
|
||||
'_billing_email',
|
||||
'_billing_phone',
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -603,6 +604,16 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
||||
WHERE order_item_name LIKE %s",
|
||||
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
|
||||
)
|
||||
),
|
||||
$wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT DISTINCT os.order_id FROM {$wpdb->prefix}wc_order_stats os
|
||||
INNER JOIN {$wpdb->prefix}wc_customer_lookup cl ON os.customer_id = cl.customer_id
|
||||
INNER JOIN {$wpdb->usermeta} um ON cl.user_id = um.user_id
|
||||
WHERE (um.meta_key = 'billing_phone' OR um.meta_key = 'shipping_phone')
|
||||
AND um.meta_value = %s",
|
||||
wc_clean( $term )
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -1170,4 +1181,20 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
||||
);
|
||||
WC_Order::prime_raw_meta_data_cache( $raw_meta_data_collection, 'orders' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to restore the specified order back to its original status (after having been trashed).
|
||||
*
|
||||
* @param WC_Order $order The order to be untrashed.
|
||||
*
|
||||
* @return bool If the operation was successful.
|
||||
*/
|
||||
public function untrash_order( WC_Order $order ): bool {
|
||||
if ( ! wp_untrash_post( $order->get_id() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$order->set_status( get_post_field( 'post_status', $order->get_id() ) );
|
||||
return (bool) $order->save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
|
||||
$changes = $product->get_changes();
|
||||
|
||||
// Only update the post when the post data changes.
|
||||
if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug' ), array_keys( $changes ) ) ) {
|
||||
if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug', 'post_password' ), array_keys( $changes ) ) ) {
|
||||
$post_data = array(
|
||||
'post_content' => $product->get_description( 'edit' ),
|
||||
'post_excerpt' => $product->get_short_description( 'edit' ),
|
||||
@@ -1137,7 +1137,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
|
||||
/**
|
||||
* Check each variation to find the one that matches the $match_attributes.
|
||||
*
|
||||
* Note: Not all meta fields will be set which is why we check existance.
|
||||
* Note: Not all meta fields will be set which is why we check existence.
|
||||
*/
|
||||
foreach ( $sorted_meta as $variation_id => $variation ) {
|
||||
$match = true;
|
||||
|
||||
@@ -119,11 +119,11 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple
|
||||
public function read_children( &$product, $force_read = false ) {
|
||||
$children_transient_name = 'wc_product_children_' . $product->get_id();
|
||||
$children = get_transient( $children_transient_name );
|
||||
if ( false === $children ) {
|
||||
if ( empty( $children ) || ! is_array( $children ) ) {
|
||||
$children = array();
|
||||
}
|
||||
|
||||
if ( empty( $children ) || ! is_array( $children ) || ! isset( $children['all'] ) || ! isset( $children['visible'] ) || $force_read ) {
|
||||
if ( ! isset( $children['all'] ) || ! isset( $children['visible'] ) || $force_read ) {
|
||||
$all_args = array(
|
||||
'post_parent' => $product->get_id(),
|
||||
'post_type' => 'product_variation',
|
||||
|
||||
@@ -282,6 +282,7 @@ class WC_Webhook_Data_Store implements WC_Webhook_Data_Store_Interface {
|
||||
$exclude = '';
|
||||
$date_created = '';
|
||||
$date_modified = '';
|
||||
$user_id = '';
|
||||
|
||||
if ( ! empty( $args['include'] ) ) {
|
||||
$args['include'] = implode( ',', wp_parse_id_list( $args['include'] ) );
|
||||
@@ -293,6 +294,10 @@ class WC_Webhook_Data_Store implements WC_Webhook_Data_Store_Interface {
|
||||
$exclude = 'AND webhook_id NOT IN (' . $args['exclude'] . ')';
|
||||
}
|
||||
|
||||
if ( ! empty( $args['user_id'] ) ) {
|
||||
$user_id = $wpdb->prepare( 'AND `user_id` = %d', absint( $args['user_id'] ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $args['after'] ) || ! empty( $args['before'] ) ) {
|
||||
$args['after'] = empty( $args['after'] ) ? '0000-00-00' : $args['after'];
|
||||
$args['before'] = empty( $args['before'] ) ? current_time( 'mysql', 1 ) : $args['before'];
|
||||
@@ -326,6 +331,7 @@ class WC_Webhook_Data_Store implements WC_Webhook_Data_Store_Interface {
|
||||
{$exclude}
|
||||
{$date_created}
|
||||
{$date_modified}
|
||||
{$user_id}
|
||||
{$order}
|
||||
{$limit}
|
||||
{$offset}"
|
||||
@@ -349,6 +355,7 @@ class WC_Webhook_Data_Store implements WC_Webhook_Data_Store_Interface {
|
||||
{$exclude}
|
||||
{$date_created}
|
||||
{$date_modified}
|
||||
{$user_id}
|
||||
{$order}
|
||||
{$limit}
|
||||
{$offset}"
|
||||
|
||||
Reference in New Issue
Block a user