PageRenderTime 139ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-forum/library/sf-permalinks.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 240 lines | 137 code | 28 blank | 75 comment | 40 complexity | 417d0859909c0c6e88ebdcf062c49245 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. Forum Permalink 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. die('Access Denied');
  10. }
  11. # --------------------------------------------------------------
  12. # sf_build_url()
  13. #
  14. # Main URL building routine
  15. # To use pass forum and topic slugs. Page if not known should
  16. # always be 1 or 0. If a post id is passed (else use zero), the
  17. # routine will go and get the correct page number for the post
  18. # within the topic
  19. # $forumslug: forum link
  20. # $topicslug: topic link
  21. # $pageid: page (if know - if post will calculate)
  22. # $postid: post link if relevant (or zero)
  23. # $postindex: sequence number of post if relevant
  24. #
  25. # CHANGE IN 4.2.0:
  26. # ==============
  27. # If the POST ID is passed AND a positive PAGE ID then it WILL
  28. # compile a url with that page number. if the page
  29. # number is NOT known when passing a Post ID then page must be
  30. # passed as zero
  31. # --------------------------------------------------------------
  32. function sf_build_url($forumslug, $topicslug, $pageid, $postid, $postindex=0)
  33. {
  34. if ($postid != 0 && $pageid == 0)
  35. {
  36. $pageid = sf_determine_page($forumslug, $topicslug, sf_esc_int($postid), sf_esc_int($postindex));
  37. }
  38. $url = SFURL;
  39. if ($forumslug) $url.= $forumslug;
  40. if ($topicslug) $url.= '/'.$topicslug;
  41. if ($pageid > 1) $url.= '/page-'.$pageid;
  42. if ($postid) {
  43. $url.= '/#p'.$postid;
  44. } else {
  45. $url = trailingslashit($url);
  46. }
  47. return esc_url($url);
  48. }
  49. # --------------------------------------------------------------
  50. # sf_build_qurl()
  51. #
  52. # Main Query String URL building routine
  53. # Must have at least one parameter of 'var=value' string
  54. # Up to five can be passed in.
  55. # --------------------------------------------------------------
  56. function sf_build_qurl($param1, $param2='', $param3='', $param4='', $param5='')
  57. {
  58. $url = rtrim(SFURL, '/');
  59. # first does it need the ?
  60. if(strpos($url, '?') === false)
  61. {
  62. $url .= '/?';
  63. $and = '';
  64. } else {
  65. $and = "&";
  66. }
  67. $url.= $and.$param1;
  68. $and = "&";
  69. if(!empty($param2)) $url.= $and.$param2;
  70. if(!empty($param3)) $url.= $and.$param3;
  71. if(!empty($param4)) $url.= $and.$param4;
  72. if(!empty($param5)) $url.= $and.$param5;
  73. return $url;
  74. }
  75. # ------------------------------------------------------------------
  76. # sf_get_sfqurl()
  77. #
  78. # Build a forum query url ready for parameters
  79. # ------------------------------------------------------------------
  80. function sf_get_sfqurl($url)
  81. {
  82. # if no ? then add one on the end
  83. $url = rtrim($url, '/');
  84. if(strpos($url, '?') === false)
  85. {
  86. $url .= '?';
  87. } else {
  88. $url .= '&amp;';
  89. }
  90. return $url;
  91. }
  92. # --------------------------------------------------------------
  93. # sf_permalink_from_forumid()
  94. #
  95. # Returns permalink for forum from the forum id
  96. # --------------------------------------------------------------
  97. function sf_permalink_from_forumid($forumid)
  98. {
  99. $url = '';
  100. if(!empty($forumid))
  101. {
  102. $url = sf_build_url(sf_get_forum_slug($forumid), '', 0, 0);
  103. }
  104. return $url;
  105. }
  106. # --------------------------------------------------------------
  107. # sf_permalink_from_topicid()
  108. #
  109. # Returns permalink for topic from the topic id
  110. # --------------------------------------------------------------
  111. function sf_permalink_from_topicid($topicid)
  112. {
  113. $url = '';
  114. if(!empty($topicid))
  115. {
  116. $forumid = sf_get_forum_from_topic($topicid);
  117. $url = sf_build_url(sf_get_forum_slug($forumid), sf_get_topic_slug($topicid), 0, 0);
  118. }
  119. return $url;
  120. }
  121. # --------------------------------------------------------------
  122. # sf_permalink_from_forumid_and_topicid()
  123. #
  124. # Returns permalink for topic from both forum and topic ids
  125. # --------------------------------------------------------------
  126. function sf_permalink_from_forumid_and_topicid($forumid, $topicid)
  127. {
  128. $url = '';
  129. if(!empty($topicid) && !empty($forumid))
  130. {
  131. $url = sf_build_url(sf_get_forum_slug($forumid), sf_get_topic_slug($topicid), 0, 0);
  132. }
  133. return $url;
  134. }
  135. # --------------------------------------------------------------
  136. # sf_permalink_from_postid()
  137. #
  138. # Returns permalink for topic from only the post id
  139. # --------------------------------------------------------------
  140. function sf_permalink_from_postid($postid)
  141. {
  142. $url = '';
  143. if(!empty($postid))
  144. {
  145. $slugs = sf_get_slugs_from_postid($postid);
  146. $url = sf_build_url($slugs->forum_slug, $slugs->topic_slug, 0, $postid, $slugs->post_index);
  147. }
  148. return $url;
  149. }
  150. # --------------------------------------------------------------
  151. # sf_determine_page()
  152. #
  153. # Determines the correct page with a topic that the post
  154. # will be displayed on based on current settings
  155. # $topicslug: to look up topic id if needed
  156. # $postid: the post to calculate page for
  157. # $postindex: post sequence ig known
  158. # --------------------------------------------------------------
  159. function sf_determine_page($forumslug, $topicslug, $postid, $postindex)
  160. {
  161. global $wpdb, $sfglobals;
  162. # establish paging count - can sometimes be out of scope so check
  163. $ppaged=$sfglobals['display']['posts']['perpage'];
  164. if(empty($ppaged) || $ppaged == 0)
  165. {
  166. $sfdisplay = sf_get_option('sfdisplay');
  167. $ppaged = $sfdisplay['posts']['perpage'];
  168. if(!$ppaged) $ppaged=20;
  169. }
  170. # establish topic sort order
  171. $order="ASC"; # default
  172. if($sfglobals['display']['posts']['sortdesc']) $order="DESC"; # global override
  173. $topicrecord = sf_get_topic_record_from_slug($topicslug);
  174. # If we do not have the postindex then we have to go and get it
  175. if($postindex == 0 || empty($postindex))
  176. {
  177. $postindex = $wpdb->get_var("SELECT post_index FROM ".SFPOSTS." WHERE post_id=".$postid);
  178. # In the remote possibility postindex is 0 or empty then...
  179. if($postindex == 0 || empty($postindex))
  180. {
  181. $forumrecord = sf_get_forum_record_from_slug($forumslug);
  182. sf_build_forum_index($forumrecord->forum_id);
  183. sf_build_post_index($topicrecord->topic_id, $topicslug);
  184. $postindex = $wpdb->get_var("SELECT post_index FROM ".SFPOSTS." WHERE post_id=".$postid);
  185. }
  186. }
  187. # Now we have what we need to do the math
  188. if($order == 'ASC')
  189. {
  190. $page = ($postindex/$ppaged);
  191. if(!is_int($page))
  192. {
  193. $page=intval(($page)+1);
  194. }
  195. } else {
  196. $page = ($topicrecord->post_count - $postindex);
  197. $page = ($page/$ppaged);
  198. $page=intval(($page)+1);
  199. }
  200. return $page;
  201. }
  202. function sf_add_get()
  203. {
  204. global $wp_rewrite;
  205. if($wp_rewrite->using_permalinks())
  206. {
  207. return '?';
  208. } else {
  209. return '&amp;';
  210. }
  211. }
  212. ?>