| 1 |
/** |
| 2 |
* Adminify UI - SSL gate. |
| 3 |
* |
| 4 |
* The Adminify UI dashboard frame only loads over HTTPS (see Inc/Admin/Admin.php). |
| 5 |
* So before letting the user enable the "Adminify UI" switcher we run an AJAX |
| 6 |
* check against the server. If HTTPS is not properly set up we revert the |
| 7 |
* toggle and show a warning notice right below the switcher. |
| 8 |
* |
| 9 |
* Plain jQuery, no build step. Enqueued only on the Adminify settings screen. |
| 10 |
*/ |
| 11 |
(function ($) { |
| 12 |
'use strict'; |
| 13 |
|
| 14 |
if (typeof PXLBSADMINIFY_SSL === 'undefined') { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
var cfg = PXLBSADMINIFY_SSL; |
| 19 |
|
| 20 |
$(function () { |
| 21 |
var $input = $('input[data-depend-id="' + cfg.field_id + '"]').first(); |
| 22 |
if (!$input.length) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
var $switcher = $input.closest('.adminify--switcher'); |
| 27 |
if (!$switcher.length) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
// If it was already enabled on page load, HTTPS was already accepted — |
| 32 |
// don't gate toggling it off and back on again. |
| 33 |
var verified = $switcher.hasClass('adminify--active'); |
| 34 |
var checking = false; |
| 35 |
|
| 36 |
// The whole settings row for this field. We drop the notice AFTER this |
| 37 |
// row (not inside the switcher column) so it can't overlap the toggle. |
| 38 |
function fieldRow() { |
| 39 |
var $row = $switcher.closest('.adminify-field'); |
| 40 |
return $row.length ? $row : $switcher.closest('.adminify-fieldset'); |
| 41 |
} |
| 42 |
|
| 43 |
function clearNotice() { |
| 44 |
fieldRow().next('.adminify-ui-ssl-warning').remove(); |
| 45 |
} |
| 46 |
|
| 47 |
function showNotice(message) { |
| 48 |
clearNotice(); |
| 49 |
// Inline styles keep the notice looking right even outside the |
| 50 |
// framework's .adminify-field-notice wrapper (which scopes its CSS). |
| 51 |
$('<div/>', { |
| 52 |
'class': 'adminify-notice adminify-notice-warning adminify-ui-ssl-warning', |
| 53 |
html: '<strong>' + cfg.i18n.label + '</strong> ' + message |
| 54 |
}).css({ |
| 55 |
'clear': 'both', |
| 56 |
'width': '100%', |
| 57 |
'box-sizing': 'border-box', |
| 58 |
'margin': '0', |
| 59 |
'padding': '10px 14px', |
| 60 |
'background-color': '#fff8e5', |
| 61 |
'border-left': '4px solid #ffbc00', |
| 62 |
'color': '#693f00', |
| 63 |
'font-size': '13px', |
| 64 |
'line-height': '1.5' |
| 65 |
}).insertAfter(fieldRow()); |
| 66 |
} |
| 67 |
|
| 68 |
function setBusy(busy) { |
| 69 |
checking = busy; |
| 70 |
$switcher.css('opacity', busy ? '0.6' : ''); |
| 71 |
$switcher.css('pointer-events', busy ? 'none' : ''); |
| 72 |
} |
| 73 |
|
| 74 |
// Programmatically flip the switcher ON the same way the framework does, |
| 75 |
// so dependency fields (templates, colors) react correctly. |
| 76 |
function enable() { |
| 77 |
$switcher.addClass('adminify--active'); |
| 78 |
$input.val(1).trigger('change'); |
| 79 |
} |
| 80 |
|
| 81 |
function runCheck() { |
| 82 |
if (checking) { |
| 83 |
return; |
| 84 |
} |
| 85 |
setBusy(true); |
| 86 |
|
| 87 |
$.post(cfg.ajax_url, { |
| 88 |
action: 'pxlbsadminify_ssl_check', |
| 89 |
nonce: cfg.nonce |
| 90 |
}).done(function (res) { |
| 91 |
if (res && res.success && res.data && res.data.https_ready) { |
| 92 |
verified = true; |
| 93 |
clearNotice(); |
| 94 |
enable(); |
| 95 |
} else { |
| 96 |
var msg = (res && res.data && res.data.message) ? res.data.message : cfg.i18n.generic_error; |
| 97 |
showNotice(msg); |
| 98 |
} |
| 99 |
}).fail(function () { |
| 100 |
showNotice(cfg.i18n.generic_error); |
| 101 |
}).always(function () { |
| 102 |
setBusy(false); |
| 103 |
}); |
| 104 |
} |
| 105 |
|
| 106 |
// Capture phase, native listener: runs BEFORE the framework's bubble-phase |
| 107 |
// jQuery click handler. Lets us block the OFF -> ON toggle until verified, |
| 108 |
// so the UI never flickers into the enabled state on an unverified site. |
| 109 |
$switcher[0].addEventListener('click', function (e) { |
| 110 |
// Turning OFF, or already verified: let the framework handle it. |
| 111 |
if ($switcher.hasClass('adminify--active') || verified) { |
| 112 |
clearNotice(); |
| 113 |
return; |
| 114 |
} |
| 115 |
// Turning ON, not verified yet: stop the framework toggle, check first. |
| 116 |
e.preventDefault(); |
| 117 |
e.stopImmediatePropagation(); |
| 118 |
runCheck(); |
| 119 |
}, true); |
| 120 |
}); |
| 121 |
})(jQuery); |
| 122 |
|