From 50eaf150a76e325a64b2807792153dc9c3048b05 Mon Sep 17 00:00:00 2001 From: Benjamin Hodovic Date: Mon, 27 Jan 2020 21:17:51 +0100 Subject: [PATCH 1/6] sync with official wordpress plugin update --- wp-simple-301-redirects.php | 291 ++++++++++++------------------------ 1 file changed, 98 insertions(+), 193 deletions(-) diff --git a/wp-simple-301-redirects.php b/wp-simple-301-redirects.php index 84840f1..1e04e2f 100644 --- a/wp-simple-301-redirects.php +++ b/wp-simple-301-redirects.php @@ -3,13 +3,12 @@ Plugin Name: Simple 301 Redirects Plugin URI: http://www.scottnelle.com/simple-301-redirects-plugin-for-wordpress/ Description: Create a list of URLs that you would like to 301 redirect to another page or site. Now with wildcard support. -Version: 1.07a +Version: 1.07 Author: Scott Nellé Author URI: http://www.scottnelle.com/ -License: GPLv3 */ -/* Copyright 2009-2014 Scott Nellé (email : contact@scottnelle.com) +/* Copyright 2009-2013 Scott Nellé (email : contact@scottnelle.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,36 +25,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -//@todo: finish javascript edit functionality -//@todo: only load js on the appropriate page - http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Link_Scripts_Only_on_a_Plugin_Administration_Screen -//@todo: notify the bulk upload author that you'll be updating the storage format. maybe even patch his plugin for him. https://wordpress.org/plugins/simple-301-redirects-addon-bulk-uploader/ - if (!class_exists("Simple301redirects")) { - - class Simple301Redirects { - function initialize_admin() { - $this->maybe_upgrade_db(); // upgrade the storage format if needed - - // load necessary js - wp_register_script( 's301r-script', plugins_url( '/js/simple-301-redirects.js', __FILE__ ), array('jquery') ); - add_action('admin_enqueue_scripts', array($this,'write_scripts')); - - // if submitted, process the data - if (isset($_POST['301_redirects'])) { $this->save_redirects(); } - } + class Simple301Redirects { - function write_scripts() { - wp_enqueue_script( 's301r-script' ); - - // make ajax_url available to the script - wp_localize_script( 's301r-script', 's301r_ajax', array( - 'ajax_url' => admin_url( 'admin-ajax.php' ), - 'delete_nonce' => wp_create_nonce( 'delete-redirect-nonce' ), - ) - ); - } - /** * create_menu function * generate the link to the options page under settings @@ -65,7 +38,7 @@ function write_scripts() { function create_menu() { add_options_page('301 Redirects', '301 Redirects', 'manage_options', '301options', array($this,'options_page')); } - + /** * options_page function * generate the options page in the wordpress admin @@ -75,74 +48,85 @@ function create_menu() { function options_page() { ?>
- + +

Settings saved

'; } ?> - +

Simple 301 Redirects

-
- - -

- Love this plugin? Feeling generous? - -

- -
- -
- + + - - - + + + + + + expand_redirects(); ?> - + - - + +
Request example: /about.htmDestination example: /about/DeleteRequestDestination
example: /about.htmexample: /about/
» Delete
- - + +

/>

- -

-
+

+

Documentation

-

Basic Redirects

-

Basic redirects work similar to the format that Apache uses: the request should be relative to your WordPress root. The destination can be either a full URL to any page on the web, or relative to your WordPress root.

+

Simple Redirects

+

Simple redirects work similar to the format that Apache uses: the request should be relative to your WordPress root. The destination can be either a full URL to any page on the web, or relative to your WordPress root.

Example

- -

Wildcard Redirects

-

Wildcard redirects can match more than one URL. They use an asterisk (*) to represent any characters. This is a powerful way to redirect an entire directory of pages with one line.

+ +

Wildcards

+

To use wildcards, put an asterisk (*) after the folder name that you want to redirect.

Example

- +

You can also use the asterisk in the destination to replace whatever it matched in the request if you like. Something like this:

Example

