PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/simple-forum/linking/sf-links-blog.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 356 lines | 210 code | 57 blank | 89 comment | 68 complexity | c269878a43346609707f6a4ed517cce8 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. Blog Linking - Blog side support routines
  5. $LastChangedDate: 2011-03-11 08:12:39 -0700 (Fri, 11 Mar 2011) $
  6. $Rev: 5665 $
  7. */
  8. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
  9. die('Access Denied');
  10. }
  11. include_once(SF_PLUGIN_DIR.'/linking/forms/sf-form-blog-link.php');
  12. include_once(SF_PLUGIN_DIR.'/linking/library/sf-links-support.php');
  13. # ------------------------------------------------------------------
  14. # sf_save_blog_link()
  15. #
  16. # Filter call
  17. # Called on a Post Save to create the blog/forum Link
  18. # $postid id of the blog post to link to
  19. # ------------------------------------------------------------------
  20. function sf_save_blog_link($postid)
  21. {
  22. global $current_user;
  23. # can the user do this?
  24. if(isset($_POST['sflink']) && $_POST['sflink'])
  25. {
  26. $blogpost = sf_get_postrecord($postid);
  27. if($blogpost)
  28. {
  29. # if revision or autosave go get parent post id
  30. if($blogpost->post_type == 'revision' && $blogpost->post_parent > 0)
  31. {
  32. $postid = $blogpost->post_parent;
  33. }
  34. # Prepare data
  35. $forumid = sf_esc_int($_POST['sfforum']);
  36. if(empty($forumid) || $forumid == 0)
  37. {
  38. return;
  39. }
  40. if($_POST['sfedit']==false ? $editmode='0' : $editmode='1');
  41. # Check if we already have a topic
  42. $topicid = '0';
  43. $checktopic = sf_get_linkedtopic($postid);
  44. if(!empty($checktopic) && $checktopic > 0)
  45. {
  46. $topicid = $checktopic;
  47. }
  48. # go get link record if already saved before
  49. $links = sf_blog_links_control('read', $postid);
  50. if($links)
  51. {
  52. # if all links fields have value no need to linger
  53. if (($links->forum_id == $forumid) && ($links->topic_id == $topicid) && ($links->syncedit == $editmode))
  54. {
  55. # all data present - so finish up
  56. return;
  57. }
  58. }
  59. # Save links record - will update of already exists
  60. sf_blog_links_control('save', $postid, $forumid, $topicid, $editmode);
  61. }
  62. }
  63. return;
  64. }
  65. # ------------------------------------------------------------------
  66. # sf_publish_blog_link()
  67. #
  68. # Filter call
  69. # Called on a Post Publish to create the blog/forum Link
  70. # $post The Actual Post Object
  71. # ------------------------------------------------------------------
  72. function sf_publish_blog_link($post)
  73. {
  74. global $wpdb, $current_user;
  75. # Check post status for published as it calls this hook regardless of state
  76. if($post->post_status != 'publish') return;
  77. # go get link record if already saved before
  78. $links = sf_blog_links_control('read', $post->ID);
  79. if(!$links) return;
  80. # Prepare data
  81. $forumid = 0;
  82. if(isset($_POST['sfforum']))
  83. {
  84. $forumid = sf_esc_int($_POST['sfforum']);
  85. } else {
  86. if($links) $forumid = $links->forum_id;
  87. }
  88. if(empty($forumid) || $forumid == 0)
  89. {
  90. return;
  91. }
  92. $editmode = '0';
  93. if((isset($_POST['sfedit']) && $_POST['sfedit'] == true) || $links->syncedit == '1')
  94. {
  95. $editmode = '1';
  96. }
  97. # Check if we already have a topic
  98. $topicid = '0';
  99. $checktopic = sf_get_linkedtopic($post->ID);
  100. if(!empty($checktopic) && $checktopic > 0)
  101. {
  102. $topicid = $checktopic;
  103. }
  104. if($links)
  105. {
  106. # if all links fields have value no need to linger
  107. if (($links->forum_id == $forumid) && ($links->topic_id == $topicid && $topicid > 0) && ($links->syncedit == $editmode))
  108. {
  109. # all data present - so finish up
  110. return;
  111. }
  112. }
  113. # so - go ahead and create the topic and link
  114. $sfpostlinking = array();
  115. $sfpostlinking = sf_get_option('sfpostlinking');
  116. $post_title = sf_filter_title_save($post->post_title);
  117. $slug = sf_create_slug($post_title, 'topic');
  118. $now = current_time('mysql');
  119. # now create the topic and post records - it should already be escaped fully.
  120. $sql = "INSERT INTO ".SFTOPICS." (topic_name, topic_slug, topic_date, forum_id, user_id, post_count, blog_post_id, post_id) VALUES ('".$post_title."', '".$slug."', '".$now."', ".$forumid.", ".$post->post_author.", 1, ".$post->ID.", ".$post->ID.");";
  121. $wpdb->query($sql);
  122. $topicid = $wpdb->insert_id;
  123. # check the topic slug and if empty use the topic id
  124. if(empty($slug))
  125. {
  126. $slug = 'topic-'.$topicid;
  127. $thistopic = $wpdb->query("
  128. UPDATE ".SFTOPICS."
  129. SET topic_slug='".$slug."', topic_name='".$slug."'
  130. WHERE topic_id=".$topicid);
  131. }
  132. $postcontent = sf_prepare_linked_topic_content($post->post_content, $post->post_excerpt, $sfpostlinking);
  133. $sql = "INSERT INTO ".SFPOSTS." (post_content, post_date, topic_id, user_id, forum_id) VALUES ('".$postcontent."', '".$now."', ".$topicid.", ".$post->post_author.", ".$forumid.");";
  134. $wpdb->query($sql);
  135. # and then update links table with forum AND topic
  136. sf_blog_links_control('save', $post->ID, $forumid, $topicid, $editmode);
  137. # Update authors forum post count
  138. $postcount = (sf_get_member_item($post->post_author, 'posts')+1);
  139. sf_update_member_item($post->post_author, 'posts', $postcount);
  140. # sync blog and forum tags
  141. sf_sync_blog_tags($post->ID, $forumid, $topicid);
  142. # Update forum, topic and post index data
  143. sf_build_forum_index(sf_esc_int($forumid));
  144. sf_build_post_index($topicid, $slug);
  145. return;
  146. }
  147. # ------------------------------------------------------------------
  148. # sf_prepare_linked_topic_content()
  149. #
  150. # prepares blog post content for the topic post
  151. # $content full content o the blog post
  152. # $excerpt excerpt of the blog post
  153. # $sfpostlinking link options
  154. # ------------------------------------------------------------------
  155. function sf_prepare_linked_topic_content($content, $excerpt, $sfpostlinking)
  156. {
  157. $content = sf_filter_content_save($content, 'new');
  158. switch($sfpostlinking['sflinkexcerpt'])
  159. {
  160. case 3:
  161. $postcontent = sf_filter_content_save($excerpt, 'new');
  162. break;
  163. case 2:
  164. $postcontent = sf_make_excerpt($content, $sfpostlinking['sflinkwords']);
  165. break;
  166. default:
  167. $postcontent = $content;
  168. break;
  169. }
  170. return apply_filters('sph_add_custom_post_content', $postcontent);
  171. }
  172. # ------------------------------------------------------------------
  173. # sf_update_blog_link()
  174. #
  175. # Filter call
  176. # Called on a Post Edit to update the blog/forum Link
  177. # $postid id of the blog post to link to
  178. # ------------------------------------------------------------------
  179. function sf_update_blog_link($postid)
  180. {
  181. global $wpdb, $current_user;
  182. # This could be an update to post content OR a new link on existing post so check
  183. # If new link on exisiting post this gets run before the 'save_post' hook so we need to force a save post first
  184. sf_save_blog_link($postid);
  185. $links = sf_blog_links_control('read', $postid);
  186. if($links && $links->topic_id == 0)
  187. {
  188. # then a new link on existing blog post so get post object
  189. $postrecord = sf_get_postrecord($postid);
  190. if($postrecord)
  191. {
  192. sf_publish_blog_link($postrecord);
  193. return;
  194. }
  195. }
  196. # probably an edit to content then
  197. if((isset($_POST['sfedit']) && $_POST['sfedit'] == true) || $links->syncedit == true)
  198. {
  199. $post = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."posts WHERE ID=".$postid);
  200. if($post)
  201. {
  202. # first - get the options
  203. $sfpostlinking = array();
  204. $sfpostlinking = sf_get_option('sfpostlinking');
  205. $postcontent = sf_prepare_linked_topic_content($post->post_content, $post->post_excerpt, $sfpostlinking);
  206. $sql = "UPDATE ".SFPOSTS." SET post_content='".$postcontent."' WHERE topic_id=".$links->topic_id." AND post_index=1";
  207. $wpdb->query($sql);
  208. }
  209. }
  210. return;
  211. }
  212. # ------------------------------------------------------------------
  213. # sf_show_blog_link()
  214. #
  215. # Filter call
  216. # Adds the user-defined link text to a blog post
  217. # $content The content of the target post
  218. # ------------------------------------------------------------------
  219. if(!function_exists('sf_show_blog_link')):
  220. function sf_show_blog_link($content)
  221. {
  222. global $wp_query;
  223. $postid = $wp_query->post->ID;
  224. $links = sf_blog_links_control('read', $postid);
  225. if(!$links) return $content;
  226. #show only on single pages?
  227. $sfpostlinking = sf_get_option('sfpostlinking');
  228. if ($sfpostlinking['sflinksingle'] && !is_single()) return $content;
  229. $out = sf_transform_bloglink_label($postid, $links, true);
  230. if($sfpostlinking['sflinkabove'])
  231. {
  232. return $out.$content;
  233. } else {
  234. return $content.$out;
  235. }
  236. }
  237. endif;
  238. # ------------------------------------------------------------------
  239. # sf_delete_blog_link()
  240. #
  241. # Action call
  242. # Removes forum link if blog post is deleted
  243. # $postid ID of the post being deleted
  244. # ------------------------------------------------------------------
  245. function sf_delete_blog_link($postid)
  246. {
  247. global $current_user;
  248. if(!$current_user->sfbreaklink) return;
  249. include_once(SF_PLUGIN_DIR.'/linking/sf-links-forum.php');
  250. $links = sf_blog_links_control('read', $postid);
  251. if($links)
  252. {
  253. # Check - this might be a Revision record
  254. if($links->forum_id != 0)
  255. {
  256. sf_break_blog_link($links->topic_id, $postid);
  257. }
  258. }
  259. return;
  260. }
  261. # ------------------------------------------------------------------
  262. # sf_add_admin_link_column()
  263. #
  264. # Filter call
  265. # Adds link column to edit posts/pages
  266. # ------------------------------------------------------------------
  267. function sf_add_admin_link_column($defaults)
  268. {
  269. $defaults['spflinked'] = __('Forum Linked', "sforum");
  270. return $defaults;
  271. }
  272. # ------------------------------------------------------------------
  273. # sf_add_admin_link_column()
  274. #
  275. # Action call
  276. # Displays link column info in edit posts/pages
  277. # ------------------------------------------------------------------
  278. function sf_show_admin_link_column($column, $postid)
  279. {
  280. if($column == 'spflinked')
  281. {
  282. $links = sf_blog_links_control('read', $postid);
  283. if($links)
  284. {
  285. echo __("Linked to forum", "sforum").':<br />'.sf_get_forum_name_from_id($links->forum_id);
  286. }
  287. }
  288. }
  289. function sf_get_postrecord($postid)
  290. {
  291. global $wpdb;
  292. return $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."posts WHERE ID=".$postid);
  293. }
  294. function sf_get_linkedtopic($blogpostid)
  295. {
  296. global $wpdb;
  297. return $wpdb->get_var("SELECT topic_id FROM ".SFTOPICS." WHERE blog_post_id=".$blogpostid);
  298. }
  299. ?>