Files
2023-04-10 12:42:23 +03:00

197 lines
5.3 KiB
PHP

<?php
/**
* wlsrm controller.
*
* @author Ivan Filipov <ivan@web-lab.solutions>
*
* @version 1.0
* @date updated Jan 26, 2023
*/
namespace wlsrm;
class wlsrm {
private static $_instance = null;
/**
* Plugin constructor.
*
* Initializing WL simple role manager plugin.
*
* @since 1.0.0
* @access private
*/
private function __construct() {
$this->register_autoloader();
if(wp_doing_ajax()){
$registerAjax = \wlsrm\Ajax::getInstance();
}else{
if(is_admin()){
$this->register_admin_menu();
global $pagenow;
if ( $pagenow === 'users.php' && isset($_GET['page']) && $_GET['page'] == 'user-management') {
// role management page
add_action('admin_enqueue_scripts', array($this, 'addScriptsAndStyles'));
}
}
}
}
/**
* Add styles and scripts if needed
*/
public function addScriptsAndStyles() {
wp_enqueue_style(' wllcf', WLSRM_URL.'/assets/css/wllcf.css', array(), filemtime(WLSRM_PATH.'/assets/css/wllcf.css'));
wp_enqueue_script('role-managemet', WLSRM_URL . '/assets/js/role-managemet.js', [ 'jquery' ], false, true);
wp_localize_script(
'role-managemet',
'wl_rm',
[
'ajax_url' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'wl-role-mngmnt' ),
'error_refresh' => __("Error! Please refresh the page and try again.", 'wlsrm'),
]
);
}
/**
* Register autoloader.
*
* WL simple role manager autoloader loads all the classes needed to run the plugin.
*
* @since 1.6.0
* @access private
*/
private function register_autoloader() {
require_once WLSRM_PATH . '/Loader.php';
Loader::registerAutoLoad();
}
/**
* Add function to admin_menu hook
*/
private function register_admin_menu() {
add_action('admin_menu', array($this, 'addRolesMenu'));
}
/**
* Add menu
*/
public function addRolesMenu() {
// Add Role management page to user menu
add_users_page(__('Roles', 'wlsrm'), __('Roles', 'wlsrm'), 'edit_users', 'user-management', array($this, 'roleManagementPage'));
}
/**
* Function that generate Role management page
*/
public function roleManagementPage() {
// $role_exclude = array('level_0', 'level_1', 'level_2', 'level_3', 'level_4', 'level_5', 'level_6', 'level_7', 'level_8', 'level_9', 'level_10');
if (!isset($_REQUEST['settings-updated']))
$_REQUEST['settings-updated'] = false; // This checks whether the form has just been submitted.
?>
<style>
.wlsrm-wrap{
padding: 20px
}
.inb{display: inline-block}
#tab_wrapper .tab>.inb{
margin:10px 0;
/*min-width: 270px*/
width: 25%;
font-size: 0.85em
}
#tab_wrapper .tab>.inb label{
margin-right: 10px
}
.wl input[type=radio]:checked::before{
content: initial
}
.wl [type=checkbox][role=switch]:before{
margin-top: 0;
margin-bottom: 0
}
.btn-sm{
font-size: 0.95em !important;
--form-element-spacing-vertical: 0.25em;
--form-element-spacing-horizontal: 0.3em;
}
</style>
<?php
echo "<h2>" . wp_get_theme() . " ". __('User managemet', 'wlsrm') . "</h2>";
// This shows the page's name and an icon if one has been provided
?>
<div class="wlsrm-wrap wl">
<div data-theme="light">
<form id="capabilities" method="post" action="">
<div class="row end-lg">
<div class="col-lg-2">
<input class="btn-sm" type="text" id="add-role-name" />
</div>
<div class="col-lg-2" data-theme="green">
<button class="btn-sm" id="add-role"><?php _e('Add role', 'wlsrm'); ?></button>
</div>
<div class="col-lg-2">
<select class="btn-sm" id="remove-role-name">
</select>
</div>
<div class="col-lg-2" data-theme="red">
<button class="btn-sm" id="remove-role"><?php _e('Remove role', 'wlsrm'); ?></button>
</div>
</div>
<div class="row end-lg">
<div class="col-lg-2">
<input class="btn-sm" type="text" id="add-capability-name" />
</div>
<div class="col-lg-2" data-theme="green">
<button class="btn-sm" id="add-capability"><?php _e('Add capability', 'wlsrm'); ?></button>
</div>
<div class="col-lg-2">
<select class="btn-sm" id="remove-capability-name">
</select>
</div>
<div class="col-lg-2" data-theme="red">
<button class="btn-sm" id="remove-capability"><?php _e('Remove capability', 'wlsrm'); ?></button>
</div>
</div>
<?php if (false !== $_REQUEST['settings-updated']) : ?>
<div class="updated fade"><p><strong><?php _e('Options saved', 'wlsrm'); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this shows the notification ?>
<section id="tab_wrapper" class="tabs" style="padding: 20px">
</section>
<p class="submit"><input id="submit" type="submit" class="button-primary" value="<?php _e("Save capabilities", 'wlsrm'); ?> " /></p>
</form>
</div>
</div>
<?php
}
/**
*
* @return \wlsrm\wlsrm
*/
public static function getInstance() {
if (self::$_instance == null) {
self::$_instance = new self;
}
return self::$_instance;
}
}
wlsrm::getInstance();