*/ function expand_redirects() { - $redirects = get_option('s301r_redirects'); + $redirects = get_option('301_redirects'); $output = ''; - $counter = 0; if (!empty($redirects)) { - foreach ($redirects as $index => $redirect) { - $counter++; - $row_class = ($counter % 2 === 0) ? 'row_static' : 'row_static alternate'; + foreach ($redirects as $request => $destination) { $output .= ' - - - '.$redirect['request'].' + + + » - '.$redirect['destination'].' - + + - + '; } } // end if return $output; } - + /** * save_redirects function * save the redirects from the options page to the database @@ -195,45 +176,35 @@ function expand_redirects() { * @param mixed $data * @return void */ - function save_redirects() { + function save_redirects($data) { if ( !current_user_can('manage_options') ) { wp_die( 'You do not have sufficient permissions to access this page.' ); } check_admin_referer( 'save_redirects', '_s301r_nonce' ); - - // get existing redirects - $redirects = get_option( 's301r_redirects' ); - if ($redirects == '') { $redirects = array(); } + $data = $_POST['301_redirects']; - // delete checked redirect - if (isset($data['delete']) && is_array($data['delete'])) { - foreach ($data['delete'] as $index) { - unset($redirects[$index]); - } - } + $redirects = array(); + + for($i = 0; $i < sizeof($data['request']); ++$i) { + $request = trim( sanitize_text_field( $data['request'][$i] ) ); + $destination = trim( sanitize_text_field( $data['destination'][$i] ) ); - // save new redirect - if ( trim( $data['request'] ) != '' && trim( $data['destination'] != '' ) ) { - $redirects[] = array( - 'request' => trim( $data['request'] ), - 'destination' => trim( $data['destination'] ), - ); + if ($request == '' && $destination == '') { continue; } + else { $redirects[$request] = $destination; } } - update_option('s301r_redirects', $redirects); - + update_option('301_redirects', $redirects); + if (isset($data['wildcard'])) { - $settings['wildcard'] = 'true'; + update_option('301_redirects_wildcard', 'true'); } else { - $settings['wildcard'] = 'false'; + delete_option('301_redirects_wildcard'); } - - update_option('s301r_settings', $settings); } - + /** * redirect function - * Read the list of redirects and if the current page + * Read the list of redirects and if the current page * is found in the list, send the visitor on her way * @access public * @return void @@ -242,38 +213,37 @@ function redirect() { // this is what the user asked for (strip out home portion, case insensitive) $userrequest = str_ireplace(get_option('home'),'',$this->get_address()); $userrequest = rtrim($userrequest,'/'); - - $this->maybe_upgrade_db(); // upgrade the storage format if needed @todo: benchmark this, tune for speed - $redirects = get_option('s301r_redirects'); + $redirects = get_option('301_redirects'); if (!empty($redirects)) { - $settings = get_option('s301r_settings'); + + $wildcard = get_option('301_redirects_wildcard'); $do_redirect = ''; - + // compare user request to each 301 stored in the db - foreach ($redirects as $key => $redirect) { - // check if we should use regex search - if ($settings['wildcard'] === 'true' && strpos($redirect['request'],'*') !== false) { + foreach ($redirects as $storedrequest => $destination) { + // check if we should use regex search + if ($wildcard === 'true' && strpos($storedrequest,'*') !== false) { // wildcard redirect - + // don't allow people to accidentally lock themselves out of admin if ( strpos($userrequest, '/wp-login') !== 0 && strpos($userrequest, '/wp-admin') !== 0 ) { // Make sure it gets all the proper decoding and rtrim action - $redirect['request'] = str_replace('*','(.*)',$redirect['request']); - $pattern = '/^' . str_replace( '/', '\/', rtrim( $redirect['request'], '/' ) ) . '/'; - $redirect['destination'] = str_replace('*','$1',$redirect['destination']); - $output = preg_replace($pattern, $redirect['destination'], $userrequest); + $storedrequest = str_replace('*','(.*)',$storedrequest); + $pattern = '/^' . str_replace( '/', '\/', rtrim( $storedrequest, '/' ) ) . '/'; + $destination = str_replace('*','$1',$destination); + $output = preg_replace($pattern, $destination, $userrequest); if ($output !== $userrequest) { // pattern matched, perform redirect $do_redirect = $output; } } } - elseif(urldecode($userrequest) == rtrim($redirect['request'],'/')) { + elseif(urldecode($userrequest) == rtrim($storedrequest,'/')) { // simple comparison redirect - $do_redirect = $redirect['destination']; + $do_redirect = $destination; } - + // redirect. the second condition here prevents redirect loops as a result of wildcards. if ($do_redirect !== '' && trim($do_redirect,'/') !== trim($userrequest,'/')) { // check if destination needs the domain prepended @@ -289,28 +259,6 @@ function redirect() { } } // end funcion redirect - function ajax_delete() { - if ( ! wp_verify_nonce( $_POST['delete_nonce'], 'delete-redirect-nonce' ) || !current_user_can( 'manage_options' )) { - echo 'failure'; exit; - } - - $row = $_POST['row_id']; - - // data check - if( preg_match('/^s301r_row_[0-9]+$/', $row) === 0 ) { echo 'failure'; exit; } // someone is messing with the dom - - $index = intval( str_replace('s301r_row_', '', $row) ); - $redirects = get_option( 's301r_redirects' ); - - if (is_array( $redirects ) && isset( $redirects[$index] )) { // delete the redirect - unset($redirects[$index]); - update_option( 's301r_redirects', $redirects ); - echo 'success'; - exit; - } - else { echo 'failure'; exit; } // something went wrong - } - /** * getAddress function * utility function to get the full address of the current request @@ -322,7 +270,7 @@ function get_address() { // return the full address return $this->get_protocol().'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } // end function get_address - + function get_protocol() { // Set the base protocol to http $protocol = 'http'; @@ -330,71 +278,28 @@ function get_protocol() { if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) { $protocol .= "s"; } - + return $protocol; } // end function get_protocol - function maybe_upgrade_db() { - $latest_db_version = 2; - $db_version = ( get_option( 's301r_db_version' ) ) ? intval( get_option( 's301r_db_version' ) ) : 1; - if ( $latest_db_version === $db_version ) return; // return early, don't slow down the admin - - while ($db_version < $latest_db_version) { // upgrade through each version of the database, in case users jump multiple versions - if ( $db_version === 1 ) $this->upgrade_db_v2(); - // elseif ( $db_version === 2 ) $this->upgrade_db_v3(); // a future version would look like this - - $db_version++; - } // end while - } // end function maybe_upgrade_db - - function upgrade_db_v2() { - // new settings format - $wildcard = ( get_option('301_redirects_wildcard') === 'true' ) ? 'true' : 'false'; - $v2_settings = array( - 'wildcard' => $wildcard, - ); - update_option( 's301r_settings', $v2_settings ); - delete_option( '301_redirects_wildcard' ); - - // new redirect format - $counter = 0; - $v1_redirects = get_option('301_redirects'); - $v2_redirects = array(); - - if (!empty($v1_redirects)) { - update_option( 's301r_archive_data', $v1_redirects ); // save a backup in case something goes wrong during upgrade - - foreach ($v1_redirects as $request => $destination) { - $v2_redirects[$counter] = array( - 'request' => $request, - 'destination' => $destination, - ); - $counter++; - } - } - update_option( 's301r_redirects', $v2_redirects ); - delete_option( '301_redirects' ); - - // new db version - update_option( 's301r_db_version', 2 ); - } // end function upgrade_db_v2 - } // end class Simple301Redirects - + } // end check for existance of class // instantiate $redirect_plugin = new Simple301Redirects(); if (isset($redirect_plugin)) { - add_action('init', array($redirect_plugin,'redirect'), 1); // add the redirect action, high priority + // add the redirect action, high priority + add_action('init', array($redirect_plugin,'redirect'), 1); - // set up admin - add_action('admin_init', array($redirect_plugin,'initialize_admin')); + // create the menu add_action('admin_menu', array($redirect_plugin,'create_menu')); - // ajax - add_action( 'wp_ajax_s301r_delete_redirect', array($redirect_plugin,'ajax_delete')); + // if submitted, process the data + if (isset($_POST['301_redirects'])) { + add_action('admin_init', array($redirect_plugin,'save_redirects')); + } } // this is here for php4 compatibility From 072c2ce752259d8491855ad97d20a5a5a9ab4070 Mon Sep 17 00:00:00 2001 From: Benjamin Hodovic Date: Mon, 27 Jan 2020 21:21:25 +0100 Subject: [PATCH 2/6] add editorconfig for tab indent --- .editorconfig | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dd9908c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root = true +[*] +end_of_line = lf +insert_final_newline = true +indent_style = tab +indent_size = 2 From 5af0052022b94793434bd3b16a783fee0b09478e Mon Sep 17 00:00:00 2001 From: Benjamin Hodovic Date: Mon, 27 Jan 2020 21:24:32 +0100 Subject: [PATCH 3/6] version and year increased --- wp-simple-301-redirects.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wp-simple-301-redirects.php b/wp-simple-301-redirects.php index 1e04e2f..ceeca24 100644 --- a/wp-simple-301-redirects.php +++ b/wp-simple-301-redirects.php @@ -3,17 +3,17 @@ Plugin Name: Simple 301 Redirects Plugin URI: http://www.scottnelle.com/simple-301-redirects-plugin-for-wordpress/ Description: Create a list of URLs that you would like to 301 redirect to another page or site. Now with wildcard support. -Version: 1.07 +Version: 1.08 Author: Scott Nellé Author URI: http://www.scottnelle.com/ */ -/* Copyright 2009-2013 Scott Nellé (email : contact@scottnelle.com) +/* Copyright 2009-2020 Scott Nellé (email : contact@scottnelle.com) - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of From 806f7ec35bfaf486965ef9aff235df042a7ec81c Mon Sep 17 00:00:00 2001 From: Benjamin Hodovic Date: Mon, 27 Jan 2020 21:26:27 +0100 Subject: [PATCH 4/6] add parse_url to redirect also when query is in URL (example: fbclientid or other socialmedia params) --- wp-simple-301-redirects.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wp-simple-301-redirects.php b/wp-simple-301-redirects.php index ceeca24..b8fcf15 100644 --- a/wp-simple-301-redirects.php +++ b/wp-simple-301-redirects.php @@ -212,7 +212,9 @@ function save_redirects($data) { function redirect() { // this is what the user asked for (strip out home portion, case insensitive) $userrequest = str_ireplace(get_option('home'),'',$this->get_address()); - $userrequest = rtrim($userrequest,'/'); + // parse request + $parsed_userrequest = parse_url($userrequest); + $userrequest = $parsed_userrequest["path"]; $redirects = get_option('301_redirects'); if (!empty($redirects)) { @@ -225,7 +227,6 @@ function redirect() { // check if we should use regex search if ($wildcard === 'true' && strpos($storedrequest,'*') !== false) { // wildcard redirect - // don't allow people to accidentally lock themselves out of admin if ( strpos($userrequest, '/wp-login') !== 0 && strpos($userrequest, '/wp-admin') !== 0 ) { // Make sure it gets all the proper decoding and rtrim action @@ -236,12 +237,22 @@ function redirect() { if ($output !== $userrequest) { // pattern matched, perform redirect $do_redirect = $output; + + // add query if set + if (isset($parsed_userrequest["query"])) { + $do_redirect .= $parsed_userrequest["query"]; + } } } } elseif(urldecode($userrequest) == rtrim($storedrequest,'/')) { // simple comparison redirect $do_redirect = $destination; + + // add query if set + if (isset($parsed_userrequest["query"])) { + $do_redirect .= $parsed_userrequest["query"]; + } } // redirect. the second condition here prevents redirect loops as a result of wildcards. From 49fb5a745a0d1acf80a8e48fde7477e3ce61b42a Mon Sep 17 00:00:00 2001 From: Benjamin Hodovic Date: Mon, 27 Jan 2020 21:27:05 +0100 Subject: [PATCH 5/6] fix indent to tab --- wp-simple-301-redirects.php | 44 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/wp-simple-301-redirects.php b/wp-simple-301-redirects.php index b8fcf15..189fae4 100644 --- a/wp-simple-301-redirects.php +++ b/wp-simple-301-redirects.php @@ -15,14 +15,14 @@ the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - 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 + 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 */ if (!class_exists("Simple301redirects")) { @@ -36,7 +36,7 @@ class Simple301Redirects { * @return void */ function create_menu() { - add_options_page('301 Redirects', '301 Redirects', 'manage_options', '301options', array($this,'options_page')); + add_options_page('301 Redirects', '301 Redirects', 'manage_options', '301options', array($this,'options_page')); } /** @@ -287,7 +287,7 @@ function get_protocol() { $protocol = 'http'; // check for https if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) { - $protocol .= "s"; + $protocol .= "s"; } return $protocol; @@ -315,15 +315,15 @@ function get_protocol() { // this is here for php4 compatibility if(!function_exists('str_ireplace')){ - function str_ireplace($search,$replace,$subject){ - $token = chr(1); - $haystack = strtolower($subject); - $needle = strtolower($search); - while (($pos=strpos($haystack,$needle))!==FALSE){ - $subject = substr_replace($subject,$token,$pos,strlen($search)); - $haystack = substr_replace($haystack,$token,$pos,strlen($search)); - } - $subject = str_replace($token,$replace,$subject); - return $subject; - } -} \ No newline at end of file + function str_ireplace($search,$replace,$subject){ + $token = chr(1); + $haystack = strtolower($subject); + $needle = strtolower($search); + while (($pos=strpos($haystack,$needle))!==FALSE){ + $subject = substr_replace($subject,$token,$pos,strlen($search)); + $haystack = substr_replace($haystack,$token,$pos,strlen($search)); + } + $subject = str_replace($token,$replace,$subject); + return $subject; + } +} From 596578a9f0403ad0a96c39d30e92636a8b13695c Mon Sep 17 00:00:00 2001 From: Benjamin Hodovic Date: Mon, 27 Jan 2020 21:36:51 +0100 Subject: [PATCH 6/6] small fix --- wp-simple-301-redirects.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-simple-301-redirects.php b/wp-simple-301-redirects.php index 189fae4..5f72c9a 100644 --- a/wp-simple-301-redirects.php +++ b/wp-simple-301-redirects.php @@ -240,7 +240,7 @@ function redirect() { // add query if set if (isset($parsed_userrequest["query"])) { - $do_redirect .= $parsed_userrequest["query"]; + $do_redirect .= '?' . $parsed_userrequest["query"]; } } } @@ -251,7 +251,7 @@ function redirect() { // add query if set if (isset($parsed_userrequest["query"])) { - $do_redirect .= $parsed_userrequest["query"]; + $do_redirect .= '?' . $parsed_userrequest["query"]; } }