-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance improvements for disputes reminder task list #9906
Changes from 6 commits
07c1a1f
46c61af
590b7b5
0fba4cc
05085fd
62ac5ce
20f7fe5
c795c21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: fix | ||
|
||
Performance improvements for Disputes Needing Response task shown in WooCommerce admin. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,13 @@ class WC_Payments_Task_Disputes extends Task { | |
*/ | ||
private $disputes_due_within_1d; | ||
|
||
/** | ||
* A memory cache of all disputes needing response. | ||
* | ||
* @var array|null | ||
*/ | ||
private $disputes_needing_response = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to clarify this a little more:
|
||
|
||
/** | ||
* WC_Payments_Task_Disputes constructor. | ||
*/ | ||
|
@@ -57,13 +64,12 @@ public function __construct() { | |
$this->api_client = \WC_Payments::get_payments_api_client(); | ||
$this->database_cache = \WC_Payments::get_database_cache(); | ||
parent::__construct(); | ||
$this->init(); | ||
} | ||
|
||
/** | ||
* Initialize the task. | ||
*/ | ||
private function init() { | ||
private function fetch_relevant_disputes() { | ||
$this->disputes_due_within_7d = $this->get_disputes_needing_response_within_days( 7 ); | ||
$this->disputes_due_within_1d = $this->get_disputes_needing_response_within_days( 1 ); | ||
} | ||
|
@@ -83,6 +89,9 @@ public function get_id() { | |
* @return string | ||
*/ | ||
public function get_title() { | ||
if ( null === $this->disputes_needing_response ) { | ||
$this->fetch_relevant_disputes(); | ||
} | ||
if ( count( (array) $this->disputes_due_within_7d ) === 1 ) { | ||
$dispute = $this->disputes_due_within_7d[0]; | ||
$amount = WC_Payments_Utils::interpret_stripe_amount( $dispute['amount'], $dispute['currency'] ); | ||
|
@@ -275,6 +284,9 @@ public function is_complete() { | |
* @return bool | ||
*/ | ||
public function can_view() { | ||
if ( null === $this->disputes_needing_response ) { | ||
$this->fetch_relevant_disputes(); | ||
} | ||
return count( (array) $this->disputes_due_within_7d ) > 0; | ||
} | ||
|
||
|
@@ -322,15 +334,24 @@ private function get_disputes_needing_response_within_days( $num_days ) { | |
* @return array|null Array of disputes awaiting a response. Null on failure. | ||
*/ | ||
private function get_disputes_needing_response() { | ||
return $this->database_cache->get_or_add( | ||
if ( null !== $this->disputes_needing_response ) { | ||
return $this->disputes_needing_response; | ||
} | ||
|
||
brucealdridge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->disputes_needing_response = $this->database_cache->get_or_add( | ||
Database_Cache::ACTIVE_DISPUTES_KEY, | ||
function () { | ||
$response = $this->api_client->get_disputes( | ||
[ | ||
'pagesize' => 50, | ||
'search' => [ 'warning_needs_response', 'needs_response' ], | ||
] | ||
); | ||
try { | ||
$response = $this->api_client->get_disputes( | ||
[ | ||
'pagesize' => 50, | ||
'search' => [ 'warning_needs_response', 'needs_response' ], | ||
] | ||
); | ||
} catch ( \Exception $e ) { | ||
// Ensure an array is always returned, even if the API call fails. | ||
return []; | ||
} | ||
|
||
$active_disputes = $response['data'] ?? []; | ||
|
||
|
@@ -347,8 +368,9 @@ function ( $a, $b ) { | |
|
||
return $active_disputes; | ||
}, | ||
// We'll consider all array values to be valid as the cache is only invalidated when it is deleted or it expires. | ||
'is_array' | ||
); | ||
|
||
return $this->disputes_needing_response; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,9 @@ class WC_Payments_Tasks { | |
* WC_Payments_Admin_Tasks constructor. | ||
*/ | ||
public static function init() { | ||
include_once WCPAY_ABSPATH . 'includes/admin/tasks/class-wc-payments-task-disputes.php'; | ||
if ( ! current_user_can( 'manage_woocommerce' ) ) { | ||
marcinbot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
|
||
add_action( 'init', [ __CLASS__, 'add_task_disputes_need_response' ] ); | ||
} | ||
|
@@ -31,9 +33,10 @@ public static function init() { | |
*/ | ||
public static function add_task_disputes_need_response() { | ||
$account_service = WC_Payments::get_account_service(); | ||
if ( ! $account_service || ! $account_service->is_stripe_account_valid() ) { | ||
if ( ! $account_service || ! $account_service->is_stripe_account_valid() || $account_service->is_account_under_review() || $account_service->is_account_rejected() ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could also be explained in a comment - i.e. define the preconditions in merchant/human terms :) |
||
return; | ||
} | ||
include_once WCPAY_ABSPATH . 'includes/admin/tasks/class-wc-payments-task-disputes.php'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying the details of the problem and how you've optimised it @brucealdridge ! 🚀 I think it's worth adding a comment in this function about why the include and task is deferred, i.e. leave breadcrumbs in the code about the performance problem. |
||
|
||
// 'extended' = 'Things to do next' task list on WooCommerce > Home. | ||
TaskLists::add_task( 'extended', new WC_Payments_Task_Disputes() ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to add this breadcrumb. Maybe even could add a "see also" linking to the code that calls this? (if you think that would be helpful)