Skip to content

Commit abed6cd

Browse files
wjrosamalithsen
andauthored
Introducing new filters to control the subscription detached tool parameters (#4475)
* Introducing new filters to control the subscription detached tool parameters * Changelog and readme entries * Update includes/class-wc-stripe-status.php Co-authored-by: Malith Senaweera <[email protected]> * Update includes/compat/class-wc-stripe-subscriptions-helper.php Co-authored-by: Malith Senaweera <[email protected]> * Changing log severity --------- Co-authored-by: Malith Senaweera <[email protected]>
1 parent a9b8fdb commit abed6cd

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
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
= 9.7.0 - xxxx-xx-xx =
4+
* Add - Adds two new safety filters to the subscriptions detached debug tool: `wc_stripe_detached_subscriptions_maximum_time` and `wc_stripe_detached_subscriptions_maximum_count`
45
* Add - Show a notice when editing an active subscription that has no payment method attached
56
* Fix - Fixes a possible fatal error when trying to generate the order signature for a `WC_Order_Refund` object
67
* Add - New WooCommerce Debug Tool to list subscriptions without a payment method attached

includes/class-wc-stripe-status.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
* Integrates with Woo Status pages to offer additional tools and insights for the Stripe extension.
1010
*/
1111
class WC_Stripe_Status {
12+
/**
13+
* Maximum number of subscriptions to process in the detached subscriptions tool.
14+
*
15+
* @var int
16+
*/
17+
private const SUBSCRIPTIONS_DETACHED_LIST_LIMIT = 1000;
18+
1219
/**
1320
* Instance of WC_Gateway_Stripe
1421
*
@@ -240,7 +247,14 @@ public function debug_tools( $tools ) {
240247
* @return void
241248
*/
242249
public function list_detached_subscriptions() {
243-
$subscriptions = WC_Stripe_Subscriptions_Helper::get_detached_subscriptions( 1000 ); // Limiting to 1000 subscriptions for safety.
250+
/**
251+
* Maximum number of subscriptions to process.
252+
*
253+
* @since 9.7.0
254+
* @param int $max_count The maximum number of subscriptions to process.
255+
*/
256+
$max_count = apply_filters( 'wc_stripe_detached_subscriptions_maximum_count', self::SUBSCRIPTIONS_DETACHED_LIST_LIMIT ); // Limit the number of subscriptions to process for safety.
257+
$subscriptions = WC_Stripe_Subscriptions_Helper::get_detached_subscriptions( $max_count );
244258
$detached_messages = WC_Stripe_Subscriptions_Helper::build_subscriptions_detached_messages( $subscriptions );
245259
echo '<div class="wrap woocommerce">';
246260
echo '<h1>' . esc_html__( 'List Detached Stripe Subscriptions', 'woocommerce-gateway-stripe' ) . '</h1>';

includes/compat/class-wc-stripe-subscriptions-helper.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ class WC_Stripe_Subscriptions_Helper {
2323

2424
/**
2525
* Maximum number of subscriptions to load per page.
26+
*
27+
* @var int
2628
*/
2729
private const MAX_SUBSCRIPTIONS_PER_PAGE = 50;
2830

31+
/**
32+
* Fallback maximum execution time in seconds.
33+
*
34+
* @var int
35+
*/
36+
private const MAX_EXECUTION_TIME_FALLBACK = 30;
37+
2938
/**
3039
* Checks if subscriptions are enabled on the site.
3140
*
@@ -60,11 +69,36 @@ public static function get_detached_subscriptions( $limit = -1 ) {
6069
return $cached_subscriptions;
6170
}
6271

63-
$subscriptions = [];
64-
$page = 1;
65-
$per_page = self::MAX_SUBSCRIPTIONS_PER_PAGE;
72+
$subscriptions = [];
73+
$num_subscriptions = 0;
74+
$page = 1;
75+
$per_page = self::MAX_SUBSCRIPTIONS_PER_PAGE;
76+
$start_time = time();
77+
78+
// Defaults maximum execution time to server's `max_execution_time` (when available, or 30 if not) minus 5 seconds.
79+
$default_max_time = ( ini_get( 'max_execution_time' ) ? ini_get( 'max_execution_time' ) : self::MAX_EXECUTION_TIME_FALLBACK ) - 5;
80+
81+
/**
82+
* Filter the maximum time allowed for fetching detached subscriptions.
83+
*
84+
* @since 9.7.0
85+
* @param int $max_time The maximum time allowed in seconds. Default is server's `max_execution_time` (when available, or 30 if not) minus 5 seconds.
86+
*/
87+
$max_time = apply_filters( 'wc_stripe_detached_subscriptions_maximum_time', $default_max_time );
6688

6789
do {
90+
if ( ( time() - $start_time ) > $max_time ) {
91+
// If we have been running for more than the default limit, stop to avoid long execution times.
92+
WC_Stripe_Logger::log(
93+
sprintf(
94+
/* translators: %d is the maximum time allowed for fetching detached subscriptions */
95+
__( 'Stopped fetching detached subscriptions before the %d seconds limit for safety.', 'woocommerce-gateway-stripe' ),
96+
$max_time
97+
)
98+
);
99+
break;
100+
}
101+
68102
$batch = wcs_get_subscriptions(
69103
[
70104
'subscriptions_per_page' => $per_page,

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 9.7.0 - xxxx-xx-xx =
114+
* Add - Adds two new safety filters to the subscriptions detached debug tool: `wc_stripe_detached_subscriptions_maximum_time` and `wc_stripe_detached_subscriptions_maximum_count`
114115
* Add - Show a notice when editing an active subscription that has no payment method attached
115116
* Fix - Fixes a possible fatal error when trying to generate the order signature for a `WC_Order_Refund` object
116117
* Add - New WooCommerce Debug Tool to list subscriptions without a payment method attached

0 commit comments

Comments
 (0)