-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-link-confirmation.php
More file actions
89 lines (73 loc) · 2.89 KB
/
class-link-confirmation.php
File metadata and controls
89 lines (73 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Link Confirmation
* Shows confirmation dialog for links pointing to production site
*/
if (!defined('ABSPATH')) {
exit;
}
class Sole_Dev_Link_Confirmation {
private $production_url;
public function __construct() {
// Only initialize if proxy URL is defined
if (!defined('SOLE_DEV_PROXY_URL') || !SOLE_DEV_PROXY_URL) {
return;
}
$this->production_url = SOLE_DEV_PROXY_URL;
$this->init();
}
private function init() {
// Add JavaScript to admin footer
add_action('admin_footer', [$this, 'add_script']);
// Add JavaScript to frontend footer
add_action('wp_footer', [$this, 'add_script']);
}
public function add_script() {
$production_url = esc_js($this->production_url);
$production_domain = esc_js(parse_url($this->production_url, PHP_URL_HOST));
?>
<script>
(function() {
// Get production domain for comparison
const productionDomain = '<?php echo $production_domain; ?>';
const productionUrl = '<?php echo $production_url; ?>';
const currentDomain = window.location.hostname;
// Only run if we're not already on production
if (currentDomain === productionDomain) {
return;
}
// Function to check if a URL points to production
function isProductionUrl(url) {
if (!url) return false;
try {
const linkUrl = new URL(url, window.location.origin);
return linkUrl.hostname === productionDomain;
} catch (e) {
// If URL parsing fails, check string match
return url.includes(productionDomain) || url.startsWith(productionUrl);
}
}
// Add click handler to all links
document.addEventListener('click', function(e) {
const link = e.target.closest('a');
if (!link || !link.href) {
return;
}
// Check if link points to production
if (isProductionUrl(link.href)) {
const confirmed = confirm(
'⚠️ WARNING: This link points to the PRODUCTION site (' + productionDomain + ').\n\n' +
'Are you sure you want to leave the development environment?'
);
if (!confirmed) {
e.preventDefault();
e.stopPropagation();
return false;
}
}
}, true); // Use capture phase to catch early
})();
</script>
<?php
}
}