rebase on oct-10-2023

This commit is contained in:
Rachit Bhargava
2023-10-10 17:23:21 -04:00
parent d37566ffb6
commit d096058d7d
4789 changed files with 254611 additions and 307223 deletions

View File

@@ -81,7 +81,22 @@ class WC_REST_Telemetry_Controller extends WC_REST_Controller {
}
$platform = $new['platform'];
if ( ! $data[ $platform ] || version_compare( $new['version'], $data[ $platform ]['version'], '>=' ) ) {
if ( isset( $data[ $platform ] ) ) {
$existing_usage = $data[ $platform ];
// Sets the installation date only if it has not been set before.
if ( isset( $new['installation_date'] ) && ! isset( $existing_usage['installation_date'] ) ) {
$data[ $platform ]['installation_date'] = $new['installation_date'];
}
if ( version_compare( $new['version'], $existing_usage['version'], '>=' ) ) {
$data[ $platform ]['version'] = $new['version'];
$data[ $platform ]['last_used'] = $new['last_used'];
}
} else {
// Only sets `first_used` when the platform usage data hasn't been set before.
$new['first_used'] = $new['last_used'];
$data[ $platform ] = $new;
}
@@ -109,10 +124,19 @@ class WC_REST_Telemetry_Controller extends WC_REST_Controller {
return;
}
return array(
'platform' => sanitize_text_field( $platform ),
'version' => sanitize_text_field( $version ),
'last_used' => gmdate( 'c' ),
// The installation date could be null from earlier mobile client versions.
$installation_date = $request->get_param( 'installation_date' );
return array_filter(
array(
'platform' => sanitize_text_field( $platform ),
'version' => sanitize_text_field( $version ),
'last_used' => gmdate( 'c' ),
'installation_date' => isset( $installation_date ) ? get_gmt_from_date( $installation_date, 'c' ) : null,
),
function( $value ) {
return null !== $value;
}
);
}
@@ -123,20 +147,27 @@ class WC_REST_Telemetry_Controller extends WC_REST_Controller {
*/
public function get_collection_params() {
return array(
'platform' => array(
'platform' => array(
'description' => __( 'Platform to track.', 'woocommerce' ),
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'version' => array(
'version' => array(
'description' => __( 'Platform version to track.', 'woocommerce' ),
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'installation_date' => array(
'description' => __( 'Installation date of the WooCommerce mobile app.', 'woocommerce' ),
'required' => false, // For backward compatibility.
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
),
);
}
}

View File

@@ -55,7 +55,7 @@ class WC_REST_Customers_V2_Controller extends WC_REST_Customers_V1_Controller {
// Format date values.
foreach ( $format_date as $key ) {
// Date created is stored UTC, date modified is stored WP local time.
$datetime = 'date_created' === $key ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $data[ $key ];
$datetime = 'date_created' === $key && is_subclass_of( $data[ $key ], 'DateTime' ) ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $data[ $key ];
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
}

View File

@@ -394,7 +394,7 @@ class WC_REST_Order_Refunds_V2_Controller extends WC_REST_Orders_V2_Controller {
'refunded_payment' => array(
'description' => __( 'If the payment was refunded via the API.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view' ),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'meta_data' => array(

View File

@@ -768,6 +768,7 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
if ( $creating ) {
$object->set_created_via( 'rest-api' );
$object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$object->save();
$object->calculate_totals();
} else {
// If items have changed, recalculate order totals.

View File

@@ -205,6 +205,7 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
if ( $creating ) {
$object->set_created_via( 'rest-api' );
$object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$object->save();
$object->calculate_totals();
} else {
// If items have changed, recalculate order totals.

View File

@@ -44,6 +44,10 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
'type' => 'integer',
),
'delete' => array(
'description' => __( 'Deletes unused variations.', 'woocommerce' ),
'type' => 'boolean',
),
),
array(
'methods' => WP_REST_Server::CREATABLE,
@@ -926,6 +930,40 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
return $params;
}
/**
* Deletes all unmatched variations (aka duplicates).
*
* @param WC_Product $product Variable product.
* @return int Number of deleted variations.
*/
private function delete_unmatched_product_variations( $product ) {
$deleted_count = 0;
if ( ! $product ) {
return $deleted_count;
}
$attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' );
// Get existing variations so we don't create duplicates.
$existing_variations = array_map( 'wc_get_product', $product->get_children() );
$possible_attribute_combinations = array_reverse( wc_array_cartesian( $attributes ) );
foreach ( $existing_variations as $existing_variation ) {
$matching_attribute_key = array_search( $existing_variation->get_attributes(), $possible_attribute_combinations ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
if ( $matching_attribute_key !== false ) {
// We only want one possible variation for each possible attribute combination.
unset( $possible_attribute_combinations[ $matching_attribute_key ] );
continue;
}
$existing_variation->delete( true );
$deleted_count ++;
}
return $deleted_count;
}
/**
* Generate all variations for a given product.
*
@@ -947,6 +985,11 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
$data_store = $product->get_data_store();
$response['count'] = $data_store->create_all_product_variations( $product, Constants::get_constant( 'WC_MAX_LINKED_VARIATIONS' ) );
if ( isset( $request['delete'] ) && $request['delete'] ) {
$deleted_count = $this->delete_unmatched_product_variations( $product );
$response['deleted_count'] = $deleted_count;
}
$data_store->sort_all_product_variations( $product->get_id() );
return rest_ensure_response( $response );

View File

@@ -432,6 +432,11 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$product->set_reviews_allowed( $request['reviews_allowed'] );
}
// Post password.
if ( isset( $request['post_password'] ) ) {
$product->set_post_password( $request['post_password'] );
}
// Virtual.
if ( isset( $request['virtual'] ) ) {
$product->set_virtual( $request['virtual'] );
@@ -1140,6 +1145,11 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
'default' => true,
'context' => array( 'view', 'edit' ),
),
'post_password' => array(
'description' => __( 'Post password.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'average_rating' => array(
'description' => __( 'Reviews average rating.', 'woocommerce' ),
'type' => 'string',
@@ -1496,6 +1506,10 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$data['has_options'] = $product->has_options( $context );
}
if ( in_array( 'post_password', $fields, true ) ) {
$data['post_password'] = $product->get_post_password( $context );
}
$post_type_obj = get_post_type_object( $this->post_type );
if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
$permalink_template_requested = in_array( 'permalink_template', $fields, true );