Cookiebot_Temp_Notice.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\admin_notices; |
| 4 | |
| 5 | use Exception; |
| 6 | use cybot\cookiebot\lib\Cookiebot_WP; |
| 7 | use InvalidArgumentException; |
| 8 | use LogicException; |
| 9 | use function cybot\cookiebot\lib\asset_url; |
| 10 | use function cybot\cookiebot\lib\include_view; |
| 11 | |
| 12 | class Cookiebot_Temp_Notice { |
| 13 | |
| 14 | const COOKIEBOT_TEMP_OPTION_KEY = 'cookiebot_notice_temp'; |
| 15 | |
| 16 | public function register_hooks() { |
| 17 | add_action( 'admin_notices', array( $this, 'show_notice_temp' ) ); |
| 18 | } |
| 19 | |
| 20 | public function show_notice_temp() { |
| 21 | /** Save actions when someone click on the notice message */ |
| 22 | $this->save_notice_link(); |
| 23 | |
| 24 | try { |
| 25 | $this->do_we_need_to_show_the_notice_message(); |
| 26 | $this->show_notice(); |
| 27 | // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 28 | } catch ( Exception $e ) { |
| 29 | // If exception has been thrown, then we don't need to show the notice. |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @throws InvalidArgumentException |
| 35 | */ |
| 36 | private function show_notice() { |
| 37 | $show_temp = wp_nonce_url( |
| 38 | add_query_arg( array( 'cookiebot_notice_temp' => 'hide' ) ), |
| 39 | 'hide_temp_notice', |
| 40 | 'temp_nonce' |
| 41 | ); |
| 42 | |
| 43 | $update_notice = array( |
| 44 | 'title' => '', |
| 45 | 'msg' => __( |
| 46 | 'Cookiebot CMP Plugin will soon no longer support PHP 5. If your website still runs on this version we recommend upgrading so you can continue enjoying the features Cookiebot CMP offers.', |
| 47 | 'cookiebot' |
| 48 | ), |
| 49 | 'link_html' => '', |
| 50 | 'later_link' => $show_temp, |
| 51 | 'int' => 14, |
| 52 | ); |
| 53 | |
| 54 | include_view( 'admin/notices/cookiebot-recommendation-notice.php', array( 'notice' => $update_notice ) ); |
| 55 | |
| 56 | wp_enqueue_style( |
| 57 | 'cookiebot-admin-notices', |
| 58 | asset_url( 'css/notice.css' ), |
| 59 | null, |
| 60 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Validate if the last user action is valid for plugin recommendation |
| 66 | * |
| 67 | * @throws Exception |
| 68 | * |
| 69 | * @version 2.0.5 |
| 70 | * @since 2.0.5 |
| 71 | */ |
| 72 | private function do_we_need_to_show_the_notice_message() { |
| 73 | $option = get_option( static::COOKIEBOT_TEMP_OPTION_KEY ); |
| 74 | |
| 75 | if ( $option !== false ) { |
| 76 | // "Never show again" is clicked |
| 77 | if ( $option === 'hide' ) { |
| 78 | throw new LogicException( 'Never show again is clicked' ); |
| 79 | } elseif ( is_numeric( $option ) && strtotime( 'now' ) < $option ) { |
| 80 | throw new LogicException( 'Show me after 1 day' ); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Save the user action on cookiebot recommendation link |
| 87 | * |
| 88 | * @version 2.0.5 |
| 89 | * @since 2.0.5 |
| 90 | */ |
| 91 | private function save_notice_link() { |
| 92 | if ( isset( $_GET['cookiebot_notice_temp'] ) && |
| 93 | isset( $_GET['temp_nonce'] ) && |
| 94 | wp_verify_nonce( $_GET['temp_nonce'], 'hide_temp_notice' ) ) { |
| 95 | update_option( static::COOKIEBOT_TEMP_OPTION_KEY, 'hide' ); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 |