/wp-content/plugins/simple-tags/inc/class.admin.suggest.php

https://bitbucket.org/Wallynm/iptb · PHP · 449 lines · 307 code · 64 blank · 78 comment · 62 complexity · 14ef5229eee0520a8fd473c73fffd1c8 MD5 · raw file

  1. <?php
  2. class SimpleTags_Admin_Suggest extends SimpleTags_Admin {
  3. // Application entrypoint -> http://redmine.beapi.fr/projects/show/simple-tags/
  4. var $yahoo_id = 'h4c6gyLV34Fs7nHCrHUew7XDAU8YeQ_PpZVrzgAGih2mU12F0cI.ezr6e7FMvskR7Vu.AA--';
  5. function SimpleTags_Admin_Suggest() {
  6. // Ajax action, JS Helper and admin action
  7. add_action('wp_ajax_'.'simpletags', array(&$this, 'ajaxCheck'));
  8. // Box for post/page
  9. add_action('admin_menu', array(&$this, 'helperSuggestTags'), 1);
  10. // Javascript
  11. add_action('admin_enqueue_scripts', array(&$this, 'initJavaScript'), 11);
  12. }
  13. /**
  14. * Init somes JS and CSS need for this feature
  15. *
  16. * @return void
  17. * @author Amaury Balmer
  18. */
  19. function initJavaScript() {
  20. global $pagenow;
  21. wp_register_script('st-helper-suggested-tags', STAGS_URL.'/inc/js/helper-suggested-tags.min.js', array('jquery', 'st-helper-add-tags'), STAGS_VERSION);
  22. wp_localize_script('st-helper-suggested-tags', 'stHelperSuggestedTagsL10n', array( 'title_bloc' => $this->getSuggestTagsTitle(), 'content_bloc' => __('Choose a provider to get suggested tags (local, yahoo or tag the net).', 'simpletags') ) );
  23. // Register location
  24. $wp_post_pages = array('post.php', 'post-new.php');
  25. $wp_page_pages = array('page.php', 'page-new.php');
  26. // Helper for posts/pages
  27. if ( in_array($pagenow, $wp_post_pages) || (in_array($pagenow, $wp_page_pages) && is_page_have_tags() ) ) {
  28. wp_enqueue_script('st-helper-suggested-tags');
  29. }
  30. }
  31. /**
  32. * Get Suggested tags title
  33. *
  34. */
  35. function getSuggestTagsTitle() {
  36. $title = '<img style="float:right; display:none;" id="st_ajax_loading" src="'.STAGS_URL.'/inc/images/ajax-loader.gif" alt="' .__('Ajax loading', 'simpletags').'" />';
  37. $title .= __('Suggested tags from :', 'simpletags').'&nbsp;&nbsp;';
  38. $title .= '<a class="local_db" href="#suggestedtags">'.__('Local tags', 'simpletags').'</a>&nbsp;&nbsp;-&nbsp;&nbsp;';
  39. $title .= '<a class="yahoo_api" href="#suggestedtags">'.__('Yahoo', 'simpletags').'</a>&nbsp;&nbsp;-&nbsp;&nbsp;';
  40. $title .= '<a class="opencalais_api" href="#suggestedtags">'.__('OpenCalais', 'simpletags').'</a>&nbsp;&nbsp;-&nbsp;&nbsp;';
  41. $title .= '<a class="alchemyapi" href="#suggestedtags">'.__('AlchemyAPI', 'simpletags').'</a>&nbsp;&nbsp;-&nbsp;&nbsp;';
  42. $title .= '<a class="zemanta" href="#suggestedtags">'.__('Zemanta', 'simpletags').'</a>&nbsp;&nbsp;-&nbsp;&nbsp;';
  43. $title .= '<a class="ttn_api" href="#suggestedtags">'.__('Tag The Net', 'simpletags').'</a>';
  44. return $title;
  45. }
  46. /**
  47. * Register metabox for suggest tags, for post, and optionnaly page.
  48. *
  49. * @return void
  50. * @author Amaury Balmer
  51. */
  52. function helperSuggestTags() {
  53. add_meta_box('suggestedtags', __('Suggested tags', 'simpletags'), array(&$this, 'boxSuggestTags'), 'post', 'advanced', 'core');
  54. if ( is_page_have_tags() )
  55. add_meta_box('suggestedtags', __('Suggested tags', 'simpletags'), array(&$this, 'boxSuggestTags'), 'page', 'advanced', 'core');
  56. }
  57. /**
  58. * Print HTML for suggest tags box
  59. *
  60. **/
  61. function boxSuggestTags() {
  62. ?>
  63. <span class="container_clicktags">
  64. <?php echo $this->getDefaultContentBox(); ?>
  65. <div class="clear"></div>
  66. </span>
  67. <?php
  68. }
  69. /**
  70. * Ajax Dispatcher
  71. *
  72. */
  73. function ajaxCheck() {
  74. if ( isset($_GET['st_action']) ) {
  75. switch( $_GET['st_action'] ) {
  76. case 'tags_from_opencalais' :
  77. $this->ajaxOpenCalais();
  78. break;
  79. case 'tags_from_alchemyapi' :
  80. $this->ajaxAlchemyApi();
  81. break;
  82. case 'tags_from_zemanta' :
  83. $this->ajaxZemanta();
  84. break;
  85. case 'tags_from_yahoo' :
  86. $this->ajaxYahooTermExtraction();
  87. break;
  88. case 'tags_from_tagthenet' :
  89. $this->ajaxTagTheNet();
  90. break;
  91. case 'tags_from_local_db' :
  92. $this->ajaxSuggestLocal();
  93. break;
  94. }
  95. }
  96. }
  97. function getParamsXML() {
  98. return '
  99. <c:params xmlns:c="http://s.opencalais.com/1/pred/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  100. <c:processingDirectives c:contentType="text/html" c:outputFormat="Text/Simple" c:enableMetadataType="GenericRelations,SocialTags"></c:processingDirectives>
  101. <c:userDirectives c:allowDistribution="false" c:allowSearch="false" c:externalID="" c:submitter="Simple Tags"></c:userDirectives>
  102. <c:externalMetadata></c:externalMetadata>
  103. </c:params>
  104. ';
  105. }
  106. /**
  107. * Suggest tags from OpenCalais Service
  108. *
  109. */
  110. function ajaxOpenCalais() {
  111. // Get options
  112. $options = get_option( STAGS_OPTIONS_NAME );
  113. status_header( 200 );
  114. header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
  115. // API Key ?
  116. if ( empty($options['opencalais_key']) ) {
  117. echo '<p>'.__('OpenCalais need an API key to work. You can register on service website to obtain a key and set it on Simple Tags options.', 'simpletags').'</p>';
  118. exit();
  119. }
  120. // Get data
  121. $content = stripslashes($_POST['content']) .' '. stripslashes($_POST['title']);
  122. $content = trim($content);
  123. if ( empty($content) ) {
  124. echo '<p>'.__('No text was sent.', 'simpletags').'</p>';
  125. exit();
  126. }
  127. $reponse = wp_remote_post('http://api.opencalais.com/enlighten/rest/', array('body' => array(
  128. 'licenseID' => $options['opencalais_key'],
  129. 'content' => $content,
  130. 'paramsXML' => $this->getParamsXML()
  131. )));
  132. if( !is_wp_error($reponse) && $reponse != null ) {
  133. if ( wp_remote_retrieve_response_code($reponse) == 200 ) {
  134. $data = $results = array();
  135. preg_match('/<CalaisSimpleOutputFormat>(.*?)<\/CalaisSimpleOutputFormat>/s', wp_remote_retrieve_body($reponse), $data );
  136. preg_match_all('/<(.*?)>(.*?)<\/(.*?)>/s', $data[1], $results );
  137. $data = $results[2];
  138. }
  139. }
  140. if ( empty($data) || is_wp_error($reponse) ) {
  141. echo '<p>'.__('No results from OpenCalais service.', 'simpletags').'</p>';
  142. exit();
  143. }
  144. // Remove empty terms
  145. $data = array_filter($data, '_delete_empty_element');
  146. $data = array_unique($data);
  147. foreach ( (array) $data as $term ) {
  148. echo '<span class="local">'.esc_html(strip_tags($term)).'</span>'."\n";
  149. }
  150. echo '<div class="clear"></div>';
  151. exit();
  152. }
  153. /**
  154. * Suggest tags from AlchemyAPI
  155. *
  156. */
  157. function ajaxAlchemyApi() {
  158. // Get options
  159. $options = get_option( STAGS_OPTIONS_NAME );
  160. status_header( 200 );
  161. header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
  162. // API Key ?
  163. if ( empty($options['alchemy_api']) ) {
  164. echo '<p>'.__('AlchemyAPI need an API key to work. You can register on service website to obtain a key and set it on Simple Tags options.', 'simpletags').'</p>';
  165. exit();
  166. }
  167. // Get data
  168. $content = stripslashes($_POST['content']) .' '. stripslashes($_POST['title']);
  169. $content = trim($content);
  170. if ( empty($content) ) {
  171. echo '<p>'.__('No text was sent.', 'simpletags').'</p>';
  172. exit();
  173. }
  174. // Build params
  175. $data = array();
  176. $reponse = wp_remote_post( 'http://access.alchemyapi.com/calls/html/HTMLGetRankedNamedEntities', array('body' => array(
  177. 'apikey' => $options['alchemy_api'],
  178. 'url' => ' ',
  179. 'html' => $content,
  180. 'outputMode' => 'json'
  181. )));
  182. if( !is_wp_error($reponse) && $reponse != null ) {
  183. if ( wp_remote_retrieve_response_code($reponse) == 200 ) {
  184. $data = wp_remote_retrieve_body($reponse);
  185. }
  186. }
  187. $data = json_decode($data);
  188. $data = $data->entities;
  189. if ( empty($data) ) {
  190. echo '<p>'.__('No results from Alchemy API.', 'simpletags').'</p>';
  191. exit();
  192. }
  193. foreach ( (array) $data as $term ) {
  194. echo '<span class="local">'.esc_html($term->text).'</span>'."\n";
  195. }
  196. echo '<div class="clear"></div>';
  197. exit();
  198. }
  199. /**
  200. * Suggest tags from Zemanta
  201. *
  202. */
  203. function ajaxZemanta() {
  204. // Get options
  205. $options = get_option( STAGS_OPTIONS_NAME );
  206. status_header( 200 );
  207. header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
  208. // API Key ?
  209. if ( empty($options['zemanta_key']) ) {
  210. echo '<p>'.__('Zemanta need an API key to work. You can register on service website to obtain a key and set it on Simple Tags options.', 'simpletags').'</p>';
  211. exit();
  212. }
  213. // Get data
  214. $content = stripslashes($_POST['content']) .' '. stripslashes($_POST['title']);
  215. $content = trim($content);
  216. if ( empty($content) ) {
  217. echo '<p>'.__('No text was sent.', 'simpletags').'</p>';
  218. exit();
  219. }
  220. // Build params
  221. $data = array();
  222. $reponse = wp_remote_post( 'http://api.zemanta.com/services/rest/0.0/', array('body' => array(
  223. 'method' => 'zemanta.suggest',
  224. 'api_key' => $options['zemanta_key'],
  225. 'text' => $content,
  226. 'format' => 'json',
  227. 'return_rdf_links' => 0,
  228. 'return_images' => 0
  229. )));
  230. if( !is_wp_error($reponse) && $reponse != null ) {
  231. if ( wp_remote_retrieve_response_code($reponse) == 200 ) {
  232. $data = wp_remote_retrieve_body($reponse);
  233. }
  234. }
  235. $data = json_decode($data);
  236. $data = $data->keywords;
  237. if ( empty($data) ) {
  238. echo '<p>'.__('No results from Zemanta API.', 'simpletags').'</p>';
  239. exit();
  240. }
  241. foreach ( (array) $data as $term ) {
  242. echo '<span class="local">'.esc_html($term->name).'</span>'."\n";
  243. }
  244. echo '<div class="clear"></div>';
  245. exit();
  246. }
  247. /**
  248. * Suggest tags from Yahoo Term Extraction
  249. *
  250. */
  251. function ajaxYahooTermExtraction() {
  252. status_header( 200 );
  253. header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
  254. // Get data
  255. $content = stripslashes($_POST['content']) .' '. stripslashes($_POST['title']);
  256. $content = trim($content);
  257. if ( empty($content) ) {
  258. echo '<p>'.__('No text was sent.', 'simpletags').'</p>';
  259. exit();
  260. }
  261. // Build params
  262. $param = 'appid='.$this->yahoo_id; // Yahoo ID
  263. $param .= '&context='.urlencode($content); // Post content
  264. if ( !empty($_POST['tags']) ) {
  265. $param .= '&query='.urlencode(stripslashes($_POST['tags'])); // Existing tags
  266. }
  267. $param .= '&output=php'; // Get PHP Array !
  268. $data = array();
  269. $reponse = wp_remote_post( 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction', array('body' =>$param) );
  270. if( !is_wp_error($reponse) && $reponse != null ) {
  271. if ( wp_remote_retrieve_response_code($reponse) == 200 ) {
  272. $data = maybe_unserialize( wp_remote_retrieve_body($reponse) );
  273. }
  274. }
  275. if ( empty($data) || empty($data['ResultSet']) || is_wp_error($data) ) {
  276. echo '<p>'.__('No results from Yahoo! service.', 'simpletags').'</p>';
  277. exit();
  278. }
  279. // Get result value
  280. $data = (array) $data['ResultSet']['Result'];
  281. // Remove empty terms
  282. $data = array_filter($data, '_delete_empty_element');
  283. $data = array_unique($data);
  284. foreach ( (array) $data as $term ) {
  285. echo '<span class="yahoo">'.esc_html($term).'</span>'."\n";
  286. }
  287. echo '<div class="clear"></div>';
  288. exit();
  289. }
  290. /**
  291. * Suggest tags from Tag The Net
  292. *
  293. */
  294. function ajaxTagTheNet() {
  295. // Send good header HTTP
  296. status_header( 200 );
  297. header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
  298. // Get data
  299. $content = stripslashes($_POST['content']) .' '. stripslashes($_POST['title']);
  300. $content = trim($content);
  301. if ( empty($content) ) {
  302. echo '<p>'.__('No text was sent.', 'simpletags').'</p>';
  303. exit();
  304. }
  305. $data = '';
  306. $reponse = wp_remote_post( 'http://tagthe.net/api/n', array('body' => 'text='.urlencode($content).'&view=json&count=200' ) );
  307. if( !is_wp_error($reponse) ) {
  308. if ( wp_remote_retrieve_response_code($reponse) == 200 ) {
  309. $data = maybe_unserialize( wp_remote_retrieve_body($reponse) );
  310. }
  311. }
  312. $data = json_decode($data);
  313. $data = $data->memes[0];
  314. $data = $data->dimensions;
  315. if ( !isset($data->topic) && !isset($data->location) && !isset($data->person) ) {
  316. echo '<p>'.__('No results from Tag The Net service.', 'simpletags').'</p>';
  317. exit();
  318. }
  319. $terms = array();
  320. // Get all topics
  321. if ( isset($data->topic) ) {
  322. foreach ( (array) $data->topic as $topic ) {
  323. $terms[] = '<span class="ttn_topic">'.esc_html($topic).'</span>';
  324. }
  325. }
  326. // Get all locations
  327. if ( isset($data->location) ) {
  328. foreach ( (array) $data->location as $location ) {
  329. $terms[] = '<span class="ttn_location">'.esc_html($location).'</span>';
  330. }
  331. }
  332. // Get all persons
  333. if ( isset($data->person) ) {
  334. foreach ( (array) $data->person as $person ) {
  335. $terms[] = '<span class="ttn_person">'.esc_html($person).'</span>';
  336. }
  337. }
  338. // Remove empty terms
  339. $terms = array_filter($terms, '_delete_empty_element');
  340. $terms = array_unique($terms);
  341. echo implode("\n", $terms);
  342. echo '<div class="clear"></div>';
  343. exit();
  344. }
  345. /**
  346. * Suggest tags from local database
  347. *
  348. */
  349. function ajaxSuggestLocal() {
  350. status_header( 200 ); // Send good header HTTP
  351. header("Content-Type: text/html; charset=" . get_bloginfo('charset'));
  352. if ( ((int) wp_count_terms('post_tag', 'ignore_empty=false')) == 0) { // No tags to suggest
  353. echo '<p>'.__('No terms in your WordPress database.', 'simpletags').'</p>';
  354. exit();
  355. }
  356. // Get data
  357. $content = stripslashes($_POST['content']) .' '. stripslashes($_POST['title']);
  358. $content = trim($content);
  359. if ( empty($content) ) {
  360. echo '<p>'.__('No text was sent.', 'simpletags').'</p>';
  361. exit();
  362. }
  363. // Get all terms
  364. $terms = $this->getTermsForAjax( 'post_tag', '' );
  365. if ( empty($terms) || $terms == false ) {
  366. echo '<p>'.__('No results from your WordPress database.', 'simpletags').'</p>';
  367. exit();
  368. }
  369. $flag = false;
  370. foreach ( (array) $terms as $term ) {
  371. $term = stripslashes($term->name);
  372. if ( is_string($term) && !empty($term) && stristr($content, $term) ) {
  373. $flag = true;
  374. echo '<span class="local">'.esc_html($term).'</span>'."\n";
  375. }
  376. }
  377. if ( $flag == false ) {
  378. echo '<p>'.__('No correspondance between your content and terms from the WordPress database.', 'simpletags').'</p>';
  379. } else {
  380. echo '<div class="clear"></div>';
  381. }
  382. exit();
  383. }
  384. }
  385. ?>