close-icon.svg
3 weeks ago
usage-tracking-notice.css
3 weeks ago
usage-tracking-notice.php
3 weeks ago
usage-tracking-notice.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | defined('ABSPATH') or die('No script kiddies please!'); |
| 4 | |
| 5 | /** @var \AmeliaVendor\Melograno\UsageTracker\Collectors\ConsentNoticeCollectorInterface $usageTrackingCollector */ |
| 6 | $usageTrackingCollector = $usageTrackingCollector ?? null; |
| 7 | $usageTrackingAjaxPrefix = $usageTrackingCollector !== null |
| 8 | ? $usageTrackingCollector->getConsentNoticeAjaxPrefix() |
| 9 | : 'amelia'; |
| 10 | $usageTrackingConsentNonce = wp_create_nonce($usageTrackingAjaxPrefix . '_usage_tracking_consent'); |
| 11 | ?> |
| 12 | <div id="melograno-usage-tracking-notice" class="melograno-usage-tracking-notice melograno-usage-tracking-notice--pending"> |
| 13 | <div class="am-usage-tracking-banner" role="region" aria-label="<?php esc_attr_e('Usage tracking notice', 'wpamelia'); ?>"> |
| 14 | <div class="am-usage-tracking-banner__bar" aria-hidden="true"></div> |
| 15 | |
| 16 | <div class="am-usage-tracking-banner__inner"> |
| 17 | <div class="am-usage-tracking-banner__brand"> |
| 18 | <img |
| 19 | src="<?php echo esc_url(AMELIA_URL . 'public/img/amelia-logo-admin-icon.svg'); ?>" |
| 20 | alt="<?php esc_attr_e('Amelia', 'wpamelia'); ?>" |
| 21 | width="32" |
| 22 | height="32" |
| 23 | /> |
| 24 | </div> |
| 25 | |
| 26 | <div class="am-usage-tracking-banner__content"> |
| 27 | <p class="am-usage-tracking-banner__title"> |
| 28 | <?php echo esc_html(\AmeliaBooking\Infrastructure\WP\Translations\BackendStrings::get('improve_amelia')); ?> |
| 29 | </p> |
| 30 | <p class="am-usage-tracking-banner__description"> |
| 31 | <?php echo esc_html(\AmeliaBooking\Infrastructure\WP\Translations\BackendStrings::get('usage_tracking_description')); ?> |
| 32 | </p> |
| 33 | <div class="am-usage-tracking-banner__actions"> |
| 34 | <button type="button" class="am-usage-tracking-banner__enable"> |
| 35 | <?php echo esc_html(\AmeliaBooking\Infrastructure\WP\Translations\BackendStrings::get('improve_plugin')); ?> |
| 36 | </button> |
| 37 | <a |
| 38 | class="am-usage-tracking-banner__learn-more" |
| 39 | href="https://wpamelia.com/usage-data-privacy/" |
| 40 | target="_blank" |
| 41 | rel="noopener noreferrer" |
| 42 | > |
| 43 | <?php echo esc_html(\AmeliaBooking\Infrastructure\WP\Translations\BackendStrings::get('learn_more')); ?> |
| 44 | </a> |
| 45 | </div> |
| 46 | </div> |
| 47 | |
| 48 | <button type="button" class="am-usage-tracking-banner__dismiss" aria-label="<?php esc_attr_e('Dismiss this notice.', 'wpamelia'); ?>"> |
| 49 | <span aria-hidden="true">×</span> |
| 50 | </button> |
| 51 | </div> |
| 52 | </div> |
| 53 | </div> |
| 54 | <script> |
| 55 | (function ($) { |
| 56 | function findMountTarget() { |
| 57 | return document.getElementById('amelia-redesign-page'); |
| 58 | } |
| 59 | |
| 60 | function showNotice(root) { |
| 61 | var target = findMountTarget(); |
| 62 | |
| 63 | if (!target) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | root.classList.remove('melograno-usage-tracking-notice--pending'); |
| 68 | |
| 69 | if (root.parentNode !== target) { |
| 70 | target.insertBefore(root, target.firstChild); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | function mountNotice() { |
| 75 | var SPA_READY_TIMEOUT_MS = 10000; |
| 76 | var root = document.getElementById('melograno-usage-tracking-notice'); |
| 77 | |
| 78 | if (!root) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | var redesignPage = document.getElementById('amelia-redesign-page'); |
| 83 | |
| 84 | if (!redesignPage) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | var revealed = false; |
| 89 | var observers = []; |
| 90 | var timeoutId; |
| 91 | |
| 92 | function stopWatching() { |
| 93 | observers.forEach(function (observer) { |
| 94 | observer.disconnect(); |
| 95 | }); |
| 96 | observers = []; |
| 97 | |
| 98 | if (timeoutId) { |
| 99 | window.clearTimeout(timeoutId); |
| 100 | timeoutId = null; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function revealWhenSpaReady() { |
| 105 | if (revealed) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | revealed = true; |
| 110 | stopWatching(); |
| 111 | showNotice(root); |
| 112 | } |
| 113 | |
| 114 | function watchAppRoot(appRoot) { |
| 115 | if (appRoot.children.length > 0) { |
| 116 | revealWhenSpaReady(); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | var appObserver = new MutationObserver(function () { |
| 121 | if (appRoot.children.length > 0) { |
| 122 | revealWhenSpaReady(); |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | appObserver.observe(appRoot, { childList: true }); |
| 127 | observers.push(appObserver); |
| 128 | } |
| 129 | |
| 130 | var appRoot = redesignPage.querySelector('#amelia-redesign'); |
| 131 | |
| 132 | if (!appRoot) { |
| 133 | if (redesignPage.children.length > 0) { |
| 134 | revealWhenSpaReady(); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | var pageObserver = new MutationObserver(function () { |
| 139 | var lateAppRoot = redesignPage.querySelector('#amelia-redesign'); |
| 140 | |
| 141 | if (!lateAppRoot) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | pageObserver.disconnect(); |
| 146 | observers = observers.filter(function (observer) { |
| 147 | return observer !== pageObserver; |
| 148 | }); |
| 149 | watchAppRoot(lateAppRoot); |
| 150 | }); |
| 151 | |
| 152 | pageObserver.observe(redesignPage, { childList: true, subtree: true }); |
| 153 | observers.push(pageObserver); |
| 154 | } else { |
| 155 | watchAppRoot(appRoot); |
| 156 | |
| 157 | if (revealed) { |
| 158 | return; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | timeoutId = window.setTimeout(revealWhenSpaReady, SPA_READY_TIMEOUT_MS); |
| 163 | } |
| 164 | |
| 165 | $(function () { |
| 166 | mountNotice(); |
| 167 | |
| 168 | var $root = $('#melograno-usage-tracking-notice'); |
| 169 | var $banner = $root.find('.am-usage-tracking-banner'); |
| 170 | |
| 171 | $banner.find('.am-usage-tracking-banner__dismiss').on('click', function (e) { |
| 172 | e.preventDefault(); |
| 173 | |
| 174 | $.post(ajaxurl, { |
| 175 | action: '<?php echo esc_js($usageTrackingAjaxPrefix); ?>_dismiss_usage_tracking_notice', |
| 176 | _ajax_nonce: '<?php echo esc_js($usageTrackingConsentNonce); ?>' |
| 177 | }).done(function () { |
| 178 | $root.slideUp('fast'); |
| 179 | }); |
| 180 | }); |
| 181 | |
| 182 | $banner.find('.am-usage-tracking-banner__enable').on('click', function (e) { |
| 183 | e.preventDefault(); |
| 184 | |
| 185 | $.post(ajaxurl, { |
| 186 | action: '<?php echo esc_js($usageTrackingAjaxPrefix); ?>_enable_usage_tracking', |
| 187 | _ajax_nonce: '<?php echo esc_js($usageTrackingConsentNonce); ?>' |
| 188 | }).done(function () { |
| 189 | $root.slideUp('fast'); |
| 190 | }); |
| 191 | }); |
| 192 | }); |
| 193 | })(jQuery); |
| 194 | </script> |
| 195 |