PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 175 lines | 111 code | 24 blank | 40 comment | 29 complexity | ba8d1fa1cabab96e1ea39f252f298451 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 - general support routines
  5. $LastChangedDate: 2010-09-11 14:51:26 -0700 (Sat, 11 Sep 2010) $
  6. $Rev: 4613 $
  7. */
  8. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
  9. die('Access Denied');
  10. }
  11. # ------------------------------------------------------------------
  12. # sf_blog_links_control()
  13. #
  14. # MOVED to sf-public.php to make it always available as can be
  15. # used for canonical urls.
  16. # ------------------------------------------------------------------
  17. # ------------------------------------------------------------------
  18. # sf_make_excerpt()
  19. #
  20. # Creates an excerpt of x number of words from post content
  21. # $content The text of the post
  22. # $words Word count required (defaults to 50)
  23. # ------------------------------------------------------------------
  24. function sf_make_excerpt($content, $words)
  25. {
  26. if((empty($words)) || ($words == 0)) $words = 50;
  27. if($content != '')
  28. {
  29. $length = $words;
  30. $content = str_replace(']]>', ']]&gt;', $content);
  31. if($length > count(preg_split('/[\s]+/', strip_tags($content), -1)))
  32. {
  33. return $content;
  34. }
  35. $text_bits = preg_split('/([\s]+)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
  36. $in_tag = false;
  37. $n_words = 0;
  38. $content = '';
  39. foreach($text_bits as $chunk)
  40. {
  41. if(0 < preg_match('/<[^>]*$/s', $chunk))
  42. {
  43. $in_tag = true;
  44. } elseif(0 < preg_match('/>[^<]*$/s', $chunk)) {
  45. $in_tag = false;
  46. }
  47. if(!$in_tag && '' != trim($chunk) && substr($chunk, -1, 1) != '>')
  48. {
  49. $n_words++;
  50. }
  51. $content .= $chunk;
  52. if($n_words >= $length && !$in_tag)
  53. {
  54. break;
  55. }
  56. }
  57. $content = $content . '&#8230;';
  58. $content = force_balance_tags($content);
  59. }
  60. return $content;
  61. }
  62. # ------------------------------------------------------------------
  63. # sf_sync_blog_tags()
  64. #
  65. # Creates topic tags based on blog post tags if used
  66. # $postid WP Post id of the link
  67. # $data forum/topic postmeta data
  68. # ------------------------------------------------------------------
  69. function sf_sync_blog_tags($postid, $forumid, $topicid)
  70. {
  71. # get tags for wp blog post
  72. $terms = apply_filters( 'get_the_tags', wp_get_object_terms($postid, 'post_tag') );
  73. # get the forum id and topic id ($data => forumid@topicid)
  74. $forum = sf_get_forum_record($forumid);
  75. # only do tags if the forum is setup for tags and the blog post has tags
  76. if ($forum->use_tags && $terms)
  77. {
  78. $tags = array();
  79. foreach ($terms as $term)
  80. {
  81. $tags[] = $term->name;
  82. }
  83. # need tags in a list
  84. $tags = implode(",", $tags);
  85. # now save the topic tags but in case its an update use change routine
  86. sf_change_topic_tags($topicid, $tags);
  87. }
  88. }
  89. # ------------------------------------------------------------------
  90. # sf_transform_bloglink_label()
  91. #
  92. # Formats the label to display on blog posts
  93. # $postid WP Post id of the link
  94. # $links forum/topic postmeta data
  95. # $show_img Support template tag no image option
  96. # ------------------------------------------------------------------
  97. function sf_transform_bloglink_label($postid, $links, $show_img=true)
  98. {
  99. if(!defined('SFRESOURCES')) {
  100. sf_setup_global_constants();
  101. }
  102. $out = '';
  103. $sfpostlinking = array();
  104. $sfpostlinking = sf_get_option('sfpostlinking');
  105. $text = sf_filter_title_display($sfpostlinking['sflinkblogtext']);
  106. $icon = '<img src="'.SFRESOURCES.'bloglink.png" alt="" />';
  107. $postcount = sf_get_posts_count_in_linked_topic($links->topic_id, false);
  108. $href = '<a href="'.sf_build_url(sf_get_forum_slug($links->forum_id), sf_get_topic_slug($links->topic_id), 1, 0).'">';
  109. if(!$postcount)
  110. {
  111. # break the link
  112. sf_blog_links_control('delete', $postid);
  113. return $content;
  114. }
  115. if ($show_img)
  116. {
  117. if(strpos($text, '%ICON%') !== false)
  118. {
  119. $text = str_replace('%ICON%', $icon, $text);
  120. }
  121. } else {
  122. $text = str_replace('%ICON%', '', $text);
  123. }
  124. if(strpos($text, '%FORUMNAME%') !== false)
  125. {
  126. $text = str_replace('%FORUMNAME%', sf_get_forum_name_from_id($links->forum_id), $text);
  127. }
  128. if(strpos($text, '%TOPICNAME%') !== false)
  129. {
  130. $text = str_replace('%TOPICNAME%', sf_get_topic_name_from_id($links->topic_id), $text);
  131. }
  132. if(strpos($text, '%POSTCOUNT%') !== false)
  133. {
  134. $text = str_replace('%POSTCOUNT%', $postcount, $text);
  135. }
  136. if(strpos($text, '%LINKSTART%') !== false)
  137. {
  138. $text = str_replace('%LINKSTART%', $href, $text);
  139. } else {
  140. $text = $href.$text;
  141. }
  142. if(strpos($text, '%LINKEND%') !== false)
  143. {
  144. $text = str_replace('%LINKEND%', '</a>', $text);
  145. } else {
  146. $text = $text.'</a>';
  147. }
  148. $out = '<span class="sfforumlink">'.$text.'</span>';
  149. return $out;
  150. }
  151. ?>