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

@@ -9,6 +9,7 @@ use Grav\Common\GPM\Licenses;
use Grav\Common\GPM\Response;
use Grav\Common\Grav;
use Grav\Common\Language\LanguageCodes;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Plugins;
@@ -16,6 +17,7 @@ use Grav\Common\Themes;
use Grav\Common\Uri;
use Grav\Common\User\User;
use Grav\Common\Utils;
use Grav\Plugin\Admin\Twig\AdminTwigExtension;
use Grav\Plugin\Admin\Utils as AdminUtils;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File;
@@ -214,7 +216,7 @@ class Admin
public static function tools()
{
$tools = [];
$event = Grav::instance()->fireEvent('onAdminTools', new Event(['tools' => &$tools]));
Grav::instance()->fireEvent('onAdminTools', new Event(['tools' => &$tools]));
return $tools;
}
@@ -227,7 +229,7 @@ class Admin
public static function siteLanguages()
{
$languages = [];
$lang_data = Grav::instance()['config']->get('system.languages.supported', []);
$lang_data = (array) Grav::instance()['config']->get('system.languages.supported', []);
foreach ($lang_data as $index => $lang) {
$languages[$lang] = LanguageCodes::getNativeName($lang);
@@ -282,6 +284,7 @@ class Admin
$page = $pages->dispatch($route);
$parent_route = null;
if ($page) {
/** @var Page $parent */
$parent = $page->parent();
$parent_route = $parent->rawRoute();
}
@@ -351,6 +354,7 @@ class Admin
* @param array $post Additional form fields.
*
* @return bool
* @TODO LOGIN
*/
public function authenticate($data, $post)
{
@@ -365,7 +369,7 @@ class Admin
}
if (!$this->user->authenticated && isset($data['username']) && isset($data['password'])) {
if (!$this->user->authenticated && isset($data['username'], $data['password'])) {
// Perform RegEX check on submitted username to check for emails
if (filter_var($data['username'], FILTER_VALIDATE_EMAIL)) {
$user = AdminUtils::findUserByEmail($data['username']);
@@ -420,6 +424,19 @@ class Admin
return false;
}
/**
* @return bool
* @todo LOGIN
*/
public static function doAnyUsersExist()
{
// check for existence of a user account
$account_dir = $file_path = Grav::instance()['locator']->findResource('account://');
$user_check = glob($account_dir . '/*.yaml');
return $user_check ? true : false;
}
/**
* Add message into the session queue.
*
@@ -490,9 +507,9 @@ class Admin
if ($translation) {
if (count($args) >= 1) {
return vsprintf($translation, $args);
} else {
return $translation;
}
return $translation;
}
}
@@ -502,7 +519,7 @@ class Admin
/**
* Checks user authorisation to the action.
*
* @param string $action
* @param string|string[] $action
*
* @return bool
*/
@@ -734,9 +751,9 @@ class Admin
{
if (method_exists($this->grav['pages'], 'accessLevels')) {
return $this->grav['pages']->accessLevels();
} else {
return [];
}
return [];
}
public function license($package_slug)
@@ -773,7 +790,7 @@ class Admin
$dependency = $dependency['name'];
}
if (!in_array($dependency, $dependencies)) {
if (!in_array($dependency, $dependencies, true)) {
if (!in_array($dependency, ['admin', 'form', 'login', 'email', 'php'])) {
$dependencies[] = $dependency;
}
@@ -829,19 +846,16 @@ class Admin
if ($local) {
return $gpm->getInstalledPlugins();
} else {
$plugins = $gpm->getRepositoryPlugins();
if ($plugins) {
return $plugins->filter(function (
$package,
$slug
) use ($gpm) {
return !$gpm->isPluginInstalled($slug);
});
} else {
return [];
}
}
$plugins = $gpm->getRepositoryPlugins();
if ($plugins) {
return $plugins->filter(function ($package, $slug) use ($gpm) {
return !$gpm->isPluginInstalled($slug);
});
}
return [];
}
/**
@@ -861,19 +875,16 @@ class Admin
if ($local) {
return $gpm->getInstalledThemes();
} else {
$themes = $gpm->getRepositoryThemes();
if ($themes) {
return $themes->filter(function (
$package,
$slug
) use ($gpm) {
return !$gpm->isThemeInstalled($slug);
});
} else {
return [];
}
}
$themes = $gpm->getRepositoryThemes();
if ($themes) {
return $themes->filter(function ($package, $slug) use ($gpm) {
return !$gpm->isThemeInstalled($slug);
});
}
return [];
}
/**
@@ -928,9 +939,7 @@ class Admin
return false;
}
$dependencies = $this->gpm->getDependencies($packages);
return $dependencies;
return $this->gpm->getDependencies($packages);
}
/**
@@ -948,7 +957,7 @@ class Admin
$latest = [];
if (is_null($pages->routes())) {
if (null === $pages->routes()) {
return null;
}
@@ -986,6 +995,7 @@ class Admin
{
$file = File::instance($this->grav['locator']->findResource("log://{$this->route}.html"));
$content = $file->content();
$file->free();
return $content;
}
@@ -1030,11 +1040,7 @@ class Admin
*/
public function isTeamGrav($info)
{
if (isset($info['author']['name']) && ($info['author']['name'] == 'Team Grav' || Utils::contains($info['author']['name'], 'Trilby Media'))) {
return true;
} else {
return false;
}
return isset($info['author']['name']) && ($info['author']['name'] === 'Team Grav' || Utils::contains($info['author']['name'], 'Trilby Media'));
}
/**
@@ -1046,11 +1052,7 @@ class Admin
*/
public function isPremiumProduct($info)
{
if (isset($info['premium'])) {
return true;
} else {
return false;
}
return isset($info['premium']);
}
/**
@@ -1069,9 +1071,9 @@ class Admin
$pinfo = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $pinfo);
return $pinfo;
} else {
return 'phpinfo() method is not available on this server.';
}
return 'phpinfo() method is not available on this server.';
}
/**
@@ -1107,7 +1109,8 @@ class Admin
if ($this->validateDate($date, "$date_format $time_format")) {
$guess[$date] = "$date_format $time_format";
break 2;
} elseif ($this->validateDate($date, "$time_format $date_format")) {
}
if ($this->validateDate($date, "$time_format $date_format")) {
$guess[$date] = "$time_format $date_format";
break 2;
}
@@ -1182,9 +1185,10 @@ class Admin
'r' => 'llll ZZ',
'U' => 'X'
];
$js_format = "";
$js_format = '';
$escaping = false;
for ($i = 0; $i < strlen($php_format); $i++) {
$len = strlen($php_format);
for ($i = 0; $i < $len; $i++) {
$char = $php_format[$i];
if ($char === '\\') // PHP date format escaping character
{
@@ -1251,18 +1255,18 @@ class Admin
$notifications = array_reverse($notifications);
// Make adminNicetimeFilter available
require_once(__DIR__ . '/../twig/AdminTwigExtension.php');
$adminTwigExtension = new AdminTwigExtension();
require_once __DIR__ . '/../classes/Twig/AdminTwigExtension.php';
$adminTwigExtension = new AdminTwigExtension;
$filename = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT,
true, true);
$read_notifications = CompiledYamlFile::instance($filename)->content();
$read_notifications = (array)CompiledYamlFile::instance($filename)->content();
$notifications_processed = [];
foreach ($notifications as $key => $notification) {
$is_valid = true;
if (in_array($notification->id, $read_notifications)) {
if (in_array($notification->id, $read_notifications, true)) {
$notification->read = true;
}
@@ -1272,7 +1276,7 @@ class Admin
if ($is_valid && isset($notification->dependencies)) {
foreach ($notification->dependencies as $dependency => $constraints) {
if ($dependency == 'grav') {
if ($dependency === 'grav') {
if (!Semver::satisfies(GRAV_VERSION, $constraints)) {
$is_valid = false;
}
@@ -1369,7 +1373,7 @@ class Admin
/** @var Pages $pages */
$pages = $this->grav['pages'];
if ($path && $path[0] != '/') {
if ($path && $path[0] !== '/') {
$path = "/{$path}";
}
@@ -1378,14 +1382,14 @@ class Admin
if (!$page) {
$slug = basename($path);
if ($slug == '') {
if ($slug === '') {
return null;
}
$ppath = str_replace('\\', '/', dirname($path));
// Find or create parent(s).
$parent = $this->getPage($ppath != '/' ? $ppath : '');
$parent = $this->getPage($ppath !== '/' ? $ppath : '');
// Create page.
$page = new Page;
@@ -1396,7 +1400,7 @@ class Admin
$pages->addPage($page, $path);
// Set if Modular
$page->modularTwig($slug[0] == '_');
$page->modularTwig($slug[0] === '_');
// Determine page type.
if (isset($this->session->{$page->route()})) {
@@ -1407,13 +1411,14 @@ class Admin
$header = ['title' => $data['title']];
if (isset($data['visible'])) {
if ($data['visible'] == '' || $data['visible']) {
if ($data['visible'] === '' || $data['visible']) {
// if auto (ie '')
$children = $page->parent()->children();
$pageParent = $page->parent();
$children = $pageParent ? $pageParent->children() : [];
foreach ($children as $child) {
if ($child->order()) {
// set page order
$page->order(AdminController::getNextOrderInFolder($page->parent()->path()));
$page->order(AdminController::getNextOrderInFolder($pageParent->path()));
break;
}
}
@@ -1424,7 +1429,7 @@ class Admin
}
if ($data['name'] == 'modular') {
if ($data['name'] === 'modular') {
$header['body_classes'] = 'modular';
}
@@ -1446,7 +1451,7 @@ class Admin
$page->name($type . CONTENT_EXT);
$page->header();
}
$page->modularTwig($slug[0] == '_');
$page->modularTwig($slug[0] === '_');
}
return $page;
@@ -1466,9 +1471,7 @@ class Admin
$reader = new Reader();
$parser = $reader->getParser($feed_url, $body, 'utf-8');
$feed = $parser->execute();
return $feed;
return $parser->execute();
}
@@ -1593,20 +1596,19 @@ class Admin
* Get all the media of a type ('images' | 'audios' | 'videos' | 'files')
*
* @param string $type
* @param Page\Page $page
* @param Page|null $page
* @param array $files
*
* @return array
*/
private function getMediaOfType($type, $page, $page_files) {
private function getMediaOfType($type, Page $page = null, array $files)
{
if ($page) {
// $path = $page->path();
$media = $page->media();
$mediaOfType = $media->$type();
foreach($mediaOfType as $title => $file) {
$page_files[] = [
$files[] = [
'title' => $title,
'type' => $type,
'page_route' => $page->route(),
@@ -1614,10 +1616,10 @@ class Admin
];
}
return $page_files;
} else {
return [];
return $files;
}
return [];
}
/**
@@ -1722,10 +1724,11 @@ class Admin
*/
public function pages()
{
/** @var Collection $pages */
$pages = $this->grav['pages']->all();
$pagesWithFiles = [];
if ($pages) foreach ($pages as $page) {
foreach ($pages as $page) {
if (count($page->media()->all())) {
$pagesWithFiles[] = $page;
}
@@ -1799,7 +1802,6 @@ class Admin
*/
public function getReferrer()
{
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
return $referrer;
return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
}
}