| 1 |
<?php namespace TierPricingTable\Forms; |
| 2 |
|
| 3 |
class Form { |
| 4 |
|
| 5 |
const FIELDS_PREFIX = 'tiered_pricing_'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Build a name for a field based on parameters. |
| 9 |
* |
| 10 |
* @param $base |
| 11 |
* @param $role |
| 12 |
* @param $loop |
| 13 |
* @param $customPrefix |
| 14 |
* |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
public static function getFieldName( $base, $role = null, $loop = null, $customPrefix = '' ) { |
| 18 |
|
| 19 |
// Example: _tiered_pricing_field{_variation}{_role}{custom_prefix}{[loop]}{[role]} |
| 20 |
|
| 21 |
$loopName = ''; |
| 22 |
$variationPrefix = ''; |
| 23 |
$roleName = ''; |
| 24 |
$rolePrefix = ''; |
| 25 |
|
| 26 |
if ( ! is_null( $loop ) ) { |
| 27 |
$loopName = "[$loop]"; |
| 28 |
$variationPrefix = '_variation'; |
| 29 |
} |
| 30 |
|
| 31 |
if ( $role ) { |
| 32 |
$roleName = "[$role]"; |
| 33 |
$rolePrefix = '_role'; |
| 34 |
} |
| 35 |
|
| 36 |
return self::FIELDS_PREFIX . $base . $variationPrefix . $rolePrefix . $customPrefix . $loopName . $roleName; |
| 37 |
} |
| 38 |
|
| 39 |
public static function getFieldValue( $base, $role = null, $loop = null, $customPrefix = '', $data = null ) { |
| 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 |
$data = $data ? $data : $_POST; |
| 46 |
|
| 47 |
$variationPrefix = ! is_null( $loop ) ? '_variation' : ''; |
| 48 |
$rolePrefix = $role ? '_role' : ''; |
| 49 |
|
| 50 |
$value = null; |
| 51 |
|
| 52 |
if ( isset( $data[ self::FIELDS_PREFIX . $base . $variationPrefix . $rolePrefix . $customPrefix ] ) ) { |
| 53 |
|
| 54 |
$value = $data[ self::FIELDS_PREFIX . $base . $variationPrefix . $rolePrefix . $customPrefix ]; |
| 55 |
|
| 56 |
if ( ! is_null( $loop ) && $value ) { |
| 57 |
$value = isset( $value[ $loop ] ) ? $value[ $loop ] : null; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $role && $value ) { |
| 61 |
$value = isset( $value[ $role ] ) ? $value[ $role ] : null; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $value; |
| 66 |
} |
| 67 |
|
| 68 |
public static function isEmpty( $value ): bool { |
| 69 |
return is_null( $value ) || '' === $value || false === $value; |
| 70 |
} |
| 71 |
} |
| 72 |
|