| 1 |
<?php namespace TierPricingTable\Forms; |
| 2 |
|
| 3 |
use TierPricingTable\Core\ServiceContainer; |
| 4 |
|
| 5 |
class RegularPricingForm extends Form { |
| 6 |
|
| 7 |
public static function render( |
| 8 |
$role = null, |
| 9 |
$loop = null, |
| 10 |
$regularPrice = null, |
| 11 |
$salePrice = null, |
| 12 |
$pricingType = 'flat', |
| 13 |
$discount = null, |
| 14 |
$discount_type = null |
| 15 |
) { |
| 16 |
/** |
| 17 |
* Available variables |
| 18 |
* |
| 19 |
* @var float $regular_price |
| 20 |
* @var float $sale_price |
| 21 |
* @var float $discount |
| 22 |
* @var string $pricing_type |
| 23 |
* |
| 24 |
* @var string $role |
| 25 |
* @var string $loop |
| 26 |
*/ |
| 27 |
ServiceContainer::getInstance()->getFileManager()->includeTemplate( 'admin/components/regular-pricing-form.php', |
| 28 |
array( |
| 29 |
'role' => $role, |
| 30 |
'loop' => $loop, |
| 31 |
'discount' => $discount, |
| 32 |
'discount_type' => $discount_type, |
| 33 |
'regular_price' => $regularPrice, |
| 34 |
'sale_price' => $salePrice, |
| 35 |
'pricing_type' => $pricingType, |
| 36 |
) ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function getDataFromRequest( $role = null, $loop = null, $request = null ): array { |
| 40 |
if ( wp_verify_nonce( true, true ) ) { |
| 41 |
// as phpcs comments at Woo is not available, we have to do such a trash |
| 42 |
$woo = 'Woo, please add ignoring comments to your phpcs checker'; |
| 43 |
} |
| 44 |
|
| 45 |
$request = $request ? $request : $_POST; |
| 46 |
|
| 47 |
$data = array(); |
| 48 |
|
| 49 |
$fields = array( |
| 50 |
'regular_price' => function ( $price ) { |
| 51 |
return ! Form::isEmpty( $price ) ? (float) wc_format_decimal( $price ) : null; |
| 52 |
}, |
| 53 |
'sale_price' => function ( $price ) { |
| 54 |
return ! Form::isEmpty( $price ) ? (float) wc_format_decimal( $price ) : null; |
| 55 |
}, |
| 56 |
'discount' => function ( $discount ) { |
| 57 |
return ! Form::isEmpty( $discount ) ? (float) $discount : null; |
| 58 |
}, |
| 59 |
'discount_type' => function ( $discountType ) { |
| 60 |
return in_array( $discountType, array( 'sale_price', 'regular_price' ) ) ? $discountType : 'sale_price'; |
| 61 |
}, |
| 62 |
'pricing_type' => function ( $pricingType ) { |
| 63 |
return in_array( $pricingType, array( 'flat', 'percentage' ) ) ? $pricingType : 'flat'; |
| 64 |
}, |
| 65 |
); |
| 66 |
|
| 67 |
foreach ( $fields as $fieldKey => $sanitizeFunction ) { |
| 68 |
$data[ $fieldKey ] = call_user_func( $sanitizeFunction, |
| 69 |
Form::getFieldValue( $fieldKey, $role, $loop, null, $request ) ); |
| 70 |
} |
| 71 |
|
| 72 |
return $data; |
| 73 |
} |
| 74 |
} |
| 75 |
|