Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| BH_WC_Checkout_Rate_Limiter | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| set_locale | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| define_woocommerce_ajax_hooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| define_woocommerce_settings_hooks | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| define_admin_hooks | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| define_plugins_page_hooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The file that defines the core plugin class |
| 4 | * |
| 5 | * A class definition that WP_Includes attributes and functions used across both the |
| 6 | * frontend-facing side of the site and the admin area. |
| 7 | * |
| 8 | * @link https://BrianHenryIE.com |
| 9 | * @since 1.0.0 |
| 10 | * |
| 11 | * @package brianhenryie/bh-wc-checkout-rate-limiter |
| 12 | */ |
| 13 | |
| 14 | namespace BrianHenryIE\Checkout_Rate_Limiter; |
| 15 | |
| 16 | use BrianHenryIE\Checkout_Rate_Limiter\Admin\Admin; |
| 17 | use BrianHenryIE\Checkout_Rate_Limiter\Admin\Plugins_Page; |
| 18 | use BrianHenryIE\Checkout_Rate_Limiter\WooCommerce\Ajax; |
| 19 | use BrianHenryIE\Checkout_Rate_Limiter\WooCommerce\Settings_Payments; |
| 20 | use BrianHenryIE\Checkout_Rate_Limiter\WP_Includes\I18n; |
| 21 | use Psr\Log\LoggerAwareTrait; |
| 22 | use Psr\Log\LoggerInterface; |
| 23 | |
| 24 | /** |
| 25 | * The core plugin class. |
| 26 | * |
| 27 | * This is used to define internationalization, admin-specific hooks, and |
| 28 | * frontend-facing site hooks. |
| 29 | * |
| 30 | * Also maintains the unique identifier of this plugin as well as the current |
| 31 | * version of the plugin. |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * @package brianhenryie/bh-wc-checkout-rate-limiter |
| 35 | * |
| 36 | * @author BrianHenryIE <BrianHenryIE@gmail.com> |
| 37 | */ |
| 38 | class BH_WC_Checkout_Rate_Limiter { |
| 39 | |
| 40 | use LoggerAwareTrait; |
| 41 | |
| 42 | /** |
| 43 | * The plugin settings. |
| 44 | * |
| 45 | * @var Settings_Interface |
| 46 | */ |
| 47 | protected Settings_Interface $settings; |
| 48 | |
| 49 | /** |
| 50 | * Define the core functionality of the plugin. |
| 51 | * |
| 52 | * Set the plugin name and the plugin version that can be used throughout the plugin. |
| 53 | * Load the dependencies, define the locale, and set the hooks for the admin area and |
| 54 | * the frontend-facing side of the site. |
| 55 | * |
| 56 | * @param Settings_Interface $settings The plugin's settings. |
| 57 | * @param LoggerInterface $logger PSR logger. |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | public function __construct( Settings_Interface $settings, LoggerInterface $logger ) { |
| 62 | |
| 63 | $this->settings = $settings; |
| 64 | $this->setLogger( $logger ); |
| 65 | |
| 66 | $this->set_locale(); |
| 67 | $this->define_woocommerce_ajax_hooks(); |
| 68 | $this->define_woocommerce_settings_hooks(); |
| 69 | $this->define_admin_hooks(); |
| 70 | $this->define_plugins_page_hooks(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Define the locale for this plugin for internationalization. |
| 75 | * |
| 76 | * Uses the i18n class in order to set the domain and to register the hook |
| 77 | * with WordPress. |
| 78 | * |
| 79 | * @since 1.0.0 |
| 80 | */ |
| 81 | protected function set_locale(): void { |
| 82 | |
| 83 | $plugin_i18n = new I18n(); |
| 84 | |
| 85 | add_action( 'plugins_loaded', array( $plugin_i18n, 'load_plugin_textdomain' ) ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Registers the hooks related to WooCommerce ajax actions. i.e. the main hook. |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | protected function define_woocommerce_ajax_hooks(): void { |
| 94 | |
| 95 | $ajax = new Ajax( $this->settings, $this->logger ); |
| 96 | |
| 97 | add_action( 'wc_ajax_checkout', array( $ajax, 'rate_limit_checkout' ), 0 ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Registers the hooks related to adding the settings page to WooCommerce. |
| 102 | * |
| 103 | * @since 1.0.0 |
| 104 | */ |
| 105 | protected function define_woocommerce_settings_hooks(): void { |
| 106 | |
| 107 | $settings_advanced = new Settings_Payments( $this->settings, $this->logger ); |
| 108 | |
| 109 | add_action( 'current_screen', array( $settings_advanced, 'record_page_visit_time' ) ); |
| 110 | |
| 111 | add_filter( 'woocommerce_get_sections_checkout', array( $settings_advanced, 'add_section' ) ); |
| 112 | add_filter( 'woocommerce_get_settings_checkout', array( $settings_advanced, 'settings' ), 10, 2 ); |
| 113 | |
| 114 | add_action( 'woocommerce_admin_field_attempts_per_interval', array( $settings_advanced, 'print_attempts_per_interval_settings_field' ) ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Register hooks for displaying and dismissing the "plugin is not yet configured" notice. |
| 119 | */ |
| 120 | protected function define_admin_hooks(): void { |
| 121 | |
| 122 | $admin = new Admin( $this->settings, $this->logger ); |
| 123 | |
| 124 | add_action( 'plugins_loaded', array( $admin, 'init_notices' ) ); |
| 125 | add_action( 'admin_init', array( $admin, 'add_setup_notice' ) ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Register hook to add a settings link on plugins.php. |
| 130 | */ |
| 131 | protected function define_plugins_page_hooks(): void { |
| 132 | |
| 133 | $plugins_page = new Plugins_Page( $this->settings, $this->logger ); |
| 134 | |
| 135 | add_filter( "plugin_action_links_{$this->settings->get_plugin_basename()}", array( $plugins_page, 'action_links' ), 10, 4 ); |
| 136 | } |
| 137 | } |