Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/actions/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

set -e

npx cypress run --spec "cypress/tests/data/10-Installation.spec.js,cypress/tests/data/20-CreateContext.spec.js"

npx cypress run --headless --browser chrome --config '{"specPattern":["plugins/generic/customHeader/cypress/tests/functional/*.cy.js"]}'


npx cypress run --config specPattern=plugins/generic/customHeader/cypress/tests/functional
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
on: [push]
on: [push, pull_request]
name: customHeader
jobs:
customHeader:
Expand Down Expand Up @@ -43,12 +43,12 @@ jobs:
- application: ops
php-version: 8.2
database: pgsql

name: customHeader
steps:
- uses: pkp/pkp-github-actions@v1
with:
node_version: 20
branch: main
repository: pkp
plugin: true
node_version: 20
branch: main
repository: pkp
plugin: true
dataset_inject: true
290 changes: 151 additions & 139 deletions CustomHeaderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,154 +3,166 @@
/**
* @file CustomHeaderPlugin.php
*
* Copyright (c) 2013-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Copyright (c) 2013-2025 Simon Fraser University
* Copyright (c) 2003-2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @brief CustomHeader plugin class
*/

namespace APP\plugins\generic\customHeader;

use PKP\plugins\GenericPlugin;
use APP\core\Application;
use Exception;
use PKP\config\Config;
use PKP\plugins\Hook;
use PKP\core\JSONMessage;
use PKP\core\PKPApplication;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\core\JSONMessage;
use APP\i18n\AppLocale;
use APP\template\TemplateManager;
use APP\core\Application;

class CustomHeaderPlugin extends GenericPlugin {
/** @var bool Whether or not the header has been injected */
var $injected = false;

/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled()) {
// Insert CustomHeader page tag to page header
Hook::add('TemplateManager::display', array($this, 'displayTemplateHook'));
// Insert custom script to the page footer
Hook::add('Templates::Common::Footer::PageFooter', array($this, 'insertFooter'));
}
return $success;
}

/**
* Get the plugin display name.
* @return string
*/
function getDisplayName() {
return __('plugins.generic.customHeader.displayName');
}

/**
* Get the plugin description.
* @return string
*/
function getDescription() {
return __('plugins.generic.customHeader.description');
}

/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $verb) {
$router = $request->getRouter();
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $verb)
);
}

/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();

$templateMgr = TemplateManager::getManager($request);
$form = new CustomHeaderSettingsForm($this, $context?$context->getId():CONTEXT_ID_NONE);

if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}

/**
* Register the CustomHeader script tag
* @param $hookName string
* @param $params array
*/
function displayTemplateHook($hookName, $params) {
if (!$this->injected) {
$this->injected = true;
$templateMgr =& $params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$templateMgr->addHeader('custom', $this->getSetting($context?$context->getId():CONTEXT_ID_NONE, 'content'));
$templateMgr->addHeader('custombackend', $this->getSetting($context?$context->getId():CONTEXT_ID_NONE, 'backendContent'), ['contexts' => ['backend']]);
}
return false;
}

/**
* Add custom footer to the page
*
* @param $hookName string
* @param $params array
*/
function insertFooter($hookName, $params) {
$templateMgr =& $params[0];
$output =& $params[2];
$request = Application::get()->getRequest();
$context = $request->getContext();

$output .= $this->getSetting($context?$context->getId():CONTEXT_ID_NONE, 'footerContent');

return false;
}

/**
* This plugin can be used site-wide or in a specific context. The
* isSitePlugin check is used to grant access to different users, so this
* plugin must return true only if the user is currently in the site-wide
* context.
*
* @see PluginGridRow::_canEdit()
* @return boolean
*/
function isSitePlugin() {
return !(Application::get()->getRequest()->getContext());
}
}
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;

if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\customHeader\CustomHeaderPlugin', '\CustomHeaderPlugin');
class CustomHeaderPlugin extends GenericPlugin
{
/** Whether the header has been injected */
public bool $injected = false;

/**
* @copydoc Plugin::register()
* @throws Exception
*/
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) {
return true;
}
if ($success && $this->getEnabled()) {
// Insert CustomHeader page tag to page header
Hook::add('TemplateManager::display', array($this, 'displayTemplateHook'));
// Insert custom script to the page footer
Hook::add('Templates::Common::Footer::PageFooter', array($this, 'insertFooter'));
}
return $success;
}

/**
* Get the plugin display name.
*/
public function getDisplayName(): string
{
return __('plugins.generic.customHeader.displayName');
}

/**
* Get the plugin description.
*/
public function getDescription(): string
{
return __('plugins.generic.customHeader.description');
}

/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb): array
{
$router = $request->getRouter();
return array_merge(
$this->getEnabled() ? array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, ['verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic']),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
) : array(),
parent::getActions($request, $verb)
);
}

/**
* @copydoc Plugin::manage()
* @throws Exception
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$form = new CustomHeaderSettingsForm(
$this,
$context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID
);

if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}

/**
* Register the CustomHeader script tag
*/
public function displayTemplateHook(string $hookName, array $params)
{
if (!$this->injected) {
$this->injected = true;
$templateMgr =& $params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$templateMgr->addHeader(
'custom',
$this->getSetting($context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID, 'content')
);
$templateMgr->addHeader(
'custombackend',
$this->getSetting(
$context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID,
'backendContent'
),
['contexts' => ['backend']]
);
}
return false;
}

/**
* Add custom footer to the page
*/
public function insertFooter(string $hookName, array $params)
{
$templateMgr =& $params[0];
$output =& $params[2];
$request = Application::get()->getRequest();
$context = $request->getContext();

$output .= $this->getSetting($context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID, 'footerContent');

return false;
}

/**
* This plugin can be used site-wide or in a specific context. The
* isSitePlugin check is used to grant access to different users, so this
* plugin must return true only if the user is currently in the site-wide
* context.
*
* @see PluginGridRow::_canEdit()
*/
public function isSitePlugin(): bool
{
return !(Application::get()->getRequest()->getContext());
}
}
Loading