| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimateStoreKit; |
| 4 |
|
| 5 |
class Notices { |
| 6 |
|
| 7 |
private static $notices = []; |
| 8 |
|
| 9 |
private static $instance; |
| 10 |
|
| 11 |
public static function get_instance() { |
| 12 |
if (!isset(self::$instance)) { |
| 13 |
self::$instance = new self; |
| 14 |
} |
| 15 |
return self::$instance; |
| 16 |
} |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
|
| 20 |
add_action('admin_notices', [$this, 'show_notices']); |
| 21 |
add_action('wp_ajax_ultimate-store-kit-notices', [$this, 'dismiss']); |
| 22 |
} |
| 23 |
|
| 24 |
public static function add_notice($args = []) { |
| 25 |
if (is_array($args)) { |
| 26 |
self::$notices[] = $args; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Dismiss Notice. |
| 32 |
*/ |
| 33 |
public function dismiss() { |
| 34 |
|
| 35 |
$id = isset($_POST['id']) ? sanitize_text_field($_POST['id']) : ''; |
| 36 |
$time = isset($_POST['time']) ? sanitize_text_field($_POST['time']) : ''; |
| 37 |
$meta = isset($_POST['meta']) ? sanitize_text_field($_POST['meta']) : ''; |
| 38 |
|
| 39 |
// Valid inputs? |
| 40 |
if (!empty($id)) { |
| 41 |
|
| 42 |
if ('user' === $meta) { |
| 43 |
update_user_meta(get_current_user_id(), $id, true); |
| 44 |
} else { |
| 45 |
set_transient($id, true, $time); |
| 46 |
} |
| 47 |
|
| 48 |
wp_send_json_success(); |
| 49 |
} |
| 50 |
|
| 51 |
wp_send_json_error(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Notice Types |
| 56 |
*/ |
| 57 |
public function show_notices() { |
| 58 |
|
| 59 |
$defaults = [ |
| 60 |
'id' => '', |
| 61 |
'type' => 'info', |
| 62 |
'show_if' => true, |
| 63 |
'message' => '', |
| 64 |
'class' => 'ultimate-store-kit-notice', |
| 65 |
'dismissible' => false, |
| 66 |
'dismissible-meta' => 'transient', |
| 67 |
'dismissible-time' => WEEK_IN_SECONDS, |
| 68 |
'data' => '', |
| 69 |
]; |
| 70 |
|
| 71 |
foreach (self::$notices as $key => $notice) { |
| 72 |
|
| 73 |
$notice = wp_parse_args($notice, $defaults); |
| 74 |
|
| 75 |
$classes = ['notice']; |
| 76 |
|
| 77 |
$classes[] = $notice['class']; |
| 78 |
if (isset($notice['type'])) { |
| 79 |
$classes[] = 'notice-' . $notice['type']; |
| 80 |
} |
| 81 |
|
| 82 |
// Is notice dismissible? |
| 83 |
if (true === $notice['dismissible']) { |
| 84 |
$classes[] = 'is-dismissible'; |
| 85 |
// Dismissable time. |
| 86 |
$notice['data'] = ' dismissible-time=' . esc_attr($notice['dismissible-time']) . ' '; |
| 87 |
} |
| 88 |
|
| 89 |
// Notice ID. |
| 90 |
$notice_id = 'ultimate-store-kit-notice-id-' . $notice['id']; |
| 91 |
$notice['id'] = $notice_id; |
| 92 |
|
| 93 |
if (!isset($notice['id'])) { |
| 94 |
$notice_id = 'ultimate-store-kit-notice-id-' . $notice['id']; |
| 95 |
$notice['id'] = $notice_id; |
| 96 |
} else { |
| 97 |
$notice_id = $notice['id']; |
| 98 |
} |
| 99 |
|
| 100 |
$notice['classes'] = implode(' ', $classes); |
| 101 |
|
| 102 |
// User meta. |
| 103 |
$notice['data'] .= ' dismissible-meta=' . esc_attr($notice['dismissible-meta']) . ' '; |
| 104 |
if ('user' === $notice['dismissible-meta']) { |
| 105 |
$expired = get_user_meta(get_current_user_id(), $notice_id, true); |
| 106 |
} elseif ('transient' === $notice['dismissible-meta']) { |
| 107 |
$expired = get_transient($notice_id); |
| 108 |
} |
| 109 |
|
| 110 |
// Notices visible after transient expire. |
| 111 |
if (isset($notice['show_if'])) { |
| 112 |
if (true === $notice['show_if']) { |
| 113 |
// Is transient expired? |
| 114 |
if (false === $expired || empty($expired)) { |
| 115 |
self::notice_layout($notice); |
| 116 |
} |
| 117 |
} |
| 118 |
} else { |
| 119 |
// No transient notices. |
| 120 |
self::notice_layout($notice); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Notice layout |
| 127 |
* @param array $notice Notice notice_layout. |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public static function notice_layout( $notice = [] ) { |
| 131 |
|
| 132 |
?> |
| 133 |
<div id="<?php echo esc_attr( $notice['id'] ); ?>" class="<?php echo esc_attr( $notice['classes'] ); ?>" <?php echo esc_attr( $notice['data'] ); ?>> |
| 134 |
<?php if(isset($notice['message']) && !empty($notice['message'])): ?> |
| 135 |
<p> |
| 136 |
<?php echo wp_kses_post( $notice['message'] ); ?> |
| 137 |
</p> |
| 138 |
<?php endif; ?> |
| 139 |
|
| 140 |
<?php |
| 141 |
if(isset($notice['html_message']) && !empty($notice['html_message'])): |
| 142 |
echo wp_kses_post( $notice['html_message'] ); |
| 143 |
endif; ?> |
| 144 |
|
| 145 |
</div> |
| 146 |
<?php |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
Notices::get_instance(); |
| 151 |
|