Safe Redirect Manager - Version 1.3

Version Description

  • safe-redirect-manager.php - Globalize SRM class for use in themes/plugins/scripts. Added create_redirect method to make importing easier.
Download this release

Release Info

Developer tlovett1
Plugin Icon 128x128 Safe Redirect Manager
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

Files changed (2) hide show
  1. readme.txt +3 -0
  2. safe-redirect-manager.php +69 -27
readme.txt CHANGED
@@ -24,6 +24,9 @@ Extract the zip file and just drop the contents in the wp-content/plugins/ direc
24
 
25
  == Changelog ==
26
 
 
 
 
27
  = 1.2 =
28
  * safe-redirect-manager.php - manage_options capabilitiy required to use redirect manager, remove checkbox column, hide view switcher, fix search feature, hide privacy stuff for bulk edit
29
 
24
 
25
  == Changelog ==
26
 
27
+ = 1.3 =
28
+ * safe-redirect-manager.php - Globalize SRM class for use in themes/plugins/scripts. Added create_redirect method to make importing easier.
29
+
30
  = 1.2 =
31
  * safe-redirect-manager.php - manage_options capabilitiy required to use redirect manager, remove checkbox column, hide view switcher, fix search feature, hide privacy stuff for bulk edit
32
 
safe-redirect-manager.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Safe Redirect Manager
4
  Plugin URI: http://www.10up.com
5
  Description: Easily and safely manage HTTP redirects.
6
  Author: Taylor Lovett (10up LLC), VentureBeat
7
- Version: 1.2
8
  Author URI: http://www.10up.com
9
 
10
  GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
