[OverwriteThemeBackground] This new plugin will let the admin set a custom background theme independent

This commit is contained in:
Diogo Cordeiro
2019-08-19 15:35:52 +01:00
committed by Diogo Peralta Cordeiro
parent 616858ea89
commit 8d9b88a8bc
8 changed files with 706 additions and 157 deletions

View File

@@ -0,0 +1,109 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
defined('GNUSOCIAL') || die();
/**
* Allows administrators to overwrite his GNU social instance's background
*
* @category Plugin
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
/**
* Handle plugin's events
*
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class OverwriteThemeBackgroundPlugin extends Plugin
{
const PLUGIN_VERSION = '0.1.0';
/**
* Route urls
*
* @param URLMapper $m
* @return bool
* @throws Exception
*/
public function onRouterInitialized(URLMapper $m): bool
{
$m->connect('plugins/OverwriteThemeBackground/css/my_custom_theme_bg',
['action' => 'OverwriteThemeBackgroundCSS']);
$m->connect('admin/overwritethemebackground',
['action' => 'overwritethemebackgroundAdminPanel']);
return true;
}
/**
* Plugin meta-data
*
* @param array $versions
* @return bool hook true
* @throws Exception
*/
public function onPluginVersion(array &$versions): bool
{
$versions[] = [
'name' => 'Custom Background',
'version' => self::PLUGIN_VERSION,
'author' => 'Diogo Cordeiro',
'homepage' => 'https://www.diogo.site/projects/GNU-social/plugins/OverwriteThemeBackgroundPlugin',
// TRANS: Plugin description for OverwriteThemeBackground plugin.
'rawdescription' => _m('A friendly plugin for overwriting your theme\'s background style.')
];
return true;
}
/**
* Add our custom background css after theme's
*
* @param Action $action
* @return bool hook true
*/
public function onEndShowStyles(Action $action): bool
{
$action->cssLink(common_local_url('OverwriteThemeBackgroundCSS'));
return true;
}
/**
* Add a menu option for this plugin in Admin's UI
*
* @param AdminPanelNav $nav
* @return bool hook true
* @throws Exception
*/
public function onEndAdminPanelNav(AdminPanelNav $nav): bool
{
if (AdminPanelAction::canAdmin('profilefields')) {
$action_name = $nav->action->trimmed('action');
$nav->out->menuItem(
common_local_url('overwritethemebackgroundAdminPanel'),
_m('Overwrite Theme Background'),
_m('Customize your theme\'s background easily'),
$action_name == 'overwritethemebackgroundAdminPanel',
'nav_overwritethemebackground_admin_panel'
);
}
return true;
}
}

View File

@@ -0,0 +1,14 @@
Allows to overwrite your theme's background style
Installation
============
add
addPlugin('OverwriteThemeBackground');
$config['admin']['panels'][] = 'overwritethemebackground';
to the bottom of your config.php
Settings
========
none

View File

@@ -0,0 +1,281 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* UI to overwrite his GNU social instance's background
*
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* Apply/Store the custom background preferences
*
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class OverwriteThemeBackgroundAdminPanelAction extends AdminPanelAction
{
/**
* Title of the page
*
* @return string Title of the page
*/
public function title(): string
{
return _m('Overwrite Theme Background');
}
/**
* Instructions for use
*
* @return string instructions for use
*/
public function getInstructions(): string
{
return _m('Customize your theme\'s background easily');
}
/**
* Show the site admin panel form
*
* @return void
*/
function showForm()
{
$form = new OverwriteThemeBackgroundAdminPanelForm($this);
$form->show();
return;
}
/**
* Save settings from the form
*
* @return void
*/
function saveSettings()
{
static $settings = [
'overwritethemebackground' => [
'background-color',
'background-image',
'sslbackground-image',
'background-repeat',
'background-attachment',
'background-position'
]
];
$values = [];
foreach ($settings as $section => $parts) {
foreach ($parts as $setting) {
$values[$section][$setting] = $this->trimmed($setting);
}
}
// This throws an exception on validation errors
$this->validate($values);
// assert(all values are valid);
$config = new Config();
$config->query('BEGIN');
foreach ($settings as $section => $parts) {
foreach ($parts as $setting) {
Config::save($section, $setting, $values[$section][$setting]);
}
}
$config->query('COMMIT');
return;
}
/**
* Validate form values
*
* @param $values
* @throws ClientException
*/
function validate(&$values)
{
// Validate background
if (!empty($values['overwritethemebackground']['background-image']) &&
!common_valid_http_url($values['overwritethemebackground']['background-image'])) {
// TRANS: Client error displayed when a background URL is not valid.
$this->clientError(_m('Invalid background URL.'));
}
if (!empty($values['overwritethemebackground']['sslbackground-image']) &&
!common_valid_http_url($values['overwritethemebackground']['sslbackground-image'], true)) {
// TRANS: Client error displayed when a SSL background URL is invalid.
$this->clientError(_m('Invalid SSL background URL.'));
}
}
}
/**
* Friendly UI for setting the custom background preferences
*
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class OverwriteThemeBackgroundAdminPanelForm extends AdminForm
{
/**
* ID of the form
*
* @return int ID of the form
*/
function id()
{
return 'form_site_admin_panel';
}
/**
* class of the form
*
* @return string class of the form
*/
function formClass()
{
return 'form_settings';
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('overwritethemebackgroundAdminPanel');
}
/**
* Data elements of the form
*
* @return void
*/
function formData()
{
$this->out->elementStart('fieldset', ['id' => 'settings_site_background']);
// TRANS: Fieldset legend for form to change background.
$this->out->element('legend', null, _m('Background'));
$this->out->elementStart('ul', 'form_data');
/* Background colour */
$this->li();
$this->input('background-color',
// TRANS: Field label for GNU social site background.
_m('Site background color'),
// TRANS: Title for field label for GNU social site background.
'Background color for the site (hexadecimal with #).',
'overwritethemebackground');
$this->unli();
/* Background image */
$this->li();
$this->input('background-image',
// TRANS: Field label for GNU social site background.
_m('Site background'),
// TRANS: Title for field label for GNU social site background.
'Background for the site (full URL).',
'overwritethemebackground');
$this->unli();
$this->li();
$this->input('sslbackground-image',
// TRANS: Field label for SSL GNU social site background.
_m('SSL background'),
// TRANS: Title for field label for SSL GNU social site background.
'Background to show on SSL pages (full URL).',
'overwritethemebackground');
$this->unli();
/* Background repeat */
$this->li();
// TRANS: Dropdown label on site settings panel.
$this->out->dropdown('background-repeat', _m('Background repeat'),
// TRANS: Dropdown title on site settings panel.
['Repeat horizontally and vertically', 'Repeat Horizontally', 'Repeat Vertically', 'Don\'t repeat'], _m('repeat horizontally and/or vertically'),
false, common_config('overwritethemebackground', 'background-repeat') ?? 'repeat');
$this->unli();
/* Background attachment */
$this->li();
// TRANS: Dropdown label on site settings panel.
$this->out->dropdown('background-attachment', _m('Background attachment'),
// TRANS: Dropdown title on site settings panel.
['Scroll with page', 'Stay fixed'], _m('Whether the background image should scroll or be fixed (will not scroll with the rest of the page)'),
false, common_config('overwritethemebackground', 'background-attachment') ?? 'scroll');
$this->unli();
/* Background position */
$background_position_options = [
'initial',
'left top',
'left center',
'left bottom',
'right top',
'right center',
'right bottom',
'center top',
'center center',
'center bottom'
];
$this->li();
// TRANS: Dropdown label on site settings panel.
$this->out->dropdown('background-position', _m('Background position'),
// TRANS: Dropdown title on site settings panel.
$background_position_options, _m('Sets the starting position of a background image'),
false, common_config('overwritethemebackground', 'background-attachment') ?? 'initial');
$this->unli();
$this->out->elementEnd('ul');
$this->out->elementEnd('fieldset');
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
$this->out->submit('submit',
// TRANS: Button text for saving site settings.
_m('BUTTON', 'Save'),
'submit',
null,
// TRANS: Button title for saving site settings.
_m('Save the site settings.'));
}
}

