PageRenderTime 3ms CodeModel.GetById 80ms app.highlight 31ms RepoModel.GetById 29ms app.codeStats 1ms

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