Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Admin | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| init_notices | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| add_setup_notice | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Display an admin notice inviting the user to configure the plugin. Stops displaying after a week. |
| 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\Admin; |
| 12 | |
| 13 | use BrianHenryIE\Checkout_Rate_Limiter\Settings_Interface; |
| 14 | use BrianHenryIE\Checkout_Rate_Limiter\WP_Includes\Activator; |
| 15 | use Psr\Log\LoggerAwareTrait; |
| 16 | use Psr\Log\LoggerInterface; |
| 17 | use BrianHenryIE\Checkout_Rate_Limiter\WPTRT\AdminNotices\Notices; |
| 18 | |
| 19 | /** |
| 20 | * Checks that: |
| 21 | * * last activated time is in the past week |
| 22 | * * current_user_can('manage_options') |
| 23 | * * plugin is not configured |
| 24 | * |
| 25 | * @see https://github.com/wptrt/admin-notices |
| 26 | */ |
| 27 | class Admin { |
| 28 | |
| 29 | use LoggerAwareTrait; |
| 30 | |
| 31 | /** |
| 32 | * The plugin's settings. |
| 33 | * |
| 34 | * @var Settings_Interface |
| 35 | */ |
| 36 | protected Settings_Interface $settings; |
| 37 | |
| 38 | /** |
| 39 | * WPTRT Notices instance. |
| 40 | * |
| 41 | * @see https://github.com/wptrt/admin-notices |
| 42 | * |
| 43 | * @var Notices |
| 44 | */ |
| 45 | protected Notices $notices; |
| 46 | |
| 47 | /** |
| 48 | * Instantiate. |
| 49 | * |
| 50 | * @param Settings_Interface $settings The plugin settings. |
| 51 | * @param LoggerInterface $logger PSR logger. |
| 52 | */ |
| 53 | public function __construct( Settings_Interface $settings, LoggerInterface $logger ) { |
| 54 | $this->settings = $settings; |
| 55 | $this->setLogger( $logger ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Initialize WPTRT\AdminNotices for presenting notices and handling dismissals. |
| 60 | * |
| 61 | * Load only on admin screens and AJAX requests. |
| 62 | * |
| 63 | * @hooked plugins_loaded |
| 64 | */ |
| 65 | public function init_notices(): void { |
| 66 | |
| 67 | if ( ! is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $this->notices = new Notices(); |
| 72 | $this->notices->boot(); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks if we are recently activated and unconfigured, then displays an admin notice to users. |
| 78 | * |
| 79 | * @hooked admin_init |
| 80 | */ |
| 81 | public function add_setup_notice(): void { |
| 82 | |
| 83 | if ( ! is_admin() ) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Don't show it on the settings page itself. |
| 88 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 89 | if ( isset( $_GET['section'] ) && 'checkout-rate-limiting' === $_GET['section'] ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $last_activated_times = get_option( Activator::ACTIVATED_TIME_OPTION_KEY, array() ); |
| 94 | |
| 95 | $last_activated = intval( array_pop( $last_activated_times ) ); |
| 96 | |
| 97 | // If last activation was longer than a week ago, stop annoying users with the admin notice. |
| 98 | if ( $last_activated < time() - WEEK_IN_SECONDS ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | $last_visited_time = get_option( 'bh_wc_checkout_rate_limiter_visited_settings_time', 0 ); |
| 103 | |
| 104 | // If the settings page has been viewed since the plug was activated, do not show it. |
| 105 | if ( $last_visited_time > $last_activated ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if ( ! current_user_can( 'manage_options' ) ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $settings_url = admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=checkout-rate-limiting' ); |
| 114 | |
| 115 | $id = $this->settings->get_plugin_slug() . '-activation-configuration'; |
| 116 | $title = ''; |
| 117 | $message = '<strong>' . $this->settings->get_plugin_name() . '</strong> ' . __( 'options can be configured under', 'bh-wc-checkout-rate-limiter' ) . " <a href=\"{$settings_url}\">WooCommerce / Settings / Payments / Rate Limiting</a>."; |
| 118 | |
| 119 | $options = array( |
| 120 | 'capability' => 'manage_options', |
| 121 | ); |
| 122 | |
| 123 | $this->notices->add( $id, $title, $message, $options ); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | } |