Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ImageLimitBanner | |
0.00% |
0 / 34 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| display_admin_banner | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| display_rate_limit_banner | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| display_ban_banner | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Images; |
| 4 | |
| 5 | /** |
| 6 | * Displays admin notices for rate limits and bans in the WP Admin area. |
| 7 | */ |
| 8 | class ImageLimitBanner { |
| 9 | /** |
| 10 | * The active brand plugin. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | private $brand; |
| 15 | |
| 16 | /** |
| 17 | * Initializes the Image Limit or Ban Banner. |
| 18 | * |
| 19 | * @param Container $container Dependency injection container. |
| 20 | */ |
| 21 | public function __construct( $container ) { |
| 22 | $this->brand = $container->plugin()->id; |
| 23 | add_action( 'admin_notices', array( $this, 'display_admin_banner' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Displays the admin banner for rate limits or bans. |
| 28 | */ |
| 29 | public function display_admin_banner() { |
| 30 | // Check for rate limiting. |
| 31 | $rate_limit_time = get_transient( ImageService::$rate_limit_transient_key ); |
| 32 | if ( $rate_limit_time ) { |
| 33 | $this->display_rate_limit_banner( $rate_limit_time ); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Check for permanent ban. |
| 38 | $is_banned = ImageSettings::is_banned(); |
| 39 | if ( $is_banned ) { |
| 40 | $this->display_ban_banner(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Displays the rate limit banner. |
| 46 | * |
| 47 | * @param int $rate_limit_time Timestamp when the rate limit will expire. |
| 48 | */ |
| 49 | private function display_rate_limit_banner( $rate_limit_time ) { |
| 50 | $retry_after = human_time_diff( time(), $rate_limit_time ); |
| 51 | |
| 52 | echo '<div class="notice notice-warning is-dismissible">'; |
| 53 | echo '<p>'; |
| 54 | printf( |
| 55 | /* translators: %s: Time remaining */ |
| 56 | esc_html__( 'This site has made too many requests in a short period. Please wait %s before trying again.', 'wp-module-performance' ), |
| 57 | esc_html( $retry_after ) |
| 58 | ); |
| 59 | echo '</p>'; |
| 60 | echo '</div>'; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Displays the permanent ban banner. |
| 65 | */ |
| 66 | private function display_ban_banner() { |
| 67 | $support_link = apply_filters( 'nfd_build_url', admin_url( "admin.php?page={$this->brand}#/help" ) ); |
| 68 | |
| 69 | echo '<div class="notice notice-error">'; |
| 70 | echo '<p>'; |
| 71 | printf( |
| 72 | wp_kses( |
| 73 | /* translators: %s: Support link */ |
| 74 | __( 'This site no longer qualifies for image optimization as it has reached its usage limits. Please <a href="%s">contact support</a> for assistance.', 'wp-module-performance' ), |
| 75 | array( |
| 76 | 'a' => array( |
| 77 | 'href' => array(), |
| 78 | ), |
| 79 | ) |
| 80 | ), |
| 81 | esc_url( $support_link ) |
| 82 | ); |
| 83 | echo '</p>'; |
| 84 | echo '</div>'; |
| 85 | } |
| 86 | } |