Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
14.81% |
4 / 27 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Settings | |
14.81% |
4 / 27 |
|
66.67% |
4 / 6 |
117.47 | |
0.00% |
0 / 1 |
| is_enabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| get_checkout_rate_limits | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |||
| get_log_level | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_plugin_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_plugin_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_plugin_basename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Instance of settings for WooCommerce checkout rate limiting, plus its logging. |
| 4 | * |
| 5 | * @author BrianHenryIE <BrianHenryIE@gmail.com> |
| 6 | * @link https://BrianHenryIE.com |
| 7 | * @since 1.0.0 |
| 8 | * @package brianhenryie/bh-wc-checkout-rate-limiter |
| 9 | */ |
| 10 | |
| 11 | namespace BrianHenryIE\Checkout_Rate_Limiter\API; |
| 12 | |
| 13 | use BrianHenryIE\Checkout_Rate_Limiter\Settings_Interface; |
| 14 | use BrianHenryIE\Checkout_Rate_Limiter\WooCommerce\Settings_Payments; |
| 15 | use BrianHenryIE\Checkout_Rate_Limiter\WP_Logger\Logger_Settings_Trait; |
| 16 | use BrianHenryIE\Checkout_Rate_Limiter\WP_Logger\WooCommerce_Logger_Settings_Interface; |
| 17 | use BrianHenryIE\Checkout_Rate_Limiter\WP_Logger\Logger; |
| 18 | use Psr\Log\LogLevel; |
| 19 | |
| 20 | /** |
| 21 | * The UI for the settings is in the Settings_Payments class. |
| 22 | * |
| 23 | * @see Settings_Payments |
| 24 | * |
| 25 | * Class Settings |
| 26 | * @package brianhenryie/bh-wc-checkout-rate-limiter |
| 27 | */ |
| 28 | class Settings implements Settings_Interface, WooCommerce_Logger_Settings_Interface { |
| 29 | use Logger_Settings_Trait; |
| 30 | |
| 31 | /** |
| 32 | * Is the setting enabled in the settings UI and ratelimits set? |
| 33 | * |
| 34 | * @return bool |
| 35 | */ |
| 36 | public function is_enabled(): bool { |
| 37 | |
| 38 | $woocommerce_setting_enabled = 'yes' === get_option( 'bh_wc_checkout_rate_limiter_checkout_rate_limiting_enabled', 'yes' ); |
| 39 | return $woocommerce_setting_enabled && count( $this->get_checkout_rate_limits() ) > 0; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Array of rates, where the key is the interval and the value is the permitted number of events during that interval. |
| 44 | * |
| 45 | * @return array<int, int> |
| 46 | */ |
| 47 | public function get_checkout_rate_limits(): array { |
| 48 | |
| 49 | $defaults = array( |
| 50 | 1 => array( |
| 51 | 'interval' => 60, |
| 52 | 'attempts' => 2, |
| 53 | ), |
| 54 | 2 => array( |
| 55 | 'interval' => 120, |
| 56 | 'attempts' => 3, |
| 57 | ), |
| 58 | 3 => array( |
| 59 | 'interval' => 300, |
| 60 | 'attempts' => 5, |
| 61 | ), |
| 62 | ); |
| 63 | |
| 64 | $rates = array(); |
| 65 | |
| 66 | foreach ( array( 1, 2, 3 ) as $index ) { |
| 67 | $rate = get_option( 'bh_wc_checkout_rate_limiter_allowed_attempts_per_interval_' . $index, $defaults[ $index ] ); |
| 68 | if ( isset( $rate['interval'] ) && 0 !== intval( $rate['interval'] ) |
| 69 | && isset( $rate['attempts'] ) && 0 !== intval( $rate['attempts'] ) ) { |
| 70 | $rates[ intval( $rate['interval'] ) ] = intval( $rate['attempts'] ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $rates; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Plugin log level. Recommended: Debug for testing, Info for normal use. |
| 79 | * |
| 80 | * NB: Not all possible log levels are used. |
| 81 | * |
| 82 | * @return string Minimum level to log events at. |
| 83 | */ |
| 84 | public function get_log_level(): string { |
| 85 | return get_option( 'bh_wc_checkout_rate_limiter_log_level', LogLevel::INFO ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Plugin name for use by the logger in messages printed to WordPress admin UI. |
| 90 | * |
| 91 | * @see Logger |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | public function get_plugin_name(): string { |
| 96 | return __( 'Checkout Rate Limiter', 'bh-wc-checkout-rate-limiter' ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * The plugin slug is used by the logger in file and URL paths. |
| 101 | * |
| 102 | * @see Logger |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_plugin_slug(): string { |
| 107 | return 'bh-wc-checkout-rate-limiter'; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * The plugin basename is used by the logger to add the plugins page action link. |
| 112 | * |
| 113 | * @see Logger |
| 114 | * |
| 115 | * @return string |
| 116 | */ |
| 117 | public function get_plugin_basename(): string { |
| 118 | return defined( 'BH_WC_CHECKOUT_RATE_LIMITER_BASENAME' ) ? BH_WC_CHECKOUT_RATE_LIMITER_BASENAME : 'bh-wc-checkout-rate-limiter/bh-wc-checkout-rate-limiter.php'; |
| 119 | } |
| 120 | } |