PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-forum/admin/panel-tags/support/sfa-tags-save.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 350 lines | 278 code | 39 blank | 33 comment | 36 complexity | 8caef5be576746d84ff06506286001d6 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /*
  3. Simple:Press
  4. Admin Tags Support Functions
  5. $LastChangedDate: 2010-03-26 16:38:27 -0700 (Fri, 26 Mar 2010) $
  6. $Rev: 3818 $
  7. */
  8. if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']))
  9. {
  10. die('Access Denied');
  11. }
  12. function sfa_save_tags_edit_tags()
  13. {
  14. global $wpdb, $sfglobals;
  15. check_admin_referer('forum-adminform_sfedittags', 'forum-adminform_sfedittags');
  16. $topic_id_list = $_POST['topic_id'];
  17. $tag_id_list = $_POST['tag_id'];
  18. $tag_list = $_POST['tags'];
  19. # take the easy way out and remove all tags and then add back in the new list
  20. for ($x=0; $x<count($topic_id_list); $x++)
  21. {
  22. if (!empty($tag_id_list[$x])) # if no tags originally, dont delete anything
  23. {
  24. # grab all the tag rows and decrement the tag count
  25. $sql = "SELECT tag_id, tag_count FROM ".SFTAGS." WHERE tag_id IN (".sf_esc_str($tag_id_list[$x]).")";
  26. $tags = $wpdb->get_results($sql);
  27. foreach ($tags as $tag)
  28. {
  29. # decrement tag count and delete if it gets to zero or update the new count
  30. $tag->tag_count--;
  31. if ($tag->tag_count == 0)
  32. {
  33. $wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tag->tag_id); # count is zero so delete
  34. } else {
  35. $wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$tag->tag_count." WHERE tag_id=".$tag->tag_id); # update count
  36. }
  37. }
  38. # remove all the tag meta entries for the topic
  39. $wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".sf_esc_int($topic_id_list[$x]));
  40. }
  41. # now add the current tags back in for the topic
  42. if (!empty($tag_list[$x]))
  43. {
  44. $tags = trim(sf_esc_str($tag_list[$x]));
  45. $tags = trim($tags, ','); # no extra commas allowed
  46. $tags = explode(',', $tags);
  47. $tags = array_unique($tags); # remove any duplicates
  48. $tags = array_values($tags); # put back in order
  49. if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
  50. {
  51. $tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']); # limit to maxt tags opton
  52. }
  53. $mess = sfc_add_tags($topic_id_list[$x], $tags);
  54. if ($mess != '')
  55. {
  56. return $mess;
  57. }
  58. }
  59. }
  60. $mess = __("Tags Updated", "sforum");
  61. return $mess;
  62. }
  63. function sfa_save_tags_rename_tags()
  64. {
  65. global $wpdb;
  66. check_admin_referer('forum-adminform_sfrenametags', 'forum-adminform_sfrenametags');
  67. $oldtags = $_POST['renametag_old'];
  68. $newtags = $_POST['renametag_new'];
  69. if (empty($oldtags) || empty($newtags))
  70. {
  71. $mess = __("Renaming Tags requires old Tag(s) and new Tags(s) entries - No Tags renamed!", "sforum");
  72. return $mess;
  73. }
  74. # prep the new tags
  75. $tags = trim(sf_esc_str($newtags));
  76. $tags = trim($tags, ','); # no extra commas allowed
  77. $tags = explode(',', $tags);
  78. $tags = array_unique($tags); # remove any duplicates
  79. $tags = array_values($tags); # put back in order
  80. # prep the old tags
  81. $otags = trim(sf_esc_str($oldtags));
  82. $otags = trim($otags, ','); # no extra commas allowed
  83. $otags = explode(',', $otags);
  84. $otags = array_unique($otags); # remove any duplicates
  85. $otags = array_values($otags); # put back in order
  86. foreach ($otags as $tag)
  87. {
  88. $tagslug = sf_create_slug($tag, 'tag', false);
  89. $tagid = $wpdb->get_var("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug='".$tagslug."'");
  90. if ($tagid)
  91. {
  92. # delete tag itself
  93. $wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid);
  94. # find the topics that use this tag
  95. $topics = $wpdb->get_results("SELECT topic_id FROM ".SFTAGMETA." WHERE tag_id=".$tagid);
  96. if ($topics)
  97. {
  98. foreach ($topics as $topic)
  99. {
  100. # delete tag metas for this topic
  101. $wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".$topic->topic_id." AND tag_id=".$tagid);
  102. # add in the new tags
  103. sfc_add_tags($topic->topic_id, $tags);
  104. }
  105. }
  106. }
  107. }
  108. $mess = __("Tags Renamed or Merged", "sforum");
  109. return $mess;
  110. }
  111. function sfa_save_tags_delete_tags()
  112. {
  113. global $wpdb;
  114. check_admin_referer('forum-adminform_sfdeletetags', 'forum-adminform_sfdeletetags');
  115. $deltags = $_POST['deletetag_name'];
  116. if (empty($deltags))
  117. {
  118. $mess = __("Deleting Tags requires Tags(s) entry - No Tags deleted!", "sforum");
  119. return $mess;
  120. }
  121. $deleted = 0; # indicate nothing deleted
  122. # loop through tags and delete the tag and the tag metas
  123. $tags = trim(sf_esc_str($deltags));
  124. $tags = trim($tags, ','); # no extra commas allowed
  125. $tags = explode(',', $tags);
  126. $tags = array_unique($tags); # remove any duplicates
  127. $tags = array_values($tags); # put back in order
  128. foreach ($tags as $tag)
  129. {
  130. $tagslug = sf_create_slug($tag, 'tag');
  131. $tagid = $wpdb->get_var("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug='".$tagslug."'");
  132. if ($tagid)
  133. {
  134. # delete tag metas with this tag id
  135. $wpdb->query("DELETE FROM ".SFTAGMETA." WHERE tag_id=".$tagid);
  136. # delete tag itself
  137. $wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid);
  138. # indicate at least some tags deleted
  139. if ($deleted == 0) $deleted = 1;
  140. } else {
  141. if ($deleted == 1) $deleted = 2; # indicate some deleted but some not found
  142. }
  143. }
  144. # output deletion results message
  145. switch ($deleted)
  146. {
  147. case 0:
  148. $mess = __("No Tags Matched For Deletion!", "sforum");
  149. break;
  150. case 1:
  151. $mess = __("Tags Successfully Deleted!", "sforum");
  152. break;
  153. case 2:
  154. $mess = __("Some Tags Deleted, but Others Not Found!", "sforum");
  155. break;
  156. }
  157. return $mess;
  158. }
  159. function sfa_save_tags_add_tags()
  160. {
  161. global $wpdb, $sfglobals;
  162. check_admin_referer('forum-adminform_sfaddtags', 'forum-adminform_sfaddtags');
  163. $matchtags = $_POST['addtag_match'];
  164. $addtags = $_POST['addtag_new'];
  165. if (empty($addtags))
  166. {
  167. $mess = __("Adding Tags requires new Tags(s) entry - No Tags added!", "sforum");
  168. return $mess;
  169. }
  170. # prep the new tags
  171. $tags = trim(sf_esc_str($addtags));
  172. $tags = trim($tags, ','); # no extra commas allowed
  173. $tags = explode(',', $tags);
  174. $tags = array_unique($tags); # remove any duplicates
  175. $tags = array_values($tags); # put back in order
  176. if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
  177. {
  178. $tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']); # limit to max tags opton
  179. }
  180. # if not match tags, add the new tags to all topics
  181. if (empty($matchtags))
  182. {
  183. # get topics
  184. $topics = $wpdb->get_results("SELECT topic_id FROM ".SFTOPICS);
  185. if ($topics)
  186. {
  187. foreach ($topics as $topic)
  188. {
  189. # now add the tags
  190. sfc_add_tags($topic->topic_id, $tags);
  191. }
  192. $mess = __("Tags Added to All Topics!", "sforum");
  193. return $mess;
  194. } else {
  195. $mess = __("No Topics to Add Tags to - No Tags added!", "sforum");
  196. return $mess;
  197. }
  198. }
  199. # alrighty, so need to match tags before we add the new ones
  200. # prep the match tags
  201. $mtags = trim(sf_esc_str($matchtags));
  202. $mtags = trim($mtags, ','); # no extra commas allowed
  203. $mtags = explode(',', $mtags);
  204. $mtags = array_unique($mtags); # remove any duplicates
  205. $mtags = array_values($mtags); # put back in order
  206. if ($mtags)
  207. {
  208. $mtag_list = '(';
  209. $first = true;
  210. # Now put the tags back together in list
  211. foreach ($mtags as $mtag)
  212. {
  213. # convert to a tag slug and build slug list
  214. $mtagslug = sf_create_slug($mtag, 'tag');
  215. if ($first)
  216. {
  217. $mtag_list.= "'".$mtagslug."'";
  218. $first = false;
  219. } else {
  220. $mtag_list.= ",'".$mtagslug."'";
  221. }
  222. }
  223. $mtag_list.= ")";
  224. # grab any topics that have a matching slug
  225. $tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug IN".$mtag_list);
  226. if ($tagids)
  227. {
  228. # now find the topics with these matched tags and add the new tags
  229. foreach ($tagids as $tagid)
  230. {
  231. $topics = $wpdb->get_results("SELECT topic_id FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
  232. if ($topics)
  233. {
  234. foreach ($topics as $topic)
  235. {
  236. # now add the tags
  237. sfc_add_tags($topic->topic_id, $tags);
  238. }
  239. }
  240. }
  241. $mess = __("Tags Added to Topics with Matched Tags!", "sforum");
  242. return $mess;
  243. } else {
  244. $mess = __("No Tags Matched!", "sforum");
  245. return $mess;
  246. }
  247. } else {
  248. $mess = __("Invalid Matching Tags Entry - No Tags Added!", "sforum");
  249. return $mess;
  250. }
  251. $mess = __("Oh Oh - This Shouldn't Happen!", "sforum");
  252. return;
  253. }
  254. function sfa_save_tags_cleanup_tags()
  255. {
  256. global $wpdb;
  257. check_admin_referer('forum-adminform_sfcleanup', 'forum-adminform_sfcleanup');
  258. # remove orphaned tags
  259. $tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS);
  260. if ($tagids)
  261. {
  262. foreach ($tagids as $tagid)
  263. {
  264. $meta = $wpdb->get_results("SELECT meta_id FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
  265. if (!$meta)
  266. {
  267. # no metas so its orphaned and can be deleted
  268. $wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid->tag_id);
  269. }
  270. }
  271. }
  272. # remove orphaned tag meta
  273. $tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGMETA);
  274. if ($tagids)
  275. {
  276. foreach ($tagids as $tagid)
  277. {
  278. $tags = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS." WHERE tag_id = ".$tagid->tag_id);
  279. if (!$tags)
  280. {
  281. # no tags so its orphaned and can be deleted
  282. $wpdb->query("DELETE FROM ".SFTAGMETA." WHERE tag_id=".$tagid->tag_id);
  283. }
  284. }
  285. }
  286. # clean up the tag counts
  287. $tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS);
  288. if ($tagids)
  289. {
  290. foreach ($tagids as $tagid)
  291. {
  292. # get the number of topics using this tag
  293. $count = $wpdb->get_var("SELECT COUNT(tag_id) FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
  294. # set the count to number of topics using
  295. $wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$count." WHERE tag_id = ".$tagid->tag_id);
  296. }
  297. }
  298. $mess = __("Tags Database Cleaned Up!", "sforum");
  299. return $mess;
  300. }
  301. ?>