improve/inc/lib.improve.action.php

182 lines
4.9 KiB
PHP
Raw Normal View History

2021-09-01 23:35:23 +00:00
<?php
/**
* @brief improve, a plugin for Dotclear 2
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @package Dotclear
2021-09-06 21:36:50 +00:00
* @subpackage Plugin
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @author Jean-Christian Denis and contributors
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
class ImproveActionTab extends ImproveAction
{
protected function init(): bool
{
$this->setProperties([
2021-11-01 21:17:44 +00:00
'id' => 'tab',
'name' => __('Tabulations'),
'desc' => __('Replace tabulation by four space in php files'),
2021-09-01 23:35:23 +00:00
'priority' => 820,
2021-11-01 21:17:44 +00:00
'types' => ['plugin', 'theme']
2021-09-01 23:35:23 +00:00
]);
return true;
}
public function readFile(&$content): ?bool
2021-09-01 23:35:23 +00:00
{
if (!in_array($this->path_extension, ['php', 'md'])) {
2021-09-01 23:35:23 +00:00
return null;
}
$clean = preg_replace('/(\t)/', ' ', $content);// . "\n";
if ($content != $clean) {
$this->setSuccess(__('Replace tabulation by spaces'));
$content = $clean;
}
2021-09-01 23:35:23 +00:00
return true;
}
public function isConfigured(): bool
{
return true;
}
}
class ImproveActionNewline extends ImproveAction
{
protected function init(): bool
{
$this->setProperties([
2021-11-01 21:17:44 +00:00
'id' => 'newline',
'name' => __('Newlines'),
'desc' => __('Replace bad and repetitive and empty newline by single newline in files'),
2021-09-01 23:35:23 +00:00
'priority' => 840,
2021-11-01 21:17:44 +00:00
'config' => true,
'types' => ['plugin', 'theme']
2021-09-01 23:35:23 +00:00
]);
2021-11-01 21:17:44 +00:00
/*
$ext = @unserialize($this->core->blog->settings->improve->newline_extensions);
$ext = Improve::cleanExtensions($ext);
if (!empty($ext)) {
$this->extensions = $ext;
}
*/
2021-09-01 23:35:23 +00:00
return true;
}
public function isConfigured(): bool
{
return !empty($this->getSetting('extensions'));
2021-09-01 23:35:23 +00:00
}
public function configure($url): ?string
{
if (!empty($_POST['save']) && !empty($_POST['newline_extensions'])) {
$this->setSettings(
2021-11-01 21:17:44 +00:00
'extensions',
2021-09-01 23:35:23 +00:00
Improve::cleanExtensions($_POST['newline_extensions'])
);
$this->redirect($url);
}
$ext = $this->getSetting('extensions');
2021-09-01 23:35:23 +00:00
if (!is_array($ext)) {
$ext = [];
}
2021-11-01 21:17:44 +00:00
return
'<p><label class="classic" for="newline_extensions">' .
2021-09-01 23:35:23 +00:00
__('List of files extension to work on:') . '<br />' .
2021-11-01 21:17:44 +00:00
form::field('newline_extensions', 65, 255, implode(',', $ext)) .
2021-09-01 23:35:23 +00:00
'</label></p><p class="form-note">' .
2021-11-01 21:17:44 +00:00
__('Use comma separated list of extensions without dot, recommand "php,js,xml,txt,md".') .
2021-09-01 23:35:23 +00:00
'</p>';
}
2021-11-07 23:59:52 +00:00
public function readFile(string &$content): ?bool
2021-09-01 23:35:23 +00:00
{
$ext = $this->getSetting('extensions');
if (!is_array($ext) || !in_array($this->path_extension, $ext)) {
2021-09-01 23:35:23 +00:00
return null;
}
2021-11-07 23:59:52 +00:00
$clean = (string) preg_replace(
2021-11-01 21:17:44 +00:00
'/(\n\s+\n)/',
"\n\n",
2021-11-07 23:59:52 +00:00
(string) preg_replace(
2021-11-01 21:17:44 +00:00
'/(\n\n+)/',
"\n\n",
2021-11-07 23:59:52 +00:00
(string) str_replace(
2021-11-01 21:17:44 +00:00
["\r\n", "\r"],
"\n",
2021-09-01 23:35:23 +00:00
$content
)
)
);
if ($content != $clean) {
$this->setSuccess(__('Replace bad new lines'));
$content = $clean;
}
2021-09-01 23:35:23 +00:00
return true;
}
}
class ImproveActionEndoffile extends ImproveAction
{
protected function init(): bool
{
$this->setProperties([
2021-11-01 21:17:44 +00:00
'id' => 'endoffile',
'name' => __('End of files'),
'desc' => __('Remove php tag and empty lines from end of files'),
2021-09-01 23:35:23 +00:00
'priority' => 860,
2021-11-01 21:17:44 +00:00
'config' => true,
'types' => ['plugin', 'theme']
2021-09-01 23:35:23 +00:00
]);
return true;
}
public function isConfigured(): bool
{
return true;
}
public function configure($url): ?string
{
if (!empty($_POST['save'])) {
$this->setSettings('psr2', !empty($_POST['endoffile_psr2']));
2021-09-01 23:35:23 +00:00
$this->redirect($url);
}
2021-11-01 21:17:44 +00:00
return
2021-09-01 23:35:23 +00:00
'<p><label class="classic" for="endoffile_psr2">' .
2021-11-01 21:17:44 +00:00
form::checkbox('endoffile_psr2', 255, $this->getSetting('psr2')) .
__('Add a blank line to the end of file') .
'</label></p><p class="form-note">' .
__('PSR2 must have a blank line, whereas PSR12 must not.') .
2021-09-01 23:35:23 +00:00
'</p>';
}
public function readFile(&$content): ?bool
2021-09-01 23:35:23 +00:00
{
if (!in_array($this->path_extension, ['php', 'md'])) {
2021-09-01 23:35:23 +00:00
return null;
}
$clean = preg_replace(
2021-11-01 21:17:44 +00:00
['/(\s*)(\?>\s*)$/', '/\n+$/'],
'',
2021-09-01 23:35:23 +00:00
$content
) . ($this->getSetting('psr2') ? "\n" : '');
if ($content != $clean) {
$this->setSuccess(__('Replace end of file'));
$content = $clean;
}
2021-09-01 23:35:23 +00:00
return true;
}
2021-11-01 21:17:44 +00:00
}