Cookiebot_Recommendation_Notice.php
123 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\get_view_html; |
| 11 | use function cybot\cookiebot\lib\include_view; |
| 12 | |
| 13 | class Cookiebot_Recommendation_Notice { |
| 14 | |
| 15 | const COOKIEBOT_RECOMMENDATION_OPTION_KEY = 'cookiebot_notice_recommend'; |
| 16 | |
| 17 | public function register_hooks() { |
| 18 | add_action( 'admin_notices', array( $this, 'show_notice_if_needed' ) ); |
| 19 | } |
| 20 | |
| 21 | public function show_notice_if_needed() { |
| 22 | /** Save actions when someone click on the notice message */ |
| 23 | $this->save_notice_link(); |
| 24 | |
| 25 | try { |
| 26 | $this->do_we_need_to_show_the_notice_message(); |
| 27 | $this->show_notice(); |
| 28 | // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 29 | } catch ( Exception $e ) { |
| 30 | // If exception has been thrown, then we don't need to show the notice. |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @throws InvalidArgumentException |
| 36 | */ |
| 37 | private function show_notice() { |
| 38 | $two_week_review_ignore = wp_nonce_url( |
| 39 | add_query_arg( array( 'cookiebot_admin_notice' => 'hide' ) ), |
| 40 | 'hide_recommendation', |
| 41 | 'nonce' |
| 42 | ); |
| 43 | $two_week_review_temp = wp_nonce_url( |
| 44 | add_query_arg( array( 'cookiebot_admin_notice' => 'two_week' ) ), |
| 45 | 'hide_recommendation_for_two_weeks', |
| 46 | 'nonce' |
| 47 | ); |
| 48 | $visit_review_temp = wp_nonce_url( |
| 49 | add_query_arg( array( 'cookiebot_admin_notice' => 'visit' ) ), |
| 50 | 'hide_recommendation_next', |
| 51 | 'nonce' |
| 52 | ); |
| 53 | |
| 54 | $notice = array( |
| 55 | 'title' => __( 'Leave A Review?', 'cookiebot' ), |
| 56 | 'msg' => __( |
| 57 | 'Hi, you have been using our Cookiebot CMP plugin to actively collect user consent - that is awesome. Could you please do us a BIG favor and give it a 5-star rating on WordPress? To help us spread the word and enable more WP websites to easily achieve compliance with GDPR and CCPA.', |
| 58 | 'cookiebot' |
| 59 | ), |
| 60 | 'link_html' => get_view_html( |
| 61 | 'admin/notices/cookiebot-recommendation-notice-links.php', |
| 62 | array( |
| 63 | 'two_week_review_ignore' => $two_week_review_ignore, |
| 64 | 'two_week_review_temp' => $two_week_review_temp, |
| 65 | 'visit_review_temp' => $visit_review_temp, |
| 66 | ) |
| 67 | ), |
| 68 | 'later_link' => $visit_review_temp, |
| 69 | 'int' => 14, |
| 70 | ); |
| 71 | |
| 72 | include_view( 'admin/notices/cookiebot-recommendation-notice.php', array( 'notice' => $notice ) ); |
| 73 | |
| 74 | wp_enqueue_style( |
| 75 | 'cookiebot-admin-notices', |
| 76 | asset_url( 'css/notice.css' ), |
| 77 | null, |
| 78 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Validate if the last user action is valid for plugin recommendation |
| 84 | * |
| 85 | * @throws Exception |
| 86 | * |
| 87 | * @version 2.0.5 |
| 88 | * @since 2.0.5 |
| 89 | */ |
| 90 | private function do_we_need_to_show_the_notice_message() { |
| 91 | $option = get_option( static::COOKIEBOT_RECOMMENDATION_OPTION_KEY ); |
| 92 | |
| 93 | if ( $option !== false ) { |
| 94 | // "Never show again" is clicked |
| 95 | if ( $option === 'hide' ) { |
| 96 | throw new LogicException( 'Never show again is clicked' ); |
| 97 | } elseif ( $option === 'visit' ) { |
| 98 | throw new LogicException( 'Show me after 1 day' ); |
| 99 | } elseif ( is_numeric( $option ) && strtotime( 'now' ) < $option ) { |
| 100 | throw new LogicException( 'Show me after 1 day' ); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Save the user action on cookiebot recommendation link |
| 107 | * |
| 108 | * @version 2.0.5 |
| 109 | * @since 2.0.5 |
| 110 | */ |
| 111 | private function save_notice_link() { |
| 112 | if ( isset( $_GET['cookiebot_admin_notice'] ) && isset( $_GET['nonce'] ) ) { |
| 113 | if ( wp_verify_nonce( $_GET['nonce'], 'hide_recommendation' ) ) { |
| 114 | update_option( static::COOKIEBOT_RECOMMENDATION_OPTION_KEY, 'hide' ); |
| 115 | } |
| 116 | |
| 117 | if ( wp_verify_nonce( $_GET['nonce'], 'hide_recommendation_next' ) ) { |
| 118 | update_option( static::COOKIEBOT_RECOMMENDATION_OPTION_KEY, strtotime( '+1 day' ) ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 |