| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\UI; |
| 4 |
|
| 5 |
use ImportWP\Common\AddonAPI\AddonManager; |
| 6 |
|
| 7 |
class AdminNotices |
| 8 |
{ |
| 9 |
|
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
add_action('admin_init', [__CLASS__, 'dismiss_notices'], 20); |
| 13 |
add_action('admin_notices', [__CLASS__, 'display_notices']); |
| 14 |
} |
| 15 |
|
| 16 |
public static function dismiss_notices() |
| 17 |
{ |
| 18 |
|
| 19 |
if (isset($_GET['iwp-hide-notice']) && isset($_GET['_iwp_notice_nonce'])) { |
| 20 |
|
| 21 |
if (! wp_verify_nonce(sanitize_key(wp_unslash($_GET['_iwp_notice_nonce'])), 'importwp_hide_notices_nonce')) { |
| 22 |
wp_die(esc_html__('Action failed. Please refresh the page and retry.', 'importwp')); |
| 23 |
} |
| 24 |
|
| 25 |
$notice_name = sanitize_text_field(wp_unslash($_GET['iwp-hide-notice'])); |
| 26 |
|
| 27 |
update_user_meta(get_current_user_id(), 'dismissed_' . $notice_name . '_notice', true); |
| 28 |
|
| 29 |
wp_safe_redirect(remove_query_arg(['iwp-hide-notice', '_iwp_notice_nonce'])); |
| 30 |
exit; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// dismiss notices |
| 35 |
public static function display_notices() |
| 36 |
{ |
| 37 |
global $pagenow; |
| 38 |
$screen = get_current_screen(); |
| 39 |
|
| 40 |
if ($screen->id != 'tools_page_importwp' && !in_array($pagenow, ['index.php', 'plugins.php'])) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$addons = AddonManager::instance()->getAddonList(); |
| 45 |
foreach ($addons as $addon) { |
| 46 |
|
| 47 |
$notice_name = 'iwp_available_addon_' . $addon['slug']; |
| 48 |
|
| 49 |
if (get_user_meta(get_current_user_id(), 'dismissed_' . $notice_name . '_notice', true)) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
$class = 'notice notice-warning is-dismissible'; |
| 54 |
$text = sprintf('We have noticed that you are using Import WP with %1$s. To ensure compatibility, we recommend you use', $addon['name']); |
| 55 |
$plugin_name = sprintf('Import WP %1$s Addon', $addon['name']); |
| 56 |
|
| 57 |
printf('<div class="%1$s"> |
| 58 |
<a href="%5$s" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></a> |
| 59 |
<p>%2$s <a href="%4$s" target="_blank">%3$s</a>.</p> |
| 60 |
</div>', esc_attr($class), esc_html($text), esc_html($plugin_name), esc_url($addon['url']), esc_url(wp_nonce_url(add_query_arg('iwp-hide-notice', $notice_name), 'importwp_hide_notices_nonce', '_iwp_notice_nonce'))); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|