Skip to content

Commit 2c9940e

Browse files
committed
Add input validation for coupon-ratio and refund-ratio parameters
1 parent 86a499c commit 2c9940e

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

includes/Generator/Order.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
class Order extends Generator {
1414

15+
/**
16+
* Probability (percentage) that a partial refund will receive a second refund.
17+
*/
18+
const SECOND_REFUND_PROBABILITY = 25;
19+
1520
/**
1621
* Return a new order.
1722
*
@@ -95,8 +100,14 @@ public static function generate( $save = true, $assoc_args = array() ) {
95100
// Handle --coupon-ratio parameter
96101
if ( ! empty( $assoc_args['coupon-ratio'] ) ) {
97102
$coupon_ratio = floatval( $assoc_args['coupon-ratio'] );
103+
104+
// Validate ratio is between 0.0 and 1.0
105+
if ( $coupon_ratio < 0.0 || $coupon_ratio > 1.0 ) {
106+
$coupon_ratio = max( 0.0, min( 1.0, $coupon_ratio ) );
107+
}
108+
98109
// Apply coupon based on ratio
99-
if ( $coupon_ratio > 0 && ( $coupon_ratio >= 1.0 || ( mt_rand() / mt_getrandmax() ) < $coupon_ratio ) ) {
110+
if ( $coupon_ratio > 0 && ( $coupon_ratio >= 1.0 || ( (float) wp_rand() / (float) getrandmax() ) < $coupon_ratio ) ) {
100111
$include_coupon = true;
101112
} else {
102113
$include_coupon = false;
@@ -112,7 +123,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
112123
}
113124
}
114125

115-
// Orders created before 2024-01-09 represents orders created before the attribution feature was added.
126+
// Orders created before 2024-01-09 represents orders created before the attribution feature was added.
116127
if ( ! ( strtotime( $date ) < strtotime( '2024-01-09' ) ) ) {
117128
OrderAttribution::add_order_attribution_meta( $order, $assoc_args );
118129
}
@@ -135,22 +146,28 @@ public static function generate( $save = true, $assoc_args = array() ) {
135146
// Handle --refund-ratio parameter for completed orders
136147
if ( ! empty( $assoc_args['refund-ratio'] ) && 'completed' === $status ) {
137148
$refund_ratio = floatval( $assoc_args['refund-ratio'] );
149+
150+
// Validate ratio is between 0.0 and 1.0
151+
if ( $refund_ratio < 0.0 || $refund_ratio > 1.0 ) {
152+
$refund_ratio = max( 0.0, min( 1.0, $refund_ratio ) );
153+
}
154+
138155
$should_refund = false;
139156

140157
if ( $refund_ratio >= 1.0 ) {
141158
// Always refund if ratio is 1.0 or higher
142159
$should_refund = true;
143160
} elseif ( $refund_ratio > 0 ) {
144161
// Use random chance for ratios between 0 and 1
145-
$random = mt_rand() / mt_getrandmax();
162+
$random = (float) wp_rand() / (float) getrandmax();
146163
$should_refund = $random < $refund_ratio;
147164
}
148165

149166
if ( $should_refund ) {
150167
$is_partial = self::create_refund( $order );
151168

152-
// 25% of partial refunds get a second refund (always partial)
153-
if ( $is_partial && wp_rand( 1, 100 ) <= 25 ) {
169+
// Some partial refunds get a second refund (always partial)
170+
if ( $is_partial && wp_rand( 1, 100 ) <= self::SECOND_REFUND_PROBABILITY ) {
154171
self::create_refund( $order, true );
155172
}
156173
}
@@ -330,10 +347,15 @@ protected static function get_or_create_coupon() {
330347
// If no coupons exist, create 6 (3 fixed, 3 percentage)
331348
if ( false === $coupon ) {
332349
// Create 3 fixed cart coupons ($5-$50)
333-
Coupon::batch( 3, array( 'min' => 5, 'max' => 50, 'discount_type' => 'fixed_cart' ) );
350+
$fixed_result = Coupon::batch( 3, array( 'min' => 5, 'max' => 50, 'discount_type' => 'fixed_cart' ) );
334351

335352
// Create 3 percentage coupons (5%-25%)
336-
Coupon::batch( 3, array( 'min' => 5, 'max' => 25, 'discount_type' => 'percent' ) );
353+
$percent_result = Coupon::batch( 3, array( 'min' => 5, 'max' => 25, 'discount_type' => 'percent' ) );
354+
355+
// If coupon creation failed, return false
356+
if ( is_wp_error( $fixed_result ) || is_wp_error( $percent_result ) ) {
357+
return false;
358+
}
337359

338360
// Now get a random coupon from the ones we just created
339361
$coupon = Coupon::get_random();

0 commit comments

Comments
 (0)