[ticket/16977] Move HTML rendering logic to template

PHPBB3-16977
This commit is contained in:
lionel-rowe 2022-04-02 18:10:53 +01:00
parent 8c982c7aa0
commit 87c1e631ef
7 changed files with 49 additions and 10 deletions

View file

@ -6,6 +6,7 @@ services:
- '@routing.helper'
- '%core.root_path%'
- '%core.php_ext%'
- '@template'
cron.lock_db:
class: phpbb\lock\db

View file

@ -363,8 +363,8 @@ class helper
if ($task)
{
$url = $task->get_url();
$this->template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="" style="position: fixed; top: -10000px" />');
$cron_task_tag = $task->get_html_tag();
$this->template->assign_var('RUN_CRON_TASK', $cron_task_tag);
}
else
{

View file

@ -59,6 +59,11 @@ class manager
*/
protected $php_ext;
/**
* @var \phpbb\template\template
*/
protected $template;
/**
* Constructor. Loads all available tasks.
*
@ -66,13 +71,15 @@ class manager
* @param helper $routing_helper Routing helper
* @param string $phpbb_root_path Relative path to phpBB root
* @param string $php_ext PHP file extension
* @param \phpbb\template\template $template
*/
public function __construct(ContainerInterface $phpbb_container, helper $routing_helper, $phpbb_root_path, $php_ext)
public function __construct(ContainerInterface $phpbb_container, helper $routing_helper, $phpbb_root_path, $php_ext, $template)
{
$this->phpbb_container = $phpbb_container;
$this->routing_helper = $routing_helper;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->template = $template;
}
/**
@ -193,6 +200,6 @@ class manager
*/
public function wrap_task(\phpbb\cron\task\task $task)
{
return new wrapper($task, $this->routing_helper, $this->phpbb_root_path, $this->php_ext);
return new wrapper($task, $this->routing_helper, $this->phpbb_root_path, $this->php_ext, $this->template);
}
}

View file

@ -41,6 +41,11 @@ class wrapper
*/
protected $php_ext;
/**
* @var \phpbb\template\template
*/
protected $template;
/**
* Constructor.
*
@ -50,13 +55,15 @@ class wrapper
* @param helper $routing_helper Routing helper for route generation
* @param string $phpbb_root_path Relative path to phpBB root
* @param string $php_ext PHP file extension
* @param \phpbb\template\template $template
*/
public function __construct(task $task, helper $routing_helper, $phpbb_root_path, $php_ext)
public function __construct(task $task, helper $routing_helper, $phpbb_root_path, $php_ext, $template)
{
$this->task = $task;
$this->routing_helper = $routing_helper;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->template = $template;
}
/**
@ -105,6 +112,23 @@ class wrapper
return $this->routing_helper->route('phpbb_cron_run', $params);
}
/**
* Returns HTML for an invisible `img` tag that can be displayed on page
* load to trigger a request to the relevant cron task endpoint.
*
* @return string HTML to render to trigger cron task
*/
public function get_html_tag()
{
$this->template->set_filenames([
'cron_html_tag' => 'cron.html',
]);
$this->template->assign_var('CRON_TASK_URL', $this->get_url());
return $this->template->assign_display('cron_html_tag');
}
/**
* Forwards all other method calls to the wrapped task implementation.
*

View file

@ -0,0 +1,7 @@
{#
Runs the cron task by triggering server request to the URL on load. `img` is
preferred over JS to ensure maximum compatibility. We use `class="sr-only"`
to hide visually and `aria-hidden="true"` to hide from screen-readers; using
`hidden` or `display: none` would prevent the task from running.
#}
<img class="sr-only" aria-hidden="true" src="{{ CRON_TASK_URL|e('html_attr') }}" width="1" height="1" alt="">

View file

@ -60,7 +60,7 @@
<div>
<a id="bottom" class="anchor" accesskey="z"></a>
<!-- IF not S_IS_BOT -->{RUN_CRON_TASK}<!-- ENDIF -->
{% if not S_IS_BOT %}{{ RUN_CRON_TASK }}{% endif %}
</div>
<script src="{T_JQUERY_LINK}"></script>

View file

@ -244,8 +244,8 @@ if (!$config['use_system_cron'])
if ($task->is_ready())
{
$url = $task->get_url();
$template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="" style="position: fixed; top: -10000px" />');
$cron_task_tag = $task->get_html_tag();
$template->assign_var('RUN_CRON_TASK', $cron_task_tag);
}
else
{
@ -255,8 +255,8 @@ if (!$config['use_system_cron'])
if ($task->is_ready())
{
$url = $task->get_url();
$template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="" style="position: fixed; top: -10000px" />');
$cron_task_tag = $task->get_html_tag();
$template->assign_var('RUN_CRON_TASK', $cron_task_tag);
}
}
}