Skip to content

Commit 28399f6

Browse files
authored
Adjustments for Custom Order Tables compatibility (#2420)
* Fixes math operation on empty string `wc_parse_relative_date_option` will return an array. For that reason, it will cause to skip the validation because it will never be empty. Later below, `$retention[‘number’]` is attempted unsuccessfully because `$retention[‘number’]` will be an empty string when the “Retain Stripe Data” option is not defined, causing an error and stopping the data removal. * User Order method to update meta data * Remove unused method * Get order meta by using the `get_meta` method * Add changelog entries * Tweaks `get_order_by_source_id` to be compatible with Custom Order Tables Avoids using the `wp_posts` table to get the order ID, instead uses the `wc_get_orders` which is compatible with COT. * Tweaks `get_order_by_charge_id` to be compatible with Custom Order Tables * Tweaks `get_order_by_intent_id` to be compatible with Custom Order Tables * Tweaks `get_order_by_setup_intent_id` to be compatible with Custom Order Tables * Remove unused method `delete_attribute` * Check OrderUtil class exists before using it * Use full path for class existence validation * Check if current order exist
1 parent 46c4219 commit 28399f6

File tree

6 files changed

+67
-37
lines changed

6 files changed

+67
-37
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 6.8.0 - 2022-xx-xx =
4+
* Fix - Minor adjustments for Custom Order Tables compatibility.
45

56
= 6.7.0 - 2022-09-06 =
67
* Fix - Check payment method before updating payment method title.

includes/admin/class-wc-stripe-privacy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public function is_retention_expired( $created_date ) {
405405
$retention = wc_parse_relative_date_option( get_option( 'woocommerce_gateway_stripe_retention' ) );
406406
$is_expired = false;
407407
$time_span = time() - strtotime( $created_date );
408-
if ( empty( $retention ) || empty( $created_date ) ) {
408+
if ( empty( $retention['number'] ) || empty( $created_date ) ) {
409409
return false;
410410
}
411411
switch ( $retention['unit'] ) {

includes/class-wc-stripe-helper.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
exit;
44
}
55

6+
use Automattic\WooCommerce\Utilities\OrderUtil;
7+
68
/**
79
* Provides static methods as helpers.
810
*
@@ -373,7 +375,20 @@ public static function get_webhook_url() {
373375
public static function get_order_by_source_id( $source_id ) {
374376
global $wpdb;
375377

376-
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) );
378+
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
379+
$orders = wc_get_orders(
380+
[
381+
'limit' => 1,
382+
'meta_query' => [
383+
'key' => '_stripe_source_id',
384+
'value' => $source_id,
385+
],
386+
]
387+
);
388+
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
389+
} else {
390+
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) );
391+
}
377392

378393
if ( ! empty( $order_id ) ) {
379394
return wc_get_order( $order_id );
@@ -396,7 +411,17 @@ public static function get_order_by_charge_id( $charge_id ) {
396411
return false;
397412
}
398413

399-
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) );
414+
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
415+
$orders = wc_get_orders(
416+
[
417+
'transaction_id' => $charge_id,
418+
'limit' => 1,
419+
]
420+
);
421+
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
422+
} else {
423+
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) );
424+
}
400425

401426
if ( ! empty( $order_id ) ) {
402427
return wc_get_order( $order_id );
@@ -415,7 +440,20 @@ public static function get_order_by_charge_id( $charge_id ) {
415440
public static function get_order_by_intent_id( $intent_id ) {
416441
global $wpdb;
417442

418-
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) );
443+
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
444+
$orders = wc_get_orders(
445+
[
446+
'limit' => 1,
447+
'meta_query' => [
448+
'key' => '_stripe_intent_id',
449+
'value' => $intent_id,
450+
],
451+
]
452+
);
453+
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
454+
} else {
455+
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) );
456+
}
419457

420458
if ( ! empty( $order_id ) ) {
421459
return wc_get_order( $order_id );
@@ -434,7 +472,20 @@ public static function get_order_by_intent_id( $intent_id ) {
434472
public static function get_order_by_setup_intent_id( $intent_id ) {
435473
global $wpdb;
436474

437-
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_setup_intent' ) );
475+
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
476+
$orders = wc_get_orders(
477+
[
478+
'limit' => 1,
479+
'meta_query' => [
480+
'key' => '_stripe_setup_intent',
481+
'value' => $intent_id,
482+
],
483+
]
484+
);
485+
$order_id = current( $orders ) ? current( $orders )->get_id() : false;
486+
} else {
487+
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_setup_intent' ) );
488+
}
438489

439490
if ( ! empty( $order_id ) ) {
440491
return wc_get_order( $order_id );

includes/payment-methods/class-wc-gateway-stripe-multibanco.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false
226226
if ( ! $sent_to_admin && 'stripe_multibanco' === $payment_method && $order->has_status( 'on-hold' ) ) {
227227
WC_Stripe_Logger::log( 'Sending multibanco email for order #' . $order_id );
228228

229-
$this->get_instructions( $order_id, $plain_text );
229+
$this->get_instructions( $order, $plain_text );
230230
}
231231
}
232232

@@ -235,10 +235,14 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false
235235
*
236236
* @since 4.1.0
237237
* @version 4.1.0
238-
* @param int $order_id
238+
* @param int|WC_Order $order
239239
*/
240-
public function get_instructions( $order_id, $plain_text = false ) {
241-
$data = get_post_meta( $order_id, '_stripe_multibanco', true );
240+
public function get_instructions( $order, $plain_text = false ) {
241+
if ( true === is_int( $order ) ) {
242+
$order = wc_get_order( $order );
243+
}
244+
245+
$data = $order->get_meta( '_stripe_multibanco' );
242246

243247
if ( $plain_text ) {
244248
esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ) . "\n\n";
@@ -289,7 +293,7 @@ public function save_instructions( $order, $source_object ) {
289293

290294
$order_id = $order->get_id();
291295

292-
update_post_meta( $order_id, '_stripe_multibanco', $data );
296+
$order->update_meta_data( '_stripe_multibanco', $data );
293297
}
294298

295299
/**

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
129129
== Changelog ==
130130

131131
= 6.8.0 - 2022-xx-xx =
132+
* Fix - Minor adjustments for Custom Order Tables compatibility.
132133

133134
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

tests/phpunit/helpers/class-wc-helper-product.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -345,23 +345,6 @@ public static function create_attribute( $raw_name = 'size', $terms = [ 'small'
345345
return $return;
346346
}
347347

348-
/**
349-
* Delete an attribute.
350-
*
351-
* @param int $attribute_id ID to delete.
352-
*
353-
* @since 2.3
354-
*/
355-
public static function delete_attribute( $attribute_id ) {
356-
global $wpdb;
357-
358-
$attribute_id = absint( $attribute_id );
359-
360-
$wpdb->query(
361-
$wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $attribute_id )
362-
);
363-
}
364-
365348
/**
366349
* Creates a new product review on a specific product.
367350
*
@@ -383,14 +366,4 @@ public static function create_product_review( $product_id, $review_content = 'Re
383366
];
384367
return wp_insert_comment( $data );
385368
}
386-
387-
/**
388-
* A helper function for hooking into save_post during the test_product_meta_save_post test.
389-
* @since 3.0.1
390-
*
391-
* @param int $id ID to update.
392-
*/
393-
public static function save_post_test_update_meta_data_direct( $id ) {
394-
update_post_meta( $id, '_test2', 'world' );
395-
}
396369
}

0 commit comments

Comments
 (0)