Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First commit

  • Loading branch information...
commit 53fad72655fd3a8328ab187499ac4fdd0e10cf77 0 parents
Kevin Langley Jr kevinlangleyjr authored

Showing 2 changed files with 249 additions and 0 deletions. Show diff stats Hide diff stats

  1. +74 0 js/wp-short-url.js
  2. +175 0 wp-short-url.php
74 js/wp-short-url.js
... ... @@ -0,0 +1,74 @@
  1 +jQuery(function($){
  2 +
  3 + var container = $('#col-container');
  4 + var ajaxResponse = $('#ajax-response');
  5 + var destination = $('#destination');
  6 + var origin = $('#origin');
  7 +
  8 + container.delegate('#doaction', 'click', function(e){
  9 + var del = [];
  10 + var data;
  11 + var checked = $('.tbody-child input[type="checkbox"]:checked');
  12 + checked.each(function(i){
  13 + del.push($(this).data('origin'));
  14 + });
  15 + data = { shorturls: del, action: 'delete_bulk_short_urls' }
  16 + $.post(ajaxurl, data, function(r){
  17 + ajaxResponse.empty();
  18 + var res = wpAjax.parseAjaxResponse(r, 'ajax-response');
  19 + if( ! res )
  20 + return;
  21 +
  22 + checked.fadeOut(300, function(){
  23 + $(this).parents('tr').remove();
  24 + $('input[type="checkbox"]').attr('checked', false);
  25 + });
  26 + });
  27 + });
  28 +
  29 + container.delegate('#submit', 'click', function(e){
  30 + var form = $(this).parents('form');
  31 + var errors = [];
  32 +
  33 + if(!validateForm(form))
  34 + return false;
  35 +
  36 + if(!is_valid_url(destination.val())){
  37 + errors.push('The destination url provided is not a valid url.');
  38 + }
  39 +
  40 + if(errors.length > 0){
  41 + ajaxResponse.empty();
  42 + var error_html = '<div class="error">';
  43 + $.each(errors, function(i){
  44 + error_html += '<p>'+errors[i]+'</p>';
  45 + });
  46 + error_html += '</div>';
  47 + ajaxResponse.html(error_html);
  48 + } else {
  49 + $.post(ajaxurl, form.serialize(), function(r){
  50 + ajaxResponse.empty();
  51 + var res = wpAjax.parseAjaxResponse(r, 'ajax-response');
  52 + if ( ! res )
  53 + return;
  54 +
  55 + if(res.responses[0].supplemental.hasOwnProperty('row')){
  56 + $('.wp_shorturls_table').append(res.responses[0].supplemental['row']);
  57 + origin.val('');
  58 + destination.val('http://');
  59 + }
  60 + });
  61 + }
  62 +
  63 + e.preventDefault();
  64 + return false;
  65 + });
  66 +
  67 +
  68 +
  69 +
  70 + function is_valid_url(str) {
  71 + var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  72 + return regexp.test(str);
  73 + }
  74 +});
