Skip to content

Commit

Permalink
Register parameters
Browse files Browse the repository at this point in the history
to reduce code replication (cleanup tbc)
  • Loading branch information
bloatware committed Nov 6, 2019
1 parent 873f152 commit 0dc822a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 86 deletions.
20 changes: 15 additions & 5 deletions textpattern/lib/txplib_misc.php
Expand Up @@ -4773,19 +4773,29 @@ function get_lastmod($unix_ts = null)
/**
* Sets headers.
*
* @param array $headers 'lower-case-name' => 'value'
* @param array $headers 'name' => 'value'
* @param bool $rewrite If TRUE, rewrites existing headers
*/

function set_headers($headers = array('content-type' => 'text/html; charset=utf-8'), $rewrite = false)
function set_headers($headers = array('Content-Type' => 'text/html; charset=utf-8'), $rewrite = false)
{
if (headers_sent()) {
return;
}

if (!$rewrite) {
foreach (headers_list() as $header) {
unset($headers[strtolower(trim(strtok($header, ':')))]);
if (!$rewrite && $headers_list = headers_list()) {
$headers_low = array();

foreach (array_keys($headers) as $name) {
$headres_low[strtolower($name)] = $name;
}

foreach ($headers_list as $header) {
$name = strtolower(trim(strtok($header, ':')));

if (isset($headers_low[$name])) {
unset($headers[$headers_low[$name]]);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion textpattern/publish.php
Expand Up @@ -825,7 +825,6 @@ function output_file_download($filename)
set_headers(array(
'content-type' => 'application/octet-stream',
'content-disposition' => 'attachment; filename="'.$filename.'"',
'content-description' => 'File Download',
'content-length' => $filesize,
// Fix for IE6 PDF bug on servers configured to send cache headers.
'cache-control' => 'private'
Expand Down
96 changes: 17 additions & 79 deletions textpattern/publish/taghandlers.php
Expand Up @@ -59,15 +59,15 @@
->register('section_list')
->register('search_input')
->register('search_term')
->register('link_to_next')
->register('link_to_prev')
->register('link_to', 'link_to_next')
->register('link_to', 'link_to_prev', 'prev')
->register('next_title')
->register('prev_title')
->register('site_name')
->register('site_slogan')
->register('link_to_home')
->register('newer')
->register('older')
->register('txp_pager', 'newer')
->register('txp_pager', 'older', false)
->register('text')
->register('article_id')
->register('article_url_title')
Expand Down Expand Up @@ -101,8 +101,8 @@
->register('body')
->register('title')
->register('excerpt')
->register('category1')
->register('category2')
->register('article_category', 'category1')
->register('article_category', 'category2', 2)
->register('category')
->register('section')
->register('keywords')
Expand Down Expand Up @@ -165,8 +165,8 @@
->register('file_download')
->register('file_download_link')
->register('file_download_size')
->register('file_download_created')
->register('file_download_modified')
->register('file_download_time', 'file_download_created')
->register('file_download_time', 'file_download_modified', 'modified')
->register('file_download_id')
->register('file_download_name')
->register('file_download_category')
Expand Down Expand Up @@ -1647,7 +1647,7 @@ function search_term($atts)
// -------------------------------------------------------------

// Link to next/prev article, if it exists.
function link_to($atts, $thing = null, $target = null)
function link_to($atts, $thing = null, $target = 'next')
{
global $pretext, $thisarticle;

Expand Down Expand Up @@ -1705,22 +1705,6 @@ function link_to($atts, $thing = null, $target = null)

// -------------------------------------------------------------

// Link to next article, if it exists.
function link_to_next($atts, $thing = null)
{
return link_to($atts, $thing, 'next');
}

// -------------------------------------------------------------

// Link to previous article, if it exists.
function link_to_prev($atts, $thing = null)
{
return link_to($atts, $thing, 'prev');
}

// -------------------------------------------------------------

function next_title()
{
global $thisarticle, $is_article_list;
Expand Down Expand Up @@ -1876,20 +1860,6 @@ function txp_pager($atts, $thing = null, $newer = true)

// -------------------------------------------------------------

function newer($atts, $thing = null)
{
return txp_pager($atts, $thing);
}

// -------------------------------------------------------------

function older($atts, $thing = null)
{
return txp_pager($atts, $thing, false);
}

// -------------------------------------------------------------

function text($atts)
{
extract(lAtts(array(
Expand Down Expand Up @@ -2979,28 +2949,14 @@ function excerpt($atts = array(), $thing = null)

// -------------------------------------------------------------

function category1($atts, $thing = null)
{
return article_category(array('number' => 1) + $atts, $thing);
}

// -------------------------------------------------------------

function category2($atts, $thing = null)
{
return article_category(array('number' => 2) + $atts, $thing);
}

// -------------------------------------------------------------

function article_category($atts, $thing = null)
function article_category($atts, $thing = null, $number = 1)
{
global $thisarticle, $s, $permlink_mode;

assert_article();

extract(lAtts(array(
'number' => 1,
'number' => $number,
'class' => '',
'link' => 0,
'title' => 0,
Expand Down Expand Up @@ -4365,12 +4321,12 @@ function txp_header($atts)
}

extract(lAtts(array(
'name' => 'Content-Type',
'name' => true,
'replace' => true,
'value' => 'text/html; charset=utf-8',
'value' => '200 OK',
), $atts));

set_headers(array(strtolower($name) => $value), !empty($replace));
set_headers(array($name => $value), !empty($replace));
}

// -------------------------------------------------------------
Expand Down Expand Up @@ -4909,35 +4865,17 @@ function file_download_size($atts)

// -------------------------------------------------------------

function file_download_created($atts)
{
global $thisfile;

assert_file();

extract(lAtts(array('format' => ''), $atts));

if ($thisfile['created']) {
return fileDownloadFormatTime(array(
'ftime' => $thisfile['created'],
'format' => $format,
));
}
}

// -------------------------------------------------------------

function file_download_modified($atts)
function file_download_time($atts, $thing = null, $time = 'created')
{
global $thisfile;

assert_file();

extract(lAtts(array('format' => ''), $atts));

if ($thisfile['modified']) {
if (!empty($thisfile[$time])) {
return fileDownloadFormatTime(array(
'ftime' => $thisfile['modified'],
'ftime' => $thisfile[$time],
'format' => $format,
));
}
Expand Down
12 changes: 11 additions & 1 deletion textpattern/vendors/Textpattern/Tag/Registry.php
Expand Up @@ -39,6 +39,7 @@ class Registry implements \Textpattern\Container\ReusableInterface
*/

private $tags = array();
private $params = array();
private $atts = array();

/**
Expand All @@ -63,6 +64,11 @@ public function register($callback, $tag = null)

if ($tag) {
$this->tags[$tag] = $callback;
$params = array_slice(func_get_args(), 2);

if (!empty($params)) {
$this->params[$tag] = $params;
}
}
}

Expand Down Expand Up @@ -113,7 +119,11 @@ public function registerAttr($callback, $tag = null)
public function process($tag, array $atts = null, $thing = null)
{
if ($this->isRegistered($tag)) {
return (string) call_user_func($this->tags[$tag], (array)$atts, $thing);
//TODO: switch to args unpacking for php 5.6+
return isset($this->params[$tag]) ?
// (string) call_user_func($this->tags[$tag], (array)$atts, $thing, ...$this->params[$tag]) :
(string) call_user_func_array($this->tags[$tag], array_merge(array((array)$atts, $thing), $this->params[$tag])) :
(string) call_user_func($this->tags[$tag], (array)$atts, $thing);
} else {
return false;
}
Expand Down

0 comments on commit 0dc822a

Please sign in to comment.