Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
172 lines (152 sloc) 5.7 KB
<?php
/**
Plugin Name: Wp Vanity
Plugin URI: http://axcoto.com/
Description: Shorten url utility for WordPress
Version: 1.0
Author: kureikain <info@axcoto.com>
Author URI: http://axcoto.com/
*/
class Vanity {
static private $_self = null;
const VERSION = '1.0';
const POST_TYPE = 'url';
public $pluginUrl = NULL;
public $pluginPath = NULL;
public $pluginName = NULL;
/**
* Always return the same instance of plugin so we can access its property and its method
* from anywhere
* @return Wfalbum
*/
static public function singleton() {
if (!self::$_self) {
$classname = __CLASS__;
self::$_self = new $classname;
self::$_self->_bootstrap();
}
return self::$_self;
}
/**
* Construct plugin and set useful property for later reference
*/
public function __construct() {
$this->pluginName = dirname(plugin_basename(__FILE__));
$this->pluginUrl = WP_PLUGIN_URL . "/$this->pluginName/";
$this->pluginPath = WP_PLUGIN_DIR . "/$this->pluginName/";
}
/**
* Init cache class!
* Load all styles, script, define JavaScript var with useful URL
*/
private function _bootstrap() {
add_action('init', array(&$this, 'init'));
add_action('add_meta_boxes', array($this, 'add_meta_box'));
add_action('save_post', array($this, 'save_url'));
add_action('get_header', array($this, 'check_url'));
add_filter('manage_edit-' . self::POST_TYPE . '_columns', array($this, 'custom_column'));
add_action('manage_' . self::POST_TYPE . '_posts_custom_column', array($this, 'column_content'), 10, 2);
}
public function custom_column($columns) {
$columns['_vanity_key'] = __('Vanity Key', 'wp-vanity');
return $columns;
}
public function column_content($column_name, $post_id) {
global $wpdb;
switch ($column_name) {
case '_vanity_key':
$key = get_post_meta($post_id, '_vanity_key', true);
if ($key) {
echo sprintf('<a target="_blank" href="%s%s" title="Original URL">%s</a>', trailingslashit(get_bloginfo('url')), $key, $key);
}
break;
}
}
public function check_url() {
global $wp_query;
global $wpdb;
if (!$wp_query->is_404) {
//This is a valid URL of WordPress!
return false;
}
$key = empty($wp_query->query['pagename']) ? false : $wp_query->query['pagename'];
if ($url = $this->_key_2_url($key)) {
wp_redirect($url);
}
}
public function init() {
$args = array(
'label' => _x('Short Urls', self::POST_TYPE),
'singular_label' => _x('Short Url', self::POST_TYPE),
'new_item' => 'Add Url',
'edit_item' => _x('Edit Url', self::POST_TYPE),
'view_item' => _x('View Url', self::POST_TYPE),
'add_new_item' => _x('Add New Url', self::POST_TYPE),
'public' => true,
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'thumbnail', 'author')
);
register_post_type(self::POST_TYPE, $args);
}
public function add_meta_box() {
add_meta_box("vanity-meta", "Short URL", array($this, 'meta_box_content'), self::POST_TYPE, "normal", "low");
}
/**
* Display custom metabox for our post type
* @global stdClass $post
*/
public static function meta_box_content() {
global $post;
wp_nonce_field('my_vanity_nonce', 'vanity_nonce');
$_vanity_key = get_post_meta($post->ID, '_vanity_key', true);
?>
<p>
<label><?php echo __('URL Key') ?>:</label>
<input name="_vanity_key" type="text" value="<?php echo $_vanity_key ?>" />
You can put your custom url key here if wanted!
</p>
<?php
if (!empty($_vanity_key)) :
?>
<p>
<label><?php echo __('Your whole shorted url') ?>:</label>
<input size="50" type="text" value="<?php echo trailingslashit(get_bloginfo('url')), $_vanity_key ?>" />
<a target="_blank" href="<?php echo trailingslashit(get_bloginfo('url')), $_vanity_key ?>">Try it</a>
</p>
<?php endif ?>
<?php
}
public function save_url($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// if our nonce isn't there, or we can't verify it, bail
if (!isset($_POST['vanity_nonce']) || !wp_verify_nonce($_POST['vanity_nonce'], 'my_vanity_nonce'))
return;
// if our current user can't edit this post, bail
if (!current_user_can('edit_post'))
return;
//If we are adding an URL, and
if (empty($_POST['_vanity_key'])) {
//Let generate a key
//We conver its Id into key
$_vanity_key = base_convert($post_id, 10, 36);
} else {
$_vanity_key = preg_replace('/[^a-z0-9_]/i', '_', $_POST['_vanity_key']);
}
update_post_meta($post_id, '_vanity_key', $_vanity_key);
}
private function _key_2_url($key) {
global $wpdb;
$sql = "
SELECT m.post_id, p.post_title as url
FROM {$wpdb->prefix}postmeta as m
LEFT JOIN {$wpdb->prefix}posts as p ON m.post_id=p.id
WHERE m.meta_key='_vanity_key' AND m.meta_value='%s'
";
$result = $wpdb->get_row($wpdb->prepare($sql, $key));
'http://' != substr($result->url, 0, '7') && $result->url = 'http://' . $result->url;
return $result->url;
}
}
$wp_vanity = Vanity::singleton();