PluginProbe
301 Redirects & 404 Error Log / 0.5
301 Redirects & 404 Error Log v0.5
trunk 0.3 0.4 0.5 1.0 1.01 1.02 1.03 1.04 1.05
301-redirects / 301-redirects.php

301-redirects.php in 301 Redirects & 404 Error Log 0.5, at 301-redirects.php

242 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: 301 Redirects
4 Plugin URI: https://wp301redirects.com/
5 Description: Easily create & manage redirections.
6 Version: 0.5
7 Author: WebFactory Ltd
8 Author URI: https://www.webfactoryltd.com/
9
10 Copyright 2015 - 2020 WebFactory Ltd (email: wp301@webfactoryltd.com)
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License, version 2, as
14 published by the Free Software Foundation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26
27 // this is an include only WP file
28 if (!defined('ABSPATH')) {
29 die;
30 }
31
32 require_once 'controllers.php';
33
34 require_once 'wp301/wp301.php';
35 new wf_wp301(__FILE__, 'settings_page_301-redirects');
36
37
38 function load_301_redirect_assets($adminpage)
39 {
40 if ($adminpage == 'settings_page_301-redirects') {
41 wp_enqueue_style('redirect_301_bootstrap_css', plugin_dir_url(__FILE__) . 'lib/bootstrap-3.3.4.css', false, '1.0.0');
42 wp_enqueue_style('redirect_301_custom_css', plugin_dir_url(__FILE__) . 'lib/style.css', false, '1.0.0');
43 }
44 } //load_301_redirect_assets
45
46
47 add_action('admin_enqueue_scripts', 'load_301_redirect_assets');
48 add_action('wp', 'redirects_301_do_redirect', 0, 1);
49
50
51 function redirects_301_do_redirect()
52 {
53 $siteurl = get_bloginfo('url');
54 $actual_link = getUrl();
55 $all_redirects = $GLOBALS['redirectsplugins']->getAll();
56
57 if ($all_redirects) {
58 foreach ($all_redirects as $redirect_id) {
59 $la_redirect = $GLOBALS['redirectsplugins']->getFields($redirect_id);
60 $la_old_link = str_replace($siteurl, "", $la_redirect['old_link']);
61 if ($actual_link == $siteurl . $la_old_link) {
62 header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
63 header('Cache-Control: post-check=0, pre-check=0', false);
64 header('Pragma: no-cache');
65 header('Location: ' . $la_redirect['new_link'], true, 301);
66 die();
67 }
68 } // foreach
69 }
70 } // redirects_301_do_redirect
71
72
73 function getUrl()
74 {
75 $url = @($_SERVER["HTTPS"] != 'on') ? 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : 'https://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
76 return $url;
77 } // getUrl
78
79
80 function redirects_301_options()
81 {
82
83 $redirects = new Redirects;
84
85 $savedsuccesfully = false;
86 if (isset($_POST['custom_id']) && isset($_POST['delete_custom'])) {
87 $custom_id = sanitize_text_field($_POST['custom_id']);
88 $redirects->remove($custom_id);
89 die();
90 }
91
92 if (isset($_POST['links_audit_submit']) && !isset($_POST['delete_custom'])) {
93
94 $redirects->delete();
95
96 if (!empty($_POST['title'])) {
97 $redirect_arr = $_POST['title'];
98 foreach ($redirect_arr as $key => $redirect_title) {
99 $title = sanitize_text_field($redirect_title);
100 $section = sanitize_text_field($_POST['section'][$key]);
101 $new_link = esc_url($_POST['new_link'][$key]);
102 $old_link = esc_url($_POST['old_link'][$key]);
103 $redirects->edit($title, $section, $new_link, $old_link);
104 }
105 }
106
107 $savedsuccesfully = true;
108 }
109 ?>
110
111 <div class="col-sm-12">
112 <h1>301 Redirects</h1><br>
113
114 <?php
115 if ($savedsuccesfully) {
116 ?>
117 <div class="alert alert-success">All redirect rules have been saved.<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button></div>
118 <?php
119 }
120 ?>
121
122 <p>
123 Add your old path <code>/old-path-from-site/</code> (don't forget to start with a forward slash) in the old link field, and the new path <code>/new-path-somewhere/</code> in the new link field. If you're redirecting to an external URL add the <code>https://</code> prefix.<br>Name and Section are there for your organization and convenience.
124 </p>
125 <form action="" method="post">
126 <input type="hidden" name="links_audit_submit" value="true">
127 <table class="table table-striped table-bordered">
128 <tr>
129 <td class="col-md-3">Name</td>
130 <td class="col-md-3">Section</td>
131 <td class="col-md-3">Old URL</td>
132 <td class="col-md-3">New URL</td>
133 </tr>
134 <?php
135 $custom_redirects = $redirects->getAll();
136
137 if ($custom_redirects) {
138 foreach ($redirects->getAll() as $custom_id) {
139
140 $fields = $redirects->getFields($custom_id);
141 ?>
142 <tr id="customRow<?php echo $custom_id; ?>">
143 <td><input type="text" class="form-control" placeholder="Redirect name (for internal use)" name="title[]" value="<?php echo $fields['title']; ?>" /></td>
144 <td><input type="text" class="form-control" placeholder="Section" name="section[]" value="<?php echo $fields['section']; ?>" /></td>
145 <td>
146 <input placeholder="Old URL" name="old_link[]" class="form-control short-field" value="<?php echo $fields['old_link']; ?>" />
147 <a title="Test redirect rule" class="link-icon" target="_blank" href="<?php echo $fields['old_link']; ?>"><span class="dashicons dashicons-external"></span></a>
148 </td>
149 <td>
150 <input type="text" class="form-control new-link short-field" placeholder="New URL" name="new_link[]" value="<?php echo $fields['new_link']; ?>" />
151 <a title="Remove redirect rule" class="remove-custom" href="#" data-id="<?php echo $custom_id; ?>"><span class="dashicons dashicons-trash"></span></a>
152 </td>
153 </tr>
154 <?php
155 } // foreach
156 }
157 ?>
158 <tr id="addRow">
159 <td colspan="10" class="text-left">
160 <button type="submit" class="btn btn-default btn-info">Save All</button>&nbsp;&nbsp;&nbsp;
161 <a id="addRowBtn" class="btn btn-default" href="#"><span style="vertical-align: middle;" class="dashicons dashicons-plus"></span> Add a New Redirect Rule</a></td>
162 </tr>
163 </table>
164 </form>
165
166 <p>Please <a href="https://wordpress.org/support/plugin/301-redirects/reviews/#new-post" target="_blank">rate the plugin &starf;&starf;&starf;&starf;&starf;</a> to <b>keep it free &amp; maintained</b>. It only takes a minute to rate. Thank you! 👋</p>
167 </div><!-- .col-sm-12 -->
168 <script>
169 jQuery(function() {
170
171 var rowId = 0;
172
173 jQuery('#addRowBtn').on('click', function(e) {
174
175 e.preventDefault();
176
177 var newRow = '<tr id="row' + rowId + '">' +
178 '<td><input type="text" class="form-control" placeholder="Redirect name (for internal use)" name="title[]" /></td>' +
179 '<td><input type="text" class="form-control" placeholder="Section" name="section[]" /></td>' +
180 '<td><input name="old_link[]" class="pull-left form-control" placeholder="Old URL" /></td>' +
181 '<td>' +
182 '<input type="text" class="form-control new-link short-field" placeholder="New URL" name="new_link[]" /> <a title="Remove redirect rule" class="remove-row" href="#" data-id="' + rowId + '"><span class="dashicons dashicons-trash"></span></a></td>' +
183 '</td>' +
184 '</tr>';
185
186 jQuery('#addRow').before(newRow);
187
188 rowId++;
189
190 jQuery('.remove-row').on('click', function(e) {
191 e.preventDefault();
192 var id = jQuery(this).data('id');
193 jQuery('#row' + id).fadeOut(function() {
194 jQuery(this).remove();
195 });
196 });
197
198 });
199
200 });
201
202 jQuery(function() {
203
204 jQuery('.remove-custom').on('click', function(e) {
205
206 e.preventDefault();
207
208 var confirmDelete = confirm("Are you sure you want to delete this redirect rule? This cannot be undone.");
209
210 if (confirmDelete) {
211
212 var id = jQuery(this).data('id');
213
214 jQuery.ajax({
215 type: "POST",
216 url: '',
217 data: {
218 delete_custom: 'true',
219 custom_id: id
220 }
221
222 }).done(function(html) {
223
224 jQuery('#customRow' + id).fadeOut(function() {
225 jQuery(this).remove();
226 });
227
228 });
229 }
230 });
231 });
232 </script>
233 <?php
234 } // redirects_301_options
235
236 add_action('admin_menu', 'redirects_301');
237
238 function redirects_301()
239 {
240 add_options_page('301 Redirects', '301 Redirects', 'manage_options', '301-redirects', 'redirects_301_options');
241 } //redirects_301
242