View File

@@ -0,0 +1,155 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* Action with the CSS preferences
*
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* Route with CSS that will come after theme's css in order to overwrite it with sysadmin's custom background preferences
*
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class overwritethemebackgroundcssAction extends Action
{
protected $needLogin = false;
protected $canPost = false;
// Eventual custom background sysadmin may have set
private $_theme_background_url = false;
private $_background_colour = false;
private $_background_colour_important = false;
/**
* Fills _theme_background_url if possible.
*
* @param array $args $_REQUEST array
* @return bool true
* @throws ClientException
*/
protected function prepare(array $args = []): bool
{
parent::prepare($args);
if (GNUsocial::isHTTPS()) {
$background_url = common_config('overwritethemebackground', 'sslbackground-image');
if (empty($background_url)) {
// if background is an uploaded file, try to fall back to HTTPS file URL
$http_url = common_config('overwritethemebackground', 'background-image');
if (!empty($http_url)) {
try {
$f = File::getByUrl($http_url);
if (!empty($f->filename)) {
// this will handle the HTTPS case
$background_url = File::url($f->filename);
}
} catch (NoResultException $e) {
// no match
}
}
}
} else {
$background_url = common_config('overwritethemebackground', 'background-image');
}
$this->_background_colour = common_config('overwritethemebackground', 'background-color');
if (empty($background_url)) {
if (!empty($this->_background_colour)) {
$this->_background_colour_important = true; // We want the colour to override theme's default background
return true;
}
/*if (file_exists(Theme::file('images/bg.png'))) {
// This should handle the HTTPS case internally
$background_url = Theme::path('images/bg.png');
}
if (!empty($background_url)) {
$this->_theme_background_url = $background_url;
}*/
} else {
$this->_theme_background_url = $background_url;
}
return true;
}
/**
* Is this action read-only?
*
* @param array $args other arguments dummy
* @return bool true
*/
public function isReadOnly($args): bool
{
return true;
}
/**
* Print the CSS
*/
public function handle(): void
{
$background_position_options = [
'initial',
'left top',
'left center',
'left bottom',
'right top',
'right center',
'right bottom',
'center top',
'center center',
'center bottom'
];
header("Content-type: text/css", true);
$background_color = $this->_background_colour;
$background_image = $this->_theme_background_url;
$background_repeat = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat'][common_config('overwritethemebackground', 'background-repeat')];
$background_position = $background_position_options[common_config('overwritethemebackground', 'background-position')];
$background_attachment = ['scroll', 'fixed'][common_config('overwritethemebackground', 'background-attachment')];
$css = 'body {';
if ($background_color) {
if (!$this->_background_colour_important) {
$css .= 'background-color: ' . $background_color . ';';
} else {
$css .= 'background: ' . $background_color . ' !important;';
}
}
if ($background_image) {
$css .= 'background-image: url(' . $background_image . ');';
}
if ($background_repeat) {
$css .= 'background-repeat: ' . $background_repeat . ';';
}
if ($background_position) {
$css .= 'background-position: ' . $background_position . ';';
}
if ($background_attachment) {
$css .= 'background-attachment: ' . $background_attachment . ';';
}
$css .= '}';
echo $css;
}
}