175 wp-short-url.php
... ... @@ -0,0 +1,175 @@
  1 +<?php
  2 +/*
  3 +Plugin Name: WP Short URLs
  4 +Plugin URI: http://voceconnect.com
  5 +Description:
  6 +Version: 0.1
  7 +Author: Kevin Langley
  8 +Author URI: http://voceconnect.com
  9 +License: GPLv2 or later
  10 +*/
  11 +
  12 +class WP_Short_URLs {
  13 +
  14 + const OPTIONS_KEY = 'wp_short_urls';
  15 +
  16 + public static function init(){
  17 + add_action('admin_menu', array(__CLASS__, 'add_options_page'));
  18 + add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'));
  19 + add_action('wp_ajax_add_short_url', array(__CLASS__, 'wp_ajax_add_short_url'));
  20 + add_action('wp_ajax_delete_bulk_short_urls', array(__CLASS__, 'wp_ajax_delete_bulk_short_urls'));
  21 + self::check_redirect();
  22 + }
  23 +
  24 + public static function enqueue_scripts(){
  25 + wp_enqueue_script('wp-short-url', plugins_url('js/wp-short-url.js', __FILE__), array('jquery', 'wp-ajax-response'));
  26 + }
  27 +
  28 + public static function check_redirect(){
  29 + if(!is_admin()){
  30 + $short_urls = get_option(self::OPTIONS_KEY, array());
  31 + $current_uri = $_SERVER['REQUEST_URI'];
  32 + foreach($short_urls as $origin => $destination){
  33 + if(untrailingslashit($current_uri) == untrailingslashit($origin)){
  34 + wp_redirect($destination, 301);
  35 + exit;
  36 + }
  37 + }
  38 + }
  39 + }
  40 +
  41 + public static function wp_ajax_delete_bulk_short_urls(){
  42 + $del_short_urls = $_POST['shorturls'];
  43 + $short_urls = get_option(self::OPTIONS_KEY, array());
  44 + foreach($del_short_urls as $del){
  45 + if(in_array($del, array_keys($short_urls))){
  46 + unset($short_urls[$del]);
  47 + }
  48 + }
  49 + update_option(self::OPTIONS_KEY, $short_urls);
  50 +
  51 + $response = new WP_Ajax_Response(array(
  52 + 'what' => 'wp_short_url',
  53 + 'action' => 'delete_bulk_short_urls',
  54 + 'id' => 1,
  55 + 'position' => 1,
  56 + 'data' => 'success'
  57 + ));
  58 + $response->send();
  59 + }
  60 +
  61 + public static function wp_ajax_add_short_url(){
  62 + $origin = preg_replace("/[^a-z0-9$-_.+!*'(),]+/i", "", $_POST['origin']);
  63 + if(strpos($origin, '/') === false){
  64 + $origin = '/'.$origin;
  65 + }
  66 + $destination = $_POST['destination'];
  67 +
  68 + $short_urls = get_option(self::OPTIONS_KEY, array());
  69 + if(in_array($origin, array_keys($short_urls))){
  70 + $response = new WP_Ajax_Response(array(
  71 + 'what' => 'wp_short_url',
  72 + 'action' => 'add_short_url',
  73 + 'id' => 0,
  74 + 'position' => 1,
  75 + 'data' => new WP_Error('shorturl_already_exists', 'There is already a short URL defined for the requested origin.')
  76 + ));
  77 + $response->send();
  78 + }
  79 +
  80 + $short_urls[$origin] = $destination;
  81 + update_option(self::OPTIONS_KEY, $short_urls);
  82 +
  83 + $row = sprintf('<tr><th class="check-column tbody-child"><input type="checkbox" class="bulk_actions_check" data-origin="%s" /></th><td><a href="%s" target="_blank">%s</a></td><td><a href="%s" target="_blank">%s</a></td></tr>', $origin, site_url($origin), site_url($origin), $destination, $destination);
  84 + $response = new WP_Ajax_Response(array(
  85 + 'what' => 'wp_short_url',
  86 + 'action' => 'add_short_url',
  87 + 'id' => 1,
  88 + 'position' => 1,
  89 + 'data' => 'success',
  90 + 'supplemental' => array('row' => $row)
  91 + ));
  92 + $response->send();
  93 + }
  94 +
  95 + public static function add_options_page(){
  96 + add_menu_page(__('WP Short URLs', 'wp_short_urls'), __('Short URLs', 'wp_short_urls'), 'manage_options', 'wp-short-url', array(__CLASS__, 'wp_shorturl_page'));
  97 + }
  98 +
  99 + public static function wp_shorturl_page(){
  100 + $short_urls = get_option(self::OPTIONS_KEY, array());
  101 + ?>
  102 + <div class="wrap">
  103 + <div id="icon-settings" class="icon32 icon-settings"><br></div>
  104 + <h2>WP Short URLs</h2>
  105 + <div id="ajax-response"></div>
  106 + <br class="clear">
  107 + <div id="col-container">
  108 + <div id="col-right">
  109 + <div class="col-wrap">
  110 + <div class="tablenav top">
  111 + <div class="alignleft actions">
  112 + <select name="action">
  113 + <option value="-1" selected="selected">Bulk Actions</option>
  114 + <option value="delete">Delete</option>
  115 + </select>
  116 + <input type="submit" name="" id="doaction" class="button-secondary action" value="Apply">
  117 + </div>
  118 + <br class="clear">
  119 + </div>
  120 + <form action="" method="post" class="validate">
  121 + <table class="widefat wp_shorturls_table">
  122 + <thead>
  123 + <tr>
  124 + <th class="check-column"><input type="checkbox"/></th>
  125 + <th>Origin URL</th>
  126 + <th>Destination URL</th>
  127 + </tr>
  128 + </thead>
  129 + <tfoot>
  130 + <tr>
  131 + <th class="check-column"><input type="checkbox"/></th>
  132 + <th>Origin URL</th>
  133 + <th>Destination URL</th>
  134 + </tr>
  135 + </tfoot>
  136 + <tbody>
  137 + <?php foreach($short_urls as $origin => $destination): ?>
  138 + <tr>
  139 + <th class="check-column tbody-child"><input type="checkbox" class="bulk_actions_check" data-origin="<?php echo esc_attr($origin); ?>" /></th>
  140 + <td><?php printf('<a href="%s" target="_blank">%s</a>', site_url($origin), site_url($origin)); ?></td>
  141 + <td><?php printf('<a href="%s" target="_blank">%s</a>', esc_attr($destination), $destination); ?></td>
  142 + </tr>
  143 + <?php endforeach; ?>
  144 + </tbody>
  145 + </table>
  146 + </form>
  147 + </div>
  148 + </div>
  149 + <div id="col-left">
  150 + <div class="col-wrap">
  151 + <div class="form-wrap">
  152 + <h3>Add New Short URL</h3>
  153 + <form id="add-short-url" method="post">
  154 + <input type="hidden" id="action" name="action" value="add_short_url"/>
  155 + <div class="form-field form-required">
  156 + <label for="origin">Origin URL</label>
  157 + <input name="origin" id="origin" type="text" value="" size="40">
  158 + <p>The relative URL you want to redirect from.<br/> (e.g. /download-wordpress)</p>
  159 + </div>
  160 + <div class="form-field form-required">
  161 + <label for="destination">Destination URL</label>
  162 + <input name="destination" id="destination" type="text" value="http://" size="40">
  163 + <p>The full URL you want to redirect to.<br/> (e.g. http://wordpress.org/download/)</p>
  164 + </div>
  165 + <p class="submit"><input type="submit" name="submit" id="submit" class="button" value="Add New Short URL"></p>
  166 + </form>
  167 + </div>
  168 + </div>
  169 + </div>
  170 + </div>
  171 + <?php
  172 + }
  173 +
  174 +}
  175 +add_action('init', array('WP_Short_URLs', 'init'));

1 comment on commit 53fad72

Michael Pretty
Owner

@klangley this needs some nonce checks for any of the callbacks that alter data.

Please sign in to comment.
Something went wrong with that request. Please try again.