Cookiebot_Recommendation_Notice.php
113 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 function cybot\cookiebot\lib\asset_url; |
| 9 | use function cybot\cookiebot\lib\get_view_html; |
| 10 | use function cybot\cookiebot\lib\include_view; |
| 11 | |
| 12 | class Cookiebot_Recommendation_Notice { |
| 13 | |
| 14 | const COOKIEBOT_RECOMMENDATION_OPTION_KEY = 'cookiebot_notice_recommend'; |
| 15 | |
| 16 | public function register_hooks() { |
| 17 | add_action( 'admin_notices', array( $this, 'show_notice_if_needed' ) ); |
| 18 | } |
| 19 | |
| 20 | public function show_notice_if_needed() { |
| 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 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @throws InvalidArgumentException |
| 34 | */ |
| 35 | private function show_notice() { |
| 36 | $two_week_review_ignore = wp_nonce_url( |
| 37 | add_query_arg( array( 'cookiebot_admin_notice' => 'hide' ) ), |
| 38 | 'hide_recommendation', |
| 39 | 'nonce' |
| 40 | ); |
| 41 | $two_week_review_temp = wp_nonce_url( |
| 42 | add_query_arg( array( 'cookiebot_admin_notice' => 'two_week' ) ), |
| 43 | 'hide_recommendation_for_two_weeks', |
| 44 | 'nonce' |
| 45 | ); |
| 46 | |
| 47 | $notice = array( |
| 48 | 'title' => __( 'Leave A Review?', 'cookiebot' ), |
| 49 | 'msg' => __( |
| 50 | 'We hope you enjoy using WordPress Cookiebot! Would you consider leaving us a review on WordPress.org?', |
| 51 | 'cookiebot' |
| 52 | ), |
| 53 | 'link_html' => get_view_html( |
| 54 | 'admin/notices/cookiebot-recommendation-notice-links.php', |
| 55 | array( |
| 56 | 'two_week_review_ignore' => $two_week_review_ignore, |
| 57 | 'two_week_review_temp' => $two_week_review_temp, |
| 58 | ) |
| 59 | ), |
| 60 | 'later_link' => $two_week_review_temp, |
| 61 | 'int' => 14, |
| 62 | ); |
| 63 | |
| 64 | include_view( 'admin/notices/cookiebot-recommendation-notice.php', array( 'notice' => $notice ) ); |
| 65 | |
| 66 | wp_enqueue_style( |
| 67 | 'cookiebot-admin-notices', |
| 68 | asset_url( 'css/notice.css' ), |
| 69 | null, |
| 70 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Validate if the last user action is valid for plugin recommendation |
| 76 | * |
| 77 | * @throws Exception |
| 78 | * |
| 79 | * @version 2.0.5 |
| 80 | * @since 2.0.5 |
| 81 | */ |
| 82 | private function do_we_need_to_show_the_notice_message() { |
| 83 | $option = get_option( static::COOKIEBOT_RECOMMENDATION_OPTION_KEY ); |
| 84 | |
| 85 | if ( $option !== false ) { |
| 86 | //"Never show again" is clicked |
| 87 | if ( $option === 'hide' ) { |
| 88 | throw new Exception( 'Never show again is clicked' ); |
| 89 | } elseif ( is_numeric( $option ) && strtotime( 'now' ) < $option ) { |
| 90 | throw new Exception( '"Show me after 2 weeks" is clicked and 2 weeks is not passed yet' ); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Save the user action on cookiebot recommendation link |
| 97 | * |
| 98 | * @version 2.0.5 |
| 99 | * @since 2.0.5 |
| 100 | */ |
| 101 | private function save_notice_link() { |
| 102 | if ( isset( $_GET['cookiebot_admin_notice'] ) && isset( $_GET['nonce'] ) ) { |
| 103 | if ( wp_verify_nonce( $_GET['nonce'], 'hide_recommendation' ) ) { |
| 104 | update_option( static::COOKIEBOT_RECOMMENDATION_OPTION_KEY, 'hide' ); |
| 105 | } |
| 106 | |
| 107 | if ( wp_verify_nonce( $_GET['nonce'], 'hide_recommendation_for_two_weeks' ) ) { |
| 108 | update_option( static::COOKIEBOT_RECOMMENDATION_OPTION_KEY, strtotime( '+2 weeks' ) ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 |