forked from tonyspiro/301-Redirects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
301-redirects.php
245 lines (187 loc) · 7.14 KB
/
301-redirects.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/*
Plugin Name: 301 Redirects
Plugin URI: http://tonyspiro.com
Description: A plugin that helps you add 301 redirects to your site.
Version: 0.2
Author: Tony Spiro
Author URI: http://tonyspiro.com
License: GPL2
Copyright 2015 Tony Spiro (email: [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* 301 Redirects
==================================== */
/*
Testing
ini_set('display_errors',1);
error_reporting(E_ALL);
*/
include('controllers.php');
function load_301_redirect_assets($adminpage)
{
if ($adminpage == 'settings_page_301-redirects')
{
wp_enqueue_style( 'redirect_301_bootstrap_css', plugin_dir_url( __FILE__ ) . 'lib/bootstrap-3.3.4.css', false, '1.0.0' );
wp_enqueue_style( 'redirect_301_custom_css', plugin_dir_url( __FILE__ ) . 'style.css', false, '1.0.0' );
wp_enqueue_script( 'bootstrap', plugin_dir_url( __FILE__ ) . 'lib/bootstrap-3.3.4.js', array(), '1.0.0', true );
}
}
add_action( 'admin_enqueue_scripts', 'load_301_redirect_assets' );
$siteurl = get_bloginfo('siteurl');
function getUrl() {
$url = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : 'https://'. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
return $url;
}
$actual_link = getUrl();
$all_redirects = $GLOBALS['redirectsplugins']->getAll();
if($all_redirects){
foreach($all_redirects as $redirect_id){
$la_redirect = $GLOBALS['redirectsplugins']->getFields($redirect_id);
$la_old_link = str_replace($siteurl, "", $la_redirect['old_link']);
$la_new_link = str_replace($siteurl, "", $la_redirect['new_link']);
if($actual_link == $siteurl . $la_old_link){
header("Location: " . $siteurl . $la_new_link);
die();
}
}
}
function redirects_301_options() {
$redirects = new Redirects;
/* Processes ========================= */
$savedsuccesfully = false;
if (isset($_POST['custom_id']) && isset($_POST['delete_custom']))
{
$custom_id = sanitize_text_field($_POST['custom_id']);
$redirects->remove($custom_id);
die();
}
if (isset($_POST['links_audit_submit']) && !isset($_POST['delete_custom']))
{
$redirects->delete();
$redirect_arr = $_POST['title'];
foreach($redirect_arr as $key => $redirect_title){
$title = sanitize_text_field($redirect_title);
$section = sanitize_text_field($_POST['section'][$key]);
$new_link = esc_url($_POST['new_link'][$key]);
$old_link = esc_url($_POST['old_link'][$key]);
$redirects->edit($title, $section, $new_link, $old_link);
}
$savedsuccesfully = true;
}
?>
<div class="col-sm-12">
<?php
if ($savedsuccesfully)
{
?>
<div class="alert alert-success">Redirects saved.<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>
<?php
}
?>
<h1>301 Redirects</h1>
<p>
Add your old paths <code>/old-path-from-old-site</code> in the old link field and the new path <code>/new-path-in-new-site</code> in the new link fields. Title and Section are there for your organization and convenience.
301 Redirects works automatically by redirecting users from your old links to your new ones.
</p>
<form action="" method="post">
<input type="hidden" name="links_audit_submit" value="true">
<table class="table table-striped table-bordered">
<tr>
<td class="col-md-3">Title</td>
<td class="col-md-3">Section</td>
<td class="col-md-3">Old Link</td>
<td class="col-md-3">New Link</td>
</tr>
<?php
/// Custom redirects
$custom_redirects = $redirects->getAll();
if($custom_redirects){
?>
<?php
foreach($redirects->getAll() as $custom_id){
$fields = $redirects->getFields($custom_id);
?>
<tr id="customRow<?php echo $custom_id; ?>">
<td><input type="text" class="form-control" placeholder="Title" name="title[]" value="<?php echo $fields['title']; ?>" /></td>
<td><input type="text" class="form-control" placeholder="Section" name="section[]" value="<?php echo $fields['section']; ?>" /></td>
<td><input placeholder="Old Link" name="old_link[]" class="form-control" value="<?php echo $fields['old_link']; ?>" /></td>
<td>
<table class="no-border col-sm-12">
<tr><td><input type="text" class="form-control" placeholder="New Link" name="new_link[]" value="<?php echo $fields['new_link']; ?>" /></td><td><a title="remove row" class="remove-custom pull-right close" href="#" data-id="<?php echo $custom_id; ?>">×</a></td></tr>
</table>
</td>
</tr>
<?php
}
}
?>
<tr id="addRow">
<td colspan="10"><a id="addRowBtn" class="btn btn-default pull-right" href="#">+ Add a new row</a></td>
</tr>
<tr>
<td colspan="10" class="text-right"><button type="submit" class="btn btn-default btn-success">Save All</button></td></td>
</tr>
</table>
</form>
</div><!-- .col-sm-12 -->
<script>
jQuery(function(){
var rowId = 0;
jQuery('#addRowBtn').on('click', function(e){
e.preventDefault();
var newRow = '<tr id="row' + rowId + '">' +
'<td><input type="text" class="form-control" placeholder="Title" name="title[]" /></td>' +
'<td><input type="text" class="form-control" placeholder="Section" name="section[]" /></td>' +
'<td><input name="old_link[]" class="pull-left form-control" placeholder="Old Link" /></td>' +
'<td><table class="no-border col-sm-12">' +
'<tr><td><input type="text" class="form-control" placeholder="New Link" name="new_link[]" /></td><td><a title="remove row" class="remove-row pull-right close" href="#" data-id="' + rowId + '">×</a></td>' +
'</tr></table></td>' +
'</tr>';
jQuery('#addRow').before(newRow);
rowId++;
jQuery('.remove-row').on('click', function(e){
e.preventDefault();
var id = jQuery(this).data('id');
jQuery('#row' + id).fadeOut(function(){
jQuery(this).remove();
});
});
});
});
</script>
<script>
jQuery(function(){
jQuery('.remove-custom').on('click', function(e){
e.preventDefault();
var confirmDelete = confirm("Are you sure you want to delete this 1 row? This cannot be undone.");
if(confirmDelete){
var id = jQuery(this).data('id');
jQuery.ajax({
type: "POST",
url: '',
data: { delete_custom : 'true', custom_id: id }
}).done(function( html ) {
jQuery('#customRow' + id).fadeOut(function(){
jQuery(this).remove();
});
});
}
});
});
</script>
<?php
}
add_action( 'admin_menu', 'redirects_301' );
function redirects_301() {
add_options_page( '301 Redirects', '301 Redirects', 'manage_options', '301-redirects', 'redirects_301_options' );
}