Cookiebot_Base_Notice.php
2 years ago
Cookiebot_Bf_Notice.php
1 year ago
Cookiebot_Notices.php
1 year ago
Cookiebot_Recommendation_Notice.php
2 years ago
Cookiebot_Temp_Notice.php
2 years ago
Cookiebot_Base_Notice.php
160 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_Base_Notice { |
| 13 | const COOKIEBOT_NOTICE_OPTION_KEY = ''; |
| 14 | const COOKIEBOT_NOTICE_TEMPLATE_PATH = ''; |
| 15 | |
| 16 | const COOKIEBOT_NOTICE_TIMES = array(); |
| 17 | const COOKIEBOT_NOTICE_TIMES_REVERT = false; |
| 18 | |
| 19 | public $translations = array(); |
| 20 | |
| 21 | public function __construct() { |
| 22 | $this->define_translations(); |
| 23 | } |
| 24 | |
| 25 | public function register_hooks() { |
| 26 | add_action( 'admin_notices', array( $this, 'show_notice_if_needed' ) ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Set translations |
| 31 | * |
| 32 | * @version 4.3.9 |
| 33 | * @since 4.3.9 |
| 34 | */ |
| 35 | public function define_translations() { |
| 36 | $this->translations = array( |
| 37 | 'title' => '', |
| 38 | 'msg' => '', |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Save the user action on cookiebot recommendation link |
| 44 | * |
| 45 | * @version 4.3.9 |
| 46 | * @since 2.0.5 |
| 47 | */ |
| 48 | private function save_notice_link() { |
| 49 | if ( isset( $_GET[ static::COOKIEBOT_NOTICE_OPTION_KEY ] ) ) { |
| 50 | foreach ( static::COOKIEBOT_NOTICE_TIMES as $item ) { |
| 51 | if ( isset( $_GET[ $item['name'] ] ) && |
| 52 | wp_verify_nonce( $_GET[ $item['name'] ], $item['action'] ) ) { |
| 53 | $option = $item['time']; |
| 54 | if ( isset( $item['str'] ) && $item['str'] === true ) { |
| 55 | $option = strtotime( $item['time'] ); |
| 56 | } |
| 57 | update_option( static::COOKIEBOT_NOTICE_OPTION_KEY, $option ); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Validate if the last user action is valid for plugin recommendation |
| 65 | * |
| 66 | * @throws Exception |
| 67 | * |
| 68 | * @version 4.3.9 |
| 69 | * @since 2.0.5 |
| 70 | */ |
| 71 | private function do_we_need_to_show_the_notice_message() { |
| 72 | $option = get_option( static::COOKIEBOT_NOTICE_OPTION_KEY ); |
| 73 | |
| 74 | if ( $option !== false ) { |
| 75 | // "Never show again" is clicked |
| 76 | if ( array_key_exists( $option, static::COOKIEBOT_NOTICE_TIMES ) ) { |
| 77 | throw new LogicException( static::COOKIEBOT_NOTICE_TIMES[ $option ]['msg'] ); |
| 78 | } elseif ( is_numeric( $option ) ) { |
| 79 | if ( ! self::notice_check_option_date( $option ) ) { |
| 80 | throw new LogicException( 'Show me after some time' ); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private function notice_check_option_date( $option ) { |
| 87 | if ( ( strtotime( 'now' ) < $option && static::COOKIEBOT_NOTICE_TIMES_REVERT ) || |
| 88 | ( strtotime( 'now' ) > $option && ! static::COOKIEBOT_NOTICE_TIMES_REVERT ) ) { |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return html for the link html parameter |
| 97 | * |
| 98 | * @version 4.3.9 |
| 99 | * @since 4.3.9 |
| 100 | */ |
| 101 | public function get_link_html() { |
| 102 | return ''; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Include notice on page |
| 107 | * |
| 108 | * @version 4.3.9 |
| 109 | * @since 4.3.9 |
| 110 | * @throws InvalidArgumentException |
| 111 | */ |
| 112 | private function show_notice() { |
| 113 | include_view( |
| 114 | static::COOKIEBOT_NOTICE_TEMPLATE_PATH, |
| 115 | array( |
| 116 | 'notice' => array( |
| 117 | 'title' => $this->translations['title'], |
| 118 | 'msg' => $this->translations['msg'], |
| 119 | 'link_html' => $this->get_link_html(), |
| 120 | 'later_link' => wp_nonce_url( |
| 121 | add_query_arg( |
| 122 | array( |
| 123 | static::COOKIEBOT_NOTICE_OPTION_KEY => static::COOKIEBOT_NOTICE_TIMES['later']['time'], |
| 124 | ) |
| 125 | ), |
| 126 | static::COOKIEBOT_NOTICE_TIMES['later']['action'], |
| 127 | static::COOKIEBOT_NOTICE_TIMES['later']['name'] |
| 128 | ), |
| 129 | ), |
| 130 | ) |
| 131 | ); |
| 132 | |
| 133 | wp_enqueue_style( |
| 134 | 'cookiebot-admin-notices', |
| 135 | asset_url( 'css/notice.css' ), |
| 136 | null, |
| 137 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Check if the notice needs to be shown |
| 143 | * |
| 144 | * @version 4.3.9 |
| 145 | * @since 4.3.9 |
| 146 | */ |
| 147 | public function show_notice_if_needed() { |
| 148 | // Save actions when someone click on the notice message |
| 149 | $this->save_notice_link(); |
| 150 | |
| 151 | try { |
| 152 | $this->do_we_need_to_show_the_notice_message(); |
| 153 | $this->show_notice(); |
| 154 | // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 155 | } catch ( Exception $e ) { |
| 156 | // If exception has been thrown, then we don't need to show the notice. |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 |