Permalink Manager Lite - Version 0.4.6

Version Description

Download this release

Release Info

Developer mbis
Plugin Icon 128x128 Permalink Manager Lite
Version 0.4.6
Comparing to
See all releases

Code changes from version 0.4.4 to 0.4.6

README.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: mbis
3
  Tags: urls, permalinks, slugs, custom url, custom permalinks, uris, url, slug, permalink
4
  Requires at least: 4.0
5
- Tested up to: 4.5.3
6
- Stable tag: 0.4.4
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -82,6 +82,14 @@ After the plugin is installed you can access its dashboard from this page: `Tool
82
 
83
  == Changelog ==
84
 
 
 
 
 
 
 
 
 
85
  = 0.4.4 =
86
  * Redirect for old URIs added.
87
  * Debug tools added.
2
  Contributors: mbis
3
  Tags: urls, permalinks, slugs, custom url, custom permalinks, uris, url, slug, permalink
4
  Requires at least: 4.0
5
+ Tested up to: 4.6
6
+ Stable tag: 0.4.6
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
82
 
83
  == Changelog ==
84
 
85
+ = 0.4.5 =
86
+ * 302 redirect fix.
87
+ * Code optimization.
88
+
89
+ = 0.4.5 =
90
+ * Bug with infinite loop fixed.
91
+ * Bug with revisions ID fixed.
92
+
93
  = 0.4.4 =
94
  * Redirect for old URIs added.
95
  * Debug tools added.
inc/permalink-manager-base-editor.php CHANGED
@@ -137,12 +137,5 @@ class Permalink_Manager_Base_Editor extends WP_List_Table {
137
  $this->items = $sliced_data;
138
  }
139
 
140
- /**
141
- * This variable is assigned in permalink-manager.php before prepare_items() function is triggered, see permalinks_table_html() function
142
- */
143
- public function set_screen_option_fields($fields) {
144
- $this->screen_options_fields = $fields;
145
- }
146
-
147
  }
148
  ?>
137
  $this->items = $sliced_data;
138
  }
139
 
 
 
 
 
 
 
 
140
  }
141
  ?>
inc/permalink-manager-editor.php CHANGED
@@ -150,6 +150,10 @@ class Permalink_Manager_Editor extends WP_List_Table {
150
  $sql_query = "SELECT * FROM {$wpdb->posts} WHERE post_status IN ($post_statuses) AND post_type IN ($post_types) ORDER BY $orderby $order LIMIT $per_page OFFSET $offset";
151
  $data = $wpdb->get_results($sql_query, ARRAY_A);
152
 
 
 
 
 
153
  // Sort posts and count all posts
154
  usort( $data, array( &$this, 'sort_data' ) );
155
 
@@ -162,12 +166,5 @@ class Permalink_Manager_Editor extends WP_List_Table {
162
  $this->items = $data;
163
  }
164
 
165
- /**
166
- * This variable is assigned in permalink-manager.php before prepare_items() function is triggered, see permalinks_table_html() function
167
- */
168
- public function set_screen_option_fields($fields) {
169
- $this->screen_options_fields = $fields;
170
- }
171
-
172
  }
173
  ?>
150
  $sql_query = "SELECT * FROM {$wpdb->posts} WHERE post_status IN ($post_statuses) AND post_type IN ($post_types) ORDER BY $orderby $order LIMIT $per_page OFFSET $offset";
151
  $data = $wpdb->get_results($sql_query, ARRAY_A);
152
 
153
+ // Debug SQL query
154
+ $debug_txt = "<textarea style=\"width:100%;height:300px\">{$sql_query} \n\nOffset: {$offset} \nPage: {$currentPage}\nPer page: {$per_page} \nTotal: {$total_items}</textarea>";
155
+ if(isset($_REQUEST['debug_editor_sql'])) { wp_die($debug_txt); }
156
+
157
  // Sort posts and count all posts
158
  usort( $data, array( &$this, 'sort_data' ) );
159
 
166
  $this->items = $data;
167
  }
168
 
 
 
 
 
 
 
 
169
  }
170
  ?>
inc/permalink-manager-helper-functions.php CHANGED
@@ -190,9 +190,7 @@ class Permalink_Manager_Helper_Functions extends Permalink_Manager_Class {
190
  }
191
 
192
  // Get options
193
- if($permastruct) {
194
- $default_base = trim($permastruct, '/');
195
- }
196
 
197
  // Get the date
198
  $date = explode(" ",date('Y m d H i s', strtotime($post->post_date)));
190
  }
191
 
192
  // Get options
193
+ $default_base = ($permastruct) ? trim($permastruct, '/') : "";
 
 
194
 
195
  // Get the date
196
  $date = explode(" ",date('Y m d H i s', strtotime($post->post_date)));
permalink-manager.php CHANGED
@@ -4,7 +4,7 @@
4
  * Plugin Name: Permalink Manager
5
  * Plugin URI: http://maciejbis.net/
6
  * Description: A simple tool that allows to mass update of slugs that are used to build permalinks for Posts, Pages and Custom Post Types.
7
- * Version: 0.4.4
8
  * Author: Maciej Bis
9
  * Author URI: http://maciejbis.net/
10
  * License: GPL-2.0+
@@ -20,7 +20,7 @@ if ( ! defined( 'WPINC' ) ) {
20
 
21
  // Define the directories used to load plugin files.
22
  define( 'PERMALINK_MANAGER_PLUGIN_NAME', 'permalink-manager' );
23
- define( 'PERMALINK_MANAGER_VERSION', '0.4.4' );
24
  define( 'PERMALINK_MANAGER_DIR', untrailingslashit( dirname( __FILE__ ) ) );
25
  define( 'PERMALINK_MANAGER_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
26
  define( 'PERMALINK_MANAGER_WEBSITE', 'http://maciejbis.net' );
@@ -40,7 +40,7 @@ class Permalink_Manager_Class {
40
  if( is_admin() ) {
41
  add_action( 'plugins_loaded', array($this, 'localize_me') );
42
  add_action( 'init', array($this, 'upgrade_plugin'), 999 );
43
- add_action( 'wp_loaded', array($this, 'debug_filters'), 999999999999 );
44
  add_action( 'wp_loaded', array($this, 'bulk_actions'), 1 );
45
  add_action( 'admin_menu', array($this, 'add_menu_page') );
46
  add_action( 'save_post', array($this, 'update_single_uri'), 10, 3 );
@@ -87,7 +87,7 @@ class Permalink_Manager_Class {
87
  global $wpdb;
88
 
89
  $Permalink_Manager_Editor = new Permalink_Manager_Editor();
90
- $Permalink_Manager_Editor->set_screen_option_fields($this->fields_arrays('screen_options'));
91
  $Permalink_Manager_Editor->prepare_items($wpdb->posts);
92
 
93
  ?>
@@ -282,32 +282,32 @@ class Permalink_Manager_Class {
282
  /**
283
  * Fields for "Screen Options"
284
  */
285
- function fields_arrays($array) {
286
 
287
  // All registered post types array
288
  $all_post_statuses_array = get_post_statuses();
289
  $all_post_types = Permalink_Manager_Helper_Functions::get_post_types_array();
290
 
291
  // Fields for "Screen Options"
292
- $screen_options = array(
293
- 'post_types' => array(
294
- 'label' => __( 'Post Types', 'permalink-manager' ),
295
- 'type' => 'checkbox',
296
- 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
297
- 'default' => array('post', 'page')
298
- ),
299
  'post_statuses' => array(
300
  'label' => __( 'Post Statuses', 'permalink-manager' ),
301
  'type' => 'checkbox',
302
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
303
  'default' => array('publish')
304
- ),
305
  'per_page' => array(
306
- 'label' => __( 'Per page', 'permalink-manager' ),
307
- 'type' => 'number',
308
- 'default' => 10
309
- )
310
- );
311
 
312
  // Fields for "Find and replace"
313
  $find_and_replace = array(
@@ -315,15 +315,15 @@ class Permalink_Manager_Class {
315
  'type' => 'clearfix'
316
  ),
317
  'old_string' => array(
318
- 'label' => __( 'Find ...', 'permalink-manager' ),
319
- 'type' => 'text',
320
- 'container_class' => 'half'
321
- ),
322
  'new_string' => array(
323
- 'label' => __( 'Replace with ...', 'permalink-manager' ),
324
- 'type' => 'text',
325
- 'container_class' => 'half half2'
326
- ),
327
  'clearfix2' => array(
328
  'type' => 'clearfix'
329
  ),
@@ -333,20 +333,20 @@ class Permalink_Manager_Class {
333
  'choices' => array('both' => '<strong>' . __('Plugin Slugs & Bases (Full URIs)', 'permalink-manager') . '</strong>', 'slugs' => '<strong>' . __('Only Plugin Slugs', 'permalink-manager') . '</strong>', 'post_names' => '<strong>' . __('Plugin Slugs & Wordpress Native Slugs (Post Names)', 'permalink-manager') . '</strong>'),
334
  'default' => array('slugs'),
335
  'desc' => __('First two options will affect settings used only by this plugin.<br />A MySQL backup is recommended before using third option - it overwrites the value of <strong>post_name</strong> field (part of <strong>$post</strong> object used by Wordpress core).', 'permalink-manager')
336
- ),
337
  'post_types' => array(
338
  'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
339
  'type' => 'checkbox',
340
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
341
  'default' => array('post', 'page')
342
- ),
343
  'post_statuses' => array(
344
  'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
345
  'type' => 'checkbox',
346
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
347
  'default' => array('publish')
348
- )
349
- );
350
 
351
  // Fields for "Regenerate slugs"
352
  $regenerate_slugs = array(
@@ -356,24 +356,24 @@ class Permalink_Manager_Class {
356
  'choices' => array('both' => '<strong>' . __('Plugin Slugs & Bases (Full URIs)', 'permalink-manager') . '</strong>', 'slugs' => '<strong>' . __('Only Plugin Slugs', 'permalink-manager') . '</strong>', 'post_names' => '<strong>' . __('Plugin Slugs & Wordpress Native Slugs (Post Names)', 'permalink-manager') . '</strong>'),
357
  'default' => array('slugs'),
358
  'desc' => __('First two options will affect settings used only by this plugin.<br />A MySQL backup is recommended before using third option - it overwrites the value of <strong>post_name</strong> field (part of <strong>$post</strong> object used by Wordpress core).', 'permalink-manager')
359
- ),
360
  'post_types' => array(
361
  'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
362
  'type' => 'checkbox',
363
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
364
  'default' => array('post', 'page')
365
- ),
366
  'post_statuses' => array(
367
  'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
368
  'type' => 'checkbox',
369
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
370
  'default' => array('publish')
371
- )
372
- );
373
 
374
  return isset($array) ? ${$array} : array();
375
 
376
- }
377
 
378
  /**
379
  * Bulk actions functions
@@ -465,7 +465,7 @@ class Permalink_Manager_Class {
465
  });
466
  }
467
  }
468
- }
469
 
470
  /**
471
  * Change permalinks for posts, pages & custom post types
@@ -513,7 +513,7 @@ class Permalink_Manager_Class {
513
 
514
  // Parse URL
515
  $url_parts = parse_url($url);
516
- $uri = trim($url_parts['path'], "/");
517
  if(empty($uri)) return $query;
518
 
519
  // Check if current URL is assigned to any post
@@ -522,23 +522,39 @@ class Permalink_Manager_Class {
522
  $post_id = array_search($uri, $uris);
523
 
524
  if(isset($post_id) && is_numeric($post_id)) {
 
 
 
 
525
  $post_to_load = get_post($post_id);
526
- $original_page_uri = get_page_uri($post_to_load->ID);
 
527
  unset($query['attachment']);
528
  unset($query['error']);
529
 
 
 
 
 
 
 
 
 
 
 
 
530
  if($post_to_load->post_type == 'page') {
531
  $query['pagename'] = $original_page_uri;
532
- } elseif($post_to_load->post_type == 'post') {
533
  $query['name'] = $original_page_uri;
534
  } else {
535
- $query['post_type'] = $post_to_load->post_type;
536
  $query['name'] = $original_page_uri;
537
- $query[$post_to_load->post_type] = $original_page_uri;
 
538
  }
539
 
540
  // Add endpoint
541
- if(isset($endpoint_value)) {
542
  $query[$endpoint] = $endpoint_value;
543
  }
544
  }
@@ -549,7 +565,8 @@ class Permalink_Manager_Class {
549
  if(isset($_REQUEST['debug_url']) && $_REQUEST['debug_url'] == true) {
550
  $debug_info['old_query_vars'] = $old_query;
551
  $debug_info['new_query_vars'] = $query;
552
- $debug_info['request'] = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
 
553
 
554
  $debug_txt = json_encode($debug_info);
555
  $debug_txt = "<textarea style=\"width:100%;height:300px\">{$debug_txt}</textarea>";
@@ -579,11 +596,18 @@ class Permalink_Manager_Class {
579
  $old_default_uri = home_url('/') . Permalink_Manager_Helper_Functions::get_uri($object_id, 'native');
580
  $correct_uri = str_replace($old_default_uri, $uri, $request_uri);
581
 
 
 
 
 
 
 
 
582
  if (isset($_REQUEST['debug_redirect'])) {
583
  wp_die("<strong>ID:</strong> {$object_id}<br /> <strong>URI:</strong> {$uri}<br/> <strong>Default URI:</strong> {$old_default_uri}<br /><strong>Correct URI:</strong> {$correct_uri}<br /><strong>Request URI</strong>{$request_uri}");
584
  // Redirect if it is old URI
585
  } else if(strpos($request_uri, $old_default_uri) !== false && ($redirects_activated == 1)) {
586
- wp_redirect( $correct_uri );
587
  exit();
588
  }
589
 
@@ -616,10 +640,15 @@ class Permalink_Manager_Class {
616
  // Ignore trashed items
617
  if($post->post_status == 'trash') return;
618
 
 
 
 
 
 
619
  $uris = $this->permalink_manager_uris;
620
 
621
  $old_default_uri = Permalink_Manager_Helper_Functions::get_uri($post, 'native');
622
- $old_uri = (isset($uris[$post_id])) ? $uris[$post_id] : $old_default_uri;
623
 
624
  $new_default_uri = Permalink_Manager_Helper_Functions::get_uri($post, true);
625
  $new_uri = '';
@@ -633,10 +662,10 @@ class Permalink_Manager_Class {
633
  $new_uri = ($new_uri) ? $new_uri : $new_default_uri;
634
 
635
  // Do not store default values
636
- if(isset($uris[$post_id]) && ($new_uri == $old_default_uri)) {
637
- unset($uris[$post_id]);
638
  } else if ($new_uri != $old_default_uri) {
639
- $uris[$post_id] = $new_uri;
640
  }
641
 
642
 
@@ -646,13 +675,19 @@ class Permalink_Manager_Class {
646
  /**
647
  * Debug helper function
648
  */
649
- function debug_filters() {
650
  global $wp_filter;
 
651
  if(isset($_REQUEST['debug_filter'])) {
652
  $hook = $_REQUEST['debug_filter'];
653
  $debug_txt = ($_REQUEST['debug_filter'] == "all") ? json_encode($wp_filter) : json_encode($wp_filter[$hook]);
654
  $debug_txt = "<textarea style=\"width:100%;height:300px\">{$debug_txt}</textarea>";
655
  wp_die($debug_txt);
 
 
 
 
 
656
  }
657
  }
658
 
4
  * Plugin Name: Permalink Manager
5
  * Plugin URI: http://maciejbis.net/
6
  * Description: A simple tool that allows to mass update of slugs that are used to build permalinks for Posts, Pages and Custom Post Types.
7
+ * Version: 0.4.6
8
  * Author: Maciej Bis
9
  * Author URI: http://maciejbis.net/
10
  * License: GPL-2.0+
20
 
21
  // Define the directories used to load plugin files.
22
  define( 'PERMALINK_MANAGER_PLUGIN_NAME', 'permalink-manager' );
23
+ define( 'PERMALINK_MANAGER_VERSION', '0.4.6' );
24
  define( 'PERMALINK_MANAGER_DIR', untrailingslashit( dirname( __FILE__ ) ) );
25
  define( 'PERMALINK_MANAGER_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
26
  define( 'PERMALINK_MANAGER_WEBSITE', 'http://maciejbis.net' );
40
  if( is_admin() ) {
41
  add_action( 'plugins_loaded', array($this, 'localize_me') );
42
  add_action( 'init', array($this, 'upgrade_plugin'), 999 );
43
+ add_action( 'init', array($this, 'debug_functions'), 999999999999 );
44
  add_action( 'wp_loaded', array($this, 'bulk_actions'), 1 );
45
  add_action( 'admin_menu', array($this, 'add_menu_page') );
46
  add_action( 'save_post', array($this, 'update_single_uri'), 10, 3 );
87
  global $wpdb;
88
 
89
  $Permalink_Manager_Editor = new Permalink_Manager_Editor();
90
+ $Permalink_Manager_Editor->set_screen_option_fields($this->fields_arrays('screen_options'));
91
  $Permalink_Manager_Editor->prepare_items($wpdb->posts);
92
 
93
  ?>
282
  /**
283
  * Fields for "Screen Options"
284
  */
285
+ function fields_arrays($array) {
286
 
287
  // All registered post types array
288
  $all_post_statuses_array = get_post_statuses();
289
  $all_post_types = Permalink_Manager_Helper_Functions::get_post_types_array();
290
 
291
  // Fields for "Screen Options"
292
+ $screen_options = array(
293
+ 'post_types' => array(
294
+ 'label' => __( 'Post Types', 'permalink-manager' ),
295
+ 'type' => 'checkbox',
296
+ 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
297
+ 'default' => array('post', 'page')
298
+ ),
299
  'post_statuses' => array(
300
  'label' => __( 'Post Statuses', 'permalink-manager' ),
301
  'type' => 'checkbox',
302
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
303
  'default' => array('publish')
304
+ ),
305
  'per_page' => array(
306
+ 'label' => __( 'Per page', 'permalink-manager' ),
307
+ 'type' => 'number',
308
+ 'default' => 10
309
+ )
310
+ );
311
 
312
  // Fields for "Find and replace"
313
  $find_and_replace = array(
315
  'type' => 'clearfix'
316
  ),
317
  'old_string' => array(
318
+ 'label' => __( 'Find ...', 'permalink-manager' ),
319
+ 'type' => 'text',
320
+ 'container_class' => 'half'
321
+ ),
322
  'new_string' => array(
323
+ 'label' => __( 'Replace with ...', 'permalink-manager' ),
324
+ 'type' => 'text',
325
+ 'container_class' => 'half half2'
326
+ ),
327
  'clearfix2' => array(
328
  'type' => 'clearfix'
329
  ),
333
  'choices' => array('both' => '<strong>' . __('Plugin Slugs & Bases (Full URIs)', 'permalink-manager') . '</strong>', 'slugs' => '<strong>' . __('Only Plugin Slugs', 'permalink-manager') . '</strong>', 'post_names' => '<strong>' . __('Plugin Slugs & Wordpress Native Slugs (Post Names)', 'permalink-manager') . '</strong>'),
334
  'default' => array('slugs'),
335
  'desc' => __('First two options will affect settings used only by this plugin.<br />A MySQL backup is recommended before using third option - it overwrites the value of <strong>post_name</strong> field (part of <strong>$post</strong> object used by Wordpress core).', 'permalink-manager')
336
+ ),
337
  'post_types' => array(
338
  'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
339
  'type' => 'checkbox',
340
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
341
  'default' => array('post', 'page')
342
+ ),
343
  'post_statuses' => array(
344
  'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
345
  'type' => 'checkbox',
346
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
347
  'default' => array('publish')
348
+ )
349
+ );
350
 
351
  // Fields for "Regenerate slugs"
352
  $regenerate_slugs = array(
356
  'choices' => array('both' => '<strong>' . __('Plugin Slugs & Bases (Full URIs)', 'permalink-manager') . '</strong>', 'slugs' => '<strong>' . __('Only Plugin Slugs', 'permalink-manager') . '</strong>', 'post_names' => '<strong>' . __('Plugin Slugs & Wordpress Native Slugs (Post Names)', 'permalink-manager') . '</strong>'),
357
  'default' => array('slugs'),
358
  'desc' => __('First two options will affect settings used only by this plugin.<br />A MySQL backup is recommended before using third option - it overwrites the value of <strong>post_name</strong> field (part of <strong>$post</strong> object used by Wordpress core).', 'permalink-manager')
359
+ ),
360
  'post_types' => array(
361
  'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
362
  'type' => 'checkbox',
363
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
364
  'default' => array('post', 'page')
365
+ ),
366
  'post_statuses' => array(
367
  'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
368
  'type' => 'checkbox',
369
  'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
370
  'default' => array('publish')
371
+ )
372
+ );
373
 
374
  return isset($array) ? ${$array} : array();
375
 
376
+ }
377
 
378
  /**
379
  * Bulk actions functions
465
  });
466
  }
467
  }
468
+ }
469
 
470
  /**
471
  * Change permalinks for posts, pages & custom post types
513
 
514
  // Parse URL
515
  $url_parts = parse_url($url);
516
+ $uri = (isset($url_parts['path'])) ? trim($url_parts['path'], "/") : "";
517
  if(empty($uri)) return $query;
518
 
519
  // Check if current URL is assigned to any post
522
  $post_id = array_search($uri, $uris);
523
 
524
  if(isset($post_id) && is_numeric($post_id)) {
525
+ // Check if it is revision (hotfix) and use original post ID instead of revision ID
526
+ $is_revision = wp_is_post_revision($post_id);
527
+ $post_id = ($is_revision) ? $is_revision : $post_id;
528
+
529
  $post_to_load = get_post($post_id);
530
+ $original_page_uri = $post_to_load->post_name;
531
+ $post_type = $post_to_load->post_type;
532
  unset($query['attachment']);
533
  unset($query['error']);
534
 
535
+ // Fix for hierarchical CPT & pages
536
+ if( isset($post_to_load->ancestors) && !(empty($post_to_load->ancestors))) {
537
+ foreach ( $post_to_load->ancestors as $parent ) {
538
+ $parent = get_post( $parent );
539
+ if ( $parent && $parent->post_name ) {
540
+ $original_page_uri = $parent->post_name . '/' . $original_page_uri;
541
+ }
542
+ }
543
+ }
544
+
545
+ // Alter query parameters
546
  if($post_to_load->post_type == 'page') {
547
  $query['pagename'] = $original_page_uri;
548
+ } else if($post_to_load->post_type == 'post') {
549
  $query['name'] = $original_page_uri;
550
  } else {
 
551
  $query['name'] = $original_page_uri;
552
+ $query['post_type'] = $post_type;
553
+ $query[$post_type] = $original_page_uri;
554
  }
555
 
556
  // Add endpoint
557
+ if(isset($endpoint_value) && !(empty($endpoint_value))) {
558
  $query[$endpoint] = $endpoint_value;
559
  }
560
  }
565
  if(isset($_REQUEST['debug_url']) && $_REQUEST['debug_url'] == true) {
566
  $debug_info['old_query_vars'] = $old_query;
567
  $debug_info['new_query_vars'] = $query;
568
+ //$debug_info['request'] = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
569
+ $debug_info['request'] = $url;
570
 
571
  $debug_txt = json_encode($debug_info);
572
  $debug_txt = "<textarea style=\"width:100%;height:300px\">{$debug_txt}</textarea>";
596
  $old_default_uri = home_url('/') . Permalink_Manager_Helper_Functions::get_uri($object_id, 'native');
597
  $correct_uri = str_replace($old_default_uri, $uri, $request_uri);
598
 
599
+ // Fix redirection loop and .html suffix issues
600
+ if(substr($correct_uri, -1) != '/' && substr($correct_uri, -4) != 'html') {
601
+ $correct_uri .= '/';
602
+ } else if(substr($correct_uri, -5) == 'html/') {
603
+ $correct_uri = strstr($correct_uri, '.html', true) . '.html';
604
+ }
605
+
606
  if (isset($_REQUEST['debug_redirect'])) {
607
  wp_die("<strong>ID:</strong> {$object_id}<br /> <strong>URI:</strong> {$uri}<br/> <strong>Default URI:</strong> {$old_default_uri}<br /><strong>Correct URI:</strong> {$correct_uri}<br /><strong>Request URI</strong>{$request_uri}");
608
  // Redirect if it is old URI
609
  } else if(strpos($request_uri, $old_default_uri) !== false && ($redirects_activated == 1)) {
610
+ wp_redirect( $correct_uri, 301 );
611
  exit();
612
  }
613
 
640
  // Ignore trashed items
641
  if($post->post_status == 'trash') return;
642
 
643
+ // Fix for revisions
644
+ $is_revision = wp_is_post_revision($post_id);
645
+ $post_id = ($is_revision) ? $is_revision : $post_id;
646
+ $post = get_post($post_id);
647
+
648
  $uris = $this->permalink_manager_uris;
649
 
650
  $old_default_uri = Permalink_Manager_Helper_Functions::get_uri($post, 'native');
651
+ $old_uri = (isset($uris[$post->ID])) ? $uris[$post->ID] : $old_default_uri;
652
 
653
  $new_default_uri = Permalink_Manager_Helper_Functions::get_uri($post, true);
654
  $new_uri = '';
662
  $new_uri = ($new_uri) ? $new_uri : $new_default_uri;
663
 
664
  // Do not store default values
665
+ if(isset($uris[$post->ID]) && ($new_uri == $old_default_uri)) {
666
+ unset($uris[$post->ID]);
667
  } else if ($new_uri != $old_default_uri) {
668
+ $uris[$post->ID] = $new_uri;
669
  }
670
 
671
 
675
  /**
676
  * Debug helper function
677
  */
678
+ function debug_functions() {
679
  global $wp_filter;
680
+
681
  if(isset($_REQUEST['debug_filter'])) {
682
  $hook = $_REQUEST['debug_filter'];
683
  $debug_txt = ($_REQUEST['debug_filter'] == "all") ? json_encode($wp_filter) : json_encode($wp_filter[$hook]);
684
  $debug_txt = "<textarea style=\"width:100%;height:300px\">{$debug_txt}</textarea>";
685
  wp_die($debug_txt);
686
+ } else if(isset($_REQUEST['debug_options'])) {
687
+ $options = get_option($_REQUEST['debug_options']);
688
+ $debug_txt = print_r($options, true);
689
+ $debug_txt = "<textarea style=\"width:100%;height:300px\">{$debug_txt}</textarea>";
690
+ wp_die($debug_txt);
691
  }
692
  }
693