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

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

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 207 lines | 148 code | 36 blank | 23 comment | 29 complexity | 2d035274b0525f7867cb063fd4f751e0 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. Suggested tags
  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. if ( $_GET['sfaction'] == 'tags_from_yahoo' ) {
  13. sf_suggest_yahoo_tags();
  14. } elseif ( $_GET['sfaction'] == 'tags_from_tagthenet' ) {
  15. sf_suggest_ttn_tags();
  16. } elseif ( $_GET['sfaction'] == 'tags_from_local_db' ) {
  17. sf_suggest_local_tags();
  18. }
  19. die();
  20. function sf_suggest_local_tags()
  21. {
  22. global $wpdb;
  23. # Send good header HTTP
  24. status_header(200);
  25. header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
  26. # Get existing tags
  27. $tags = $wpdb->get_col("SELECT DISTINCT tag_name FROM ".SFTAGS);
  28. if (empty($tags)) # No tags to suggest
  29. {
  30. echo '<p>'.__('There are no tags for Simple:Press.', 'sforum').'</p>';
  31. exit();
  32. }
  33. # Get topic name and data
  34. $content = $_POST['content'] .' '. $_POST['title'];
  35. $content = trim($content);
  36. if (empty($content))
  37. {
  38. echo '<p>'.__('You need to create a topic title and post content before tags can be suggested.', 'sforum').'</p>';
  39. exit();
  40. }
  41. $found = 0;
  42. foreach ((array) $tags as $tag)
  43. {
  44. $tag = $tag;
  45. if (is_string($tag) && !empty($tag) && stristr($content, $tag))
  46. {
  47. echo '<span class="local">'.$tag.'</span>'."\n";
  48. $found = 1;
  49. }
  50. }
  51. if (!$found)
  52. {
  53. echo '<p>'.__('No suggested tags from existing Simple:Press tags.', 'sforum').'</p>';
  54. exit();
  55. }
  56. echo '<div class="clear"></div>';
  57. exit();
  58. }
  59. function sf_suggest_ttn_tags()
  60. {
  61. # Send good header HTTP
  62. status_header( 200 );
  63. header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
  64. # Get topic name and data
  65. $content = $_POST['content'] .' '. $_POST['title'];
  66. $content = trim($content);
  67. if (empty($content))
  68. {
  69. echo '<p>'.__('You need to create a topic title and post content before tags can be suggested.', 'sforum').'</p>';
  70. exit();
  71. }
  72. # Get Tag This Net tags
  73. $data = '';
  74. $reponse = wp_remote_post('http://tagthe.net/api/?text='.urlencode($content).'&view=json&count=200');
  75. if (!is_wp_error($reponse) ) {
  76. $code = wp_remote_retrieve_response_code($reponse);
  77. if ($code == 200)
  78. {
  79. $data = maybe_unserialize(wp_remote_retrieve_body($reponse));
  80. }
  81. }
  82. $data = json_decode($data);
  83. $data = $data->memes[0];
  84. $data = $data->dimensions;
  85. if ( !isset($data->topic) && !isset($data->location) && !isset($data->person) ) {
  86. echo '<p>'.__('No suggested tags from Tag The Net service.', 'sforum').'</p>';
  87. exit();
  88. }
  89. $tags = array();
  90. # Get all topics
  91. foreach ((array) $data->topic as $topic)
  92. {
  93. $tags[] = '<span class="ttn_topic">'.$topic.'</span>';
  94. }
  95. # Get all locations
  96. foreach ( (array) $data->location as $location ) {
  97. $tags[] = '<span class="ttn_location">'.$location.'</span>';
  98. }
  99. # Get all persons
  100. foreach ((array) $data->person as $person)
  101. {
  102. $tags[] = '<span class="ttn_person">'.$person.'</span>';
  103. }
  104. # Remove empty terms
  105. $tags = array_filter($tags, 'sf_delete_empty_element');
  106. $tags = array_unique($tags);
  107. echo implode("\n", $tags);
  108. echo '<div class="clear"></div>';
  109. exit();
  110. }
  111. function sf_suggest_yahoo_tags()
  112. {
  113. # Send good header HTTP
  114. status_header( 200 );
  115. header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
  116. # Get topic name and data
  117. $content = $_POST['content'] .' '. $_POST['title'];
  118. $content = trim($content);
  119. if (empty($content))
  120. {
  121. echo '<p>'.__('You need to create a topic title and post content before tags can be suggested.', 'sforum').'</p>';
  122. exit();
  123. }
  124. # Application entrypoint -> http://code.google.com/p/simple-tags/
  125. # Yahoo ID : h4c6gyLV34Fs7nHCrHUew7XDAU8YeQ_PpZVrzgAGih2mU12F0cI.ezr6e7FMvskR7Vu.AA--
  126. $yahoo_id = 'h4c6gyLV34Fs7nHCrHUew7XDAU8YeQ_PpZVrzgAGih2mU12F0cI.ezr6e7FMvskR7Vu.AA--';
  127. # Build params
  128. $param = 'appid='.$yahoo_id; # Yahoo ID
  129. $param .= '&context='.urlencode($content); # Post content
  130. if (!empty($_POST['tags']))
  131. {
  132. $param .= '&query='.urlencode($_POST['tags']); # Existing tags
  133. }
  134. $param .= '&output=php'; # Get PHP Array !
  135. $data = array();
  136. $reponse = wp_remote_post('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction?'.$param);
  137. if (!is_wp_error($reponse) && $reponse != null)
  138. {
  139. $code = wp_remote_retrieve_response_code($reponse);
  140. if ($code == 200)
  141. {
  142. $data = maybe_unserialize(wp_remote_retrieve_body($reponse));
  143. }
  144. }
  145. if (empty($data) || empty($data['ResultSet']) || is_wp_error($data))
  146. {
  147. echo '<p>'.__('No suggested tags from Yahoo! service.', 'sforum').'</p>';
  148. exit();
  149. }
  150. # Get result value
  151. $data = (array) $data['ResultSet']['Result'];
  152. # Remove empty terms
  153. $data = array_filter($data, 'sf_delete_empty_element');
  154. $data = array_unique($data);
  155. foreach ((array) $data as $tag)
  156. {
  157. echo '<span class="yahoo">'.$tag.'</span>'."\n";
  158. }
  159. echo '<div class="clear"></div>';
  160. exit();
  161. }
  162. function sf_delete_empty_element(&$element)
  163. {
  164. $element = $element;
  165. $element = trim($element);
  166. if (!empty($element))
  167. {
  168. return $element;
  169. }
  170. }
  171. ?>