Code cleanup

This commit is contained in:
Matias Griese
2018-05-10 10:14:18 +03:00
parent 939eee0d3f
commit 12cf325518
5 changed files with 360 additions and 366 deletions

View File

@@ -16,6 +16,7 @@ use Grav\Plugin\Admin\AdminTwigExtension;
use Grav\Plugin\Admin\Popularity;
use Grav\Plugin\Admin\Themes;
use Grav\Plugin\Admin\AdminController;
use Grav\Plugin\Admin\Twig\AdminTwigExtension;
use Grav\Plugin\Login\Login;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\Session\Session;
@@ -118,7 +119,7 @@ class AdminPlugin extends Plugin
// Autoloader
spl_autoload_register(function ($class) {
if (Utils::startsWith($class, 'Grav\Plugin\Admin')) {
require_once __DIR__ .'/classes/' . strtolower(basename(str_replace("\\", "/", $class))) . '.php';
require_once __DIR__ .'/classes/' . strtolower(basename(str_replace("\\", '/', $class))) . '.php';
}
});
@@ -220,7 +221,7 @@ class AdminPlugin extends Plugin
$data = [];
$username = $form->value('username');
if ($form->value('password1') != $form->value('password2')) {
if ($form->value('password1') !== $form->value('password2')) {
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $this->grav['language']->translate('PLUGIN_LOGIN.PASSWORDS_DO_NOT_MATCH')
@@ -245,11 +246,8 @@ class AdminPlugin extends Plugin
}
}
unset($data['password1']);
unset($data['password2']);
// Don't store the username: that is part of the filename
unset($data['username']);
// Don't store plain text password or username (part of the filename).
unset($data['password1'], $data['password2'], $data['username']);
// Extra lowercase to ensure file is saved lowercase
$username = strtolower($username);
@@ -313,7 +311,7 @@ class AdminPlugin extends Plugin
}
// Turn on Twig autoescaping
if (method_exists($this->grav['twig'], 'setAutoescape') && $this->grav['uri']->param('task') != 'processmarkdown') {
if (method_exists($this->grav['twig'], 'setAutoescape') && $this->grav['uri']->param('task') !== 'processmarkdown') {
$this->grav['twig']->setAutoescape(true);
}
@@ -361,9 +359,9 @@ class AdminPlugin extends Plugin
$this->session->expert = $this->session->expert ?: false;
// set session variable if it's passed via the url
if ($this->uri->param('mode') == 'expert') {
if ($this->uri->param('mode') === 'expert') {
$this->session->expert = true;
} elseif ($this->uri->param('mode') == 'normal') {
} elseif ($this->uri->param('mode') === 'normal') {
$this->session->expert = false;
}
@@ -391,7 +389,7 @@ class AdminPlugin extends Plugin
$this->admin->task = $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task');
if ($task) {
$this->initializeController($task, $post);
} elseif ($this->template == 'logs' && $this->route) {
} elseif ($this->template === 'logs' && $this->route) {
// Display RAW error message.
echo $this->admin->logEntry();
exit();
@@ -637,7 +635,7 @@ class AdminPlugin extends Plugin
}
// Can't access path directly...
if ($path && $path != 'register') {
if ($path && $path !== 'register') {
$array = explode('/', $path, 2);
$this->template = array_shift($array);
$this->route = array_shift($array);
@@ -768,8 +766,9 @@ class AdminPlugin extends Plugin
*/
public function onTwigExtensions()
{
require_once(__DIR__ . '/twig/AdminTwigExtension.php');
$this->grav['twig']->twig->addExtension(new AdminTwigExtension());
require_once __DIR__ . '/classes/Twig/AdminTwigExtension.php';
$this->grav['twig']->twig->addExtension(new AdminTwigExtension);
}
/**
@@ -779,23 +778,19 @@ class AdminPlugin extends Plugin
*/
public function isAdminPath()
{
if ($this->uri->route() == $this->base || substr($this->uri->route(), 0,
strlen($this->base) + 1) == $this->base . '/'
) {
return true;
}
$route = $this->uri->route();
return false;
return $route === $this->base || 0 === strpos($route, $this->base . '/');
}
public function onAdminAfterSave(Event $event)
{
// Special case to redirect after changing the admin route to avoid 'breaking'
$obj = $event['object'];
if (!is_null($event['object'])) {
if (null !== $obj) {
$blueprint = $obj->blueprints()->getFilename();
if ($blueprint == 'admin/blueprints' && isset($obj->route) && $this->admin_route !== $obj->route) {
if ($blueprint === 'admin/blueprints' && isset($obj->route) && $this->admin_route !== $obj->route) {
$redirect = preg_replace('/^' . str_replace('/','\/',$this->admin_route) . '/',$obj->route,$this->uri->path());
$this->grav->redirect($redirect);
}
@@ -827,7 +822,7 @@ class AdminPlugin extends Plugin
// Clear flash objects for previously uploaded files
// whenever the user switches page / reloads
// ignoring any JSON / extension call
if (is_null($this->uri->extension()) && $this->admin->task !== 'save') {
if ($this->admin->task !== 'save' && empty($this->uri->extension())) {
// Discard any previously uploaded files session.
// and if there were any uploaded file, remove them from the filesystem
if ($flash = $this->session->getFlashObject('files-upload')) {
@@ -884,7 +879,7 @@ class AdminPlugin extends Plugin
// First filter by configuration
$hideTypes = Grav::instance()['config']->get('plugins.admin.hide_page_types', []);
foreach ($hideTypes as $type) {
foreach ((array) $hideTypes as $type) {
unset($types[$type]);
}
@@ -908,7 +903,7 @@ class AdminPlugin extends Plugin
$types = Pages::modularTypes();
// First filter by configuration
$hideTypes = Grav::instance()['config']->get('plugins.admin.hide_modular_page_types', []);
$hideTypes = (array) Grav::instance()['config']->get('plugins.admin.hide_modular_page_types', []);
foreach ($hideTypes as $type) {
unset($types[$type]);
}