source: trunk/www.guidonia.net/wp/wp-content/plugins/jeromes-keywords.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 25.3 KB
Line 
1<?php
2/*
3Plugin Name: Jerome's Keywords
4Plugin URI: http://vapourtrails.ca/wp-keywords
5Version: 1.9
6Description: Allows keywords to be associated with each post. These keywords can be used for page meta tags, included in posts for site searching or linked like Technorati tags.
7Author: Jerome Lavigne
8Author URI: http://vapourtrails.ca
9*/
10
11/* Copyright 2005 Jerome Lavigne (email : darkcanuck@vapourtrails.ca)
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26*/
27
28/* Credits:
29 Special thanks also to Dave Metzener, Mark Eckenrode, Dan Taarin, N. Godbout, "theanomaly", "oso", Wayne @ AcmeTech, Will Luke,
30 Gyu-In Lee, Denis de Bernardy and the many others who have provided feedback, spotted bugs, and suggested improvements.
31 WP2.0 compatibility fixes suggested by Horst Gutmann.
32*/
33
34/* ChangeLog:
35
3629-Dec-2005: Version 1.9
37 - Added support for WordPress 2.0 (which unnecessarily broke things)
38
3923-Jun-2005: Version 1.8
40 - Fixed bug in get_the_post_keytag() that did not return a valid category link.
41
4220-Jun-2005: Version 1.7
43 - Fixed uksort bug that appeared on the edit page when there are no keywords in the database.
44 - Fixed divide-by-zero error in all_keywords() scaling routine.
45 - Local keyword search now includes pages
46 - Added ability to create flickr- and del.icio.us-safe keyword links for use in cosmos/top-X keywords lists.
47
489-May-2005: Version 1.6
49 - Tag cosmos now uses a natural case-insensitive sort.
50 - Added a very simple keyword suggestion feature when editing posts (suggestions refresh after every save)
51
5216-Apr-2005: Version 1.5
53 - Added functions all_keywords() and get_all_keywords() for creating a "tag cosmos".
54 - Added functions top_keywords() and get_top_keywords() to return a "Top X Tags" list.
55 - Fixed slashes bug in get_the_search_keytag().
56 - Fixed get_the_keywords() routine:
57 - no longer lists duplicate keywords
58 - only relevant categories are output, except on the home page where all are shown (can be overridden with parameters)
59 - Added filter for adding keywords to pages.
60 - Added fix for conflicting JOINs on wp_postmeta (or how to make friends with other plugins) and removed mini-posts "fix"
61
6213-Mar-2005: Version 1.4
63 - Added ability to automatically generate .htaccess rewrite rules for keyword searches.
64 - Can be turned off with new KEYWORDS_REWRITERULES flag.
65 - Necessary for sites that use /index.php/*%blah% style permalinks
66 - Thanks to Dave Metzener for finding the original bug and beta testing the fix.
67 - Added formatting parameters to the_keywords() and get_the_keywords() to allow more control over the output.
68 - Fixed XHTML validation bug: added a space prior to title attribute in keyword links.
69 - Fixed keyword link encoding for links that include '/' (now left as-is rather than encoded)
70 - Temporary fix to prevent conflicts with mini-posts plugin: removes mini-post's filters when a tag search is performed.
71
721-Mar-2005: Version 1.3
73 - Added ability to do site keyword searches. This now the default keyword link behaviour.
74 - Keyword search can also use its own template file.
75 - If including categories, local links will return that category (not the keyword search).
76 - Added filter for Atom feed content if not sending rss summaries only.
77
7827-Feb-2005: Version 1.2
79 - Fixed search URL for sites not using permalinks (this is automatically detected)
80 - If not using permalinks then the Atom feed will contain Technorati links instead of local search link (local search can't be parsed by Technorati)
81
8226-Feb-2005: Version 1.1
83 - added ability to suppress link title if value passed is an empty string (used for Atom feed)
84 - updated keywords_appendtags() to suppress link title.
85
8625-Feb-2005: Version 1.0 publicly released
87
88*/
89
90/* *****INSTRUCTIONS*****
91
92Entering Keywords - simply type all of your keywords into the keywords field when creating/editing posts. Keywords
93 should be separated by commas and can include spaces (key phrases).
94
95Template Tags - you can use the following php template tags to insert the keywords into your template
96
97 the_keywords() - can be used outside the loop
98 Outputs a comma-separated list of all categories & keywords on the current page. You can use this
99 to add a keyword meta tag to your page's title block:
100 <meta name="keywords" content="<?php the_keywords(); ?>" />
101
102 This function can take three optional parameters:
103 before (default = blank) - text/html to insert before each keyword
104 after (default = blank) - text/html to insert after each keyword
105 separator (default = ",") - text/html to insert between keywords
106 get_the_keywords() is a non-echoing version
107
108 the_post_keywords() - must be used inside the loop
109 Outputs a comma-separated list of the keywords for the current post. This function can take one optional parameters:
110 include_cats (default=false) - if true, post categories are included in the list.
111 get_the_post_keywords() is a non-echoing version.
112
113 the_post_keytags() - must be used inside the loop
114 Outputs the keywords for the current post as a series of links. By default these link a query for other posts with matching
115 keywords (can also link to the WordPress search function or to Technorati's page for that tag)
116 This function can take three optional parameters:
117 include_cats (default=false) - if true, post categories are included in the list.
118 local_search (default="tag") - if false or "technorati", the links will be to Technorati's tag page for the keyword instead,
119 if "search", the links will be to the local Wordpress search function.
120 link_title (default="") - alternate link title text to use, e.g. "My link title for" (tag name will be added at the end)
121
122 An example from my site:
123 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
124 [...]
125 <div class="post">
126 [...]
127 <div class="subtags">
128 >>> Tags: <?php the_post_keytags(); ?>
129 </div>
130 [...]
131 <?php endwhile; else: ?>
132
133 get_the_post_keytags() is a non-echoing version.
134
135 is_keyword() - can be used outside the loop
136 Returns true if the current view is a keyword/tag search
137
138 the_search_keytag() - can be used outside the loop
139 Outputs the keyword/tag used for the search
140 get_the_search_keytag() is a non-echoing version
141
142
143Rewrite Rules - The plugin can generate new tag search rewrite rules automatically. You need to
144 re-save your permalinks settings (Options -> Permalinks) for this to occur.
145 If your .htaccess file cannot be written to by WordPress, add the following to your
146 .htaccess file to use the tag search feature, preferably below the "# END WordPress" line:
147
148RewriteRule ^tag/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?tag=$1&feed=$2 [QSA,L]
149RewriteRule ^tag/(.+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?tag=$1&feed=$2 [QSA,L]
150RewriteRule ^tag/(.+)/page/?([0-9]{1,})/?$ /index.php?tag=$1&paged=$2 [QSA,L]
151RewriteRule ^tag/(.+)/?$ /index.php?tag=$1 [QSA,L]
152
153*/
154
155
156/* You can change these constants if you wish for further customization*/
157define('KEYWORDS_META', 'keywords'); // post meta key used in the wp database
158define('KEYWORDS_TECHNORATI', 'http://technorati.com/tag'); // Technorati link to use if local search is false
159define('KEYWORDS_ATOMTAGSON', '1'); // flag to add tags to Atom feed (required for Technorati)
160define('KEYWORDS_QUERYVAR', 'tag'); // get/post variable name for querying tag/keyword from WP
161define('KEYWORDS_TAGURL', 'tag'); // URL to use when querying tags
162define('KEYWORDS_TEMPLATE', 'keywords.php'); // template file to use for displaying tag queries
163define('KEYWORDS_SEARCHURL', 'search'); // local search URL (from mod_rewrite rules)
164define('KEYWORDS_REWRITERULES', '1'); // flag to determine if plugin can change WP rewrite rules
165define('KEYWORDS_SUGGESTED', '8'); // maximum number of keywords suggested
166
167/* WP 2.0 doesn't initialize the rewrite object before plugins are loaded anymore
168 so these constants are set later
169*/
170function keywords_init() {
171 global $wp_rewrite;
172
173 /* Shouldn't need to change this - can set to 0 if you want to force permalinks off */
174 if (isset($wp_rewrite) && $wp_rewrite->using_permalinks()) {
175 define('KEYWORDS_REWRITEON', '1'); // nice permalinks, yes please!
176 define('KEYWORDS_LINKBASE', $wp_rewrite->root); // set to "index.php/" if using that style
177 } else {
178 define('KEYWORDS_REWRITEON', '0'); // old school links
179 define('KEYWORDS_LINKBASE', ''); // don't need this
180 }
181
182 /* generate rewrite rules for above queries */
183 if (KEYWORDS_REWRITEON && KEYWORDS_REWRITERULES)
184 add_filter('search_rewrite_rules', 'keywords_createRewriteRules');
185}
186add_action('init','keywords_init');
187
188/* use in the loop*/
189function get_the_post_keywords($include_cats=true) {
190 $keywords = '';
191
192 if ($include_cats) {
193 $categories = get_the_category();
194 foreach($categories as $category) {
195 if (!empty($keywords))
196 $keywords .= ",";
197 $keywords .= $category->cat_name;
198 }
199 }
200
201 $post_keywords = get_post_custom_values(KEYWORDS_META);
202 if (is_array($post_keywords)) {
203 foreach($post_keywords as $post_keys) {
204 if (!empty($post_keys))
205 $keywords .= ",";
206 $keywords .= $post_keys;
207 }
208 }
209 return( $keywords );
210}
211
212/* use in the loop*/
213function the_post_keywords($include_cats=true) {
214 echo get_the_post_keywords($include_cats);
215}
216
217/* use in the loop*/
218function get_the_post_keytags($include_cats=false, $localsearch="tag", $linktitle=false) {
219 // determine link mode
220 $linkmode = strtolower(trim($localsearch));
221 switch ($linkmode) {
222 case '':
223 case 'technorati':
224 $linkmode = 'technorati';
225 break;
226 case 'search':
227 $linkmode = 'search';
228 break;
229 //case 'tag':
230 //case 'keyword':
231 default:
232 $linkmode = 'tag';
233 break;
234 }
235
236 $output = "";
237 if ($linktitle === false)
238 $linktitle = ($linkmode == 'technorati') ? "Technorati tag page for" : "Search site for";
239
240 // do categories separately to get category links instead of tag links
241 if ($include_cats) {
242 $categories = get_the_category();
243 foreach($categories as $category) {
244 $keyword = $category->cat_name;
245 if ($linkmode == 'technorati')
246 $taglink = KEYWORDS_TECHNORATI . "/" . jkeywords_localLink($keyword);
247 else
248 $taglink = get_category_link($category->cat_ID);
249 $tagtitle = empty($linktitle) ? "" : " title=\"$linktitle $keyword\"";
250
251 if (!empty($output))
252 $output .= ", ";
253 $output .= "<a href=\"$taglink\" rel=\"tag\"$tagtitle>$keyword</a>";
254 }
255 }
256
257 $post_keywords = get_post_custom_values(KEYWORDS_META);
258 if (is_array($post_keywords)) {
259 $keywordlist = array();
260 foreach($post_keywords as $post_keys)
261 $keywordlist = array_merge($keywordlist, explode(",", $post_keys));
262
263 foreach($keywordlist as $keyword) {
264 $keyword = trim($keyword);
265 if (!empty($keyword)) {
266 switch ($linkmode) {
267 case 'tag':
268 if (KEYWORDS_REWRITEON)
269 $taglink = get_settings('home') . '/' . KEYWORDS_LINKBASE . KEYWORDS_TAGURL .
270 '/' . jkeywords_localLink($keyword);
271 else
272 $taglink = get_settings('home') . "/?" . KEYWORDS_TAGURL . "=" . urlencode($keyword);
273 break;
274 case 'technorati':
275 $taglink = KEYWORDS_TECHNORATI . "/" . jkeywords_localLink($keyword);
276 break;
277 case 'search':
278 if (KEYWORDS_REWRITEON)
279 $taglink = get_settings('home') . '/' . KEYWORDS_LINKBASE . KEYWORDS_SEARCHURL .
280 '/' . jkeywords_localLink($keyword);
281 else
282 $taglink = get_settings('home') . '/?s=' . urlencode($keyword) . '&submit=Search';
283 break;
284 }
285 $tagtitle = empty($linktitle) ? "" : " title=\"$linktitle $keyword\"";
286
287 if (!empty($output))
288 $output .= ", ";
289 $output .= "<a href=\"$taglink\" rel=\"tag\"$tagtitle>$keyword</a>";
290 }
291 }
292 }
293 return($output);
294}
295
296/* use in the loop*/
297function the_post_keytags($include_cats=false, $localsearch=true, $linktitle=false) {
298 $taglist = get_the_post_keytags($include_cats, $localsearch, $linktitle);
299
300 if (empty($taglist))
301 echo "none";
302 else
303 echo $taglist;
304}
305
306/* works outside the loop*/
307function get_the_keywords($before='', $after='', $separator=',', $include_cats='default') {
308 global $cache_categories, $category_cache, $post_meta_cache;
309
310 $keywords = "";
311
312 if ($include_cats) {
313 if ( isset($cache_categories) && ( ($include_cats == 'all') ||
314 (($include_cats == 'default') && is_home()) ) ) {
315 foreach($cache_categories as $category)
316 $keywordarray[$category->cat_name] += 1;
317 } elseif (isset($category_cache)) {
318 foreach($category_cache as $post_category) {
319 foreach($post_category as $category)
320 $keywordarray[$category->cat_name] += 1;
321 }
322 }
323 }
324
325 if (isset($post_meta_cache)) {
326 foreach($post_meta_cache as $post_meta) {
327 if (is_array($post_meta[KEYWORDS_META])) {
328 foreach($post_meta[KEYWORDS_META] as $post_keys) {
329 $keywordlist = explode(",", $post_keys);
330 foreach($keywordlist as $keyvalue)
331 if (!empty($keyvalue))
332 $keywordarray[$keyvalue] += 1;
333 }
334 }
335 }
336 }
337
338 if (is_array($keywordarray)) {
339 foreach($keywordarray as $key => $count) {
340 if (!empty($keywords))
341 $keywords .= $separator;
342 $keywords .= $before . $key . $after;
343 }
344 }
345
346 return ($keywords);
347}
348
349/* works outside the loop */
350function the_keywords($before='', $after='', $separator=',') {
351 echo get_the_keywords($before, $after, $separator);
352}
353
354function is_keyword() {
355 global $wp_version;
356 $keyword = ( isset($wp_version) && ($wp_version >= 2.0) ) ?
357 get_query_var(KEYWORDS_QUERYVAR) :
358 $GLOBALS[KEYWORDS_QUERYVAR];
359 if (!is_null($keyword) && ($keyword != ''))
360 return true;
361 else
362 return false;
363}
364
365function get_the_search_keytag() {
366 $keyword = ( isset($wp_version) && ($wp_version >= 2.0) ) ?
367 get_query_var(KEYWORDS_QUERYVAR) :
368 $GLOBALS[KEYWORDS_QUERYVAR];
369 $searchtag = stripslashes($keyword);
370 return(get_magic_quotes_gpc() ? stripslashes($searchtag) : $searchtag);
371}
372
373function the_search_keytag() {
374 echo get_the_search_keytag();
375}
376
377
378/***** Tag cosmos functions *****/
379function get_all_keywords($include_cats = false) {
380 global $wpdb, $cache_categories;
381
382 if ($include_cats && isset($cache_categories)) {
383 $catkeys = $wpdb->get_results("SELECT p2c.category_id AS cat_id, COUNT(p2c.rel_id) AS cat_count
384 FROM $wpdb->post2cat p2c, $wpdb->posts posts
385 WHERE posts.ID = p2c.post_id
386 AND posts.post_status IN('publish', 'static')
387 GROUP BY p2c.category_id");
388 if (is_array($catkeys)) {
389 foreach($catkeys as $category)
390 $keywordarray[ $cache_categories[$category->cat_id]->cat_name .
391 '::Category::' . $category->cat_id ] += $category->cat_count;
392 }
393 }
394
395 $metakeys = $wpdb->get_results("SELECT meta.meta_id, meta.meta_value
396 FROM $wpdb->posts posts, $wpdb->postmeta meta
397 WHERE posts.ID = meta.post_id
398 AND posts.post_status IN('publish', 'static')
399 AND meta.meta_key = '" . KEYWORDS_META . "'");
400 if (is_array($metakeys)) {
401 foreach($metakeys as $post_meta) {
402 if (!empty($post_meta->meta_value)) {
403 $post_keys = explode(',', $post_meta->meta_value);
404
405 foreach($post_keys as $keyword) {
406 $keyword = trim($keyword);
407 if (!empty($keyword))
408 $keywordarray[ $keyword ] += 1;
409 }
410 }
411 }
412 }
413
414 if(is_array($keywordarray))
415 uksort($keywordarray, 'strnatcasecmp');
416
417 return($keywordarray);
418}
419
420function jkeywords_localLink($keyword) {
421 return str_replace('%2F', '/', urlencode($keyword));
422}
423
424function jkeywords_flickrLink($keyword) {
425 return urlencode(preg_replace('/[^a-zA-Z0-9]/', '', strtolower($keyword)));
426}
427
428function jkeywords_deliciousLink($keyword) {
429 $del = preg_replace('/\s/', '', $keyword);
430 if (strstr($del, '+'))
431 $del = '"' . $del . '"';
432 return str_replace('%2F', '/', rawurlencode($del));
433}
434
435function all_keywords($element = '<li class="cosmos keyword%count%"><a href="/tag/%keylink%">%keyword%</a></li>',
436 $element_cat = '', $min_scale = 1, $max_scale = false, $min_include = 0) {
437
438 $include_cats = !empty($element_cat);
439
440 $allkeys = get_all_keywords($include_cats);
441
442 $keywords = '';
443 if (is_array($allkeys)) {
444
445 // scaling
446 if ($max_scale !== false) {
447 $pre_scale = min($allkeys);
448 $pre_scale = ($pre_scale < $min_include) ? $min_include : $pre_scale;
449 $spread = (max($allkeys) - $pre_scale);
450 $spread = ($spread > 0 ? $spread : 1);
451 $scale_factor = ($max_scale - $min_scale) / $spread;
452 }
453
454 foreach($allkeys as $key => $count) {
455 if ($count >= $min_include) {
456 if ($max_scale !== false)
457 $keycount = (int) (($count - $pre_scale) * $scale_factor + $min_scale);
458 else
459 $keycount = $count + $min_scale - 1;
460
461 // need to do category stuff first so that we can decide between $element and $element_cat at the outset
462 if ($include_cats && (strstr($key, '::Category::'))) {
463 $keycat = explode('::Category::', $key);
464 $key = $keycat[0];
465 $keytemp = str_replace('%keylink%', get_category_link((int)$keycat[1]), $element_cat);
466 $keytemp = str_replace('%flickr%', get_category_link((int)$keycat[1]), $keytemp);
467 $keytemp = str_replace('%delicious%', get_category_link((int)$keycat[1]), $keytemp);
468 } else {
469 $keytemp = str_replace('%keylink%', jkeywords_localLink($key), $element);
470 $keytemp = str_replace('%flickr%', jkeywords_flickrLink($key), $keytemp);
471 $keytemp = str_replace('%delicious%', jkeywords_deliciousLink($key), $keytemp);
472 }
473 $keytemp = str_replace('%count%', $keycount, $keytemp);
474 if (strstr($keytemp, '%em%')) {
475 $keytemp = str_replace('%em%', str_repeat('<em>', $keycount), $keytemp);
476 $keytemp = str_replace('%/em%', str_repeat('</em>', $keycount), $keytemp);
477 }
478 $keytemp = str_replace('%keyword%', str_replace(' ', '&nbsp;', $key), $keytemp);
479 $keywords .= $keytemp . ' ';
480 }
481 }
482 }
483 echo $keywords;
484}
485
486
487/***** Top keywords/tags functions *****/
488function get_top_keywords($number = false, $include_cats = false, $min_include = 0) {
489
490 $allkeys = get_all_keywords($include_cats);
491
492 $topkeys = array();
493 if (is_array($allkeys)) {
494
495 arsort($allkeys);
496 if (($number <= 0) && ($min_include <= 1))
497 return($allkeys);
498
499 $topcount = 0;
500 foreach ($allkeys as $key => $count) {
501 if ($count >= $min_include) {
502 $topkeys[$key] = $count;
503 $topcount++;
504 }
505 if (($number > 0) && ($topcount >= $number))
506 break;
507 }
508 }
509 return($topkeys);
510}
511
512function top_keywords($number = false, $element='<li><a href="/tag/%keylink%">%keyword%</a></li>',
513 $element_cat = '', $min_include = 0) {
514
515 $include_cats = !empty($element_cat);
516
517 $topkeys = get_top_keywords($number, $include_cats, $min_include);
518
519 $keywords = '';
520 if (is_array($topkeys)) {
521 foreach($topkeys as $key => $count) {
522 // need to do category stuff first so that we can decide between $element and $element_cat at the outset
523 if ($include_cats && (strstr($key, '::Category::'))) {
524 $keycat = explode('::Category::', $key);
525 $key = $keycat[0];
526 $keytemp = str_replace('%keylink%', get_category_link((int)$keycat[1]), $element_cat);
527 $keytemp = str_replace('%flickr%', get_category_link((int)$keycat[1]), $keytemp);
528 $keytemp = str_replace('%delicious%', get_category_link((int)$keycat[1]), $keytemp);
529 } else {
530 $keytemp = str_replace('%keylink%', jkeywords_localLink($key), $element);
531 $keytemp = str_replace('%flickr%', jkeywords_flickrLink($key), $keytemp);
532 $keytemp = str_replace('%delicious%', jkeywords_deliciousLink($key), $keytemp);
533 }
534
535 $keytemp = str_replace('%count%', $count, $keytemp);
536 if (strstr($keytemp, '%em%')) {
537 $keytemp = str_replace('%em%', str_repeat('<em>', $keycount), $keytemp);
538 $keytemp = str_replace('%/em%', str_repeat('</em>', $keycount), $keytemp);
539 }
540 $keytemp = str_replace('%keyword%', str_replace(' ', '&nbsp;', $key), $keytemp);
541 $keywords .= $keytemp . ' ';
542 }
543 }
544 echo $keywords;
545}
546
547
548/***** Add actions *****/
549
550/* editing */
551add_filter('simple_edit_form', 'keywords_edit_form');
552add_filter('edit_form_advanced', 'keywords_edit_form');
553add_filter('edit_page_form', 'keywords_edit_form');
554add_action('edit_post', 'keywords_update');
555add_action('publish_post', 'keywords_update');
556add_action('save_post', 'keywords_update');
557
558/* for keyword/tag queries */
559add_filter('query_vars', 'keywords_addQueryVar');
560add_action('parse_query', 'keywords_parseQuery');
561
562/* Atom feed */
563if (KEYWORDS_ATOMTAGSON) {
564 add_filter('the_excerpt_rss', 'keywords_appendTags');
565 if (!get_settings('rss_use_excerpt'))
566 add_filter('the_content', 'keywords_appendTags');
567}
568
569/***** Callback functions *****/
570function keywords_edit_form() {
571 global $post, $postdata, $content;
572
573 $id = isset($post) ? $post->ID : $postdata->ID;
574 $post_keywords = get_post_meta($id, KEYWORDS_META, true);
575
576 echo "
577 <fieldset id=\"postkeywords\">
578 <legend>Keywords</legend>
579 <div>
580 <textarea rows=\"1\" cols=\"40\" name=\"keywords_list\" tabindex=\"4\" id=\"keywords_list\" style=\"margin-left: 1%; width: 97%; height: 1.8em;\">$post_keywords</textarea>
581 ";
582
583 if (KEYWORDS_SUGGESTED > 0) {
584 $top_keywords = get_top_keywords();
585
586 $suggested = array();
587
588 foreach($top_keywords as $keyword=>$keycount) {
589 if (stristr($content, $keyword)) {
590 $suggested[] = $keyword;
591 if (count($suggested) >= KEYWORDS_SUGGESTED)
592 break;
593 }
594 }
595 if (count($suggested) < KEYWORDS_SUGGESTED) {
596 foreach($top_keywords as $keyword=>$keycount) {
597 if (!in_array($keyword, $suggested)) {
598 $suggested[] = $keyword;
599 if (count($suggested) >= KEYWORDS_SUGGESTED)
600 break;
601 }
602 }
603 }
604 $suggested_keys = implode(', ', $suggested);
605 echo " <br /> <div style=\"font-size: 80%; margin-left: 1%;\">Suggested Keywords: <em>$suggested_keys</em> </div>";
606 }
607
608 echo "
609 </div>
610 </fieldset>
611 ";
612}
613
614function keywords_update($id) {
615
616 // remove old value
617 delete_post_meta($id, KEYWORDS_META);
618
619 // clean up keywords list & save
620 $keyword_list = "";
621 $post_keywords = explode(",", $_REQUEST['keywords_list']);
622 foreach($post_keywords as $keyword) {
623 if ( !empty($keyword ) ) {
624 if ( !empty($keyword_list) )
625 $keyword_list .= ",";
626 $keyword_list .= trim($keyword);
627 }
628 }
629
630 if (!empty($keyword_list) )
631 add_post_meta($id, KEYWORDS_META, $keyword_list);
632}
633
634function keywords_appendTags(&$text) {
635 global $doing_rss, $feed;
636
637 if ( (!$doing_rss) || ($feed != 'atom') )
638 return($text);
639
640 $local = KEYWORDS_REWRITEON ? "tag" : "technorati";
641
642 $taglist = get_the_post_keytags(true, $local, "");
643 if (empty($taglist))
644 return($text);
645 else
646 return($text . " \n Tags: " . $taglist);
647}
648
649function keywords_addQueryVar($wpvar_array) {
650 $wpvar_array[] = KEYWORDS_QUERYVAR;
651 return($wpvar_array);
652}
653
654function keywords_parseQuery() {
655 // if this is a keyword query, then reset other is_x flags and add query filters
656 if (is_keyword()) {
657 global $wp_query;
658 $wp_query->is_single = false;
659 $wp_query->is_page = false;
660 $wp_query->is_archive = false;
661 $wp_query->is_search = false;
662 $wp_query->is_home = false;
663
664 add_filter('posts_where', 'keywords_postsWhere');
665 add_filter('posts_join', 'keywords_postsJoin');
666 add_action('template_redirect', 'keywords_includeTemplate');
667 }
668}
669
670function keywords_postsWhere($where) {
671 global $wp_version;
672 $keyword = ( isset($wp_version) && ($wp_version >= 2.0) ) ?
673 get_query_var(KEYWORDS_QUERYVAR) :
674 $GLOBALS[KEYWORDS_QUERYVAR];
675
676 $where .= " AND jkeywords_meta.meta_key = '" . KEYWORDS_META . "' ";
677 $where .= " AND jkeywords_meta.meta_value LIKE '%" . $keyword . "%' ";
678
679 // include pages in search (from jeromes-search.php)
680 $where = str_replace(' AND (post_status = "publish"', ' AND ((post_status = \'static\' OR post_status = \'publish\')', $where);
681
682 return ($where);
683}
684
685function keywords_postsJoin($join) {
686 global $wpdb;
687 $join .= " LEFT JOIN $wpdb->postmeta AS jkeywords_meta ON ($wpdb->posts.ID = jkeywords_meta.post_id) ";
688 return ($join);
689}
690
691function keywords_includeTemplate() {
692
693 if (is_keyword()) {
694 $template = '';
695
696 if ( file_exists(TEMPLATEPATH . "/" . KEYWORDS_TEMPLATE) )
697 $template = TEMPLATEPATH . "/" . KEYWORDS_TEMPLATE;
698 else if ( file_exists(TEMPLATEPATH . "/tags.php") )
699 $template = TEMPLATEPATH . "/tags.php";
700 else
701 $template = get_category_template();
702
703 if ($template) {
704 load_template($template);
705 exit;
706 }
707 }
708 return;
709}
710
711function keywords_createRewriteRules($rewrite) {
712 global $wp_rewrite;
713
714 // add rewrite tokens
715 $keytag_token = '%' . KEYWORDS_QUERYVAR . '%';
716 $wp_rewrite->add_rewrite_tag($keytag_token, '(.+)', KEYWORDS_QUERYVAR . '=');
717
718 $keywords_structure = $wp_rewrite->root . KEYWORDS_QUERYVAR . "/$keytag_token";
719 $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
720
721 return ( $rewrite + $keywords_rewrite );
722}
723
724?>
Note: See TracBrowser for help on using the repository browser.