.
*/
// JSON services for PHP4
if(!function_exists('json_encode'))
{
include_once('json.php');
$GLOBALS['JSON_OBJECT'] = new Services_JSON();
function json_encode($value)
{
return $GLOBALS['JSON_OBJECT']->encode($value);
}
function json_decode($value)
{
return $GLOBALS['JSON_OBJECT']->decode($value);
}
}
class RetweetAnywhereWidget extends WP_Widget {
function RetweetAnywhereWidget()
{
$widget_ops = array('classname' => 'widget-retweet-anywhere', 'description' => __("A retweet widget for your blog"));
$this->WP_Widget('widget-retweet-anywhere', __('Retweet Anywhere'), $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$title = apply_filters('widget_title', $instance['title']);
$format = $instance['format'];
$width = $instance['width'];
$height = $instance['height'];
global $wp_query;
if ($wp_query->in_the_loop || is_singular())
{
global $post;
$post_id = $post->ID;
}
else
$post_id = 0;
echo $before_widget;
echo "
";
echo $after_widget;
}
function update($new_instance, $old_instance)
{
return $new_instance;
}
function form($instance)
{
$instance = wp_parse_args((array) $instance, array('title' => 'Retweet', 'format' => '%s %l', 'width' => '200', 'height' => '70'));
$title = $instance['title'];
$format = $instance['format'];
$width = $instance['width'];
$height = $instance['height'];
?>
Described in the FAQ
default_settings = array(
"title" => "Retweet This Post",
"format" => "%s %l",
"shortener" => "native",
"placement" => "end",
"style" => "default",
"opacity" => "0.5",
"widget" => "no",
);
// Load the plugin settings and merge with defaults
$this->settings = (array) get_option("retweet-anywhere");
$this->settings = array_merge($this->default_settings, $this->settings);
// Action hooks
add_action("wp_enqueue_scripts", array(&$this, "wp_enqueue_scripts"));
// AJAX hooks to get message
add_action("wp_ajax_rta_getmessage", array(&$this, "ajax_getmessage"));
add_action("wp_ajax_nopriv_rta_getmessage", array(&$this, "ajax_getmessage"));
// Admin system customization (menus, notices, etc)
add_action("admin_menu", array(&$this, "admin_menu"));
add_action("admin_init", array(&$this, "admin_init"));
add_action("admin_notices", array(&$this, "admin_notices"));
// Shortcode and content filters
if ($this->settings["placement"] != "manual")
add_filter("the_content", array(&$this, "the_content"));
add_shortcode("retweet-anywhere", array(&$this, "shortcode"));
// Enable the shortcode for widget text if set in the settings
if ($this->settings["widget"] == "yes")
{
add_filter("widget_text", "do_shortcode");
}
// Administration notices
if (empty($this->settings["api_key"]))
$this->notices[] = "Please configure your Twitter API key in order to use Retweet Anywhere: Plugin Settings";
if ($this->settings["shortener"] == "bitly" && (empty($this->settings["bitly_username"]) || empty($this->settings["bitly_api_key"])))
$this->notices[] = "Bit.ly requires a username and a valid API key in order to work. Plugin Settings";
}
// Widget registration
function widgets_init()
{
register_widget("RetweetAnywhereWidget");
}
// Settings management
function admin_init()
{
register_setting('retweet-anywhere', 'retweet-anywhere');
}
// Plugin settings page
function admin_menu()
{
$page = add_submenu_page('options-general.php', 'Retweet Anywhere Settings', 'Retweet Anywhere', 'administrator', __FILE__, array(&$this, "settings_page"));
add_action("admin_print_scripts-{$page}", array(&$this, "admin_print_scripts"));
}
// Admin panel scripts
function admin_print_scripts()
{
wp_enqueue_script("retweet-anywhere-admin", plugins_url("/js/admin.js", __FILE__), array("jquery"));
}
// Settings page content
function settings_page()
{
?>
Retweet Anywhere Settings
settings["placement"])
{
case "beginning":
$content = "[retweet-anywhere]\r\n" . $content;
break;
case "end":
$content = $content . "\r\n[retweet-anywhere]";
break;
}
return $content;
}
// Shortcode
function shortcode($atts)
{
// Extract the attributes
extract(shortcode_atts(array(
"html" => false,
"format" => false,
"title" => false,
), $atts));
if (!$html) $html = $this->get_button();
if (!$format) $format = "";
if (!$title) $title = $this->settings["title"];
$message = '';
// Let's see if we're inside the loop
global $wp_query;
if ($wp_query->in_the_loop || is_singular())
{
global $post;
return 'guid) . '" class="retweet-anywhere" title="' . $title . '" rev="' . $format . '" rel="' . $post->ID . '">' . $html . '';
}
else
{
// Not inside the loop, link to the current page
return '';
}
}
// Echo the button according to the style
function get_button()
{
switch ($this->settings["style"])
{
case "default":
return '
';
break;
case "text":
return 'Retweet';
break;
case "html":
return $this->settings["style_html"];
break;
}
}
function wp_enqueue_scripts()
{
wp_enqueue_style("facebox", plugins_url("/css/facebox.css", __FILE__));
$api_key = $this->settings["api_key"];
wp_enqueue_script("twitter-anywhere", "http://platform.twitter.com/anywhere.js?id={$api_key}&v=1");
wp_enqueue_script("facebox", plugins_url("/js/facebox.js", __FILE__), array("jquery"));
wp_enqueue_script("retweet-anywhere", plugins_url("/js/retweet-anywhere.js", __FILE__), array("jquery", "facebox"));
wp_localize_script("retweet-anywhere", "RetweetAnywhere", array(
"ajaxurl" => admin_url('admin-ajax.php'),
"loadingImage" => plugins_url("/images/facebox/loading.gif", __FILE__),
"closeImage" => plugins_url("/images/facebox/closelabel.gif", __FILE__),
"opacity" => $this->settings["opacity"],
"title" => $this->settings["title"]
));
}
// The AJAX call to retrieve the message
function ajax_getmessage()
{
// Get all the details
$post_id = $_POST["post_id"];
$format = $_POST["format"];
if (empty($format))
$format = $this->settings["format"];
// If we don't need %s or %l data then we don't query for the post
if (strpos($format, "%s") === false && strpos($format, "%l") === false)
{
$message = $format;
}
else
{
if ($post_id == 0)
{
// If the post_id is 0 then link to the homepage
$title = get_bloginfo("title");
$url = get_bloginfo("home");
}
else
{
// Get the post data
$post = get_post($post_id);
$title = $post->post_title;
$url = get_permalink($post_id);
}
// Shorten the link if we need to
if ($this->settings["shortener"] == "bitly")
$url = $this->shorten($url, $post_id);
elseif ($this->settings["shortener"] == "native")
$url = $this->shorten_native($url, $post_id);
elseif ($this->settings['shortener'] != 'none')
{
$this->shorteners = apply_filters('retweet-anywhere-shorteners', $this->shorteners);
if (function_exists($this->shorteners[$this->settings['shortener']]['callback']))
{
$f = $this->shorteners[$this->settings['shortener']]['callback'];
$url = $f($url);
}
}
// Format the message
$replace = array(
"%s" => $title,
"%l" => $url
);
$message = str_replace(array_keys($replace), array_values($replace), $format);
}
// Format the response array
$response = array(
"message" => $message
);
// JSON encode, print and die
echo json_encode($response);
die();
}
function shorten_native($url, $post_id = 0)
{
global $post;
$post = get_post($post_id);
$shortlink = wp_get_shortlink();
if ($shortlink)
return $shortlink;
else
return $url;
}
// Shorten the URL via bit.ly
function shorten($url, $post_id = 0)
{
if ($post_id > 0)
{
$short = get_post_meta($post_id, 'rta-shorturl', true);
if (!empty($short))
return $short;
}
// Let's get the WP_Http class if we don't have one
if(!class_exists('WP_Http'))
include_once(ABSPATH . WPINC . '/class-http.php');
// Encode the url
$url_encoded = urlencode($url);
// Get the bit.ly settings
$bitly_login = urlencode(trim($this->settings["bitly_username"]));
$bitly_key = urlencode(trim($this->settings["bitly_api_key"]));
// Init $http and fire the request
$http = new WP_Http();
$result = $http->request("http://api.bit.ly/v3/shorten?login={$bitly_login}&apiKey={$bitly_key}&uri={$url_encoded}&format=json");
if (gettype($result) == "object")
if (get_class($result) == "WP_Error")
return $url;
// JSON decode the result body and return the data->url
$result = json_decode($result["body"]);
$result = $result->data;
$shorturl = $result->url;
// Store the shortened URL
if (!empty($shorturl) && $post_id > 0)
{
$post = get_post($post_id);
//return print_r($post, true);
if ($post->post_status != 'draft' && $post->post_type != 'revision')
update_post_meta($post_id, 'rta-shorturl', $shorturl);
}
return $shorturl;
}
// Administration notices
function admin_notices()
{
$this->notices = array_unique($this->notices);
foreach($this->notices as $key => $value)
{
echo "Retweet Anywhere: " . $value . "
";
}
}
}
// Used for manual mode (PHP)
function retweet_anywhere() {
echo do_shortcode("[retweet-anywhere]");
}
// Initialize the environment
add_action("init", create_function('', 'global $RetweetAnywhere; $RetweetAnywhere = new RetweetAnywhere();'));
add_action("widgets_init", create_function('', 'return register_widget("RetweetAnywhereWidget");'));