if(class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('soo_wordfest')
->register('soo_wordfest_rules')
->register('soo_if_wordfest')
->register('soo_if_wordfestive')
;
}
function soo_wordfest( $atts ) {
global $soo_wordfest;
$integer_atts = array(
'word_count' => '',
'max_word_count' => '',
'min_word_length' => '',
'max_word_length' => '',
'not_word_length' => '',
);
$atts = lAtts(array(
'must_contain' => '',
'must_begin' => '',
'must_end' => '',
'anagram' => '',
) + $integer_atts, $atts);
foreach ( $atts as $name => $value ) {
if ( array_key_exists($name, $integer_atts) )
$value = abs(intval($value));
if ( is_string($value) )
$value = trim($value);
if ( $name == 'anagram' )
$value = preg_replace('/[^a-z\s\']/i', '', $value);
if ( $value )
$soo_wordfest[$name] = $value;
}
}
function soo_wordfest_rules( $atts ) {
global $soo_wordfest;
extract(lAtts(array(
'html_id' => '',
'class' => '',
'wraptag' => '',
'break' => '',
), $atts));
foreach ( $soo_wordfest as $rule => $value ) {
$display = soo_wordfest_gTxt($rule);
if ( is_string($value) ) {
$is_plural = str_word_count($value) > 1;
$pattern = '/\{([^}]+)\|([^}]+)\}/';
preg_match($pattern, $display, $match);
if ( count($match) ) {
$replacement = $is_plural ? $match[2] : $match[1];
$display = preg_replace($pattern, $replacement, $display);
}
$value = soo_wordfest_gTxt('lquo') . $value .
soo_wordfest_gTxt('rquo');
}
$display = str_replace('{x}', $value, $display);
if ( $value )
$out[] = $display;
}
return doWrap($out, $wraptag, $break, $class, '', '', '', $html_id);
}
function soo_if_wordfest( $atts, $thing ) {
global $soo_wordfest;
return parse(EvalElse($thing, is_array($soo_wordfest)));
}
function soo_if_wordfestive( $atts, $thing ) {
global $soo_wordfest, $thiscomment;
if ( is_array($soo_wordfest) )
extract($soo_wordfest);
else
return;
assert_comment();
$comment = trim(strtolower(strip_tags($thiscomment['message'])));
$pass = true;
if ( ! empty($must_contain) )
if ( $comment == str_ireplace($must_contain, '', $comment) )
$pass = false;
if ( $pass and ! empty($must_begin) )
if ( strtolower($must_begin) != substr($comment, 0, strlen($must_begin)) )
$pass = false;
if ( $pass and ! empty($must_end) )
if ( strtolower($must_end) != substr($comment, - strlen($must_end)) )
$pass = false;
if ( $pass and ! empty($word_count) )
if ( $word_count != str_word_count($comment) )
$pass = false;
if ( $pass and ! empty($max_word_count) )
if ( $max_word_count < str_word_count($comment) )
$pass = false;
if ( $pass and ( ! empty($min_word_length) or ! empty($max_word_length)
or ! empty($not_word_length) or ! empty($anagram) ) ) {
$words = preg_split('/[\W]+/', $comment);
foreach ( $words as $word )
$word_lengths[] = strlen($word);
$s_word_lengths = array_unique($word_lengths);
sort($s_word_lengths);
if ( $s_word_lengths[0] == 0 )
array_shift($s_word_lengths);
if ( ! empty($min_word_length) )
if ( $min_word_length > $s_word_lengths[0] )
$pass = false;
if ( $pass and ! empty($max_word_length) )
if ( $max_word_length < $s_word_lengths[count($s_word_lengths) - 1] )
$pass = false;
if ( $pass and ! empty($not_word_length) )
if ( in_array($not_word_length, $s_word_lengths) )
$pass = false;
if ( $pass and ! empty($anagram) ) {
$anagram = strtolower(preg_replace('/[^a-z]/i', '', $anagram));
$letters = str_split($anagram);
sort($letters);
$count = count($letters);
$words = preg_replace('/[^a-z]/i', '', $words);
$match = false;
while ( ! $match and count($words) ) {
$test_string = $words[0];
$c_match = strlen($test_string) == $count;
$i = 1;
while ( ! $c_match and $i < count($words) ) {
$test_string .= $words[$i];
$c_match = strlen($test_string) == $count;
if ( strlen($test_string) > $count )
$i = count($words);
$i++;
}
if ( $c_match ) {
$test_string = strtolower($test_string);
$test_array = str_split($test_string);
sort($test_array);
$match = ( $letters == $test_array and $anagram != $test_string );
}
array_shift($words);
}
$pass = $match;
}
}
return parse(EvalElse($thing, $pass));
}
define('SOO_WORDFEST_PREFIX', 'soo_fest');
global $soo_wordfest_strings;
$soo_wordfest_strings = array(
'must_contain' => 'contain the {word|phrase} {x}',
'must_begin' => 'begin with the {word|phrase} {x}',
'must_end' => 'end with the {word|phrase} {x}',
'word_count' => 'be exactly {x} words long',
'max_word_count' => 'not exceed {x} words',
'min_word_length' => 'only use words that are at least {x} letters long',
'max_word_length' => 'only use words that are not more than {x} letters long',
'not_word_length' => 'avoid using {x}-letter words',
'anagram' => 'contain an anagram of {x}',
'lquo' => '“',
'rquo' => '”',
);
register_callback('soo_wordfest_enumerate_strings', 'l10n.enumerate_strings');
function soo_wordfest_enumerate_strings( $event, $step = '', $pre = 0 ) {
global $soo_wordfest_strings;
$r = array(
'owner' => 'soo_wordfest',
'prefix' => SOO_WORDFEST_PREFIX,
'lang' => 'en-us',
'event' => 'public',
'strings' => $soo_wordfest_strings,
);
return $r;
}
function soo_wordfest_gTxt( $what , $args = array() ) {
global $textarray;
global $soo_wordfest_strings;
$key = SOO_WORDFEST_PREFIX . '-' . $what;
$key = strtolower($key);
if(isset($textarray[$key]))
$str = $textarray[$key];
else {
$key = strtolower($what);
if( isset( $soo_wordfest_strings[$key] ) )
$str = $soo_wordfest_strings[$key];
else
$str = $what;
}
if( !empty($args) )
$str = strtr( $str , $args );
return $str;
}