| 1 |
<?php |
| 2 |
|
| 3 |
namespace TableBuilderScopedDeps\Wpmet\UtilityPackage\Notice; |
| 4 |
|
| 5 |
\defined('ABSPATH') || exit; |
| 6 |
use TableBuilderScopedDeps\Wpmet\UtilityPackage\Helper\Helper as UtilsHelper; |
| 7 |
/** |
| 8 |
* Showing Notice |
| 9 |
* other stuffs |
| 10 |
* Class Notice |
| 11 |
* @package Wpmet\UtilityPackage |
| 12 |
*/ |
| 13 |
class Notice |
| 14 |
{ |
| 15 |
/** |
| 16 |
* scripts version |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
// protected $script_version = '2.1.1'; |
| 21 |
/** |
| 22 |
* Unique ID to identify each notice |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $notice_id; |
| 27 |
/** |
| 28 |
* Plugin text-domain |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $text_domain; |
| 33 |
/** |
| 34 |
* Unique ID |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $unique_id; |
| 39 |
/** |
| 40 |
* Notice div container's class |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
protected $class; |
| 45 |
/** |
| 46 |
* Single button's data |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
protected $button; |
| 51 |
/** |
| 52 |
* Size class |
| 53 |
* |
| 54 |
* @var array |
| 55 |
*/ |
| 56 |
protected $size; |
| 57 |
/** |
| 58 |
* List of all buttons with it's config data |
| 59 |
* |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
protected $buttons; |
| 63 |
/** |
| 64 |
* Notice title |
| 65 |
* |
| 66 |
* @var string |
| 67 |
*/ |
| 68 |
protected $title; |
| 69 |
/** |
| 70 |
* Notice message |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
protected $message; |
| 75 |
/** |
| 76 |
* Left logo |
| 77 |
* |
| 78 |
* @var string |
| 79 |
*/ |
| 80 |
protected $logo; |
| 81 |
/** |
| 82 |
* Container gutter |
| 83 |
* |
| 84 |
* @var string |
| 85 |
*/ |
| 86 |
protected $gutter; |
| 87 |
/** |
| 88 |
* Left logo style |
| 89 |
* |
| 90 |
* @var string |
| 91 |
*/ |
| 92 |
protected $logo_style; |
| 93 |
/** |
| 94 |
* Left logo style |
| 95 |
* |
| 96 |
* @var string |
| 97 |
*/ |
| 98 |
protected $dismissible; |
| 99 |
protected $expired_time; |
| 100 |
/** |
| 101 |
* html markup for notice |
| 102 |
* |
| 103 |
* @var string |
| 104 |
*/ |
| 105 |
protected $html; |
| 106 |
/** |
| 107 |
* get_version |
| 108 |
* |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function get_version() |
| 112 |
{ |
| 113 |
// return $this->script_version; |
| 114 |
return UtilsHelper::get_pac_version(); |
| 115 |
} |
| 116 |
/** |
| 117 |
* get_script_location |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
public function get_script_location() |
| 122 |
{ |
| 123 |
return __FILE__; |
| 124 |
} |
| 125 |
// config |
| 126 |
/** |
| 127 |
* Configures all setter variables |
| 128 |
* |
| 129 |
* @param string $prefix |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function config($text_domain = '', $unique_id = '') |
| 133 |
{ |
| 134 |
$this->text_domain = $text_domain; |
| 135 |
$this->unique_id = $unique_id; |
| 136 |
$this->notice_id = $text_domain . '-' . $unique_id; |
| 137 |
$this->dismissible = \false; |
| 138 |
// false, user, global |
| 139 |
$this->expired_time = 1; |
| 140 |
$this->html = ''; |
| 141 |
$this->title = ''; |
| 142 |
$this->message = ''; |
| 143 |
$this->class = ''; |
| 144 |
$this->gutter = \true; |
| 145 |
$this->logo = ''; |
| 146 |
$this->logo_style = ''; |
| 147 |
$this->size = array(); |
| 148 |
$this->button = array( |
| 149 |
'default_class' => 'button', |
| 150 |
'class' => 'button-secondary ', |
| 151 |
// button-primary button-secondary button-small button-large button-link |
| 152 |
'text' => 'Button', |
| 153 |
'url' => '#', |
| 154 |
'icon' => '', |
| 155 |
); |
| 156 |
$this->buttons = array(); |
| 157 |
return $this; |
| 158 |
} |
| 159 |
// setters begin |
| 160 |
/** |
| 161 |
* Adds classes to the container |
| 162 |
* |
| 163 |
* @param string $classname |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function set_class($classname = '') |
| 167 |
{ |
| 168 |
$this->class .= $classname; |
| 169 |
return $this; |
| 170 |
} |
| 171 |
public function set_type($type = '') |
| 172 |
{ |
| 173 |
$this->class .= ' notice-' . $type; |
| 174 |
return $this; |
| 175 |
} |
| 176 |
public function set_button($button = array()) |
| 177 |
{ |
| 178 |
$button = \array_merge($this->button, $button); |
| 179 |
$this->buttons[] = $button; |
| 180 |
return $this; |
| 181 |
} |
| 182 |
public function set_id($id) |
| 183 |
{ |
| 184 |
$this->notice_id = $id; |
| 185 |
return $this; |
| 186 |
} |
| 187 |
public function set_title($title = '') |
| 188 |
{ |
| 189 |
$this->title .= $title; |
| 190 |
return $this; |
| 191 |
} |
| 192 |
public function set_message($message = '') |
| 193 |
{ |
| 194 |
$this->message .= $message; |
| 195 |
return $this; |
| 196 |
} |
| 197 |
public function set_gutter($gutter = \true) |
| 198 |
{ |
| 199 |
$this->gutter .= $gutter; |
| 200 |
$this->class .= $gutter === \true ? '' : ' no-gutter'; |
| 201 |
return $this; |
| 202 |
} |
| 203 |
public function set_logo($logo = '', $logo_style = '') |
| 204 |
{ |
| 205 |
$this->logo = $logo; |
| 206 |
$this->logo_style = $logo_style; |
| 207 |
return $this; |
| 208 |
} |
| 209 |
public function set_html($html = '') |
| 210 |
{ |
| 211 |
$this->html .= $html; |
| 212 |
return $this; |
| 213 |
} |
| 214 |
// setters ends |
| 215 |
// group getter |
| 216 |
public function get_data() |
| 217 |
{ |
| 218 |
return array('message' => $this->message, 'title' => $this->title, 'buttons' => $this->buttons, 'class' => $this->class, 'html' => $this->html); |
| 219 |
} |
| 220 |
public function call() |
| 221 |
{ |
| 222 |
add_action('admin_notices', array($this, 'get_notice')); |
| 223 |
} |
| 224 |
public function get_notice() |
| 225 |
{ |
| 226 |
// dismissible conditions |
| 227 |
if ('user' === $this->dismissible) { |
| 228 |
$expired = get_user_meta(get_current_user_id(), $this->notice_id, \true); |
| 229 |
} elseif ('global' === $this->dismissible) { |
| 230 |
$expired = get_transient($this->notice_id); |
| 231 |
} else { |
| 232 |
$expired = ''; |
| 233 |
} |
| 234 |
global $oxaim_lib_notice_list; |
| 235 |
if (!isset($oxaim_lib_notice_list[$this->notice_id])) { |
| 236 |
$oxaim_lib_notice_list[$this->notice_id] = __FILE__; |
| 237 |
// is transient expired? |
| 238 |
if (\false === $expired || empty($expired)) { |
| 239 |
$this->generate_html(); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
public function set_dismiss($scope = 'global', $time = 3600 * 24 * 7) |
| 244 |
{ |
| 245 |
$this->dismissible = $scope; |
| 246 |
$this->expired_time = $time; |
| 247 |
return $this; |
| 248 |
} |
| 249 |
public function generate_html() |
| 250 |
{ |
| 251 |
?> |
| 252 |
<div |
| 253 |
id="<?php |
| 254 |
echo esc_attr($this->notice_id); |
| 255 |
?>" |
| 256 |
class="notice wpmet-notice notice-<?php |
| 257 |
echo esc_attr($this->notice_id . ' ' . $this->class); |
| 258 |
?> <?php |
| 259 |
echo \false === $this->dismissible ? '' : 'is-dismissible'; |
| 260 |
?>" |
| 261 |
|
| 262 |
expired_time="<?php |
| 263 |
echo esc_attr($this->expired_time); |
| 264 |
?>" |
| 265 |
dismissible="<?php |
| 266 |
echo esc_attr($this->dismissible); |
| 267 |
?>" |
| 268 |
> |
| 269 |
<?php |
| 270 |
if (!empty($this->logo)) { |
| 271 |
?> |
| 272 |
<img class="notice-logo" style="<?php |
| 273 |
echo esc_attr($this->logo_style); |
| 274 |
?>" src="<?php |
| 275 |
echo \esc_url($this->logo); |
| 276 |
?>" /> |
| 277 |
<?php |
| 278 |
} |
| 279 |
?> |
| 280 |
|
| 281 |
<div class="notice-right-container <?php |
| 282 |
echo empty($this->logo) ? 'notice-container-full-width' : ''; |
| 283 |
?>"> |
| 284 |
|
| 285 |
<?php |
| 286 |
if (empty($this->html)) { |
| 287 |
?> |
| 288 |
<?php |
| 289 |
echo empty($this->title) ? '' : \sprintf('<div class="notice-main-title notice-vert-space">%s</div>', \esc_html($this->title)); |
| 290 |
?> |
| 291 |
|
| 292 |
<div class="notice-message notice-vert-space"> |
| 293 |
<?php |
| 294 |
echo \wp_kses($this->message, UtilsHelper::get_kses_array()); |
| 295 |
?> |
| 296 |
</div> |
| 297 |
|
| 298 |
<?php |
| 299 |
if (!empty($this->buttons)) { |
| 300 |
?> |
| 301 |
<div class="button-container notice-vert-space"> |
| 302 |
<?php |
| 303 |
foreach ($this->buttons as $button) { |
| 304 |
?> |
| 305 |
<a id="<?php |
| 306 |
echo !isset($button['id']) ? '' : esc_attr($button['id']); |
| 307 |
?>" href="<?php |
| 308 |
echo \esc_url($button['url']); |
| 309 |
?>" class="wpmet-notice-button <?php |
| 310 |
echo esc_attr($button['class']); |
| 311 |
?>"> |
| 312 |
<?php |
| 313 |
if (!empty($button['icon'])) { |
| 314 |
?> |
| 315 |
<i class="notice-icon <?php |
| 316 |
echo esc_attr($button['icon']); |
| 317 |
?>"></i> |
| 318 |
<?php |
| 319 |
} |
| 320 |
?> |
| 321 |
<?php |
| 322 |
echo \esc_html($button['text']); |
| 323 |
?> |
| 324 |
</a> |
| 325 |
|
| 326 |
<?php |
| 327 |
} |
| 328 |
?> |
| 329 |
</div> |
| 330 |
<?php |
| 331 |
} |
| 332 |
?> |
| 333 |
|
| 334 |
<?php |
| 335 |
} else { |
| 336 |
?> |
| 337 |
<?php |
| 338 |
echo \wp_kses($this->html, UtilsHelper::get_kses_array()); |
| 339 |
?> |
| 340 |
<?php |
| 341 |
} |
| 342 |
?> |
| 343 |
|
| 344 |
</div> |
| 345 |
|
| 346 |
<?php |
| 347 |
if (\false !== $this->dismissible) { |
| 348 |
?> |
| 349 |
<button type="button" class="notice-dismiss"> |
| 350 |
<span class="screen-reader-text">x#test-console-04</span> |
| 351 |
</button> |
| 352 |
<?php |
| 353 |
} |
| 354 |
?> |
| 355 |
|
| 356 |
<div style="clear:both"></div> |
| 357 |
|
| 358 |
</div> |
| 359 |
<?php |
| 360 |
} |
| 361 |
public static function init() |
| 362 |
{ |
| 363 |
add_action('wp_ajax_wpmet-notices', array(__CLASS__, 'dismiss_ajax_call')); |
| 364 |
add_action('admin_head', array(__CLASS__, 'enqueue_scripts')); |
| 365 |
} |
| 366 |
public static function dismiss_ajax_call() |
| 367 |
{ |
| 368 |
if (empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'wpmet-notices')) { |
| 369 |
return \false; |
| 370 |
} |
| 371 |
$notice_id = isset($_POST['notice_id']) ? sanitize_text_field(wp_unslash($_POST['notice_id'])) : ''; |
| 372 |
$dismissible = isset($_POST['dismissible']) ? sanitize_text_field(wp_unslash($_POST['dismissible'])) : ''; |
| 373 |
$expired_time = isset($_POST['expired_time']) ? sanitize_text_field(wp_unslash($_POST['expired_time'])) : ''; |
| 374 |
if (!empty($notice_id)) { |
| 375 |
if ('user' === $dismissible) { |
| 376 |
update_user_meta(get_current_user_id(), $notice_id, \true); |
| 377 |
} else { |
| 378 |
set_transient($notice_id, \true, $expired_time); |
| 379 |
} |
| 380 |
wp_send_json_success(); |
| 381 |
} |
| 382 |
wp_send_json_error(); |
| 383 |
} |
| 384 |
public static function enqueue_scripts() |
| 385 |
{ |
| 386 |
echo "\n\t\t<script>\n\t\t\tjQuery(document).ready(function (\$) {\n\t\t\t\t\$( '.wpmet-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {\n\n\t\t\t\t\t_this \t\t = \$( this ).parents('.wpmet-notice').eq(0);\n\t\t\t\t\tvar notice_id \t = _this.attr( 'id' ) || '';\n\t\t\t\t\tvar expired_time \t= _this.attr( 'expired_time' ) || '';\n\t\t\t\t\tvar dismissible \t= _this.attr( 'dismissible' ) || '';\n\t\t\t\t\tvar x = \$( this ).attr('class');\n\n\t\t\t\t\t// console.log({\n\t\t\t\t\t// _this, x, notice_id, expired_time, dismissible\n\t\t\t\t\t// });\n\t\t\t\t\t// return;\n\n\t\t\t\t\t_this.hide();\n\n\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\turl: ajaxurl,\n\t\t\t\t\t\ttype: 'POST',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\taction \t : 'wpmet-notices',\n\t\t\t\t\t\t\tnotice_id \t\t: notice_id,\n\t\t\t\t\t\t\tdismissible \t: dismissible,\n\t\t\t\t\t\t\texpired_time \t: expired_time,\n\t\t\t\t\t\t\tnonce \t\t\t: '" . esc_js(wp_create_nonce('wpmet-notices')) . "'\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t});\n\t\t</script>\n\t\t<style>\n\t\t\t.wpmet-notice{\n\t\t\t\tmargin-bottom: 15px;\n\t\t\t\tpadding: 0!important;\n\t\t\t\tdisplay: flex;\n\t\t\t\tflex-direction: row;\n\t\t\t\tjustify-content: flex-start;\n\t\t\t\talign-items: center;\n\t\t\t}\n\n\t\t\t.wpmet-notice .notice-right-container{\n\t\t\t\tmargin: .7rem .8rem .8rem;\n\t\t\t}\n\n\t\t\t.notice-container-full-width{\n\t\t\t\twidth:100%!important;\n\t\t\t}\n\t\t\t\n\t\t\t.wpmet-notice.no-gutter{\n\t\t\t\tpadding: 0!important;\n\t\t\t\tborder-width: 0!important;\n\t\t\t}\n\t\t\t.wpmet-notice.no-gutter .notice-right-container{\n\t\t\t\tpadding: 0!important;\n\t\t\t\tmargin: 0!important;\n\t\t\t}\n\n\t\t\t.notice-right-container .notice-vert-space{\n\t\t\t\tmargin-bottom: .8rem;\n\t\t\t}\n\n\t\t\t.notice-right-container .notice-vert-space:last-child,\n\t\t\t.notice-right-container .notice-vert-space:only-child{\n\t\t\t\tmargin-bottom: 0;\n\t\t\t}\n\n\t\t\t.wpmet-notice .notice-logo{\n\t\t\t\tpadding: 3px;\n\t\t\t\tmax-width: 110px;\n\t\t\t\tmax-height: 110px;\n\t\t\t}\n\t\t\t\n\t\t\t.wpmet-notice-button {\n\t\t\t\ttext-decoration:none;\n\t\t\t}\n\t\t\t\n\t\t\t.wpmet-notice-button > i{\n\t\t\t\tmargin-right: 3px;\n\t\t\t}\n\t\t\t\n\t\t\t.wpmet-notice-button .notice-icon{\n\t\t\t\tdisplay:inline-block;\n\t\t\t}\n\n\t\t\t.wpmet-notice-button .notice-icon:before{\n\t\t\t\tvertical-align: middle!important;\n\t\t\t\tmargin-top: -1px;\n\t\t\t}\n\n\t\t\t.wpmet-notice .notice-main-title{\n\t\t\t\tcolor: #1d2327;\n\t\t\t\tfont-size: 1.2rem;\n\t\t\t}\n\t\t\t\n\t\t</style>\n\t"; |
| 387 |
} |
| 388 |
private static $instance; |
| 389 |
/** |
| 390 |
* Method: instance -> Return Notice module class instance |
| 391 |
* |
| 392 |
* @param string|null $text_domain |
| 393 |
* @param string|null $unique_id |
| 394 |
* @return mixed |
| 395 |
*/ |
| 396 |
public static function instance($text_domain = null, $unique_id = null) |
| 397 |
{ |
| 398 |
if ($text_domain == null) { |
| 399 |
return \false; |
| 400 |
} |
| 401 |
self::$instance = new self(); |
| 402 |
return self::$instance->config($text_domain, \is_null($unique_id) ? \uniqid() : $unique_id); |
| 403 |
} |
| 404 |
} |
| 405 |
|