| 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 ( ! current_user_can( 'manage_options' ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
if ( isset( $_GET[ static::COOKIEBOT_NOTICE_OPTION_KEY ] ) ) { |
| 66 |
foreach ( static::COOKIEBOT_NOTICE_TIMES as $item ) { |
| 67 |
if ( isset( $_GET[ $item['name'] ] ) && |
| 68 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce value verified by wp_verify_nonce(). |
| 69 |
wp_verify_nonce( wp_unslash( $_GET[ $item['name'] ] ), $item['action'] ) ) { |
| 70 |
$option = $item['time']; |
| 71 |
if ( isset( $item['str'] ) && $item['str'] === true ) { |
| 72 |
$option = strtotime( $item['time'] ); |
| 73 |
} |
| 74 |
update_option( static::COOKIEBOT_NOTICE_OPTION_KEY, $option ); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Validate if the last user action is valid for plugin recommendation |
| 82 |
* |
| 83 |
* @throws Exception |
| 84 |
* |
| 85 |
* @version 4.3.9 |
| 86 |
* @since 2.0.5 |
| 87 |
*/ |
| 88 |
private function do_we_need_to_show_the_notice_message() { |
| 89 |
$option = get_option( static::COOKIEBOT_NOTICE_OPTION_KEY ); |
| 90 |
|
| 91 |
if ( $option !== false ) { |
| 92 |
if ( $this->hide_condition !== false ) { |
| 93 |
throw new LogicException( 'Hidden' ); |
| 94 |
} |
| 95 |
// "Never show again" is clicked |
| 96 |
if ( array_key_exists( $option, static::COOKIEBOT_NOTICE_TIMES ) ) { |
| 97 |
throw new LogicException( esc_html( static::COOKIEBOT_NOTICE_TIMES[ $option ]['msg'] ) ); |
| 98 |
} elseif ( is_numeric( $option ) ) { |
| 99 |
if ( ! self::notice_check_option_date( $option ) ) { |
| 100 |
throw new LogicException( 'Show me after some time' ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private function notice_check_option_date( $option ) { |
| 107 |
if ( ( strtotime( 'now' ) < $option && static::COOKIEBOT_NOTICE_TIMES_REVERT ) || |
| 108 |
( strtotime( 'now' ) > $option && ! static::COOKIEBOT_NOTICE_TIMES_REVERT ) ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Return html for the link html parameter |
| 117 |
* |
| 118 |
* @version 4.3.9 |
| 119 |
* @since 4.3.9 |
| 120 |
*/ |
| 121 |
public function get_link_html() { |
| 122 |
return ''; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Include notice on page |
| 127 |
* |
| 128 |
* @throws InvalidArgumentException |
| 129 |
* @since 4.3.9 |
| 130 |
* @version 4.3.9 |
| 131 |
*/ |
| 132 |
private function show_notice() { |
| 133 |
include_view( |
| 134 |
static::COOKIEBOT_NOTICE_TEMPLATE_PATH, |
| 135 |
array( |
| 136 |
'notice' => array( |
| 137 |
'title' => $this->translations['title'], |
| 138 |
'msg' => $this->translations['msg'], |
| 139 |
'link_html' => $this->get_link_html(), |
| 140 |
'later_link' => wp_nonce_url( |
| 141 |
add_query_arg( |
| 142 |
array( |
| 143 |
static::COOKIEBOT_NOTICE_OPTION_KEY => static::COOKIEBOT_NOTICE_TIMES['later']['time'], |
| 144 |
) |
| 145 |
), |
| 146 |
static::COOKIEBOT_NOTICE_TIMES['later']['action'], |
| 147 |
static::COOKIEBOT_NOTICE_TIMES['later']['name'] |
| 148 |
), |
| 149 |
), |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
wp_enqueue_style( |
| 154 |
'cookiebot-admin-notices', |
| 155 |
asset_url( 'css/notice.css' ), |
| 156 |
null, |
| 157 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Check if the notice needs to be shown |
| 163 |
* |
| 164 |
* @version 4.3.9 |
| 165 |
* @since 4.3.9 |
| 166 |
*/ |
| 167 |
public function show_notice_if_needed() { |
| 168 |
// Save actions when someone click on the notice message |
| 169 |
$this->save_notice_link(); |
| 170 |
|
| 171 |
try { |
| 172 |
$this->do_we_need_to_show_the_notice_message(); |
| 173 |
$this->show_notice(); |
| 174 |
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 175 |
} catch ( Exception $e ) { |
| 176 |
// If exception has been thrown, then we don't need to show the notice. |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|