@@ -97,8 +97,10 @@ class SRM_Safe_Redirect_Manager {
97
  /**
98
  * Return distinct search results
99
  *
 
100
  * @param string $distinct
101
- * return string
 
102
  */
103
  public function filter_search_distinct( $distinct ) {
104
  if ( $this->redirect_post_type != get_query_var( 'post_type' ) )
@@ -191,6 +193,45 @@ class SRM_Safe_Redirect_Manager {
191
  return array();
192
  }
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  /**
195
  * Whether or not this is an admin page specific to the plugin
196
  *
@@ -567,8 +608,8 @@ class SRM_Safe_Redirect_Manager {
567
  if ( ! in_array( $with_www, $content ) ) $content[] = $with_www;
568
  }
569
 
570
- return $content;
571
- }
572
 
573
  /**
574
  * Force update on the redirect cache and return cache
@@ -649,7 +690,7 @@ class SRM_Safe_Redirect_Manager {
649
  $this->whitelist_hosts[] = $parsed_redirect['host'];
650
  add_filter( 'allowed_redirect_hosts' , array( $this, 'filter_allowed_redirect_hosts' ) );
651
  }
652
-
653
  // if we have a valid status code, then redirect with it
654
  if ( in_array( $status_code, $this->valid_status_codes ) )
655
  wp_safe_redirect( esc_url_raw( $redirect_to ), $status_code );
@@ -672,16 +713,16 @@ class SRM_Safe_Redirect_Manager {
672
  * @return string
673
  */
674
  public function sanitize_redirect_to( $path ) {
675
- $path = trim( $path );
676
 
677
- if ( preg_match( '/^www\./i', $path ) )
678
- $path = 'http://' . $path;
679
 
680
- if ( ! preg_match( '/^https?:\/\//i', $path ) )
681
- if ( strpos( $path, '/' ) !== 0 )
682
- $path = '/' . $path;
683
 
684
- return esc_url_raw( $path );
685
  }
686
 
687
  /**
@@ -694,23 +735,24 @@ class SRM_Safe_Redirect_Manager {
694
  */
695
  public function sanitize_redirect_from( $path ) {
696
 
697
- $path = trim( $path );
698
-
699
- if ( empty( $path ) )
700
- return '';
701
 
702
- // dont accept paths starting with a .
703
- if ( strpos( $path, '.' ) === 0 )
704
- return '';
705
 
706
- // turn path in to absolute
707
- if ( preg_match( '/https?:\/\//i', $path ) )
708
- $path = preg_replace( '/^(http:\/\/|https:\/\/)(www\.)?[^\/?]+\/?(.*)/i', '/$3', $path );
709
- elseif ( strpos( $path, '/' ) !== 0 )
710
- $path = '/' . $path;
711
 
712
- return esc_url_raw( $path );
713
- }
714
  }
715
 
716
- new SRM_Safe_Redirect_Manager();
 
4
  Plugin URI: http://www.10up.com
5
  Description: Easily and safely manage HTTP redirects.
6
  Author: Taylor Lovett (10up LLC), VentureBeat
7
+ Version: 1.3
8
  Author URI: http://www.10up.com
9
 
10
  GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
97
  /**
98
  * Return distinct search results
99
  *
100
+ * @since 1.2
101
  * @param string $distinct
102
+ * @uses get_query_var
103
+ * @return string
104
  */
105
  public function filter_search_distinct( $distinct ) {
106
  if ( $this->redirect_post_type != get_query_var( 'post_type' ) )
193
  return array();
194
  }
195
 
196
+ /**
197
+ * Creates a redirect post, this function will be useful for import/exports scripts
198
+ *
199
+ * @param string $redirect_from
200
+ * @param string $redirect_to
201
+ * @param int $status_code
202
+ * @since 1.3
203
+ * @uses wp_insert_post, update_post_meta
204
+ * @return int
205
+ */
206
+ public function create_redirect( $redirect_from, $redirect_to, $status_code ) {
207
+ $sanitized_redirect_from = $this->sanitize_redirect_from( $redirect_from );
208
+ $sanitized_redirect_to = $this->sanitize_redirect_to( $redirect_to );
209
+ $sanitized_status_code = absint( $status_code );
210
+
211
+ // check and make sure no parameters are empty or invalid after sanitation
212
+ if ( empty( $sanitized_redirect_from ) || empty( $sanitized_redirect_to ) || ! in_array( $sanitized_status_code, $this->valid_status_codes ) )
213
+ return 0;
214
+
215
+ // create the post
216
+ $post_args = array(
217
+ 'post_type' => $this->redirect_post_type,
218
+ 'post_status' => 'publish',
219
+ 'post_author' => 1
220
+ );
221
+
222
+ $post_id = wp_insert_post( $post_args );
223
+
224
+ if ( 0 >= $post_id )
225
+ return 0;
226
+
227
+ // update the posts meta info
228
+ update_post_meta( $post_id, $this->meta_key_redirect_from, $sanitized_redirect_from );
229
+ update_post_meta( $post_id, $this->meta_key_redirect_to, $sanitized_redirect_to );
230
+ update_post_meta( $post_id, $this->meta_key_redirect_status_code, $sanitized_status_code );
231
+
232
+ return $post_id;
233
+ }
234
+
235
  /**
236
  * Whether or not this is an admin page specific to the plugin
237
  *
608
  if ( ! in_array( $with_www, $content ) ) $content[] = $with_www;
609
  }
610
 
611
+ return $content;
612
+ }
613
 
614
  /**
615
  * Force update on the redirect cache and return cache
690
  $this->whitelist_hosts[] = $parsed_redirect['host'];
691
  add_filter( 'allowed_redirect_hosts' , array( $this, 'filter_allowed_redirect_hosts' ) );
692
  }
693
+
694
  // if we have a valid status code, then redirect with it
695
  if ( in_array( $status_code, $this->valid_status_codes ) )
696
  wp_safe_redirect( esc_url_raw( $redirect_to ), $status_code );
713
  * @return string
714
  */
715
  public function sanitize_redirect_to( $path ) {
716
+ $path = trim( $path );
717
 
718
+ if ( preg_match( '/^www\./i', $path ) )
719
+ $path = 'http://' . $path;
720
 
721
+ if ( ! preg_match( '/^https?:\/\//i', $path ) )
722
+ if ( strpos( $path, '/' ) !== 0 )
723
+ $path = '/' . $path;
724
 
725
+ return esc_url_raw( $path );
726
  }
727
 
728
  /**
735
  */
736
  public function sanitize_redirect_from( $path ) {
737
 
738
+ $path = trim( $path );
739
+
740
+ if ( empty( $path ) )
741
+ return '';
742
 
743
+ // dont accept paths starting with a .
744
+ if ( strpos( $path, '.' ) === 0 )
745
+ return '';
746
 
747
+ // turn path in to absolute
748
+ if ( preg_match( '/https?:\/\//i', $path ) )
749
+ $path = preg_replace( '/^(http:\/\/|https:\/\/)(www\.)?[^\/?]+\/?(.*)/i', '/$3', $path );
750
+ elseif ( strpos( $path, '/' ) !== 0 )
751
+ $path = '/' . $path;
752
 
753
+ return esc_url_raw( $path );
754
+ }
755
  }
756
 
757
+ global $safe_redirect_manager;
758
+ $safe_redirect_manager = new SRM_Safe_Redirect_Manager();