Skip to content

Commit c924707

Browse files
committed
Release 1.4.0
Added disable-checkout-and-limit-login.php
1 parent c4185d6 commit c924707

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## Release 1.4.0
3+
* Added disable-checkout-and-limit-login.php
4+
5+
26
## Release 1.3.9
37
* feat(debug): Added wp-redirect-log.php mu-plugin
48

README-snippets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| Title | Type | Status | Description |
1111
| ----- | ---- | ------ | ----------- |
1212
| [Change Admin Email](./admin/change-admin-email.php) | mu-plugin | :white_check_mark: | Changes the admin email address if it hasn't been changed already without triggering email validation. |
13+
| [Disable WooCommerce Checkout & Restrict Logins to Admins](./admin/disable-checkout-and-limit-login.php) | mu-plugin | :white_check_mark: | Disables WooCommerce checkout and blocks login for all users except administrators. |
1314
| [log-admin-notices.php](./admin/log-admin-notices.php) | snippet | :white_check_mark: | Log's admin notices on admin_init, great for finding admin_notices to hide. |
1415
| [mu-dashboard-shortcode.php](./admin/mu-dashboard-shortcode.php) | snippet | :construction: | Unsure what the usage of this snippet is. |
1516

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The status field is used to indicate the current status of the snippet. This is
2424
| Title | Type | Status | Description |
2525
| ----- | ---- | ------ | ----------- |
2626
| [Change Admin Email](./admin/change-admin-email.php) | mu-plugin | :white_check_mark: | Changes the admin email address if it hasn't been changed already without triggering email validation. |
27+
| [Disable WooCommerce Checkout & Restrict Logins to Admins](./admin/disable-checkout-and-limit-login.php) | mu-plugin | :white_check_mark: | Disables WooCommerce checkout and blocks login for all users except administrators. |
2728
| [log-admin-notices.php](./admin/log-admin-notices.php) | snippet | :white_check_mark: | Log's admin notices on admin_init, great for finding admin_notices to hide. |
2829
| [mu-dashboard-shortcode.php](./admin/mu-dashboard-shortcode.php) | snippet | :construction: | Unsure what the usage of this snippet is. |
2930

@@ -138,6 +139,10 @@ The status field is used to indicate the current status of the snippet. This is
138139
| [Failover Status Monitor](./wp-failover/wp-failover.php) | Plugin | :white_check_mark: | Monitors failover status and provides notifications. |
139140

140141
# Changelog
142+
## Release 1.4.0
143+
* Added disable-checkout-and-limit-login.php
144+
145+
141146
## Release 1.3.9
142147
* feat(debug): Added wp-redirect-log.php mu-plugin
143148

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Plugin Name: Disable WooCommerce Checkout & Restrict Logins to Admins
4+
* Description: Disables WooCommerce checkout and blocks login for all users except administrators.
5+
* Type: mu-plugin
6+
* Status: Complete
7+
*/
8+
9+
// Block checkout by redirecting to cart with disabled message
10+
add_action('template_redirect', function () {
11+
if (function_exists('is_checkout') && is_checkout() && !current_user_can('manage_options')) {
12+
// Add notice about disabled checkout
13+
wc_add_notice(__('Checkout is currently disabled due to site maintenance, please try again later.'), 'error');
14+
15+
// Redirect to cart page
16+
wp_redirect(wc_get_cart_url());
17+
exit;
18+
}
19+
});
20+
21+
// Disable cart and checkout endpoints for non-admins
22+
add_filter('woocommerce_cart_redirect_after_error', function ($url) {
23+
if (!current_user_can('manage_options')) {
24+
return wc_get_cart_url();
25+
}
26+
return $url;
27+
});
28+
29+
// Prevent non-admins from logging in
30+
add_action('wp_authenticate_user', function ($user) {
31+
if (!user_can($user, 'administrator')) {
32+
return new WP_Error('access_denied', __('Only administrators can log in at this time due to site maintenance.'));
33+
}
34+
return $user;
35+
}, 10, 1);
36+
37+
// Optional: Show message on login page
38+
add_action('login_message', function () {
39+
echo '<p style="color: red;">Logins are currently restricted to administrators due to site maintenance.</p>';
40+
});
41+
42+
// Add warning to custom login pages (works with custom login forms)
43+
add_action('wp_head', function () {
44+
// Check if we're on a login page (including custom login pages)
45+
if (is_page('login') || strpos($_SERVER['REQUEST_URI'], '/login') !== false || is_page('log-in') || is_page('signin') || is_page('sign-in')) {
46+
echo '<style>
47+
.login-maintenance-warning {
48+
background-color: #ffebee;
49+
border: 1px solid #f44336;
50+
color: #d32f2f;
51+
padding: 15px;
52+
margin: 20px 0;
53+
border-radius: 4px;
54+
text-align: center;
55+
font-weight: bold;
56+
}
57+
</style>';
58+
echo '<script>
59+
document.addEventListener("DOMContentLoaded", function() {
60+
var loginForm = document.querySelector("form[action*=\"login\"], .login-form, #loginform, .wp-login-form, form[name=\"loginform\"]");
61+
if (loginForm) {
62+
var warningDiv = document.createElement("div");
63+
warningDiv.className = "login-maintenance-warning";
64+
warningDiv.innerHTML = "⚠️ Logins are currently restricted to administrators due to site maintenance.";
65+
loginForm.parentNode.insertBefore(warningDiv, loginForm);
66+
}
67+
});
68+
</script>';
69+
}
70+
});
71+
72+
// Handle AJAX login requests (if using AJAX login)
73+
add_action('wp_ajax_nopriv_custom_login', function () {
74+
wp_send_json_error([
75+
'message' => 'Logins are currently restricted to administrators due to site maintenance.'
76+
]);
77+
});
78+
79+
// Handle REST API login attempts
80+
add_action('rest_api_init', function () {
81+
add_filter('rest_authentication_errors', function ($result) {
82+
if (!is_wp_error($result) && !current_user_can('manage_options')) {
83+
return new WP_Error('maintenance_mode', 'Logins are currently restricted to administrators due to site maintenance.', ['status' => 503]);
84+
}
85+
return $result;
86+
});
87+
});

0 commit comments

Comments
 (0)