Eggplant 301 Redirects - Version 1.2

Version Description

  • Fixed some little bugs.
Download this release

Release Info

Developer shawn@eggplantstudios.ca
Plugin Icon 128x128 Eggplant 301 Redirects
Version 1.2
Comparing to
See all releases

Code changes from version 1.1 to 1.2

Files changed (2) hide show
  1. eps-301-redirects.php +95 -16
  2. readme.txt +22 -3
eps-301-redirects.php CHANGED
@@ -15,7 +15,7 @@
15
  *
16
  * @package EPS 301 Redirects
17
  * @author Shawn Wernig ( shawn@eggplantstudios.ca )
18
- * @version 1.1
19
  */
20
 
21
 
@@ -23,14 +23,15 @@
23
  Plugin Name: Eggplant 301 Redirects
24
  Plugin URI: http://www.eggplantstudios.ca
25
  Description: Create your own 301 redirects using this powerful plugin.
26
- Version: 1.1
27
  Author: Shawn Wernig http://www.eggplantstudios.ca
28
  License: GPLv2 or later
29
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
30
  */
 
31
  define ( 'EPS_REDIRECT_PATH', plugin_dir_path(__FILE__) );
32
  define ( 'EPS_REDIRECT_URL', plugin_dir_url( __FILE__ ) );
33
- define ( 'EPS_REDIRECT_VERSION', 1);
34
 
35
  register_activation_hook(__FILE__, array('EPS_Redirects', 'eps_redirect_activation'));
36
  register_deactivation_hook(__FILE__, array('EPS_Redirects', 'eps_redirect_deactivation'));
