This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.uw-vanity-url-builder.php
207 lines (160 loc) · 6.89 KB
/
class.uw-vanity-url-builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// Plugin Name: UW Vanity URL Builder
// Description: This plugin allows custom vanity urls to be created
// Version: 1.0
// Author: UW Web Team
// Author URI: http://www.washington.edu/marketing/web/
class UW_Vanity_URL_Builder
{
const title = 'Vanity URLs';
function __construct()
{
add_action('admin_menu', array($this, 'admin_menu'));
add_action('template_redirect', array($this, 'redirect'), 99);
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
}
function enqueue_scripts()
{
wp_enqueue_script('backbone');
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui-smoothness', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/smoothness/jquery-ui.css');
}
function redirect()
{
$vanity_urls = get_option('_vanity_urls');
$pagename = get_query_var('pagename');
if ( is_array($vanity_urls) && array_key_exists($pagename, $vanity_urls ) )
{
$forward_to = $vanity_urls[$pagename]['to'];
$url = filter_var($forward_to, FILTER_VALIDATE_URL) === false ? home_url($forward_to) : $forward_to;
wp_redirect( $url );
exit;
}
return;
}
function save()
{
$redirects = array_filter(array_map('array_filter', $_POST['vanity'] )); // magic to remove blank values
foreach ($redirects as $redirect) {
$redirect = array_filter(array_map('trim', $redirect)); // remove empty string values
if ( isset($redirect['from']) && isset($redirect['to']) ) {
$vanity_urls[strtolower($redirect['from'])]['from'] = trim($redirect['from']);
$vanity_urls[strtolower($redirect['from'])]['to'] = trim($redirect['to']);
$vanity_urls[strtolower($redirect['from'])]['created'] = $redirect['created'];
$vanity_urls[strtolower($redirect['from'])]['expires'] = $redirect['expires'];
$vanity_urls[strtolower($redirect['from'])]['notes'] = $redirect['notes'];
}
}
update_option('_vanity_urls', $vanity_urls );
}
function admin_menu()
{
add_options_page(self::title, self::title, 'manage_options', 'vanity_urls', array($this, 'settings_page'));
}
function settings_page()
{
if ( isset($_POST['vanity']) ) self::save();
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2><?php echo self::title; ?></h2>
<p>
Add or remove vanity urls for <?php bloginfo(); ?> site. The first column is the vanity url while the second column is the partial or full url to redirect to. All the urls are case insensitive. The expiration date will default to 30 days. Expired redirects will be labeled "Expired" in red but will still work and must be removed manually.
</p>
<h3>List of redirects</h3>
<form action="" method="post">
<div id="vanity-list">
</div>
<p>
<input id="add-vanity-url" type="button" class="button tagadd" value="Add New">
</p>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"></p>
</form>
</div>
<style>
.ui-datepicker-trigger { width:25px; float:left; margin: 0 5px;}
</style>
<script type="text/template" id="redirect-template">
<div class="redirect" style="background-color:read" data-count=<%= count %> >
<p>
<input style="width:10%" name="vanity[<%= count %>][from]" type="text" value="<%= from %>" class="regular-text url" placeholder="Vanity url"/>
→
<input style="width:40%" name="vanity[<%= count %>][to]" type="text" value="<%= to %>" class="regular-text url" placeholder="Actual url"/>
</p>
<p>
<input name="vanity[<%= count %>][created]" type="hidden" value="<%= created %>"/>
<input class="expires" name="vanity[<%= count %>][expires]" type="hidden" value="<%= expires %>"/>
<input class="expires-friendly" type="text" value="<%= friendlyDate %>" size="8" disabled="disabled" />
<input name="vanity[<%= count %>][notes]" type="text" value="<%= notes %>" class="regular-text url" placeholder="Notes" style="width:40%"/>
<input class="button delete remove-vanity-url" type="button" style="margin-left:30px;" value="Remove">
<% if ( expired ) { %>
<small style="color:red;"> Expired </small>
<% } %>
</p>
<hr/>
</div>
</script>
<script type="text/javascript">
jQuery(window).load(function() {
jQuery( ".expires" ).datepicker();
})
jQuery(document).ready(function($) {
var DEFAULT_DATE = 30;
var DATE_FORMAT = 'm/d/yy'
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "/cms/wp-content/themes/maps/images/date.gif",
buttonImageOnly: true,
dateFormat: '@',
defaultDate: DEFAULT_DATE,
onSelect : function(dateText, datepicker) {
var friendlyDate = $.datepicker.formatDate( DATE_FORMAT, new Date( Number(dateText) ) )
$(this).siblings('.expires-friendly').val( friendlyDate )
}
})
$('#add-vanity-url').click(function() {
var $list = $('div.redirect')
, $last = $list.last()
$('#vanity-list').append(
_.template( $('#redirect-template').html(), { count : $last.data('count') + 1 || 0, from:null, to:null, created:(new Date().getTime()), expires:null, friendlyDate:null, expired:null, notes:null })
).find('.expires').last().datepicker().datepicker('setDate', DEFAULT_DATE)
})
$('#vanity-list').on('click', 'input.remove-vanity-url', function() {
var $this = $(this)
$list = $('div.redirect')
if ( $list.length === 1) {
$this.attr('disabled', true)
//return;
}
$this.closest('.redirect').remove()
})
$('#vanity-list').on('blur', 'input.url', function() {
var $this = $(this)
$this.val($.trim($this.val()));
})
var redirects = <?php echo json_encode(get_option('_vanity_urls')) ?>;
$.each(redirects, function(index,el) {
//var expired = $.datepicker.parseDate('@', el.expires).getTime() - new Date() < 0;
var expired = Number(el.expires) - new Date().getTime() < 0
, friendlyDate = $.datepicker.formatDate( DATE_FORMAT, new Date( Number(el.expires) ) )
$('#vanity-list').append(
_.template( $('#redirect-template').html(), {
count:index,
from:el.from,
to:el.to,
created:el.created || (new Date().getTime()) ,
expires:el.expires,
friendlyDate : friendlyDate,
expired:expired,
notes:el.notes
})
)
})
})
</script>
<?php
}
}
new UW_Vanity_URL_Builder;