| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\NoticeDashboard; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if ( ! defined( 'EASYEL_NOTICE_SOURCE_URL' ) ) { |
| 9 |
define( 'EASYEL_NOTICE_SOURCE_URL', 'https://reactheme.com/products/license/' ); |
| 10 |
} |
| 11 |
|
| 12 |
class NoticeDashboard { |
| 13 |
|
| 14 |
/** |
| 15 |
* Slug this plugin identifies itself as when calling the notice API. |
| 16 |
* Each consuming plugin MUST set its own slug here — using a global |
| 17 |
* constant breaks when multiple notice-consuming plugins are activated |
| 18 |
* (the first one to load wins, and others request notices for the |
| 19 |
* wrong slug). |
| 20 |
*/ |
| 21 |
const PLUGIN_SLUG = 'easy-elements'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Display names for known plugin slugs. Used as a fallback when the |
| 25 |
* API response doesn't include a sub_title — we derive the name from |
| 26 |
* whichever plugin is currently consuming this NoticeDashboard. |
| 27 |
*/ |
| 28 |
private static $plugin_slug_titles = array( |
| 29 |
'boldpost' => 'boldpost', |
| 30 |
'easy-bookmark' => 'Easy bookmark', |
| 31 |
'easy-currency' => 'Easy Currency', |
| 32 |
'easy-elements' => 'Easy Elements', |
| 33 |
'easy-hotel' => 'easy hotel', |
| 34 |
'easy-menu-icons' => 'easy menu icons', |
| 35 |
'easy-wishlist' => 'easy wishlist', |
| 36 |
'themewant-product-gallery' => 'easy product gallery', |
| 37 |
'rt-mega-menu' => 'RT Mega Menu', |
| 38 |
); |
| 39 |
|
| 40 |
private static $instance = null; |
| 41 |
|
| 42 |
public static function get_instance() { |
| 43 |
if ( self::$instance === null ) { |
| 44 |
self::$instance = new self(); |
| 45 |
} |
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
public function __construct() { |
| 50 |
add_action( 'admin_notices', array( $this, 'easyel_notice_add_to_notice_bar' ) ); |
| 51 |
add_action( 'wp_dashboard_setup', array( $this, 'easyel_notice_add_to_dashboard_widget' ), 0 ); |
| 52 |
add_action( 'wp_ajax_easyel_notice_ignore_plugin_notice', array( $this, 'easyel_notice_ignore_plugin_notice' ) ); |
| 53 |
add_action( 'admin_enqueue_scripts', array( $this, 'easyel_notice_enqueue_widget_assets' ) ); |
| 54 |
add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'easyel_notice_force_widget_to_top' ) ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Load CSS on every admin page so notice-bar (which can render |
| 59 |
* anywhere) is styled correctly. Dashboard widget styles are also |
| 60 |
* included in the same file — harmless on non-dashboard pages. |
| 61 |
*/ |
| 62 |
public function easyel_notice_enqueue_widget_assets( $hook ) { |
| 63 |
wp_register_style( |
| 64 |
'easyel-notice-admin-style', |
| 65 |
EASYELEMENTS_DIR_URL . 'includes/NoticeDashboard/notice.css', |
| 66 |
array( 'dashicons' ), |
| 67 |
EASYELEMENTS_VER |
| 68 |
); |
| 69 |
wp_enqueue_style( 'easyel-notice-admin-style' ); |
| 70 |
|
| 71 |
wp_register_script( |
| 72 |
'easyel-notice-admin-script', |
| 73 |
EASYELEMENTS_DIR_URL . 'includes/NoticeDashboard/notice.js', |
| 74 |
array( 'jquery' ), |
| 75 |
EASYELEMENTS_VER, |
| 76 |
true |
| 77 |
); |
| 78 |
wp_localize_script( 'easyel-notice-admin-script', 'easyelNoticeData', array( |
| 79 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 80 |
'nonce' => wp_create_nonce( 'easyel_notice_nonce' ), |
| 81 |
) ); |
| 82 |
wp_enqueue_script( 'easyel-notice-admin-script' ); |
| 83 |
} |
| 84 |
|
| 85 |
public function easyel_notice_ignore_plugin_notice() { |
| 86 |
|
| 87 |
$user_id = get_current_user_id(); |
| 88 |
|
| 89 |
check_ajax_referer( 'easyel_notice_nonce', 'nonce' ); |
| 90 |
|
| 91 |
if ( isset( $_POST['notice_id'] ) && ! empty( $_POST['notice_id'] ) ) { |
| 92 |
|
| 93 |
$notice_id = sanitize_text_field( wp_unslash( $_POST['notice_id'] ) ); |
| 94 |
|
| 95 |
// NOTE: user_meta key kept as `thewtmc_notice_ignore_*` for |
| 96 |
// backward compatibility with previously dismissed notices. |
| 97 |
if ( $user_id && ! get_user_meta( $user_id, 'thewtmc_notice_ignore_' . $notice_id, true ) ) { |
| 98 |
add_user_meta( $user_id, 'thewtmc_notice_ignore_' . $notice_id, 'true', true ); |
| 99 |
} else { |
| 100 |
update_user_meta( $user_id, 'thewtmc_notice_ignore_' . $notice_id, 'true' ); |
| 101 |
} |
| 102 |
|
| 103 |
wp_send_json_success(); |
| 104 |
} |
| 105 |
|
| 106 |
wp_die(); |
| 107 |
} |
| 108 |
|
| 109 |
public function get_notice_status( $notice_id ) { |
| 110 |
$user_id = get_current_user_id(); |
| 111 |
return get_user_meta( $user_id, 'thewtmc_notice_ignore_' . $notice_id, true ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Return the display name for the currently consuming plugin (based on |
| 116 |
* PLUGIN_SLUG). Empty string if the slug isn't in the known list. |
| 117 |
*/ |
| 118 |
public function get_active_plugin_display_name() { |
| 119 |
if ( isset( self::$plugin_slug_titles[ self::PLUGIN_SLUG ] ) ) { |
| 120 |
return self::$plugin_slug_titles[ self::PLUGIN_SLUG ]; |
| 121 |
} |
| 122 |
return ''; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Fetch notices from the central source. Always tags requests with this |
| 127 |
* plugin's own slug so the API returns global + plugin-targeted notices. |
| 128 |
*/ |
| 129 |
public function easyel_notice_get_notices( $args = array() ) { |
| 130 |
|
| 131 |
if ( empty( $args['plugin'] ) ) { |
| 132 |
$args['plugin'] = self::PLUGIN_SLUG; |
| 133 |
} |
| 134 |
|
| 135 |
// API endpoint path stays as `/get_thewtmc` because it's the server |
| 136 |
// contract; renaming it would break the connection to the API. |
| 137 |
$notice_source_url = trailingslashit( EASYEL_NOTICE_SOURCE_URL ) . 'wp-json/reacthemes/v1/get_thewtmc'; |
| 138 |
|
| 139 |
$response = wp_remote_post( |
| 140 |
$notice_source_url, |
| 141 |
array( |
| 142 |
'headers' => array( 'Content-Type' => 'application/json' ), |
| 143 |
'timeout' => 30, |
| 144 |
'redirection' => 5, |
| 145 |
'blocking' => true, |
| 146 |
'sslverify' => false, |
| 147 |
'data_format' => 'body', |
| 148 |
'body' => wp_json_encode( $args ), |
| 149 |
) |
| 150 |
); |
| 151 |
|
| 152 |
if ( is_wp_error( $response ) ) { |
| 153 |
return ''; |
| 154 |
} |
| 155 |
|
| 156 |
return wp_remote_retrieve_body( $response ); |
| 157 |
} |
| 158 |
|
| 159 |
public function expire_notice_by_date( $notice_id, $expire_timestamp ) { |
| 160 |
|
| 161 |
$today_date = gmdate( 'Y-m-d' ); |
| 162 |
$today_timestamp = strtotime( $today_date ); |
| 163 |
|
| 164 |
if ( $today_timestamp >= $expire_timestamp ) { |
| 165 |
$user_id = get_current_user_id(); |
| 166 |
delete_user_meta( $user_id, 'thewtmc_notice_ignore_' . $notice_id ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public function easyel_notice_add_to_notice_bar() { |
| 171 |
|
| 172 |
$args = array( 'screen' => 'notice-bar' ); |
| 173 |
|
| 174 |
$all_notice = $this->easyel_notice_get_notices( $args ); |
| 175 |
|
| 176 |
if ( empty( $all_notice ) ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
$today_date = gmdate( 'Y-m-d' ); |
| 181 |
$today_timestamp = strtotime( $today_date ); |
| 182 |
$all_notice = json_decode( $all_notice, true ); |
| 183 |
|
| 184 |
if ( ! is_array( $all_notice ) ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
foreach ( $all_notice as $notice ) { |
| 189 |
|
| 190 |
$notice_id = isset( $notice['notice_id'] ) ? $notice['notice_id'] : ''; |
| 191 |
$thumbnail_url = isset( $notice['thumbnail_url'] ) ? $notice['thumbnail_url'] : ''; |
| 192 |
$thumbnail_link = isset( $notice['thumbnail_link'] ) ? $notice['thumbnail_link'] : ''; |
| 193 |
$content = isset( $notice['content'] ) ? $notice['content'] : ''; |
| 194 |
$action_buttons = isset( $notice['action_buttons'] ) ? $notice['action_buttons'] : array(); |
| 195 |
$expire_timestamp = isset( $notice['expire_date'] ) ? strtotime( $notice['expire_date'] ) : ''; |
| 196 |
|
| 197 |
$this->expire_notice_by_date( $notice_id, $expire_timestamp ); |
| 198 |
|
| 199 |
$notice_ignore_status = $this->get_notice_status( $notice_id ); |
| 200 |
|
| 201 |
if ( $notice_ignore_status !== 'true' && $today_timestamp <= $expire_timestamp ) : |
| 202 |
?> |
| 203 |
<div data-notice_id="<?php echo esc_attr( $notice_id ); ?>" id="easyel-notice-<?php echo esc_attr( $notice_id ); ?>" class="easyel-notice notice is-dismissible"> |
| 204 |
|
| 205 |
<?php if ( ! empty( $thumbnail_url ) ) : ?> |
| 206 |
<?php if ( ! empty( $thumbnail_link ) ) : ?> |
| 207 |
<a href="<?php echo esc_url( $thumbnail_link ); ?>" class="notice-logo-link" target="_blank" rel="noopener noreferrer" title="<?php echo esc_attr( $thumbnail_link ); ?>"> |
| 208 |
<img class="notice-logo" src="<?php echo esc_url( $thumbnail_url ); ?>" alt=""> |
| 209 |
</a> |
| 210 |
<?php else : ?> |
| 211 |
<img class="notice-logo" src="<?php echo esc_url( $thumbnail_url ); ?>" alt=""> |
| 212 |
<?php endif; ?> |
| 213 |
<?php endif; ?> |
| 214 |
|
| 215 |
<div class="notice-right-container"> |
| 216 |
<div class="notice-contents"> |
| 217 |
<?php echo wp_kses_post( $content ); ?> |
| 218 |
</div> |
| 219 |
|
| 220 |
<div class="easyel-notice-action-buttons"> |
| 221 |
<?php if ( ! empty( $action_buttons ) ) : ?> |
| 222 |
<?php foreach ( $action_buttons as $button ) : |
| 223 |
$action_url = isset( $button['url'] ) ? $button['url'] : ''; |
| 224 |
$action_title = isset( $button['title'] ) ? $button['title'] : ''; |
| 225 |
if ( empty( $action_url ) ) { |
| 226 |
continue; |
| 227 |
} |
| 228 |
?> |
| 229 |
<a href="<?php echo esc_url( $action_url ); ?>" class="easyel-notice-button" target="_blank"> |
| 230 |
<?php echo esc_html( $action_title ); ?> |
| 231 |
</a> |
| 232 |
<?php endforeach; ?> |
| 233 |
<?php endif; ?> |
| 234 |
<button type="button" class="easyel-notice-maybe-later" data-notice_id="<?php echo esc_attr( $notice_id ); ?>">Maybe Later</button> |
| 235 |
</div> |
| 236 |
|
| 237 |
</div> |
| 238 |
|
| 239 |
<div style="clear:both"></div> |
| 240 |
</div> |
| 241 |
<?php |
| 242 |
endif; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public function easyel_notice_add_to_dashboard_widget() { |
| 247 |
|
| 248 |
// Register at 'high' priority so it sits above all 'core'/'default' widgets. |
| 249 |
add_meta_box( |
| 250 |
'easyel_notice_widget', |
| 251 |
'ThemeWant Stories', |
| 252 |
array( $this, 'easyel_notice_widget_callback' ), |
| 253 |
'dashboard', |
| 254 |
'normal', |
| 255 |
'high' |
| 256 |
); |
| 257 |
|
| 258 |
// Make sure ours is the very first entry within the 'high' bucket. |
| 259 |
global $wp_meta_boxes; |
| 260 |
|
| 261 |
if ( ! isset( $wp_meta_boxes['dashboard']['normal']['high']['easyel_notice_widget'] ) ) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
$high_widgets = $wp_meta_boxes['dashboard']['normal']['high']; |
| 266 |
$our_widget = array( 'easyel_notice_widget' => $high_widgets['easyel_notice_widget'] ); |
| 267 |
unset( $high_widgets['easyel_notice_widget'] ); |
| 268 |
|
| 269 |
$wp_meta_boxes['dashboard']['normal']['high'] = $our_widget + $high_widgets; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Override the user's saved meta-box order so the Stories widget always |
| 274 |
* appears at the top of the 'normal' column. Without this filter, a user |
| 275 |
* who has previously dragged widgets around would keep the saved order |
| 276 |
* and our widget would end up wherever they left it. |
| 277 |
*/ |
| 278 |
public function easyel_notice_force_widget_to_top( $order ) { |
| 279 |
if ( ! is_array( $order ) || empty( $order['normal'] ) ) { |
| 280 |
return $order; |
| 281 |
} |
| 282 |
|
| 283 |
$ids = array_filter( explode( ',', $order['normal'] ) ); |
| 284 |
$ids = array_values( array_diff( $ids, array( 'easyel_notice_widget' ) ) ); |
| 285 |
array_unshift( $ids, 'easyel_notice_widget' ); |
| 286 |
|
| 287 |
$order['normal'] = implode( ',', $ids ); |
| 288 |
|
| 289 |
return $order; |
| 290 |
} |
| 291 |
|
| 292 |
public function easyel_notice_widget_callback() { |
| 293 |
|
| 294 |
$args = array( 'screen' => 'in-widget' ); |
| 295 |
|
| 296 |
$all_notice = $this->easyel_notice_get_notices( $args ); |
| 297 |
|
| 298 |
if ( empty( $all_notice ) ) { |
| 299 |
echo '<p class="easyel-notice-widget-empty">No stories available right now.</p>'; |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
$today_date = gmdate( 'Y-m-d' ); |
| 304 |
$today_timestamp = strtotime( $today_date ); |
| 305 |
$all_notice = json_decode( $all_notice, true ); |
| 306 |
|
| 307 |
if ( ! is_array( $all_notice ) ) { |
| 308 |
echo '<p class="easyel-notice-widget-empty">No stories available right now.</p>'; |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
echo '<div class="easyel-notice-widget">'; |
| 313 |
|
| 314 |
foreach ( $all_notice as $notice ) { |
| 315 |
|
| 316 |
$notice_id = isset( $notice['notice_id'] ) ? $notice['notice_id'] : ''; |
| 317 |
$sub_title = isset( $notice['sub_title'] ) ? $notice['sub_title'] : ''; |
| 318 |
$thumbnail_url = isset( $notice['thumbnail_url'] ) ? $notice['thumbnail_url'] : ''; |
| 319 |
$thumbnail_link = isset( $notice['thumbnail_link'] ) ? $notice['thumbnail_link'] : ''; |
| 320 |
$content = isset( $notice['content'] ) ? $notice['content'] : ''; |
| 321 |
$action_buttons = isset( $notice['action_buttons'] ) ? $notice['action_buttons'] : array(); |
| 322 |
$expire_timestamp = isset( $notice['expire_date'] ) ? strtotime( $notice['expire_date'] ) : ''; |
| 323 |
|
| 324 |
if ( empty( $sub_title ) ) { |
| 325 |
$sub_title = $this->get_active_plugin_display_name(); |
| 326 |
} |
| 327 |
|
| 328 |
$this->expire_notice_by_date( $notice_id, $expire_timestamp ); |
| 329 |
|
| 330 |
$notice_ignore_status = $this->get_notice_status( $notice_id ); |
| 331 |
|
| 332 |
if ( $notice_ignore_status !== 'true' && $today_timestamp <= $expire_timestamp ) : |
| 333 |
?> |
| 334 |
<div class="easyel-notice-widget-card"> |
| 335 |
|
| 336 |
<?php if ( ! empty( $sub_title ) ) : ?> |
| 337 |
<div class="easyel-notice-widget-eyebrow"><?php echo esc_html( $sub_title ); ?></div> |
| 338 |
<?php endif; ?> |
| 339 |
|
| 340 |
<?php if ( ! empty( $thumbnail_url ) ) : ?> |
| 341 |
<div class="easyel-notice-widget-thumb<?php echo ! empty( $thumbnail_link ) ? ' has-link' : ''; ?>"> |
| 342 |
<?php if ( ! empty( $thumbnail_link ) ) : ?> |
| 343 |
<a href="<?php echo esc_url( $thumbnail_link ); ?>" class="easyel-notice-widget-thumb-link" target="_blank" rel="noopener noreferrer"> |
| 344 |
<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt=""> |
| 345 |
<span class="easyel-notice-widget-thumb-overlay"> |
| 346 |
<span class="easyel-notice-widget-thumb-overlay-text"> |
| 347 |
<span class="dashicons dashicons-external" aria-hidden="true"></span> |
| 348 |
</span> |
| 349 |
</span> |
| 350 |
</a> |
| 351 |
<?php else : ?> |
| 352 |
<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt=""> |
| 353 |
<?php endif; ?> |
| 354 |
</div> |
| 355 |
<?php endif; ?> |
| 356 |
|
| 357 |
<?php if ( ! empty( $content ) ) : ?> |
| 358 |
<div class="easyel-notice-widget-content"> |
| 359 |
<?php echo wp_kses_post( $content ); ?> |
| 360 |
</div> |
| 361 |
<?php endif; ?> |
| 362 |
|
| 363 |
<?php if ( ! empty( $action_buttons ) ) : ?> |
| 364 |
<div class="easyel-notice-widget-actions"> |
| 365 |
<?php foreach ( $action_buttons as $button ) : |
| 366 |
$action_url = isset( $button['url'] ) ? $button['url'] : ''; |
| 367 |
$action_title = isset( $button['title'] ) ? $button['title'] : ''; |
| 368 |
if ( empty( $action_url ) ) { |
| 369 |
continue; |
| 370 |
} |
| 371 |
?> |
| 372 |
<a href="<?php echo esc_url( $action_url ); ?>" class="easyel-notice-widget-action" target="_blank" rel="noopener noreferrer"> |
| 373 |
<span class="easyel-notice-widget-action-text"><?php echo esc_html( $action_title ); ?></span> |
| 374 |
<span aria-hidden="true" class="dashicons dashicons-external"></span> |
| 375 |
</a> |
| 376 |
<?php endforeach; ?> |
| 377 |
</div> |
| 378 |
<?php endif; ?> |
| 379 |
|
| 380 |
</div> |
| 381 |
<?php |
| 382 |
endif; |
| 383 |
} |
| 384 |
|
| 385 |
echo '</div>'; |
| 386 |
} |
| 387 |
} |
| 388 |
|