<?php
/**
 * @package Rjmprogramming_Wikipedia_Lookup
 * @version 1.6
 */
/*
Plugin Name: Rjmprogramming Wikipedia Lookup
Description: Float a Wikipedia lookup link for highlighted text of an admin webpage.
Author: Robert James Metcalfe
Version: 1.6
Author URI: http://www.rjmprogramming.com.au/
*/

// This creates the Wikipedia lookup hyperlink
function rjmprogramming_wikipedia_lookup() {
	$wikipedia_page = "Wikipedia Main Page";
	echo '<a target="_blank" title="Wikipedia lookup of ..." onmouseover="var xsel=' . 
	  "''" . '; xsel=window.getSelection().toString(); if (xsel.length == 0) {' . 
	  ' xsel=document.selection.createRange().htmlText;' . 
	  ' } if (xsel.length != 0) { this.innerHTML=xsel; this.href=' . "'" . '//wikipedia.org/wiki/' . 
	  "'" . ' + xsel.replace(/\ /g,' . "'" . '_' . "'" . '); } " ontouchstart="var xsel=' . 
	  '; xsel=window.getSelection().toString(); if (xsel.length == 0) {' . 
	  ' xsel=document.selection.createRange().htmlText; }' . 
	  ' if (xsel.length != 0) { this.innerHTML=xsel; this.href=' . "'" . '//wikipedia.org/wiki/' . "'" . 
	  ' + xsel.replace(/\ /g,' . "'" . '_' . "'" . '); } " id="rjmprogramming_wikipedia_lookup"' . 
	  ' href="//wikipedia.org/wiki/Main_Page">' . $wikipedia_page . '</a>';
}

// Now we set that function up to execute when the admin_notices action is called
add_action( 'admin_notices', 'rjmprogramming_wikipedia_lookup' );

// We need some CSS to position the hyperlink
function rjmprogramming_wikipedia_css() {
	// This makes sure that the positioning is also good for right-to-left languages
	$x = is_rtl() ? 'left' : 'right';

	echo "
	<style type='text/css'>
	#rjmprogramming_wikipedia_lookup {
		float: $x;
		padding-$x: 15px;
		padding-top: 5px;		
		margin: 0;
		font-size: 11px;
	}
	</style>
	";
}

add_action( 'admin_footer', 'rjmprogramming_wikipedia_css' );

?>
