| 1 |
<?php |
| 2 |
namespace WPEXtra\Modules\Common; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use WPEXtra\Settings; |
| 9 |
use WPEXtra\Helper; |
| 10 |
use WPEXtra\Base; |
| 11 |
|
| 12 |
class Permission extends Base { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
parent::__construct(); |
| 16 |
add_action('admin_bar_menu', [$this, 'admin_bar_extra'], 150); |
| 17 |
} |
| 18 |
|
| 19 |
public function admin_bar_extra($meta = true) { |
| 20 |
global $wp_admin_bar; |
| 21 |
if (!is_user_logged_in() || !is_super_admin() || !is_admin_bar_showing()) { |
| 22 |
return; |
| 23 |
} |
| 24 |
$wp_admin_bar->add_menu([ |
| 25 |
'id' => 'wp-extra', |
| 26 |
'title' => __('WP EXtra', 'wp-extra'), |
| 27 |
'href' => admin_url('admin.php?page=wp-extra'), |
| 28 |
]); |
| 29 |
} |
| 30 |
|
| 31 |
protected $features = [ |
| 32 |
'wp_toolbar', |
| 33 |
'admin_site_link', |
| 34 |
'wp_adminbar', |
| 35 |
'wp_adminbar_auto', |
| 36 |
'adminmenu_list', |
| 37 |
'adminplugin_list', |
| 38 |
'scrolltotop', |
| 39 |
'registration_date', |
| 40 |
'last_login', |
| 41 |
'application_passwords', |
| 42 |
'profile_pw', |
| 43 |
'profile_email', |
| 44 |
'no_backend', |
| 45 |
'adminmenu_extra', |
| 46 |
]; |
| 47 |
|
| 48 |
public function scrolltotop() { |
| 49 |
if (is_admin()) { |
| 50 |
add_action('admin_footer', [$this, 'render_scrolltotop']); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public function render_scrolltotop() { |
| 55 |
$screen = get_current_screen(); |
| 56 |
if ($screen && !empty($screen->is_block_editor)) { |
| 57 |
return; |
| 58 |
} |
| 59 |
?> |
| 60 |
<style> |
| 61 |
#wpex-backtotop { |
| 62 |
position: fixed; |
| 63 |
right: 20px; |
| 64 |
bottom: 20px; |
| 65 |
z-index: 99999; |
| 66 |
width: 40px; |
| 67 |
height: 40px; |
| 68 |
display: none; |
| 69 |
align-items: center; |
| 70 |
justify-content: center; |
| 71 |
background: var(--wp-admin-theme-color, #2271b1); |
| 72 |
color: #ffffff; |
| 73 |
border-radius: 4px; |
| 74 |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.16); |
| 75 |
text-decoration: none; |
| 76 |
cursor: pointer; |
| 77 |
transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.2s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.2s ease; |
| 78 |
outline: none; |
| 79 |
border: none; |
| 80 |
padding: 0; |
| 81 |
margin: 0; |
| 82 |
box-sizing: border-box; |
| 83 |
} |
| 84 |
.rtl #wpex-backtotop { |
| 85 |
right: auto; |
| 86 |
left: 20px; |
| 87 |
} |
| 88 |
#wpex-backtotop:hover { |
| 89 |
background: var(--wp-admin-theme-color-darker-10, #135e96); |
| 90 |
color: #ffffff; |
| 91 |
transform: translateY(-2px); |
| 92 |
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.22); |
| 93 |
} |
| 94 |
#wpex-backtotop:active { |
| 95 |
transform: translateY(0); |
| 96 |
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18); |
| 97 |
} |
| 98 |
#wpex-backtotop svg { |
| 99 |
width: 20px; |
| 100 |
height: 20px; |
| 101 |
display: block; |
| 102 |
fill: currentColor; |
| 103 |
} |
| 104 |
</style> |
| 105 |
<a id="wpex-backtotop" href="#" title="<?php esc_attr_e('Scroll to top', 'wp-extra'); ?>" aria-label="<?php esc_attr_e('Scroll to top', 'wp-extra'); ?>"> |
| 106 |
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"> |
| 107 |
<path d="M12 4.5l-7 7 1.4 1.4 4.6-4.6V19.5h2V8.3l4.6 4.6 1.4-1.4-7-7z"/> |
| 108 |
</svg> |
| 109 |
</a> |
| 110 |
<script> |
| 111 |
jQuery(function($) { |
| 112 |
var $btn = $('#wpex-backtotop'); |
| 113 |
$(window).on('scroll', function() { |
| 114 |
if ($(this).scrollTop() > 180) { |
| 115 |
if (!$btn.is(':visible')) { |
| 116 |
$btn.css('display', 'flex').hide().fadeIn(200); |
| 117 |
} |
| 118 |
} else { |
| 119 |
if ($btn.is(':visible')) { |
| 120 |
$btn.fadeOut(200); |
| 121 |
} |
| 122 |
} |
| 123 |
}); |
| 124 |
$btn.on('click', function(e) { |
| 125 |
e.preventDefault(); |
| 126 |
$('html, body').animate({ scrollTop: 0 }, 250); |
| 127 |
}); |
| 128 |
}); |
| 129 |
</script> |
| 130 |
<?php |
| 131 |
} |
| 132 |
|
| 133 |
public function registration_date() { |
| 134 |
add_filter('manage_users_columns', [$this, 'registration_date_columns']); |
| 135 |
add_filter('manage_users_custom_column', [$this, 'registration_date_custom_column'], 10, 3); |
| 136 |
add_filter('manage_users_sortable_columns', [$this, 'registration_date_sortable_columns']); |
| 137 |
} |
| 138 |
|
| 139 |
public function registration_date_columns($columns) { |
| 140 |
$columns['registration_date'] = __('User Registration Date', 'wp-extra'); |
| 141 |
return $columns; |
| 142 |
} |
| 143 |
|
| 144 |
public function registration_date_custom_column($row_output, $column_id_attr, $user_id) { |
| 145 |
if ($column_id_attr === 'registration_date') { |
| 146 |
$registered = get_the_author_meta('registered', $user_id); |
| 147 |
return $registered ? date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($registered)) : ''; |
| 148 |
} |
| 149 |
return $row_output; |
| 150 |
} |
| 151 |
|
| 152 |
public function registration_date_sortable_columns($columns) { |
| 153 |
return wp_parse_args(['registration_date' => 'registered'], $columns); |
| 154 |
} |
| 155 |
|
| 156 |
public function last_login() { |
| 157 |
add_filter('manage_users_columns', [$this, 'last_login_columns']); |
| 158 |
add_filter('manage_users_custom_column', [$this, 'last_login_custom_column'], 10, 3); |
| 159 |
add_filter('manage_users_sortable_columns', [$this, 'last_login_sortable_columns']); |
| 160 |
add_action('wp_login', [$this, 'update_last_login'], 10, 2); |
| 161 |
} |
| 162 |
|
| 163 |
public function last_login_columns($columns) { |
| 164 |
$columns['last_login'] = __('Last Login', 'wp-extra'); |
| 165 |
return $columns; |
| 166 |
} |
| 167 |
|
| 168 |
public function last_login_custom_column($row_output, $column_id_attr, $user_id) { |
| 169 |
if ($column_id_attr === 'last_login') { |
| 170 |
$last_login = get_user_meta($user_id, 'last_login', true); |
| 171 |
if (!empty($last_login)) { |
| 172 |
return sprintf(__('%s ago', 'wp-extra'), human_time_diff(strtotime($last_login), current_time('timestamp'))); |
| 173 |
} |
| 174 |
return __('Never', 'wp-extra'); |
| 175 |
} |
| 176 |
return $row_output; |
| 177 |
} |
| 178 |
|
| 179 |
public function update_last_login($login, $user = null) { |
| 180 |
if (!$user) { |
| 181 |
$user = get_user_by('login', $login); |
| 182 |
} |
| 183 |
if ($user) { |
| 184 |
update_user_meta($user->ID, 'last_login', current_time('mysql')); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
public function last_login_sortable_columns($columns) { |
| 189 |
return wp_parse_args(['last_login' => 'lastlogin'], $columns); |
| 190 |
} |
| 191 |
|
| 192 |
public function wp_toolbar() { |
| 193 |
add_action('admin_bar_menu', [$this, 'remove_nodes'], 999); |
| 194 |
} |
| 195 |
|
| 196 |
public function admin_site_link() { |
| 197 |
add_action('admin_bar_menu', [$this, 'custom_site_name_link'], 99); |
| 198 |
} |
| 199 |
|
| 200 |
public function custom_site_name_link($wp_admin_bar) { |
| 201 |
if (Helper::get_option('admin_site_link')) { |
| 202 |
$node = $wp_admin_bar->get_node('site-name'); |
| 203 |
if ($node) { |
| 204 |
$node->href = admin_url(); |
| 205 |
$wp_admin_bar->add_node((array) $node); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
public function remove_nodes($wp_admin_bar) { |
| 211 |
$toolbar_nodes = Helper::get_option('wp_toolbar'); |
| 212 |
if (is_array($toolbar_nodes)) { |
| 213 |
foreach ($toolbar_nodes as $menu_id) { |
| 214 |
$wp_admin_bar->remove_node($menu_id); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public function wp_adminbar() { |
| 220 |
add_filter('show_admin_bar', [$this, 'filter_show_admin_bar'], 99); |
| 221 |
} |
| 222 |
|
| 223 |
public function filter_show_admin_bar($show) { |
| 224 |
if (is_admin()) { |
| 225 |
return $show; |
| 226 |
} |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
public function wp_adminbar_auto() { |
| 231 |
add_action('admin_enqueue_scripts', [$this, 'adminbar_auto_css']); |
| 232 |
add_action('wp_enqueue_scripts', [$this, 'adminbar_auto_css']); |
| 233 |
} |
| 234 |
|
| 235 |
public function adminbar_auto_css() { |
| 236 |
if (!is_admin_bar_showing()) { |
| 237 |
return; |
| 238 |
} |
| 239 |
$css = '@media (min-width: 783px) { |
| 240 |
html.wp-toolbar { padding-top: 0 !important; } |
| 241 |
html { margin-top: 0 !important; } |
| 242 |
body.admin-bar { margin-top: 0 !important; } |
| 243 |
#wpadminbar { |
| 244 |
top: 0 !important; |
| 245 |
transform: translateY(-100%); |
| 246 |
opacity: 0; |
| 247 |
pointer-events: none; |
| 248 |
transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1); |
| 249 |
z-index: 999999 !important; |
| 250 |
} |
| 251 |
#wpadminbar::after { |
| 252 |
content: ""; |
| 253 |
position: absolute; |
| 254 |
top: 100%; |
| 255 |
left: 0; |
| 256 |
right: 0; |
| 257 |
height: 12px; |
| 258 |
pointer-events: auto; |
| 259 |
} |
| 260 |
#wpadminbar:hover, |
| 261 |
#wpadminbar:focus-within { |
| 262 |
transform: translateY(0); |
| 263 |
opacity: 1; |
| 264 |
pointer-events: auto; |
| 265 |
} |
| 266 |
}'; |
| 267 |
wp_add_inline_style('admin-bar', Helper::minifyCSS($css)); |
| 268 |
} |
| 269 |
|
| 270 |
public function adminmenu_list() { |
| 271 |
add_action('admin_menu', [$this, 'admin_menu_roles'], 9999); |
| 272 |
} |
| 273 |
|
| 274 |
public function admin_menu_roles() { |
| 275 |
if (!is_admin()) { |
| 276 |
return; |
| 277 |
} |
| 278 |
$admin_menus = (array) Helper::get_option('adminmenu_list', []); |
| 279 |
if (isset($_GET['page']) && $_GET['page'] === 'wp-extra') { |
| 280 |
return; |
| 281 |
} |
| 282 |
foreach ($admin_menus as $menu_page) { |
| 283 |
remove_menu_page($menu_page); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
public function adminplugin_list() { |
| 288 |
add_filter('all_plugins', [$this, 'filter_hidden_plugins']); |
| 289 |
} |
| 290 |
|
| 291 |
public function filter_hidden_plugins($plugins) { |
| 292 |
$hide_plugins = (array) Helper::get_option('adminplugin_list', []); |
| 293 |
if (!empty($hide_plugins) && is_array($plugins)) { |
| 294 |
foreach ($hide_plugins as $plugin_file) { |
| 295 |
if (isset($plugins[$plugin_file])) { |
| 296 |
unset($plugins[$plugin_file]); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
return $plugins; |
| 301 |
} |
| 302 |
|
| 303 |
public function application_passwords() { |
| 304 |
add_filter('wp_is_application_passwords_available', '__return_false'); |
| 305 |
} |
| 306 |
|
| 307 |
public function profile_pw() { |
| 308 |
if (!current_user_can('administrator')) { |
| 309 |
add_filter('show_password_fields', '__return_false'); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
public function profile_email() { |
| 314 |
add_action('personal_options_update', [$this, 'disableEmailChangeBack']); |
| 315 |
add_action('edit_user_profile_update', [$this, 'disableEmailChangeBack']); |
| 316 |
add_action('show_user_profile', [$this, 'disableEmailChangeBackFront']); |
| 317 |
add_action('edit_user_profile', [$this, 'disableEmailChangeBackFront']); |
| 318 |
} |
| 319 |
|
| 320 |
public function disableEmailChangeBack($user_id) { |
| 321 |
if (!current_user_can('administrator')) { |
| 322 |
$user = get_user_by('ID', $user_id); |
| 323 |
if ($user) { |
| 324 |
$_POST['email'] = $user->user_email; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
public function disableEmailChangeBackFront($user) { |
| 330 |
if (!current_user_can('administrator')) { |
| 331 |
?> |
| 332 |
<script> |
| 333 |
document.addEventListener('DOMContentLoaded', function(){ |
| 334 |
var emailInput = document.getElementById('email'); |
| 335 |
if (emailInput) { |
| 336 |
emailInput.setAttribute('disabled', 'disabled'); |
| 337 |
} |
| 338 |
}); |
| 339 |
</script> |
| 340 |
<?php |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
public function no_backend() { |
| 345 |
add_action('admin_init', [$this, 'redirect_non_admin_user']); |
| 346 |
} |
| 347 |
|
| 348 |
public function redirect_non_admin_user() { |
| 349 |
if (wp_doing_ajax() || wp_doing_cron() || (defined('REST_REQUEST') && REST_REQUEST)) { |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
if (current_user_can('administrator')) { |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
$user = wp_get_current_user(); |
| 358 |
$blocked_roles = (array) Helper::get_option('no_backend', []); |
| 359 |
|
| 360 |
if (!empty($blocked_roles) && !empty($user->roles) && array_intersect($blocked_roles, (array)$user->roles)) { |
| 361 |
wp_safe_redirect(home_url('/')); |
| 362 |
exit; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
public function adminmenu_extra() { |
| 367 |
add_action('admin_menu', [$this, 'restrict_settings_access'], 999); |
| 368 |
add_action('admin_init', [$this, 'block_settings_page_access']); |
| 369 |
} |
| 370 |
|
| 371 |
public function restrict_settings_access() { |
| 372 |
$super_admins = (array) Helper::get_option('adminmenu_extra', []); |
| 373 |
if (!empty($super_admins)) { |
| 374 |
$current_user_id = (string) get_current_user_id(); |
| 375 |
if (!in_array($current_user_id, array_map('strval', $super_admins), true)) { |
| 376 |
remove_menu_page('wp-extra'); |
| 377 |
remove_submenu_page('wp-extra', 'wp-extra'); |
| 378 |
remove_submenu_page('wp-extra', 'wpex-email-logs'); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
public function block_settings_page_access() { |
| 384 |
$super_admins = (array) Helper::get_option('adminmenu_extra', []); |
| 385 |
if (!empty($super_admins) && isset($_GET['page']) && in_array($_GET['page'], ['wp-extra', 'wpex-email-logs'], true)) { |
| 386 |
$current_user_id = (string) get_current_user_id(); |
| 387 |
if (!in_array($current_user_id, array_map('strval', $super_admins), true)) { |
| 388 |
wp_die(esc_html__('You do not have permission to access WP EXtra settings.', 'wp-extra'), 403); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
} |