Wordfest Page 3 of 3

soo_wordfest Source code

  1 : if(class_exists('\Textpattern\Tag\Registry')) {
  2 :     Txp::get('\Textpattern\Tag\Registry')
  3 :         ->register('soo_wordfest')
  4 :         ->register('soo_wordfest_rules')
  5 :         ->register('soo_if_wordfest')
  6 :         ->register('soo_if_wordfestive')
  7 :         ;
  8 : }
  9 : 
 10 : function soo_wordfest$atts ) {
 11 :     global $soo_wordfest;
 12 :     $integer_atts = array(
 13 :         'word_count'        => '',
 14 :         'max_word_count'    => '',
 15 :         'min_word_length'   => '',
 16 :         'max_word_length'   => '',
 17 :         'not_word_length'   => '',
 18 :     );
 19 :     $atts lAtts(array(
 20 :         'must_contain'      => '',
 21 :         'must_begin'        => '',
 22 :         'must_end'          => '',
 23 :         'anagram'           => '',
 24 :     ) + $integer_atts$atts);
 25 :     foreach ( $atts as $name => $value ) {
 26 :         if ( array_key_exists($name$integer_atts) )
 27 :             $value abs(intval($value));
 28 :         if ( is_string($value) )
 29 :             $value trim($value);
 30 :         if ( $name == 'anagram' )
 31 :             $value preg_replace('/[^a-z\s\']/i'''$value);
 32 :         if ( $value )
 33 :             $soo_wordfest[$name] = $value;
 34 :     }
 35 : }
 36 : 
 37 : function soo_wordfest_rules$atts ) {
 38 :     global $soo_wordfest;
 39 :     extract(lAtts(array(
 40 :         'html_id'       =>  '',
 41 :         'class'         =>  '',
 42 :         'wraptag'       =>  '',
 43 :         'break'         =>  '',
 44 :     ), $atts));
 45 : 
 46 :     foreach ( $soo_wordfest as $rule => $value ) {
 47 :         $display soo_wordfest_gTxt($rule);
 48 :         if ( is_string($value) ) {
 49 :             $is_plural str_word_count($value) > 1;
 50 :             $pattern '/\{([^}]+)\|([^}]+)\}/';
 51 :             preg_match($pattern$display$match);
 52 :             if ( count($match) ) {
 53 :                 $replacement $is_plural $match[2] : $match[1];
 54 :                 $display preg_replace($pattern$replacement$display);
 55 :             }
 56 :             $value soo_wordfest_gTxt('lquo') . $value .
 57 :                 soo_wordfest_gTxt('rquo');
 58 :         }
 59 :         $display str_replace('{x}'$value$display);
 60 :         if ( $value )
 61 :             $out[] = $display;
 62 :     }
 63 :     return doWrap($out$wraptag$break$class''''''$html_id);
 64 : }
 65 : 
 66 : function soo_if_wordfest$atts$thing ) {
 67 :     global $soo_wordfest;
 68 :     return parse(EvalElse($thingis_array($soo_wordfest)));
 69 : }
 70 : 
 71 : function soo_if_wordfestive$atts$thing ) {
 72 : 
 73 :     global $soo_wordfest$thiscomment;
 74 : 
 75 :     if ( is_array($soo_wordfest) )
 76 :         extract($soo_wordfest);
 77 :     else
 78 :         return;
 79 : 
 80 :     assert_comment();
 81 :     $comment trim(strtolower(strip_tags($thiscomment['message'])));
 82 : 
 83 :     $pass true;
 84 : 
 85 :     if ( ! empty($must_contain) )
 86 :         if ( $comment == str_ireplace($must_contain''$comment) )
 87 :             $pass false;
 88 : 
 89 :     if ( $pass and ! empty($must_begin) )
 90 :         if ( strtolower($must_begin) != substr($comment0strlen($must_begin)) )
 91 :             $pass false;
 92 : 
 93 :     if ( $pass and ! empty($must_end) )
 94 :         if ( strtolower($must_end) != substr($comment, - strlen($must_end)) )
 95 :             $pass false;
 96 : 
 97 :     if ( $pass and ! empty($word_count) )
 98 :         if ( $word_count != str_word_count($comment) )
 99 :             $pass false;
100 : 
101 :     if ( $pass and ! empty($max_word_count) )
102 :         if ( $max_word_count str_word_count($comment) )
103 :             $pass false;
104 : 
105 :     if ( $pass and ( ! empty($min_word_length) or ! empty($max_word_length)
106 :         or ! empty($not_word_length) or ! empty($anagram) ) ) {
107 : 
108 :         $words preg_split('/[\W]+/'$comment);
109 :         foreach ( $words as $word )
110 :             $word_lengths[] = strlen($word);
111 :         $s_word_lengths array_unique($word_lengths);
112 :         sort($s_word_lengths);
113 :         if ( $s_word_lengths[0] == )
114 :             array_shift($s_word_lengths);
115 : 
116 :         if ( ! empty($min_word_length) )
117 :             if ( $min_word_length $s_word_lengths[0] )
118 :                 $pass false;
119 : 
120 :         if ( $pass and ! empty($max_word_length) )
121 :             if ( $max_word_length $s_word_lengths[count($s_word_lengths) - 1] )
122 :                 $pass false;
123 : 
124 :         if ( $pass and ! empty($not_word_length) )
125 :             if ( in_array($not_word_length$s_word_lengths) )
126 :                 $pass false;
127 : 
128 :         if ( $pass and ! empty($anagram) ) {
129 :             $anagram strtolower(preg_replace('/[^a-z]/i'''$anagram));
130 :             $letters str_split($anagram);
131 :             sort($letters);
132 :             $count count($letters);
133 :             $words preg_replace('/[^a-z]/i'''$words);
134 :             $match false;
135 :             while ( ! $match and count($words) ) {
136 :                 $test_string $words[0];
137 :                 $c_match strlen($test_string) == $count;
138 :                 $i 1;
139 :                 while ( ! $c_match and $i count($words) ) {
140 :                     $test_string .= $words[$i];
141 :                     $c_match strlen($test_string) == $count;
142 :                     if ( strlen($test_string) > $count )
143 :                         $i count($words);
144 :                     $i++;
145 :                 }
146 :                 if ( $c_match ) {
147 :                     $test_string strtolower($test_string);
148 :                     $test_array str_split($test_string);
149 :                     sort($test_array);
150 :                     $match = ( $letters == $test_array and $anagram != $test_string );
151 :                 }
152 :                 array_shift($words);
153 :             }
154 :             $pass $match;
155 :         }
156 :     }
157 : 
158 :     return parse(EvalElse($thing$pass));
159 : }
160 : 
161 :   //---------------------------------------------------------------------//
162 :  //                         MLP Pack definitions                        //
163 : //---------------------------------------------------------------------//
164 : 
165 : define('SOO_WORDFEST_PREFIX''soo_fest');
166 : global $soo_wordfest_strings;
167 : $soo_wordfest_strings = array(
168 :     'must_contain'      => 'contain the {word|phrase} {x}',
169 :     'must_begin'        => 'begin with the {word|phrase} {x}',
170 :     'must_end'          => 'end with the {word|phrase} {x}',
171 :     'word_count'        => 'be exactly {x} words long',
172 :     'max_word_count'    => 'not exceed {x} words',
173 :     'min_word_length'   => 'only use words that are at least {x} letters long',
174 :     'max_word_length'   => 'only use words that are not more than {x} letters long',
175 :     'not_word_length'   => 'avoid using {x}-letter words',
176 :     'anagram'           => 'contain an anagram of {x}',
177 :     'lquo'              => '“',
178 :     'rquo'              => '”',
179 : );
180 : 
181 : register_callback('soo_wordfest_enumerate_strings''l10n.enumerate_strings');
182 : 
183 : function soo_wordfest_enumerate_strings$event$step ''$pre ) {
184 :     global $soo_wordfest_strings;
185 :     $r = array(
186 :         'owner'     => 'soo_wordfest',
187 :         'prefix'    => SOO_WORDFEST_PREFIX,
188 :         'lang'      => 'en-us',
189 :         'event'     => 'public',
190 :         'strings'   => $soo_wordfest_strings,
191 :                 );
192 :     return $r;
193 : }
194 : 
195 : function soo_wordfest_gTxt$what $args = array() ) {
196 :     global $textarray;
197 :     global $soo_wordfest_strings;
198 : 
199 :     $key SOO_WORDFEST_PREFIX '-' $what;
200 :     $key strtolower($key);
201 : 
202 :     if(isset($textarray[$key]))
203 :         $str $textarray[$key];
204 : 
205 :     else {
206 :         $key strtolower($what);
207 : 
208 :         if( isset( $soo_wordfest_strings[$key] ) )
209 :             $str $soo_wordfest_strings[$key];
210 :         else
211 :             $str $what;
212 :     }
213 : 
214 :     if( !empty($args) )
215 :         $str strtr$str $args );
216 : 
217 :     return $str;
218 : }