PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jeromes-keywords/jeromes-keywords.php

http://cartonbank.googlecode.com/
PHP | 710 lines | 466 code | 95 blank | 149 comment | 71 complexity | 11a581ee45e014f3c665a41f48574549 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  1. <?php
  2. /*
  3. Plugin Name: Jerome's Keywords
  4. Plugin URI: http://vapourtrails.ca/wp-keywords
  5. Version: 2.0-beta3
  6. Description: Allows keywords to be associated with each post, which can be used as meta keywords and for creating a local tag system.
  7. Author: Jerome Lavigne
  8. Author URI: http://vapourtrails.ca
  9. */
  10. /* Copyright 2005 Jerome Lavigne (email : darkcanuck@vapourtrails.ca)
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /* Credits:
  24. Special thanks also to Dave Metzener, Mark Eckenrode, Dan Taarin, N. Godbout,
  25. "theanomaly", "oso", Wayne @ AcmeTech, Will Luke, Gyu-In Lee, Denis de Bernardy,
  26. Horst Gutmann, "Chip" Camden, Christian Davén, Johannes Jarolim, Mike Koepke
  27. and the many others who have provided feedback, spotted bugs, and suggested
  28. improvements.
  29. */
  30. /* ChangeLog:
  31. 9-Sep-2006: Version 2.0-beta3
  32. - major overhaul of entire plugin
  33. - added tag management features from LightPress
  34. - added default options which are saved to the db
  35. - moved old functions (template tags) to legacy.php
  36. - many minor improvements, including random sorting of tag cloud & automatic meta keyword generation
  37. */
  38. /* uncomment these lines to see how many warnings WP can throw */
  39. //error_reporting(E_ALL);
  40. //ini_set('display_errors', 1);
  41. class JeromesKeywords {
  42. /* plugin options */
  43. var $option = array(
  44. 'version' => '2.0', // keywords options version
  45. 'keywords_table' => 'jkeywords', // table where keywords/tags are stored
  46. 'query_varname' => 'tag', // HTTP var name used for tag searches
  47. 'template' => 'keywords.php', // template file to use for displaying tag queries
  48. 'meta_always_include' => '', // meta keywords to always include
  49. 'meta_includecats' => 'default', // default' => include cats in meta keywords only for home page
  50. // all' => includes cats on every page, none' => never included
  51. 'meta_autoheader' => '1', // automatically output meta keywords in header
  52. 'search_strict' => '1', // returns only exact tag matches if true
  53. 'use_feed_cats' => '1', // insert tags into feeds as categories
  54. /* post tag options */
  55. 'post_linkformat' => '', // post tag format (initialized to $link_localsearch)
  56. 'post_tagseparator' => ', ', // tag separator character(s)
  57. 'post_includecats' => '0', // include categories in post's tag list
  58. 'post_notagstext' => 'none', // text to display if no tags found
  59. /* tag cloud options */
  60. 'cloud_linkformat' => '', // post tag format (initialized to $link_tagcloud)
  61. 'cloud_tagseparator' => ' ', // tag separator character(s)
  62. 'cloud_includecats' => '0', // include categories in tag cloud
  63. 'cloud_sortorder' => 'natural', // tag sorting: natural, countup/asc, countdown/desc, alpha
  64. 'cloud_displaymax' => '0', // maximum # of tags to display (all if set to zero)
  65. 'cloud_displaymin' => '0', // minimum tag count to include in tag cloud
  66. 'cloud_scalemax' => '0', // maximum value for count scaling (no scaling if zero)
  67. 'cloud_scalemin' => '0' // minimum value for count scaling
  68. );
  69. /* standard tag link formats */
  70. var $link_localsearch = '<a href="%fulltaglink%" title="Search site for %tagname%" rel="tag">%tagname%</a>';
  71. var $link_technorati = '<a href="http://technorati.com/tag/%taglink%/" title="Technorati tag page for %tagname%" rel="tag">%tagname%</a>';
  72. var $link_tagcloud = '<li class="cosmos keyword%count%"><a href="%fulltaglink%">%tagname%</a></li>';
  73. /* set during class setup */
  74. var $keyword = ''; // keyword search value
  75. var $rewriteon = false; // defaults to using no rewrite rules
  76. var $base_url = ''; // base URL for local tags (depending on permalink style)
  77. var $admin = null; // reference to admin class
  78. /* private members */
  79. var $_table = ''; // full name of tags table
  80. var $_postids = ''; // stores comma-separated list of post IDs in current view
  81. var $_posttags = null; // post tag data cache
  82. var $_alltags = null; // all published tag data cache
  83. var $_allcats = null; // all published categories cache
  84. var $_allcombined = null; // sorted compilation of tags & cats
  85. var $_initdone = false;
  86. var $_flushrules = false;
  87. /* initialization and basic setup methods */
  88. function JeromesKeywords() {
  89. $this->loadOptions();
  90. /* set custom table name */
  91. global $table_prefix;
  92. $this->_table = $table_prefix . $this->option['keywords_table'];
  93. /* setup filter/action triggers */
  94. add_action('init', array(&$this, 'initRewrite')); // can't use WP rewrite flags until "init" hook
  95. add_filter('the_posts', array(&$this, 'getPostIds'), 90); // get post IDs once WP query is done
  96. add_filter('query_vars', array(&$this, 'addQueryVar')); // used for keyword searches
  97. add_action('parse_query', array(&$this, 'parseQuery')); // used for keyword searches
  98. if (is_admin())
  99. add_action('admin_menu', array(&$this, 'setAdminHooks')); // administration interface
  100. if ($this->option['use_feed_cats']) // insert tags into feeds as categories
  101. add_filter('the_category_rss', array(&$this, 'createFeedCategories'), 5, 2);
  102. if ($this->option['meta_autoheader']) // automagic meta keywords in header
  103. add_action('wp_head', array(&$this, 'outputHeader'));
  104. }
  105. function initRewrite() {
  106. global $wp_rewrite;
  107. /* detect permalink type & construct base URL for local links */
  108. $this->base_url = get_option('home') . '/';
  109. if (isset($wp_rewrite) && $wp_rewrite->using_permalinks()) {
  110. $this->rewriteon = true; // using rewrite rules
  111. $this->base_url .= $wp_rewrite->root; // set to "index.php/" if using that style
  112. $this->base_url .= $this->option['query_varname'] . '/';
  113. } else {
  114. $this->base_url .= '?' . $this->option['query_varname'] . '=';
  115. }
  116. /* generate rewrite rules for tag queries */
  117. if ($this->rewriteon)
  118. add_filter('search_rewrite_rules', array(&$this, 'createRewriteRules'));
  119. /* flush rules if requested */
  120. $this->_initdone = true;
  121. if ($this->_flushrules)
  122. $wp_rewrite->flush_rules();
  123. }
  124. function createRewriteRules($rewrite) {
  125. global $wp_rewrite;
  126. /* add rewrite tokens */
  127. $qvar =& $this->option['query_varname'];
  128. $token = '%' . $qvar . '%';
  129. $wp_rewrite->add_rewrite_tag($token, '(.+)', $qvar . '=');
  130. $keywords_structure = $wp_rewrite->root . $qvar . "/$token";
  131. $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
  132. return ( $rewrite + $keywords_rewrite );
  133. }
  134. function setAdminHooks() {
  135. /* load administration class */
  136. include (dirname(__FILE__) . DIRECTORY_SEPARATOR . 'LightPressTagging.php');
  137. $this->admin =& new LightPressTagging($this->_table);
  138. /* add tag management menu */
  139. add_management_page('Manage Jerome\'s Keywords', 'Keywords/Tags', 'manage_categories', 'jeromes-keywords/managetags.php');
  140. add_options_page('Jerome\'s Keywords Options', 'Jerome\'s Keywords', 'manage_options', 'jeromes-keywords/options-jkeywords.php');
  141. }
  142. function loadOptions() {
  143. /* check if options exist */
  144. if (substr(get_option('jkeywords_version'),1,-1) < 2.0) {
  145. $this->option['post_linkformat'] = $this->link_localsearch;
  146. $this->option['cloud_linkformat'] = $this->link_tagcloud;
  147. $this->saveOptions();
  148. } else {
  149. /* read option values from the database, stripping our spacer characters */
  150. foreach($this->option as $optname => $optval) {
  151. $newval = substr(get_option('jkeywords_' . $optname), 1, -1);
  152. if ($newval != '')
  153. $this->option[$optname] = $newval;
  154. }
  155. }
  156. }
  157. function setOption($optname, $optval) {
  158. /* update option value */
  159. $this->option[$optname] = $optval;
  160. }
  161. function saveOptions() {
  162. /* flush WP rewrite rules if query var has changed */
  163. global $wp_rewrite;
  164. $qvar = 'query_varname';
  165. if ($this->option[$qvar] != substr(get_option("jkeywords_$qvar"),1,-1)) {
  166. if ($this->_initdone)
  167. $wp_rewrite->flush_rules();
  168. else
  169. $this->_flushrules = true;
  170. }
  171. /* write current option values to the database */
  172. /* add a dummy char to beginning & end because WP won't save spaces! */
  173. foreach($this->option as $optname => $optval)
  174. update_option('jkeywords_' . $optname, '*' . $optval . '*');
  175. }
  176. /* tag fetch & caching */
  177. function getPostIds($posts) {
  178. /* extract list of post IDs from the posts array */
  179. if (!is_null($posts) && is_array($posts)) {
  180. foreach($posts as $p) {
  181. $this->_postids .= (!empty($this->_postids) ? ',' : '') . $p->ID; //create comma-separated list
  182. }
  183. }
  184. return $posts; //send'em back to WP
  185. }
  186. function getPostTags($postid=null, $force=false) {
  187. /*
  188. * caches all tags matching current post selection
  189. */
  190. if (is_null($this->_posttags) || $force) {
  191. if (empty($this->_postids)) {
  192. $this->_posttags = array(); // no posts, thus no tags
  193. return $this->_posttags;
  194. }
  195. global $wpdb;
  196. $q = "SELECT DISTINCT post_id, tag_name AS name
  197. FROM {$this->_table} tags
  198. WHERE post_id IN ({$this->_postids})
  199. ORDER BY post_id, name";
  200. $tag_results = $wpdb->get_results($q);
  201. $post_tags = array();
  202. if (!is_null($tag_results) && is_array($tag_results)) {
  203. foreach ($tag_results as $tag) {
  204. $post_id = $tag->post_id;
  205. if (!isset($post_tags[$post_id]))
  206. $post_tags[$post_id] = array();
  207. $post_tags[$post_id][] = $tag->name;
  208. }
  209. }
  210. $this->_posttags =& $post_tags;
  211. }
  212. if (is_null($postid))
  213. return $this->_posttags;
  214. elseif (isset($this->_posttags[$postid]))
  215. return $this->_posttags[$postid];
  216. else
  217. return array();
  218. }
  219. function getAllTags($force=false) {
  220. /*
  221. * gets all published site tags
  222. */
  223. if (is_null($this->_alltags) || $force) {
  224. global $wpdb;
  225. $q = "SELECT tag.tag_name AS name, COUNT(tag.post_id) AS numposts
  226. FROM {$this->_table} tag
  227. INNER JOIN {$wpdb->posts} p ON tag.post_id=p.id
  228. WHERE (p.post_status='publish' OR p.post_status='static')
  229. AND p.post_date_gmt<='" . gmdate("Y-m-d H:i:s", time()) . "'
  230. GROUP BY tag.tag_name
  231. ORDER BY numposts DESC ";
  232. $tag_results = $wpdb->get_results($q);
  233. $alltags = array();
  234. if (!is_null($tag_results) && is_array($tag_results)) {
  235. foreach ($tag_results as $tag) {
  236. $alltags[$tag->name] = array('name' => $tag->name,
  237. 'count'=> $tag->numposts,
  238. 'link' => $this->getTagPermalink($tag->name)
  239. );
  240. }
  241. }
  242. $this->_alltags =& $alltags;
  243. }
  244. return $this->_alltags;
  245. }
  246. function getAllCats($force=false) {
  247. /*
  248. * gets all published site categories (same format as getAllTags)
  249. */
  250. if (is_null($this->_allcats) || $force) {
  251. global $wpdb;
  252. $q = "SELECT p2c.category_id AS cat_id, COUNT(p2c.rel_id) AS numposts,
  253. UNIX_TIMESTAMP(max(p.post_date_gmt)) + '" . get_option('gmt_offset') . "' AS last_post_date,
  254. UNIX_TIMESTAMP(max(p.post_date_gmt)) AS last_post_date_gmt
  255. FROM {$wpdb->post2cat} p2c
  256. INNER JOIN {$wpdb->posts} p ON p2c.post_id=p.id
  257. WHERE (p.post_status='publish' OR p.post_status='static')
  258. AND p.post_date_gmt<='" . gmdate("Y-m-d H:i:s", time()) . "'
  259. GROUP BY p2c.category_id
  260. ORDER BY numposts DESC ";
  261. $results = $wpdb->get_results($q);
  262. $allcats = array();
  263. if (!is_null($results) && is_array($results)) {
  264. foreach ($results as $cat) {
  265. $catname = get_catname($cat->cat_id);
  266. $allcats[$catname] = array('name' => get_catname($cat->cat_id),
  267. 'count'=> $cat->numposts,
  268. 'link' => get_category_link((int)$cat->cat_id)
  269. );
  270. }
  271. }
  272. $this->_allcats =& $allcats;
  273. }
  274. return $this->_allcats;
  275. }
  276. function getAllCombined($force=false) {
  277. /*
  278. * combines all published tags & categories
  279. */
  280. if (is_null($this->_allcombined) || $force) {
  281. $combined = array_merge($this->getAllTags(), $this->getAllCats());
  282. uasort($combined, array(&$this, 'sortCombined'));
  283. $this->_allcombined =& $combined;
  284. }
  285. return $this->_allcombined;
  286. }
  287. function sortCombined($a, $b) {
  288. /* sort by descending count, ties broken by nat case ascending name */
  289. if ($a['count'] == $b['count'])
  290. return strnatcasecmp($a['name'], $b['name']);
  291. else
  292. return ( ($a['count'] > $b['count']) ? -1 : 1 );
  293. }
  294. /* meta keywords methods */
  295. function getMetaKeywords($before='', $after='', $separator=',', $include_cats=null) {
  296. /* add pre-defined keywords */
  297. $pagekeys = explode(',', $this->option['meta_always_include']);
  298. /* get tags for all posts in current view */
  299. foreach($this->getPostTags() as $post_tags)
  300. $pagekeys = array_merge($pagekeys, $post_tags);
  301. /* add categories if necessary */
  302. if (is_null($include_cats))
  303. $include_cats = $this->option['meta_includecats'];
  304. if ($include_cats) {
  305. global $category_cache;
  306. if ( ($include_cats == 'all') || (($include_cats == 'default') && is_home()) ) {
  307. /* include all site categories */
  308. foreach($this->getAllCats() as $category)
  309. $pagekeys[] = $category['name'];
  310. } elseif (isset($category_cache) && ($include_cats != 'none')) {
  311. /* include only categories from posts in current view */
  312. foreach($category_cache as $post_category) {
  313. foreach($post_category as $category)
  314. $pagekeys[] = $category->cat_name;
  315. }
  316. }
  317. }
  318. $pagekeys = array_unique($pagekeys); // remove duplicates
  319. /* setup meta keywords for page header */
  320. $keywordlist = '';
  321. foreach($pagekeys as $keyword) {
  322. $keywordlist .= (($keywordlist != '') ? $separator : '') .
  323. $before . $keyword . $after;
  324. }
  325. return htmlspecialchars($keywordlist);
  326. }
  327. function outputMetaKeywords($before='', $after='', $separator=',', $include_cats=null) {
  328. /* output meta keyword list */
  329. echo $this->getMetaKeywords($before, $after, $separator, $include_cats);
  330. }
  331. function outputHeader() {
  332. /* automagically output meta keywords tag */
  333. echo "\t" . '<meta name="keywords" content="' . $this->getMetaKeywords() . '" />';
  334. }
  335. /* post keywords methods */
  336. function formatPostTags($include_cats=null, $linkformat=null) {
  337. /* check parameters vs. class options */
  338. $include_cats = (is_null($include_cats)) ? $this->option['post_includecats'] : $include_cats;
  339. $linkformat = (is_null($linkformat)) ? $this->option['post_linkformat'] : $linkformat;
  340. /* create array of tags & full links */
  341. $taglinks = array();
  342. if ($include_cats) {
  343. $categories = get_the_category();
  344. foreach($categories as $cat)
  345. $taglinks[ $cat->cat_name ] = get_category_link((int)$cat->cat_ID);
  346. }
  347. global $id;
  348. $post_tags = $this->getPostTags($id);
  349. foreach($post_tags as $tag) {
  350. $taglinks [ $tag ] = $this->getTagPermalink($tag);
  351. }
  352. /* substitute values into link format */
  353. $output = '';
  354. foreach($taglinks as $tag => $url) {
  355. $output .= (($output != '') ? $this->option['post_tagseparator'] : '') .
  356. $this->formatLink($tag, $url, $linkformat);
  357. }
  358. return($output);
  359. }
  360. function outputPostTags($include_cats=null, $linkformat=null) {
  361. /* output formatted tag list or text indicating no tags */
  362. $tags = $this->formatPostTags($include_cats, $linkformat);
  363. if (empty($tags))
  364. echo $this->option['post_notagstext'];
  365. else
  366. echo $tags;
  367. }
  368. /* tag cloud methods */
  369. function formatAllTags($include_cats=null, $linkformat=null, $sort_order=null,
  370. $display_max=null, $display_min=null,
  371. $scale_max=null, $scale_min=null) {
  372. /* check parameters vs. class options */
  373. $include_cats = (is_null($include_cats)) ? $this->option['cloud_includecats']: $include_cats;
  374. $linkformat = (is_null($linkformat)) ? $this->option['cloud_linkformat'] : $linkformat;
  375. $sort_order = (is_null($sort_order)) ? $this->option['cloud_sortorder'] : $sort_order;
  376. $display_max = (is_null($display_max)) ? $this->option['cloud_displaymax'] : $display_max;
  377. $display_min = (is_null($display_min)) ? $this->option['cloud_displaymin'] : $display_min;
  378. $scale_max = (is_null($scale_max)) ? $this->option['cloud_scalemax'] : $scale_max;
  379. $scale_min = (is_null($scale_min)) ? $this->option['cloud_scalemin'] : $scale_min;
  380. /* creatarray of tags & full links */
  381. if ($include_cats)
  382. $alltags = $this->getAllCombined();
  383. else
  384. $alltags = $this->getAllTags();
  385. /* limit results */
  386. $limit = (int) $display_max;
  387. if (($limit > 0) && (count($alltags) > $limit))
  388. $alltags = array_slice($alltags, 0, $limit, true); // already in descending order
  389. /* re-sort results */
  390. switch(strtolower($sort_order)) {
  391. case 'alpha':
  392. ksort($alltags);
  393. break;
  394. case 'countup':
  395. case 'asc':
  396. $alltags = array_reverse($alltags, true); // reverse array order to be ascending
  397. break;
  398. case 'countdown':
  399. case 'desc':
  400. // already in descending order
  401. break;
  402. case 'random':
  403. srand((float)microtime() * 1000000);
  404. shuffle($alltags); // WARNING: keys not kept!
  405. break;
  406. default: // case for 'natural'
  407. uksort($alltags, 'strnatcasecmp');
  408. break;
  409. }
  410. /* scaling */
  411. $do_scale = ($scale_max != 0);
  412. if ($do_scale) {
  413. $minval = $maxval = $alltags[ key($alltags) ]['count'];
  414. foreach($alltags as $tag) {
  415. $minval = min($tag['count'], $minval);
  416. $maxval = max($tag['count'], $maxval, $display_min);
  417. }
  418. $minval = max($minval, $display_min);
  419. $minout = max($scale_min, 0);
  420. $maxout = max($scale_max, $minout);
  421. $scale = ($maxval > $minval) ? (($maxout - $minout) / ($maxval - $minval)) : 0;
  422. }
  423. /* scale counts & format links */
  424. $output = '';
  425. foreach($alltags as $tag) {
  426. if ($tag['count'] >= $display_min) {
  427. $output .= (($output != '') ? $this->option['cloud_tagseparator'] : '') .
  428. $this->formatLink($tag['name'], $tag['link'], $linkformat,
  429. ( ($do_scale) ?
  430. (int) (($tag['count'] - $minval) * $scale + $minout)
  431. : $tag['count'] )
  432. );
  433. }
  434. }
  435. return($output);
  436. }
  437. /* link formatting */
  438. function formatLink($name, $full_link, $format, $count=0) {
  439. /* substitute values into link format */
  440. $newlink = $format;
  441. $newlink = str_replace(array('%tagname%', '%keyword%'), $name, $newlink);
  442. $newlink = str_replace(array('%taglink%', '%keylink%'), $this->localLink($name), $newlink);
  443. $newlink = str_replace('%flickr%', $this->flickrLink($name), $newlink);
  444. $newlink = str_replace('%delicious%', $this->deliciousLink($name), $newlink);
  445. $newlink = str_replace(array('%fulltaglink%', '%fullkeylink%'), $full_link, $newlink);
  446. $newlink = str_replace('%count%', $count, $newlink);
  447. $newlink = str_replace('%em%', str_repeat('<em>', $count), $newlink);
  448. $newlink = str_replace('%/em%', str_repeat('</em>', $count), $newlink);
  449. return $newlink;
  450. }
  451. function localLink($keyword) {
  452. return str_replace('%2F', '/', urlencode($keyword));
  453. }
  454. function getTagPermalink($tag) {
  455. return ($this->base_url .
  456. (($this->rewriteon) ? $this->localLink($tag) : urlencode($tag)));
  457. }
  458. function flickrLink($keyword) {
  459. return urlencode(preg_replace('/[^a-zA-Z0-9]/', '', strtolower($keyword)));
  460. }
  461. function deliciousLink($keyword) {
  462. $del = preg_replace('/\s/', '', $keyword);
  463. if (strstr($del, '+'))
  464. $del = '"' . $del . '"';
  465. return str_replace('%2F', '/', rawurlencode($del));
  466. }
  467. /* keyword search results */
  468. function is_keyword() {
  469. if (!is_null($this->keyword) && ($this->keyword != ''))
  470. return true;
  471. else
  472. return false;
  473. }
  474. function addQueryVar($wpvar_array) {
  475. $wpvar_array[] = $this->option['query_varname'];
  476. return($wpvar_array);
  477. }
  478. function parseQuery() {
  479. /* set the search keyword if it's available */
  480. /* WP2.0's new rewrite rules mean we need to grab it from the query vars */
  481. $this->keyword = stripslashes(get_query_var($this->option['query_varname']));
  482. if (get_magic_quotes_gpc())
  483. $this->keyword = stripslashes($this->keyword); // why so many freakin' slashes?
  484. /* if this is a keyword query, then reset other is_x flags and add query filters */
  485. if ($this->keyword != '') {
  486. global $wp_query;
  487. $wp_query->is_single = false;
  488. $wp_query->is_page = false;
  489. $wp_query->is_archive = false;
  490. $wp_query->is_search = false;
  491. $wp_query->is_home = false;
  492. add_filter('posts_where', array(&$this, 'postsWhere'));
  493. add_filter('posts_join', array(&$this, 'postsJoin'));
  494. add_action('template_redirect', array(&$this, 'includeTemplate'));
  495. }
  496. }
  497. function postsWhere($where) {
  498. /* update where clause to search on keywords table */
  499. if ($this->option['search_strict'])
  500. $where .= " AND jkeywords.tag_name='" . addslashes($this->keyword) . "' ";
  501. else
  502. $where .= " AND jkeywords.tag_name LIKE '%" . addslashes($this->keyword) . "%' ";
  503. // include pages in search (from jeromes-search.php)
  504. return str_replace(' AND (post_status = "publish"', ' AND ((post_status = \'static\' OR post_status = \'publish\')', $where);
  505. }
  506. function postsJoin($join) {
  507. /* update join clause to include keywords table */
  508. global $wpdb;
  509. $join .= " LEFT JOIN {$this->_table} AS jkeywords ON ({$wpdb->posts}.ID = jkeywords.post_id) ";
  510. return ($join);
  511. }
  512. function includeTemplate() {
  513. /* switch template when doing a keyword search */
  514. if ($this->is_keyword()) {
  515. $template = '';
  516. if ( file_exists(TEMPLATEPATH . "/" . $this->option['template']) )
  517. $template = TEMPLATEPATH . "/" . $this->option['template'];
  518. else if ( file_exists(TEMPLATEPATH . "/tags.php") )
  519. $template = TEMPLATEPATH . "/tags.php";
  520. else
  521. $template = get_category_template();
  522. if ($template) {
  523. load_template($template);
  524. exit;
  525. }
  526. }
  527. return;
  528. }
  529. /* Tagging feeds */
  530. function createFeedCategories($list, $type) {
  531. /* insert post's tags as categories */
  532. global $id;
  533. $post_tags = $this->getPostTags($id);
  534. foreach($post_tags as $tag) {
  535. if ($type == "rdf")
  536. $list .= "<dc:subject>$tag</dc:subject>";
  537. else
  538. $list .= "<category>$tag</category>";
  539. }
  540. return $list; //send'em back to WP
  541. }
  542. }
  543. $JKeywords =& new JeromesKeywords();
  544. /* 2.0 "template tags" */
  545. /* jkeywords_post_tags
  546. *
  547. * Outputs the list of tags related to the current post.
  548. * Use this function "in the loop", e.g.
  549. * <?php jkeywords_post_tags(); ? >
  550. */
  551. function jkeywords_post_tags($include_cats=null, $linkformat=null) {
  552. global $JKeywords;
  553. $JKeywords->outputPostTags($include_cats, $linkformat);
  554. }
  555. /* jkeywords_meta_keywords
  556. *
  557. * Outputs the list of meta keywords for the current view
  558. * Use this within your site's header, e.g.
  559. * <meta name="keywords" content="<?php jkeywords_meta_keywords(); ? >" />
  560. */
  561. function jkeywords_meta_keywords($before='', $after='', $separator=',', $include_cats=null) {
  562. global $JKeywords;
  563. $JKeywords->outputMetaKeywords($before, $after, $separator, $include_cats);
  564. }
  565. /* jkeywords_is_keyword
  566. *
  567. * Use this to check if the current view will be returning keyword search results.
  568. * Returns TRUE if a keyword search was requested, FALSE otherwise.
  569. */
  570. function jkeywords_is_keyword() {
  571. global $JKeywords;
  572. return $JKeywords->is_keyword();
  573. }
  574. /* jkeywords_search_keyword
  575. *
  576. * Outputs the keyword used in a keyword search. Useful in your tags.php page:
  577. * <h2>All results for "<?php jkeywords_search_keyword(); ? >"</h2>
  578. */
  579. function jkeywords_search_keyword() {
  580. global $JKeywords;
  581. echo $JKeywords->keyword;
  582. }
  583. /* jkeywords_tag_cloud
  584. *
  585. * Outputs a tag cloud for your entire weblog, e.g.
  586. * <h2>All tags on my site</h2>
  587. * <?php jkeywords_tag_cloud(); ? >
  588. */
  589. function jkeywords_tag_cloud($include_cats=null, $linkformat=null, $sort_order=null,
  590. $display_max=null, $display_min=null,
  591. $scale_max=null, $scale_min=null) {
  592. global $JKeywords;
  593. echo $JKeywords->formatAllTags($include_cats, $linkformat, $sort_order, $display_max,
  594. $display_min, $scale_max, $scale_min);
  595. }
  596. /* jkeywords_top_tags
  597. *
  598. * Similar to a tag cloud, but sorted with the most popular tags listed first.
  599. */
  600. function jkeywords_top_tags($display_max=null,
  601. $linkformat='<li><a href="%fulltaglink%">%keyword%</a></li>',
  602. $sort_order='desc', $display_min=null,
  603. $include_cats=null, $scale_max=null, $scale_min=null) {
  604. global $JKeywords;
  605. echo $JKeywords->formatAllTags($include_cats, $linkformat, $sort_order, $display_max,
  606. $display_min, $scale_max, $scale_min);
  607. }
  608. /* to provide compatibility with 1.x "template tags" */
  609. include (dirname(__FILE__) . DIRECTORY_SEPARATOR . 'legacy.php');
  610. ?>