@@ -72,7 +73,6 @@ class EPS_Redirects {
72
  public function enqueue_resources(){
73
  wp_enqueue_script('jquery');
74
  wp_enqueue_script('eps_redirect_script', EPS_REDIRECT_URL .'/js/scripts.js');
75
-
76
  wp_enqueue_style('eps_redirect_styles', EPS_REDIRECT_URL .'css/eps_redirect.css');
77
  }
78
 
@@ -106,7 +106,7 @@ class EPS_Redirects {
106
  // Get current url
107
  $url_request = self::get_url();
108
 
109
- foreach ($redirects as $to => $from ) {
110
 
111
  if( rtrim($url_request,'/') == self::format_from_url($from) ) {
112
  // Match, this needs to be redirected
@@ -207,11 +207,13 @@ class EPS_Redirects {
207
  $to = filter_var( $to, FILTER_SANITIZE_URL);
208
  $from = filter_var( $from, FILTER_SANITIZE_URL);
209
 
 
 
210
  // If this is a valid entry, add it to the save array.
211
- if ( !empty($to) && !empty($from)) $redirects[$to] = $from;
212
  }
213
  // If we then have a valid array - save
214
- if (!empty( $redirects) ) update_option( self::$option_slug, $redirects );
215
 
216
  }
217
 
@@ -229,9 +231,8 @@ class EPS_Redirects {
229
  $redirects = get_option( self::$option_slug );
230
  if (empty($redirects)) return false;
231
 
232
- foreach ($redirects as $to => $from ) {
233
- $response_code = self::get_response( self::format_from_url($from) );
234
-
235
  $class = ( $response_code == 301 ) ? 'valid' : 'invalid';
236
 
237
  $html .= '
@@ -305,6 +306,48 @@ class EPS_Redirects {
305
  return $html;
306
  }
307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
  /**
309
  *
310
  * GET_POST_TYPE_SELECT
@@ -319,13 +362,39 @@ class EPS_Redirects {
319
  */
320
  private function get_post_type_select( $post_type ){
321
  global $wpdb;
322
- $entries = $wpdb->get_results("SELECT ID, post_title
323
  FROM $wpdb->posts
324
  WHERE post_status = 'publish'
325
- AND post_type = '$post_type'");
 
326
 
327
- if (!$entries) return false; //
 
 
 
328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
  // Start the select.
330
  $html = '<select class="'.$post_type.' url-selector" style="display:none;">';
331
  $html .= '<option value="" selected default>...</option>';
@@ -342,12 +411,22 @@ class EPS_Redirects {
342
 
343
  // Get all entries and insert them as options.
344
  foreach ($entries as $entry ) {
345
- $html .= '<option value="'.get_permalink($entry->ID).'">'. $entry->post_title. '</option>';
346
  }
347
  $html .= '</select>';
348
  return $html;
349
  }
350
 
 
 
 
 
 
 
 
 
 
 
351
 
352
  /**
353
  *
@@ -452,8 +531,8 @@ function eps_prettify( $string ) {
452
  }
453
 
454
 
455
-
456
-
457
  // Run the plugin.
458
  $EPS_Redirects = new EPS_Redirects();
459
  ?>
15
  *
16
  * @package EPS 301 Redirects
17
  * @author Shawn Wernig ( shawn@eggplantstudios.ca )
18
+ * @version 1.3.1
19
  */
20
 
21
 
23
  Plugin Name: Eggplant 301 Redirects
24
  Plugin URI: http://www.eggplantstudios.ca
25
  Description: Create your own 301 redirects using this powerful plugin.
26
+ Version: 1.3.1
27
  Author: Shawn Wernig http://www.eggplantstudios.ca
28
  License: GPLv2 or later
29
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
30
  */
31
+
32
  define ( 'EPS_REDIRECT_PATH', plugin_dir_path(__FILE__) );
33
  define ( 'EPS_REDIRECT_URL', plugin_dir_url( __FILE__ ) );
34
+ define ( 'EPS_REDIRECT_VERSION', 1.2);
35
 
36
  register_activation_hook(__FILE__, array('EPS_Redirects', 'eps_redirect_activation'));
37
  register_deactivation_hook(__FILE__, array('EPS_Redirects', 'eps_redirect_deactivation'));
73
  public function enqueue_resources(){
74
  wp_enqueue_script('jquery');
75
  wp_enqueue_script('eps_redirect_script', EPS_REDIRECT_URL .'/js/scripts.js');
 
76
  wp_enqueue_style('eps_redirect_styles', EPS_REDIRECT_URL .'css/eps_redirect.css');
77
  }
78
 
106
  // Get current url
107
  $url_request = self::get_url();
108
 
109
+ foreach ($redirects as $from => $to ) {
110
 
111
  if( rtrim($url_request,'/') == self::format_from_url($from) ) {
112
  // Match, this needs to be redirected
207
  $to = filter_var( $to, FILTER_SANITIZE_URL);
208
  $from = filter_var( $from, FILTER_SANITIZE_URL);
209
 
210
+ if( empty($to) ) $to = home_url() . '/'; // default
211
+
212
  // If this is a valid entry, add it to the save array.
213
+ if ( !empty($from)) $redirects[$from] = $to;
214
  }
215
  // If we then have a valid array - save
216
+ update_option( self::$option_slug, $redirects );
217
 
218
  }
219
 
231
  $redirects = get_option( self::$option_slug );
232
  if (empty($redirects)) return false;
233
 
234
+ foreach ($redirects as $from => $to ) {
235
+ $response_code = self::get_response( self::format_from_url( $from ) );
 
236
  $class = ( $response_code == 301 ) ? 'valid' : 'invalid';
237
 
238
  $html .= '
306
  return $html;
307
  }
308
 
309
+
310
+
311
+ /**
312
+ *
313
+ * GET_PARENT_INDEX
314
+ *
315
+ * Scans a custom array of posts to find a parent's position in the array. Used in GET_POST_TYPE_SELECT
316
+ *
317
+ * @return html string
318
+ * @param $post_type = the post type slug.
319
+ * @author epstudios
320
+ *
321
+ */
322
+ function find_parent_index( $id, $entries ){
323
+ foreach($entries as $k => $entry ) {
324
+ //print_r($entry); echo '<br>';
325
+ if ( $entry->ID == $id ) return( $k );
326
+ }
327
+ return false;
328
+ }
329
+ /**
330
+ *
331
+ * GET_PARENT_INDEX
332
+ *
333
+ * Scans a custom array of posts to find a parent's position in the array. Used in GET_POST_TYPE_SELECT
334
+ *
335
+ * @return html string
336
+ * @param $post_type = the post type slug.
337
+ * @author epstudios
338
+ *
339
+ */
340
+ function get_post_depth( $id ){
341
+ global $wpdb;
342
+ $depth = 0;
343
+ $parent_id = $id;
344
+ while ($parent_id > 0) {
345
+ $parent_id = $wpdb->get_var( "SELECT post_parent FROM $wpdb->posts WHERE ID = $parent_id" );
346
+ $depth ++;
347
+ }
348
+ return $depth;
349
+ }
350
+
351
  /**
352
  *
353
  * GET_POST_TYPE_SELECT
362
  */
363
  private function get_post_type_select( $post_type ){
364
  global $wpdb;
365
+ $entries = $wpdb->get_results("SELECT ID, post_title, post_parent
366
  FROM $wpdb->posts
367
  WHERE post_status = 'publish'
368
+ AND post_type = '$post_type'
369
+ ORDER BY post_title ASC");
370
 
371
+
372
+ if (!$entries) return false;
373
+
374
+ // create heirarchy
375
 
376
+ // get depths
377
+ $max_depth = 0;
378
+ foreach($entries as $k => $entry ) {
379
+ $entry->depth = self::get_post_depth( $entry->post_parent );
380
+ if($entry->depth > $max_depth) $max_depth = $entry->depth;
381
+ }
382
+
383
+ // Nest arrays as parent >> children
384
+ for( $i = $max_depth; $i >= 0; $i -- ) {
385
+ foreach( $entries as $k => $entry ) {
386
+ if ( $entry->depth == $i ) {
387
+ if ( $entry->post_parent > 0 ) {
388
+ $entry->post_title = '&nbsp;' . str_repeat("-", $depth). ' ' . $entry->post_title;
389
+ $parent_index = self::find_parent_index( $entry->post_parent, $entries );
390
+
391
+ $entries[$parent_index]->children[] = $entry;
392
+ unset($entries[$k]);
393
+ }
394
+ }
395
+ }
396
+ }
397
+
398
  // Start the select.
399
  $html = '<select class="'.$post_type.' url-selector" style="display:none;">';
400
  $html .= '<option value="" selected default>...</option>';
411
 
412
  // Get all entries and insert them as options.
413
  foreach ($entries as $entry ) {
414
+ $html .= self::do_post_heirarchy_selects( $entry );
415
  }
416
  $html .= '</select>';
417
  return $html;
418
  }
419
 
420
+ function do_post_heirarchy_selects( $entry ) {
421
+ $html .= '<option value="'.get_permalink($entry->ID).'">'. str_repeat("-", $entry->depth) . $entry->post_title . '</option>';
422
+
423
+ if( isset( $entry->children ) && !empty( $entry->children ) ) {
424
+ foreach ($entry->children as $child ) {
425
+ $html .= self::do_post_heirarchy_selects($child);
426
+ }
427
+ }
428
+ return $html;
429
+ }
430
 
431
  /**
432
  *
531
  }
532
 
533
 
534
+
535
+
536
  // Run the plugin.
537
  $EPS_Redirects = new EPS_Redirects();
538
  ?>
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: shawneggplantstudiosca
3
  Donate link: none
4
  Tags: 301 redirects, redirects
5
  Requires at least: 3.0.1
6
- Tested up to: 3.4
7
- Stable tag: trunk
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -75,6 +75,16 @@ Click the small X beside the redirect you wish to remove. Save changes.
75
 
76
  == Changelog ==
77
 
 
 
 
 
 
 
 
 
 
 
78
  = 1.1 =
79
  * Minor CSS and usability fixes. Also checking out the SVN!
80
 
@@ -83,7 +93,16 @@ Click the small X beside the redirect you wish to remove. Save changes.
83
 
84
  == Upgrade Notice ==
85
 
86
- = 1.0 =
 
 
 
 
 
 
 
 
 
87
  Cosmetic updates.
88
 
89
  = 1.0 =
3
  Donate link: none
4
  Tags: 301 redirects, redirects
5
  Requires at least: 3.0.1
6
+ Tested up to: 3.5
7
+ Stable tag: 1.3.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
75
 
76
  == Changelog ==
77
 
78
+ = 1.3.1 =
79
+ * Added hierarchy to heirarchical post type selects.
80
+
81
+
82
+ = 1.3 =
83
+ * Fixed a bug where duplicate urls were being overwritten, fixed a bug where you could not completely remove all redirects.
84
+
85
+ = 1.2 =
86
+ * Fixed some little bugs.
87
+
88
  = 1.1 =
89
  * Minor CSS and usability fixes. Also checking out the SVN!
90
 
93
 
94
  == Upgrade Notice ==
95
 
96
+ = 1.3.1 =
97
+ Functionality update, Cosmetic.
98
+
99
+ = 1.3 =
100
+ Bug fixes; Update ASAP.
101
+
102
+ = 1.2 =
103
+ Cosmetic updates.
104
+
105
+ = 1.1 =
106
  Cosmetic updates.
107
 
108
  = 1.0 =