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