Skip to content
131 changes: 100 additions & 31 deletions textpattern/publish/taghandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
->register('article')
->register('article_custom')
->register('txp_die')
->register('txp_eval', 'evaluate')
->register('comments_help')
->register('comment_name_input')
->register('comment_email_input')
Expand Down Expand Up @@ -4932,57 +4933,37 @@ function file_download_description($atts)

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

function hide($atts, $thing=null)
function hide($atts = array(), $thing = null)
{
global $txp_parsed, $txp_else;

if (empty($atts)) {
if (empty($atts) || empty($thing)) {
return '';
}

extract(lAtts(array(
'test' => false,
'insert' => false,
'ignore' => false
'process' => null
), $atts));

$test = $test === true || empty($test) || is_numeric($test) ? array() : do_list_unique($test);
$insert = $insert ? do_list_unique($insert) : array();
$ignore = $ignore ? do_list_unique($ignore) : array();
global $txp_parsed, $txp_else;

$hash = sha1($thing);
$tag = $txp_parsed[$hash];

if (empty($tag)) {
return $thing;
if (empty($tag) || empty($txp_else[$hash]) || !($process = trim($process))) {
return '';
}

$nr = $txp_else[$hash][0] - 2;
$out = array($tag[0]);
$process = is_numeric($process) ? true : do_list_unique($process);

for ($isempty = true, $tags = array(), $n = 1; $n <= $nr; $n++) {
for ($n = 1; $n <= $nr; $n++) {
$t = $tag[$n];

if (in_array($t[1], $insert)) {
$out[] = $t;
$tags[] = $n;
} else {
$nextag = processTags($t[1], $t[2], $t[3]);
$out[] = $nextag;
$isempty &= trim($nextag) === '' || ($test ? !in_array($t[1], $test) : in_array($t[1], $ignore));
}

$out[] = $tag[++$n];
}

if (!$isempty) {
foreach ($tags as $n) {
$t = $out[$n];
$out[$n] = processTags($t[1], $t[2], $t[3]);
if ($process === true || in_array($t[1], $process)) {
processTags($t[1], $t[2], $t[3]);
}
}

return $isempty ? parse($thing, false) : implode('', $out);
return '';
}

// -------------------------------------------------------------
Expand Down Expand Up @@ -5055,3 +5036,91 @@ function if_variable($atts, $thing = null)

return isset($thing) ? parse($thing, $x) : $x;
}

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

function txp_eval($atts, $thing = null)
{
global $txp_parsed, $txp_else;
static $xpath = null, $functions = null;

extract(lAtts(array(
'query' => null,
'test' => null
), $atts));

if (!isset($query)) {
$x = true;
} elseif (!($query = trim($query))) {
$x = $query;
} elseif (class_exists('DOMDocument')) {
if (!isset($xpath)) {
$xpath = new DOMXpath(new DOMDocument);
$functions = do_list_unique(get_pref('txp_functions'));

if($functions) {
$xpath->registerNamespace('php', 'http://php.net/xpath');
$xpath->registerPHPFunctions($functions);
}

$functions = implode('|', $functions);
}

if ($functions) {
$query = preg_replace('/\b('.$functions.')\s*\(/', "php:function('$1',", $query);
}

$x = $xpath->evaluate($query);

if($x instanceOf DOMNodeList) {
$x = $x->length;
}
} else {
trigger_error('PHP DOM extension '.gTxt('gd_unavailable'));
return;
}

if (!isset($thing)) {
return $x;
} elseif (empty($x)) {
return parse($thing, false);
} elseif (!($test = trim($test))) {
return parse($thing, true);
}

$hash = sha1($thing);
$tag = $txp_parsed[$hash];

if (empty($tag) || empty($txp_else[$hash])) {
return $thing;
}

$test = is_numeric($test) ? false : do_list_unique($test);
$isempty = true;
$nr = $txp_else[$hash][0] - 2;
$out = array($tag[0]);

for ($tags = array(), $n = 1; $n <= $nr; $n++) {
$t = $tag[$n];

if ($test && !in_array($t[1], $test)) {
$out[] = $t;
$tags[] = $n;
} else {
$nextag = processTags($t[1], $t[2], $t[3]);
$out[] = $nextag;
$isempty &= trim($nextag) === '';
}

$out[] = $tag[++$n];
}

if (!$isempty) {
foreach ($tags as $n) {
$t = $out[$n];
$out[$n] = processTags($t[1], $t[2], $t[3]);
}
}

return $isempty ? parse($thing, false) : implode('', $out);
}
31 changes: 14 additions & 17 deletions textpattern/setup/pages/archive.txp
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,42 @@
<!-- ...or if you want to list all articles from all sections instead, then replace txp:article with txp:article_custom -->

<!-- add pagination links to foot of article listings if there are more articles available -->
<txp:hide test>
<txp:evaluate test>
<p class="paginator">
<txp:hide test="newer">
<txp:evaluate test="newer">
<a rel="prev" href="<txp:newer />">
<txp:text item="newer" />
</a>
</txp:hide>
</txp:evaluate>

<txp:hide test="older">
<txp:evaluate test="older">
<a rel="next" href="<txp:older />">
<txp:text item="older" />
</a>
</txp:hide>
</txp:evaluate>
</p>
</txp:hide>
</txp:evaluate>

<txp:else />

<txp:article />

<!-- add pagination links to foot of article if there are more articles available -->
<txp:hide test>
<txp:evaluate test>
<p class="paginator">
<txp:hide test="link_to_prev">
<txp:evaluate test="link_to_prev">
<a rel="prev" href="<txp:link_to_prev />" title="<txp:prev_title />">
<txp:text item="older" />
</a>
</txp:hide>
</txp:evaluate>

<txp:hide test="link_to_next">
<txp:evaluate test="link_to_next">
<a rel="next" href="<txp:link_to_next />" title="<txp:next_title />">
<txp:text item="newer" />
</a>
</txp:hide>
</txp:evaluate>
</p>
</txp:hide>
</txp:evaluate>

</txp:if_article_list>

Expand All @@ -138,10 +138,7 @@
</p>

<!-- if links exist, renders a links list -->
<txp:variable name="links" value='<txp:linklist limit="1" />' />

<txp:if_variable name="links" value="">
<txp:else />
<txp:evaluate test="linklist">
<section>
<h4>
<txp:text item="links" />
Expand All @@ -150,7 +147,7 @@
<!-- links by default to form: 'plainlinks.link.txp' unless you specify a different form -->
<txp:linklist wraptag="ul" break="li" />
</section>
</txp:if_variable>
</txp:evaluate>

</aside>

Expand Down
19 changes: 8 additions & 11 deletions textpattern/setup/pages/default.txp
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,21 @@
</txp:if_category>

<!-- add pagination links to foot of article listings/category listings/author listings if there are more articles available -->
<txp:hide test>
<txp:evaluate test>
<p class="paginator">
<txp:hide test="newer">
<txp:evaluate test="newer">
<a rel="prev" href="<txp:newer />">
<txp:text item="newer" />
</a>
</txp:hide>
</txp:evaluate>

<txp:hide test="older">
<txp:evaluate test="older">
<a rel="next" href="<txp:older />">
<txp:text item="older" />
</a>
</txp:hide>
</txp:evaluate>
</p>
</txp:hide>
</txp:evaluate>

</txp:if_search>

Expand All @@ -176,10 +176,7 @@
</p>

<!-- if links exist, renders a links list -->
<txp:variable name="links" value='<txp:linklist limit="1" />' />

<txp:if_variable name="links" value="">
<txp:else />
<txp:evaluate test="linklist">
<section>
<h4>
<txp:text item="links" />
Expand All @@ -188,7 +185,7 @@
<!-- links by default to form: 'plainlinks.link.txp' unless you specify a different form -->
<txp:linklist wraptag="ul" break="li" />
</section>
</txp:if_variable>
</txp:evaluate>

</aside>

Expand Down