wizard
1 year ago
class-dashboard-controller.php
1 year ago
class-newsletter-signup-controller.php
1 year ago
class-notice-controller.php
1 year ago
class-settings-controller.php
1 year ago
class-troubleshooting-controller.php
1 year ago
class-notice-controller.php
203 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Admin\Controllers; |
| 4 | |
| 5 | use SuperbAddons\Admin\Controllers\Wizard\WizardController; |
| 6 | |
| 7 | defined('ABSPATH') || exit(); |
| 8 | |
| 9 | // 1.0 |
| 10 | |
| 11 | class AdminNoticeController |
| 12 | { |
| 13 | const PREFIX = 'spbaddons_notice_'; |
| 14 | const PREFIX_DELAY = 'spbaddons_notice_delay_'; |
| 15 | |
| 16 | const ALLOWED_HTML = [ |
| 17 | 'div' => [ |
| 18 | 'class' => [], |
| 19 | 'style' => [], |
| 20 | ], |
| 21 | 'p' => [ |
| 22 | 'class' => [], |
| 23 | ], |
| 24 | 'h2' => [ |
| 25 | 'class' => [], |
| 26 | ], |
| 27 | 'ul' => [ |
| 28 | 'class' => [], |
| 29 | ], |
| 30 | 'li' => [ |
| 31 | 'class' => [], |
| 32 | ], |
| 33 | 'span' => [ |
| 34 | 'class' => [], |
| 35 | ], |
| 36 | 'a' => [ |
| 37 | 'class' => [], |
| 38 | 'href' => [], |
| 39 | 'rel' => [], |
| 40 | 'target' => [], |
| 41 | ], |
| 42 | 'em' => [ |
| 43 | 'class' => [], |
| 44 | ], |
| 45 | 'strong' => [ |
| 46 | 'class' => [], |
| 47 | ], |
| 48 | 'img' => [ |
| 49 | 'class' => [], |
| 50 | 'alt' => [], |
| 51 | 'src' => [], |
| 52 | 'width' => [], |
| 53 | 'height' => [], |
| 54 | ], |
| 55 | 'br' => [], |
| 56 | 'style' => [], |
| 57 | ]; |
| 58 | |
| 59 | private static $notices = []; |
| 60 | |
| 61 | public static function init($options) |
| 62 | { |
| 63 | $notices = []; |
| 64 | if (isset($options['notices']) && is_array($options['notices'])) { |
| 65 | foreach ($options['notices'] as $notice) { |
| 66 | if (!isset($notice['unique_id']) || !isset($notice['content'])) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | $notices[] = $notice; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | self::$notices = $notices; |
| 75 | |
| 76 | add_action('admin_notices', array(__CLASS__, 'AdminNotices')); |
| 77 | add_action('wp_ajax_spbtic_dismiss_notice', array(__CLASS__, 'MaybeDismissNotice')); |
| 78 | } |
| 79 | |
| 80 | public static function AdminNotices() |
| 81 | { |
| 82 | foreach (self::$notices as $notice) { |
| 83 | $notice_path = trailingslashit(SUPERBADDONS_PLUGIN_DIR) . 'src/admin/notices/' . $notice['content']; |
| 84 | if (!file_exists($notice_path)) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Check if the notice has been dismissed. |
| 89 | if (get_user_meta(get_current_user_id(), self::PREFIX . $notice['unique_id'], true)) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | // Check if the notice is delayed |
| 94 | if (isset($notice['delay'])) { |
| 95 | $delay_init = get_user_meta(get_current_user_id(), self::PREFIX_DELAY . $notice['unique_id'], true); |
| 96 | if (!$delay_init) { |
| 97 | update_user_meta(get_current_user_id(), self::PREFIX_DELAY . $notice['unique_id'], time()); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | $delay = strtotime($notice['delay'], $delay_init); |
| 102 | if ($delay > time()) { |
| 103 | continue; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | ob_start(); |
| 108 | include_once $notice_path; |
| 109 | $content = ob_get_clean(); |
| 110 | echo wp_kses($content, self::ALLOWED_HTML); |
| 111 | } |
| 112 | |
| 113 | self::PrintScripts(); |
| 114 | } |
| 115 | |
| 116 | public static function PrintScripts() |
| 117 | { |
| 118 | ?> |
| 119 | <script> |
| 120 | window.addEventListener("load", function() { |
| 121 | setTimeout(function() { |
| 122 | var notice_ids = <?php echo json_encode(array_column(self::$notices, 'unique_id')); ?>; |
| 123 | var nonce = "<?php echo esc_attr(wp_create_nonce('spbtic_dismiss_notice')); ?>"; |
| 124 | var ajaxurl = "<?php echo esc_url(admin_url('admin-ajax.php')); ?>"; |
| 125 | |
| 126 | notice_ids.forEach(function(notice) { |
| 127 | var dismissBtn = document.querySelector( |
| 128 | "." + notice + " .notice-dismiss" |
| 129 | ); |
| 130 | |
| 131 | if (!dismissBtn) return; |
| 132 | |
| 133 | // Add an event listener to the dismiss button. |
| 134 | dismissBtn.addEventListener("click", function(event) { |
| 135 | var httpRequest = new XMLHttpRequest(), |
| 136 | postData = ""; |
| 137 | |
| 138 | // Build the data to send in our request. |
| 139 | // Data has to be formatted as a string here. |
| 140 | postData += "id=" + notice; |
| 141 | postData += "&action=spbtic_dismiss_notice"; |
| 142 | postData += "&nonce=" + nonce; |
| 143 | |
| 144 | httpRequest.open("POST", ajaxurl); |
| 145 | httpRequest.setRequestHeader( |
| 146 | "Content-Type", |
| 147 | "application/x-www-form-urlencoded" |
| 148 | ); |
| 149 | httpRequest.send(postData); |
| 150 | }); |
| 151 | }); |
| 152 | }, 0); |
| 153 | }); |
| 154 | </script> |
| 155 | <?php |
| 156 | } |
| 157 | |
| 158 | public static function MaybeDismissNotice() |
| 159 | { |
| 160 | // Sanity check: Early exit if we're not on a spbtic_dismiss_notice action. |
| 161 | if (!isset($_POST['action']) || 'spbtic_dismiss_notice' !== $_POST['action']) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // Sanity check: Early exit if the ID of the notice does not exist. |
| 166 | if (!isset($_POST['id']) || !in_array($_POST['id'], array_column(self::$notices, 'unique_id'))) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // Notice ID exists in array, so we can safely use it. |
| 171 | $notice_id = sanitize_text_field($_POST['id']); |
| 172 | |
| 173 | // Security check: Make sure nonce is OK. check_ajax_referer exits if it fails. |
| 174 | check_ajax_referer('spbtic_dismiss_notice', 'nonce', true); |
| 175 | |
| 176 | // Dismiss the notice. |
| 177 | self::DismissNotice($notice_id); |
| 178 | } |
| 179 | |
| 180 | public static function DismissNotice($notice_id) |
| 181 | { |
| 182 | if ($notice_id == 'wizard_recommender') { |
| 183 | WizardController::RemoveWizardRecommenderTransient(); |
| 184 | } elseif ($notice_id == 'wizard_woocommerce') { |
| 185 | WizardController::RemoveWizardWooCommerceTransient(); |
| 186 | } else { |
| 187 | update_user_meta(get_current_user_id(), self::PREFIX . $notice_id, true); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public static function Cleanup() |
| 192 | { |
| 193 | foreach (self::$notices as $notice) { |
| 194 | delete_metadata('user', 0, self::PREFIX . $notice['unique_id'], false, true); |
| 195 | if (isset($notice['delay'])) { |
| 196 | delete_metadata('user', 0, self::PREFIX_DELAY . $notice['unique_id'], false, true); |
| 197 | } |
| 198 | } |
| 199 | WizardController::RemoveWizardRecommenderTransient(); |
| 200 | WizardController::RemoveWizardWooCommerceTransient(); |
| 201 | } |
| 202 | } |
| 203 |