PageRenderTime 52ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/mu-plugins/MuTags.php

https://github.com/alx/blogsfera
PHP | 1078 lines | 763 code | 173 blank | 142 comment | 147 complexity | 3372991151178541de1cda0d84384817 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0
  1. <?php
  2. /*
  3. Plugin Name: MuTags
  4. Plugin URI: http://wpmudev.org
  5. Description: Another tag cloud solution for WordpressMU
  6. Author: Henri Simonen
  7. Version: 0.1
  8. Author URI: http://betraise.org
  9. License URI: http://www.gnu.org/licenses/gpl.html
  10. */
  11. /* Copyright 2007 Henri Simonen
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. 0.1 - Original version
  24. 0.1b - Fixed a bug
  25. 0.2 - Fixed bugs, added new options, added better language support
  26. 0.2b - Added new tag sort options and support for multiple custom tag clouds
  27. */
  28. if (!class_exists('Mu_tags')) {
  29. class Mu_tags {
  30. //this sets the name that will be used to save and retrive your options. This needs to be unique
  31. var $TagOptName = "Mu_tags_options";
  32. var $version = "0.2";
  33. var $dbtable;
  34. function Mu_tags(){
  35. global $wpmuBaseTablePrefix;
  36. add_action('template_redirect', array(&$this, 'TemplateRedirect'));
  37. add_action('wp_insert_post', array(&$this, 'mu_insert_post'));
  38. add_action('delete_post', array(&$this, 'mu_delete_post'));
  39. add_action('admin_menu', array(&$this,'add_admin_pages'));
  40. $this->TagOpt = $this->getTagOpt();
  41. $this->dbtable = "".$wpmuBaseTablePrefix."global_tags";
  42. }
  43. /******************************************************************
  44. * Method: getTagOpt()
  45. * Purpose: Gets options from site_meta.
  46. *****************************************************************/
  47. function getTagOpt() {
  48. //Default options array here
  49. $TagOpt = array(
  50. "tag_base" => "tag",
  51. "results_template" => "tag_results",
  52. "record_cats" => "0",
  53. "record_empty" => "0",
  54. "limit_tags" => "100",
  55. "exclude" => "",
  56. "bexclude" => "", //exclude blogs (stupid name)
  57. "daysback" => "365",
  58. "tag_min_size" => "100",
  59. "tag_max_size" => "350",
  60. "tag_min_color" => "#999999",
  61. "tag_max_color" => "#000000",
  62. "tag_size_format" => "%",
  63. "tag_link_title" => "%amount% things tagged with %tag%",
  64. "tag_sort_format" => "wd",
  65. "include_cats" => "0");
  66. //*****************************************************************************************
  67. $savedOptions = get_site_option($this->TagOptName);
  68. if (!empty($savedOptions)) {
  69. foreach ($savedOptions as $key => $option) {
  70. $TagOpt["$key"] = $option;
  71. }
  72. }
  73. update_site_option($this->TagOptName, $TagOpt);
  74. return $TagOpt;
  75. }
  76. function saveTagOpt(){
  77. update_site_option($this->TagOptName, $this->TagOpt);
  78. }
  79. function add_admin_pages(){
  80. add_submenu_page("wpmu-admin.php","Mu Tags","Mu Tags",10, __FILE__, array(&$this,"AdminMenuOutput"));
  81. }
  82. /******************************************************************
  83. * Method: MuTagsVersion()
  84. * Purpose: Checks this plugin version and makes changes to the db
  85. *****************************************************************/
  86. function MuTagsVersion() {
  87. global $wpdb;
  88. if(empty($this->TagOpt['version'])) {
  89. $wpdb->query("ALTER TABLE $this->dbtable
  90. ADD taxonomy tinyint(1) unsigned
  91. NOT NULL default '0'");
  92. $TagOpt['version'] = $this->version; update_site_option($this->TagOptName, $TagOpt);
  93. }
  94. }
  95. /******************************************************************
  96. * Method: AdminMenuOutput()
  97. * Purpose: Outputs the adminmenu.
  98. *****************************************************************/
  99. function AdminMenuOutput(){
  100. ?>
  101. <?php if($_POST['import_tags'] || $_POST['import_cats']) { ?>
  102. <div id="message" class="updated fade">
  103. <p><strong>Tags imported</strong><p>
  104. </div>
  105. <?php } ?>
  106. <?php if($_POST['tag_options_submit']) { ?>
  107. <div id="message" class="updated fade">
  108. <p><strong>Options Updated</strong><p>
  109. </div>
  110. <?php } ?>
  111. <?php if($_POST['createdb'] || $_POST['recreatedb']){ ?>
  112. <div id="message" class="updated fade">
  113. <p><strong>Tables created</strong><p>
  114. </div>
  115. <?php } ?>
  116. <?php if($_POST['delete_categories_submit'] || $_POST['delete_tags_submit'] || $_POST['delete_term_submit']) { ?>
  117. <div id="message" class="updated fade">
  118. <p><strong>Tag(s) deleted</strong><p>
  119. </div>
  120. <?php } ?>
  121. <div class="wrap">
  122. <h2>MuTags Options</h2>
  123. <form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="text-align: right;">
  124. <input type="hidden" name="cmd" value="_s-xclick">
  125. <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
  126. <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
  127. <input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----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-----END PKCS7-----
  128. ">
  129. </form>
  130. <form method="post" name="options" action="<?php $this->SaveNewValues();?>">
  131. <?php if(!empty($this->TagOpt['dbcreated'])) : ?>
  132. <p class="submit"><input type="submit" name="tag_options_submit" value="Update Options &raquo;" /></p>
  133. <h3>General</h3>
  134. <table width="100%" cellspacing="2" cellpadding="5" class="optiontable editform">
  135. <tr valign="top">
  136. <th width="33%" scope="row"> Tag base:</th>
  137. <td><input name="tag_base" type="text" id="" value="<?php echo $this->TagOpt['tag_base'];?>" size="2" style="width: 16.5em; " /> This needs to be the slug of an actual page.
  138. </td>
  139. </tr>
  140. <tr valign="top">
  141. <th width="33%" scope="row"> Tag results template:</th>
  142. <td><input name="results_template" type="text" id="" value="<?php echo $this->TagOpt['results_template'];?>" size="2" style="width: 16.5em; " />
  143. .php (The actual filename without ".php")</td>
  144. </tr>
  145. <tr valign="top">
  146. <th width="33%" scope="row"> Record categories:</th>
  147. <td><input name="record_cats" type="checkbox" value="1" <?php if($this->TagOpt['record_cats'] == "1") { echo 'checked="checked"';}?> />
  148. Do you want post categories recorded into the MuTags table, when a post is posted?</td>
  149. </tr>
  150. <tr valign="top">
  151. <th width="33%" scope="row"> Record empty posts:</th>
  152. <td><input name="record_empty" type="checkbox" value="1" <?php if($this->TagOpt['record_empty'] == "1") { echo 'checked="checked"';}?>/>
  153. Insert posts with no tags or categories in the MuTags -table?</td>
  154. </tr>
  155. </table>
  156. <h3>Tag Cloud</h3>
  157. <table width="100%" cellspacing="2" cellpadding="5" class="optiontable editform">
  158. <tr valign="top">
  159. <th width="33%" scope="row"> Exclude tags:</th>
  160. <td><input name="exclude" type="text" id="" value="<?php echo $this->TagOpt['exclude'];?>" size="2" style="width: 16.5em; " />
  161. Comma separated list of Term id's (13,29,14)
  162. </td>
  163. </tr>
  164. <tr valign="top">
  165. <th width="33%" scope="row"> Exclude blogs:</th>
  166. <td><input name="bexclude" type="text" id="" value="<?php echo $this->TagOpt['bexclude'];?>" size="2" style="width: 16.5em; " />
  167. Comma separated list of blog id's (1,2,3)
  168. </td>
  169. </tr>
  170. <tr valign="top">
  171. <th width="33%" scope="row"> Show tags from last:</th>
  172. <td><input name="daysback" type="text" id="" value="<?php echo $this->TagOpt['daysback'];?>" size="2" style="width: 6em; " />
  173. days (Leave empty for all tags)
  174. </td>
  175. </tr>
  176. <tr valign="top">
  177. <th width="33%" scope="row"> Limit tags in the tag cloud:</th>
  178. <td><input name="limit_tags" type="text" id="" value="<?php echo $this->TagOpt['limit_tags'];?>" size="2" style="width: 6em; " />
  179. tags (Leave empty for all tags)</td>
  180. </tr>
  181. <tr valign="top">
  182. <th width="33%" scope="row"> Smallest tag size:</th>
  183. <td><input name="tag_min_size" type="text" id="" value="<?php echo $this->TagOpt['tag_min_size'];?>" size="2" style="width: 6em; " />
  184. </td>
  185. </tr>
  186. <tr valign="top">
  187. <th width="33%" scope="row"> Biggest tag size:</th>
  188. <td><input name="tag_max_size" type="text" id="" value="<?php echo $this->TagOpt['tag_max_size'];?>" size="2" style="width: 6em; " />
  189. </td>
  190. </tr>
  191. </tr>
  192. <tr valign="top">
  193. <th width="33%" scope="row"> Smallest tag color:</th>
  194. <td><input name="tag_min_color" type="text" id="" value="<?php echo $this->TagOpt['tag_min_color'];?>" size="2" style="width: 6em; " />
  195. </td>
  196. </tr>
  197. <tr valign="top">
  198. <th width="33%" scope="row"> Biggest tag color:</th>
  199. <td><input name="tag_max_color" type="text" id="" value="<?php echo $this->TagOpt['tag_max_color'];?>" size="2" style="width: 6em; " />
  200. </td>
  201. </tr>
  202. <tr valign="top">
  203. <th scope="row"> Tag size in:</th>
  204. <td><select name="tag_size_format">
  205. <option value='%' <?php if($this->TagOpt['tag_size_format'] == "%") {
  206. echo 'selected="selected"';
  207. }?>>%</option>
  208. <option value='px' <?php if($this->TagOpt['tag_size_format'] == "px") {
  209. echo 'selected="selected"';
  210. }?> >px</option>
  211. <option value='em' <?php if($this->TagOpt['tag_size_format'] == "em") {
  212. echo 'selected="selected"';
  213. }?>>em</option>
  214. </select></td>
  215. </tr>
  216. <th scope="row"> Sort tags by:</th>
  217. <td><select name="tag_sort_format" style="width: 16.5em;">
  218. <option value='rand' <?php if($this->TagOpt['tag_sort_format'] == "rand") {
  219. echo 'selected="selected"';
  220. }?> >Random</option>
  221. <option value='wd' <?php if($this->TagOpt['tag_sort_format'] == "wd") {
  222. echo 'selected="selected"';
  223. }?>>Weight Desc</option>
  224. <option value='wa' <?php if($this->TagOpt['tag_sort_format'] == "wa") {
  225. echo 'selected="selected"';
  226. }?>>Weight Asc</option>
  227. <option value='nd' <?php if($this->TagOpt['tag_sort_format'] == "nd") {
  228. echo 'selected="selected"';
  229. }?>>Name Desc</option>
  230. <option value='na' <?php if($this->TagOpt['tag_sort_format'] == "na") {
  231. echo 'selected="selected"';
  232. }?>>Name Asc</option>
  233. </select></td>
  234. </tr>
  235. <tr valign="top">
  236. <th width="33%" scope="row"> What to show in tag link title:</th>
  237. <td><input name="tag_link_title" type="text" id="" value="<?php echo $this->TagOpt['tag_link_title'];?>" size="2" style="width: 22.5em; " /> (%amount% things tagged with %tag%)
  238. </td>
  239. </tr>
  240. <tr valign="top">
  241. <th width="33%" scope="row"> Tag css class:</th>
  242. <td><input name="css_class" type="text" id="" value="<?php echo $this->TagOpt['css_class'];?>" size="2" style="width: 16.5em; " />
  243. </td>
  244. </tr>
  245. <tr valign="top">
  246. <th width="33%" scope="row"> Include categories in tag cloud:</th>
  247. <td><input name="include_cats" type="checkbox" value="1" <?php if($this->TagOpt['include_cats'] == "1") {echo 'checked="checked"'; } ?>/>
  248. </td>
  249. </tr>
  250. </table>
  251. <p class="submit">
  252. <input type="submit" name="tag_options_submit" value="Update Options &raquo;" />
  253. </p>
  254. <h3>Import existing WordPress tags to taglist</h3>
  255. <p>You should backup your database before trying this!</p>
  256. <p class="submit">
  257. <input type="submit" name="import_tags" value="Import"/>
  258. </p>
  259. <h3>Import existing WordPress categories to taglist</h3>
  260. <p>You should backup your database before trying this!</p>
  261. <p class="submit">
  262. <input type="submit" name="import_cats" value="Import"/>
  263. </p>
  264. <h3>Remove tags</h3>
  265. <p><b>Remove a tag from the MuTags -table by term id</b></p>
  266. <p><input type="text" style="width: 6em; " name="delete_term" /></p>
  267. <p class="submit">
  268. <input type="submit" name="delete_term_submit" value="Remove"/>
  269. </p>
  270. <p><b>Remove tags older than</b></p>
  271. <p><input type="text" style="width: 6em; " name="delete_tags" /> days.</p>
  272. <p class="submit">
  273. <input type="submit" name="delete_tags_submit" value="Remove"/>
  274. </p>
  275. <p><b>Remove categories older than</b></p>
  276. <p><input type="text" style="width: 6em; " name="delete_cats" /> days.</p>
  277. <p class="submit">
  278. <input type="submit" name="delete_cats_submit" value="Remove"/>
  279. </p>
  280. <h3>Recreate Database Table</h3>
  281. <p>This drops the existing table and creates a new <b>empty</b> one.</p>
  282. <p class="submit">
  283. <input type="submit" name="recreatedb" value="Create!" />
  284. </p>
  285. <?php else: ?>
  286. <h3>Create Database Table</h3>
  287. <p>This message appears if you are installing or upgrading this plugin. Don't worry. Existing data in the MuTags -table wont be deleted. (Though it's allways good to backup your database before making changes to it...)</p>
  288. <p class="submit">
  289. <input type="submit" name="createdb" value="Create!"/>
  290. </p>
  291. <?php endif; ?>
  292. </form>
  293. </div>
  294. <?php
  295. if($_POST["import_tags"])
  296. $this->ImportWpTags();
  297. if($_POST["import_cats"])
  298. $this->ImportWpTags("cats");
  299. if($_POST["delete_term_submit"])
  300. $this->dbmaintain($_POST["delete_term"]);
  301. if($_POST["delete_cats_submit"])
  302. $this->dbmaintain("cats", $_POST["delete_cats"]);
  303. if($_POST["delete_tags_submit"])
  304. $this->dbmaintain("tags", $_POST["delete_tags"]);
  305. if($_POST["createdb"])
  306. $this->setup();
  307. if($_POST["recreatedb"])
  308. $this->setup('true');
  309. }
  310. function saveNewValues() {
  311. if($_POST['createdb']) {
  312. $this->TagOpt['dbcreated'] = "1";
  313. }
  314. if($_POST['tag_options_submit']) {
  315. $this->TagOpt['tag_base'] = $_POST['tag_base'];
  316. $this->TagOpt['results_template'] = $_POST['results_template'];
  317. $this->TagOpt['record_cats'] = $_POST['record_cats'];
  318. $this->TagOpt['record_empty'] = $_POST['record_empty'];
  319. $this->TagOpt['limit_tags'] = $_POST['limit_tags'];
  320. $this->TagOpt['tag_min_size'] = $_POST['tag_min_size'];
  321. $this->TagOpt['tag_max_size'] = $_POST['tag_max_size'];
  322. $this->TagOpt['tag_min_color'] = $_POST['tag_min_color'];
  323. $this->TagOpt['tag_max_color'] = $_POST['tag_max_color'];
  324. $this->TagOpt['tag_size_format'] = $_POST['tag_size_format'];
  325. $this->TagOpt['tag_sort_format'] = $_POST['tag_sort_format'];
  326. $this->TagOpt['css_class'] = $_POST['css_class'];
  327. $this->TagOpt['tag_link_title'] = $_POST['tag_link_title'];
  328. $this->TagOpt['exclude'] = $_POST['exclude'];
  329. $this->TagOpt['bexclude'] = $_POST['bexclude'];
  330. $this->TagOpt['daysback'] = $_POST['daysback'];
  331. $this->TagOpt['include_cats'] = $_POST['include_cats'];
  332. }
  333. $this->saveTagOpt();
  334. }
  335. /******************************************************************
  336. * Method: mu_insert_post()
  337. * Purpose: This is triggered whenever a post is posted or updated.
  338. * This finds the post_id, blog_id, "tag_id" and post_date of the post
  339. *******************************************************************/
  340. function mu_insert_post($post_ID) {
  341. global $wpdb, $blog_id;
  342. if($this->is_public($blog_id)) {
  343. $postdata = $this->get_post_data($post_ID);
  344. $post_type = $postdata->post_type;
  345. $post_status = $postdata->post_status;
  346. $post_date = $postdata->post_date;
  347. $post_modified = $postdata->post_modified;
  348. $update = false;
  349. if ($post_date != $post_modified) {
  350. $update = true;
  351. }
  352. if ($update) {
  353. $this->mu_delete_post($post_ID);
  354. }
  355. if (($post_type = 'post') && ($post_status != 'draft')) {
  356. $tagsmap = $wpdb->get_results("SELECT term_taxonomy_id
  357. FROM $wpdb->term_relationships WHERE object_id = $post_ID AND term_taxonomy_id != 1");
  358. if($tagsmap) {
  359. //If tags are found
  360. foreach ($tagsmap as $id) {
  361. $term = $wpdb->get_row("SELECT term_id, taxonomy
  362. FROM $wpdb->term_taxonomy WHERE
  363. term_taxonomy_id = $id->term_taxonomy_id");
  364. if($term->taxonomy == "post_tag") {
  365. $taxonomy = "0";
  366. }
  367. elseif($this->TagOpt['record_cats'] == "1") {
  368. $taxonomy = "1";
  369. }
  370. else {
  371. $taxonomy = "-1";
  372. }
  373. if($taxonomy > "-1") {
  374. $this->mu_query($blog_id, $term->term_id, $post_date, $post_ID, $taxonomy);
  375. }
  376. }
  377. }
  378. elseif($this->TagOpt['record_empty'] == "1") {
  379. //If the post wasn't tagged with anything we insert a 0 for the tag_id
  380. $this->mu_query($blog_id, "0", $post_date, $post_ID, "1");
  381. }
  382. else {
  383. return;
  384. }
  385. }
  386. }
  387. }
  388. /******************************************************************
  389. * Method: get_post_data()
  390. * Purpose: Just a tiny helper...
  391. *******************************************************************/
  392. function get_post_data($post_ID) {
  393. global $wpdb;
  394. $data = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_ID");
  395. return $data;
  396. }
  397. /******************************************************************
  398. * Method: mu_query()
  399. * Purpose: Just handles the inserting of the data into our
  400. * global_tags table
  401. *****************************************************************/
  402. function mu_query($blog_id, $term_id, $post_date, $post_ID, $taxonomy) {
  403. $sql_query = mysql_query(
  404. "INSERT IGNORE INTO $this->dbtable
  405. (tag_id, blog_id, post_id, post_date, taxonomy)
  406. VALUES
  407. ('$term_id', '$blog_id', '$post_ID', '$post_date', '$taxonomy')");
  408. return $sql_query;
  409. }
  410. /******************************************************************
  411. * Method: mu_delete_post()
  412. * Purpose: Deletes the post also from our global_tags table when
  413. * a user deletes a post.
  414. *****************************************************************/
  415. function mu_delete_post($post_ID) {
  416. global $blog_id;
  417. $post_type = get_post_field('post_type', $post_ID);
  418. $post_status = get_post_field('post_status', $post_ID);
  419. if ($post_type = 'post') {
  420. $sql_query = mysql_query("DELETE FROM $this->dbtable
  421. WHERE post_id = $post_ID
  422. AND blog_id = $blog_id");
  423. }
  424. }
  425. /******************************************************************
  426. * Method: is_public()
  427. * Purpose: Checks if the blog is public.
  428. *****************************************************************/
  429. function is_public($blog_id) {
  430. // global $wpdb;
  431. //
  432. // $check = $wpdb->get_row("SELECT public FROM $wpdb->blogs WHERE blog_id = $blog_id");
  433. // if($check->public != 1) {
  434. // return false;
  435. // }
  436. // else {
  437. // return true;
  438. // }
  439. return true;
  440. }
  441. /******************************************************************
  442. * Method: get_global_tags()
  443. * Purpose: Gets tags from the database.
  444. *****************************************************************/
  445. function get_global_tags($values = "", $customvals = 'false') {
  446. global $wpdb;
  447. if(is_array($values))
  448. extract($values);
  449. $exclusions = '';
  450. if ( !empty($exclude) ) {
  451. $exposts = preg_split('/[\s,]+/',$exclude);
  452. if ( count($exposts) ) {
  453. foreach ( $exposts as $expost ) {
  454. if (empty($exclusions))
  455. $exclusions = ' tag_id <> "' . intval($expost) . '" AND ';
  456. else
  457. $exclusions .= 'tag_id <> "' . intval($expost) . '" AND ';
  458. }
  459. }
  460. }
  461. if (!empty($exclusions))
  462. $exclusions .= '';
  463. $bexclusions = '';
  464. if ( !empty($bexclude) ) {
  465. $bexposts = preg_split('/[\s,]+/',$bexclude);
  466. if ( count($bexposts) ) {
  467. foreach ( $bexposts as $bexpost ) {
  468. if (empty($bexclusions))
  469. $bexclusions = ' blog_id <> "' . intval($bexpost) . '" AND ';
  470. else
  471. $bexclusions .= 'blog_id <> "' . intval($bexpost) . '" AND ';
  472. }
  473. }
  474. }
  475. if (!empty($bexclusions))
  476. $bexclusions .= '';
  477. if($orderby == "wd")
  478. $orderby = "cnnt DESC";
  479. if($orderby == "wa")
  480. $orderby = "cnnt ASC";
  481. if($orderby == "rand")
  482. $orderby = "rand()";
  483. if($orderby == "nd")
  484. $orderby = "cat_name DESC";
  485. if($orderby == "na")
  486. $orderby = "cat_name ASC";
  487. if($includecats == "1") {
  488. $includecats = "";
  489. }
  490. else {
  491. $includecats = "taxonomy != '1' AND";
  492. }
  493. if(!empty($limit)) {
  494. $limit = "LIMIT 0, $limit";
  495. }
  496. else {
  497. $limit = "";
  498. }
  499. if(!empty($daysback)) {
  500. $ANDdaysback = "AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $daysback DAY)";
  501. }
  502. else {
  503. $ANDdaysback = "";
  504. }
  505. if($customvals == false)
  506. $tag_list = wp_cache_get('mu_tag_list');
  507. else
  508. $tag_list = false;
  509. if($tag_list == false) {
  510. $tag_list = $wpdb->get_results("SELECT $this->dbtable.tag_id,
  511. $wpdb->sitecategories.category_nicename,
  512. $wpdb->sitecategories.cat_name
  513. AS tag, COUNT(ID) as cnnt ".
  514. "FROM $this->dbtable LEFT JOIN $wpdb->sitecategories ".
  515. "ON $this->dbtable.tag_id = $wpdb->sitecategories.cat_ID
  516. WHERE $exclusions $bexclusions $includecats tag_id != '0'
  517. $ANDdaysback GROUP BY tag_id ORDER BY $orderby $limit");
  518. if($customvals == false)
  519. wp_cache_set('mu_tag_list', $tag_list);
  520. }
  521. return $tag_list;
  522. }
  523. /******************************************************************
  524. * Method: get_tag_results()
  525. * Purpose: Returns results for current tag
  526. *****************************************************************/
  527. function get_tag_results($showtag = false) {
  528. global $wpdb;
  529. $url = $_SERVER['REQUEST_URI'];
  530. $tagbase = $this->TagOpt['tag_base'];
  531. $tag = preg_replace("/(.*)$tagbase\/(.*)/", "\\2", $url);
  532. if(substr($tag, -1, 1) == "/") {
  533. $taglength = strlen($tag);
  534. $tag = substr($tag, 0, $taglength-1);
  535. }
  536. // $tag = str_replace("/", "", $tag);
  537. //$tag = str_replace("%20", " ", $tag);
  538. // $tag = urldecode($tag);
  539. /* $trans = get_html_translation_table(HTML_ENTITIES);
  540. $tag = strtr($tag, $trans);*/
  541. //$tag = remove_accents("ééé");
  542. if($showtag == true) {
  543. $tag = urldecode($tag);
  544. $tag = str_replace("-", " ", $tag);
  545. return $tag;
  546. }
  547. //$tag = remove_accents($tag);
  548. $bexclude = $this->TagOpt['bexclude'];
  549. $bexclusions = '';
  550. if ( !empty($bexclude) ) {
  551. $bexposts = preg_split('/[\s,]+/',$bexclude);
  552. if ( count($bexposts) ) {
  553. foreach ( $bexposts as $bexpost ) {
  554. if (empty($bexclusions))
  555. $bexclusions = ' blog_id <> "' . intval($bexpost) . '" AND ';
  556. else
  557. $bexclusions .= 'blog_id <> "' . intval($bexpost) . '" AND ';
  558. }
  559. }
  560. }
  561. if (!empty($bexclusions))
  562. $bexclusions .= '';
  563. $results = wp_cache_get('tag_results_'.$tag.'');
  564. if($results == false) {
  565. $results = $wpdb->get_results("SELECT $this->dbtable.tag_id,
  566. $this->dbtable.post_id,
  567. $this->dbtable.blog_id,
  568. $this->dbtable.post_date,
  569. $wpdb->sitecategories.cat_name ".
  570. "FROM $this->dbtable LEFT JOIN $wpdb->sitecategories ".
  571. "ON $this->dbtable.tag_id = $wpdb->sitecategories.cat_ID
  572. WHERE $bexclusions category_nicename = '$tag' ORDER BY post_date DESC");
  573. wp_cache_set('tag_results_'.$tag.'', $results);
  574. }
  575. return $results;
  576. }
  577. /******************************************************************
  578. * Method: get_recent_posts()
  579. * Purpose: Returns a list of recent posts
  580. *****************************************************************/
  581. function get_recent_posts($offset = 0, $ppp = 10) {
  582. global $wpdb;
  583. $post_list = wp_cache_get('mu_recent_posts');
  584. if($post_list == false) {
  585. $post_list = $wpdb->get_results("SELECT blog_id, post_id, post_date
  586. FROM $this->dbtable
  587. GROUP BY post_id
  588. ORDER BY post_date
  589. DESC LIMIT $offset, $ppp");
  590. wp_cache_set('mu_recent_posts', $post_list);
  591. }
  592. if($post_list) {
  593. return $post_list;
  594. }
  595. else {
  596. return FALSE;
  597. }
  598. }
  599. /****************************************************************
  600. * Method: setup()
  601. * Purpose: creates our global_tags table if needed.
  602. *****************************************************************/
  603. function setup($drop = "false") {
  604. global $wpdb;
  605. if($drop == "true") {
  606. $drop = "DROP TABLE IF EXISTS $this->dbtable";
  607. }
  608. $this->MuTagsVersion(); //Checks the version and makes some changes
  609. $create_table = "CREATE TABLE IF NOT EXISTS $this->dbtable (
  610. id int(11) unsigned NOT NULL auto_increment,
  611. tag_id int(11) unsigned NOT NULL,
  612. blog_id int(11) unsigned NOT NULL,
  613. post_id int(11) unsigned NOT NULL default '0',
  614. post_date datetime NOT NULL,
  615. taxonomy tinyint(1) unsigned NOT NULL default '0',
  616. UNIQUE KEY id (id)
  617. )";
  618. $wpdb->query($drop);
  619. $wpdb->query($create_table);
  620. $this->TagOpt['dbcreated'] = "1";
  621. $this->saveTagOpt();
  622. }
  623. /****************************************************************
  624. * Method: ImportWptags()
  625. * Purpose: Imports existing Wordpress tags. (Very hazardous...)
  626. *****************************************************************/
  627. function ImportWptags($type = 'tags') {
  628. global $wpdb, $wpmuBaseTablePrefix;
  629. if($type == 'tags') {
  630. $delete = mysql_query("DELETE FROM $this->dbtable WHERE taxonomy <> '1'");
  631. $taxonomy = "post_tag";
  632. $taxonomyid = "0";
  633. }
  634. if($type == 'cats') {
  635. $delete = mysql_query("DELETE FROM $this->dbtable WHERE taxonomy <> '0'");
  636. $taxonomy = "category";
  637. $taxonomyid = "1";
  638. }
  639. $blogs = $wpdb->get_results( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = '$wpdb->siteid'");
  640. if($blogs) {
  641. foreach($blogs as $blog) {
  642. $table_blog_posts = $wpmuBaseTablePrefix.$blog->blog_id."_posts";
  643. $table_term_relationships = $wpmuBaseTablePrefix.$blog->blog_id."_term_relationships";
  644. $table_term_taxonomy = $wpmuBaseTablePrefix.$blog->blog_id."_term_taxonomy";
  645. $results = $wpdb->get_results("SELECT ID, post_date
  646. FROM $table_blog_posts
  647. WHERE post_status = 'publish'
  648. AND post_type = 'post'");
  649. if(!$results) {
  650. // Can be empty blog
  651. //die('There was an error');
  652. }
  653. foreach ($results as $result) {
  654. $term_ids = $wpdb->get_results("SELECT $table_term_relationships.term_taxonomy_id,
  655. $table_term_taxonomy.term_id ".
  656. "FROM $table_term_relationships LEFT JOIN $table_term_taxonomy ".
  657. "ON $table_term_relationships.term_taxonomy_id = $table_term_taxonomy.term_taxonomy_id
  658. WHERE object_id = $result->ID
  659. AND object_id != '1'
  660. AND taxonomy = '$taxonomy'");
  661. foreach($term_ids as $term) {
  662. $tag_id = $term->term_id;
  663. if(!empty($tag_id)) {
  664. $this->mu_query($blog->blog_id, $tag_id, $result->post_date, $result->ID, $taxonomyid);
  665. }
  666. }
  667. }
  668. }
  669. }
  670. if($_POST['import_tags']) {
  671. return TRUE;
  672. }
  673. }
  674. /****************************************************************
  675. * Method: dbmaintain()
  676. * Purpose: Removes data from the database.
  677. *****************************************************************/
  678. function dbmaintain($term = "", $daysback = "") {
  679. global $wpdb;
  680. if(!empty($term)) {
  681. // "last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $daysback DAY)"
  682. if($term == "tags") {
  683. $todelete = "taxonomy = '0'";
  684. }
  685. elseif($term == "cats") {
  686. $todelete = "taxonomy = '1'";
  687. }
  688. else {
  689. $todelete = "tag_id = '$term'";
  690. }
  691. if(!empty($daysback)) {
  692. $ANDdaysback = "AND post_date < DATE_SUB(CURRENT_DATE(), INTERVAL $daysback DAY)";
  693. }
  694. else {
  695. $ANDdaysback = "";
  696. }
  697. $delete = "DELETE FROM $this->dbtable
  698. WHERE $todelete $ANDdaysback";
  699. $wpdb->query($delete);
  700. }
  701. }
  702. /****************************************************************
  703. * Method: get_the_posts()
  704. * Purpose: Get current postdata to display with tag results.
  705. * Credits: From zappo_wpmu_topposts by Brad Hefta-Gaub (http://heftagaub.wordpress.com/)
  706. *****************************************************************/
  707. function get_the_posts($blogarray) {
  708. global $wpdb, $wpmuBaseTablePrefix;
  709. $posts = array();
  710. foreach($blogarray as $post_mapping) {
  711. $table_blog_posts = $wpmuBaseTablePrefix.$post_mapping->blog_id."_posts";
  712. $query ="SELECT *,'$post_mapping->blog_id' AS 'blog_id'
  713. FROM $table_blog_posts
  714. WHERE ID = $post_mapping->post_id";
  715. $results = $wpdb->get_results($query);
  716. if ( !empty($results) ) {
  717. /* sleazy - should really be certain this only returned 1 row... */
  718. $single_post = $results[0];
  719. $posts[] = $single_post;
  720. }
  721. }
  722. return $posts;
  723. }
  724. /******************************************************************
  725. * Method: TemplateRedirect()
  726. * Purpose: Redirects to the tag results template if tags are found
  727. *****************************************************************/
  728. function TemplateRedirect(){
  729. global $blog_id;
  730. $url = $_SERVER['REQUEST_URI'];
  731. $tagbase = $this->TagOpt['tag_base'];
  732. $tags_found = FALSE;
  733. if(preg_match("'$tagbase/(.*)+'", $url))
  734. $tags_found = TRUE;
  735. if($blog_id == '1') {
  736. if ($tags_found) {
  737. include(TEMPLATEPATH . '/'.$this->TagOpt['results_template'].'.php');
  738. //it is essential to include the exit. This prevents more than one template file being included
  739. exit;
  740. }
  741. }
  742. }
  743. }
  744. }
  745. //instantiate the class
  746. if (class_exists('Mu_tags')) {
  747. $Mu_tags = new Mu_tags();
  748. }
  749. /******************************************************************
  750. * Function: ColorWeight()
  751. * Purpose: Generates the appropriate color according to tag size
  752. * Credits: This is from CTC (configurable tag cloud widget),
  753. * but it think
  754. * that originally this is from UTW. I dunno thanks anysay!
  755. *****************************************************************/
  756. function ColorWeight($weight, $mincolor, $maxcolor) {
  757. if ($weight) {
  758. $weight = $weight/100;
  759. if (strlen($mincolor) == 4) {
  760. $r = substr($mincolor, 1, 1);
  761. $g = substr($mincolor, 2, 1);
  762. $b = substr($mincolor, 3, 1);
  763. $mincolor = "#$r$r$g$g$b$b";
  764. }
  765. if (strlen($maxcolor) == 4) {
  766. $r = substr($maxcolor, 1, 1);
  767. $g = substr($maxcolor, 2, 1);
  768. $b = substr($maxcolor, 3, 1);
  769. $maxcolor = "#$r$r$g$g$b$b";
  770. }
  771. $minr = hexdec(substr($mincolor, 1, 2));
  772. $ming = hexdec(substr($mincolor, 3, 2));
  773. $minb = hexdec(substr($mincolor, 5, 2));
  774. $maxr = hexdec(substr($maxcolor, 1, 2));
  775. $maxg = hexdec(substr($maxcolor, 3, 2));
  776. $maxb = hexdec(substr($maxcolor, 5, 2));
  777. $r = dechex(intval((($maxr - $minr) * $weight) + $minr));
  778. $g = dechex(intval((($maxg - $ming) * $weight) + $ming));
  779. $b = dechex(intval((($maxb - $minb) * $weight) + $minb));
  780. if (strlen($r) == 1) $r = "0" . $r;
  781. if (strlen($g) == 1) $g = "0" . $g;
  782. if (strlen($b) == 1) $b = "0" . $b;
  783. $color = "#$r$g$b";
  784. $color = substr($color,0,7);
  785. return $color;
  786. }
  787. }
  788. //This is just a tiny tiny helper.
  789. function linktitle($key, $value) {
  790. global $Mu_tags;
  791. $linktitle = $Mu_tags->TagOpt['tag_link_title'];
  792. $linktitle = stripslashes($linktitle);
  793. $linktitle = str_replace("'", "", $linktitle);
  794. $linktitle = str_replace("\\", "", $linktitle);
  795. $linktitle = str_replace('"', "\"", $linktitle);
  796. $linktitle = preg_replace("/%amount%/", $value, $linktitle);
  797. $linktitle = preg_replace("/%tag%/", $key, $linktitle);
  798. return $linktitle;
  799. }
  800. /******************************************************************
  801. * Function: mu_tag_cloud()
  802. * Purpose: Generates the tag cloud
  803. * Credits: This is from DeltaKids global categories hack/plugin,
  804. * but originally from somewhere else. Thanks to the creators!
  805. *****************************************************************/
  806. function mu_tag_cloud($custom = "") {
  807. global $wpdb, $table_prefix, $Mu_tags;
  808. $defaults = array(
  809. 'maxsize' => $Mu_tags->TagOpt['tag_max_size'],
  810. 'minsize' => $Mu_tags->TagOpt['tag_min_size'],
  811. 'mincolor' => $Mu_tags->TagOpt['tag_min_color'],
  812. 'maxcolor' => $Mu_tags->TagOpt['tag_max_color'],
  813. 'limit' => $Mu_tags->TagOpt['limit_tags'],
  814. 'exclude' => $Mu_tags->TagOpt['exclude'],
  815. 'bexclude' => $Mu_tags->TagOpt['bexclude'],
  816. 'cssclass' => $Mu_tags->TagOpt['css_class'],
  817. 'orderby' => $Mu_tags->TagOpt['tag_sort_format'],
  818. 'daysback' => $Mu_tags->TagOpt['daysback'],
  819. 'includecats' => $Mu_tags->TagOpt['include_cats']
  820. );
  821. extract($defaults);
  822. //If custom values are found use them insead
  823. if(is_array($custom)) {
  824. extract($custom);
  825. $customvals = true;
  826. }
  827. else {
  828. $customvals = false;
  829. }
  830. $notagsmsg = '<ul><li>No tags found...</li></ul>';
  831. $tag_list = $Mu_tags->get_global_tags($values = array(
  832. 'limit' => $limit,
  833. 'exclude' => $exclude,
  834. 'bexclude' => $bexclude,
  835. 'orderby' => $orderby,
  836. 'daysback' => $daysback,
  837. 'includecats' => $includecats
  838. ), $customvals);
  839. if ($tag_list) {
  840. foreach($tag_list as $tmp) {
  841. $tags[$tmp->tag] = $tmp->cnnt;
  842. }
  843. // get the largest and smallest array values
  844. $max_qty = max(array_values($tags));
  845. $min_qty = min(array_values($tags));
  846. // find the range of values
  847. $spread = $max_qty - $min_qty;
  848. if (0 == $spread) { // we don't want to divide by zero
  849. $spread = 1;
  850. }
  851. // determine the font-size increment
  852. // this is the increase per tag quantity (times used)
  853. $step = ($maxsize - $minsize)/($spread);
  854. // loop through our tag array
  855. foreach ($tag_list as $tag) {
  856. $link = $tag->category_nicename;
  857. $key = $tag->tag;
  858. $value = $tag->cnnt;
  859. // calculate CSS font-size
  860. // find the $value in excess of $min_qty
  861. // multiply by the font-size increment ($size)
  862. // and add the $min_size set above
  863. $size = $minsize + (($value - $min_qty) * $step);
  864. // uncomment if you want sizes in whole %:
  865. //$size = ceil($size);
  866. $color_weight = round(99*($size-$minsize)/($maxsize-$minsize)+1);
  867. echo '<a href="/'.$Mu_tags->TagOpt['tag_base'].'/'.$link.'" class="'.$cssclass.'" style="font-size: '.$size.''.$Mu_tags->TagOpt['tag_size_format'].'; color: '.ColorWeight($color_weight, $mincolor, $maxcolor).';"';
  868. echo ' title="'.linktitle($key, $value).'"';
  869. echo '>'.$key.'</a> ';
  870. }
  871. }
  872. else {
  873. echo $notagsmsg;
  874. }
  875. }
  876. function tag_results() {
  877. global $Mu_tags, $post;
  878. $blogarray = $Mu_tags->get_tag_results();
  879. return $Mu_tags->get_the_posts($blogarray);
  880. }
  881. function mu_recent_posts() {
  882. global $Mu_tags, $post;
  883. $blogarray = $Mu_tags->get_recent_posts();
  884. return $Mu_tags->get_the_posts($blogarray);
  885. }
  886. function current_tag() {
  887. global $Mu_tags;
  888. $tag = $Mu_tags->get_tag_results('true');
  889. echo $tag;
  890. }
  891. ?>