| 1 |
<?php |
| 2 |
/** |
| 3 |
* BackendController.php |
| 4 |
* |
| 5 |
* The BackendController class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
|
| 16 |
declare(strict_types=1); |
| 17 |
|
| 18 |
namespace UserAccessManager\Controller\Backend; |
| 19 |
|
| 20 |
use UserAccessManager\Config\WordpressConfig; |
| 21 |
use UserAccessManager\Controller\Controller; |
| 22 |
use UserAccessManager\File\FileHandler; |
| 23 |
use UserAccessManager\Setup\SetupHandler; |
| 24 |
use UserAccessManager\User\UserHandler; |
| 25 |
use UserAccessManager\UserAccessManager; |
| 26 |
use UserAccessManager\Wrapper\Php; |
| 27 |
use UserAccessManager\Wrapper\Wordpress; |
| 28 |
|
| 29 |
/** |
| 30 |
* Class BackendController |
| 31 |
* |
| 32 |
* @package UserAccessManager\Controller |
| 33 |
*/ |
| 34 |
class BackendController extends Controller |
| 35 |
{ |
| 36 |
public const HANDLE_STYLE_ADMIN = 'UserAccessManagerAdmin'; |
| 37 |
public const HANDLE_SCRIPT_GROUP_SUGGEST = 'UserAccessManagerGroupSuggest'; |
| 38 |
public const HANDLE_SCRIPT_TIME_INPUT = 'UserAccessManagerTimeInput'; |
| 39 |
public const HANDLE_SCRIPT_ADMIN = 'UserAccessManagerFunctions'; |
| 40 |
public const UAM_ERRORS = 'UAM_ERRORS'; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var UserHandler |
| 44 |
*/ |
| 45 |
private $userHandler; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var FileHandler |
| 49 |
*/ |
| 50 |
private $fileHandler; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var SetupHandler |
| 54 |
*/ |
| 55 |
private $setupHandler; |
| 56 |
|
| 57 |
/** |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
private $notice = ''; |
| 61 |
|
| 62 |
/** |
| 63 |
* BackendController constructor. |
| 64 |
* @param Php $php |
| 65 |
* @param Wordpress $wordpress |
| 66 |
* @param WordpressConfig $wordpressConfig |
| 67 |
* @param UserHandler $userHandler |
| 68 |
* @param FileHandler $fileHandler |
| 69 |
* @param SetupHandler $setupHandler |
| 70 |
*/ |
| 71 |
public function __construct( |
| 72 |
Php $php, |
| 73 |
Wordpress $wordpress, |
| 74 |
WordpressConfig $wordpressConfig, |
| 75 |
UserHandler $userHandler, |
| 76 |
FileHandler $fileHandler, |
| 77 |
SetupHandler $setupHandler |
| 78 |
) { |
| 79 |
parent::__construct($php, $wordpress, $wordpressConfig); |
| 80 |
$this->userHandler = $userHandler; |
| 81 |
$this->fileHandler = $fileHandler; |
| 82 |
$this->setupHandler = $setupHandler; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Shows the admin notices. |
| 87 |
*/ |
| 88 |
public function showAdminNotice() |
| 89 |
{ |
| 90 |
$messages = isset($_SESSION[self::UAM_ERRORS]) === true ? $_SESSION[self::UAM_ERRORS] : []; |
| 91 |
$updateAction = $this->getRequestParameter('uam_update_db'); |
| 92 |
|
| 93 |
if ($this->setupHandler->getDatabaseHandler()->isDatabaseUpdateNecessary() === true |
| 94 |
&& $updateAction !== SetupController::UPDATE_BLOG |
| 95 |
&& $updateAction !== SetupController::UPDATE_NETWORK |
| 96 |
) { |
| 97 |
$messages[] = sprintf(TXT_UAM_NEED_DATABASE_UPDATE, 'admin.php?page=uam_setup'); |
| 98 |
} |
| 99 |
|
| 100 |
if ($messages !== []) { |
| 101 |
$this->notice = implode('<br>', $messages); |
| 102 |
echo $this->getIncludeContents('AdminNotice.php'); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns the set notice. |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public function getNotice(): string |
| 111 |
{ |
| 112 |
return $this->notice; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Register styles and scripts with handle for admin panel. |
| 117 |
*/ |
| 118 |
private function registerStylesAndScripts() |
| 119 |
{ |
| 120 |
$urlPath = $this->wordpressConfig->getUrlPath(); |
| 121 |
|
| 122 |
$this->wordpress->registerStyle( |
| 123 |
self::HANDLE_STYLE_ADMIN, |
| 124 |
$urlPath . 'assets/css/uamAdmin.css', |
| 125 |
[], |
| 126 |
UserAccessManager::VERSION, |
| 127 |
'screen' |
| 128 |
); |
| 129 |
|
| 130 |
$this->wordpress->registerScript( |
| 131 |
self::HANDLE_SCRIPT_GROUP_SUGGEST, |
| 132 |
$urlPath . 'assets/js/jquery.uam-group-suggest.js', |
| 133 |
['jquery'], |
| 134 |
UserAccessManager::VERSION |
| 135 |
); |
| 136 |
|
| 137 |
$this->wordpress->registerScript( |
| 138 |
self::HANDLE_SCRIPT_TIME_INPUT, |
| 139 |
$urlPath . 'assets/js/jquery.uam-time-input.js', |
| 140 |
['jquery'], |
| 141 |
UserAccessManager::VERSION |
| 142 |
); |
| 143 |
|
| 144 |
$this->wordpress->registerScript( |
| 145 |
self::HANDLE_SCRIPT_ADMIN, |
| 146 |
$urlPath . 'assets/js/functions.js', |
| 147 |
['jquery'], |
| 148 |
UserAccessManager::VERSION |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* The function for the admin_enqueue_scripts action for styles and scripts. |
| 154 |
*/ |
| 155 |
public function enqueueStylesAndScripts() |
| 156 |
{ |
| 157 |
$this->registerStylesAndScripts(); |
| 158 |
$this->wordpress->enqueueStyle(self::HANDLE_STYLE_ADMIN); |
| 159 |
$this->wordpress->enqueueScript(self::HANDLE_SCRIPT_GROUP_SUGGEST); |
| 160 |
$this->wordpress->enqueueScript(self::HANDLE_SCRIPT_TIME_INPUT); |
| 161 |
$this->wordpress->enqueueScript(self::HANDLE_SCRIPT_ADMIN); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* The function for the wp_dashboard_setup action. |
| 167 |
* Removes widgets to which a user should not have access. |
| 168 |
*/ |
| 169 |
public function setupAdminDashboard() |
| 170 |
{ |
| 171 |
if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === false) { |
| 172 |
$metaBoxes = $this->wordpress->getMetaBoxes(); |
| 173 |
unset($metaBoxes['dashboard']['normal']['core']['dashboard_recent_comments']); |
| 174 |
$this->wordpress->setMetaBoxes($metaBoxes); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|