PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 0.3
Permalink Manager Lite v0.3
2.5.3.4 2.2.18 2.2.19.2 2.2.19.3 2.2.19.3.1 2.2.2 2.2.20 2.2.20.1 2.2.20.3 2.2.4 2.2.5 2.2.6 2.2.7.2 2.2.7.3 2.2.7.5 2.2.7.6 2.2.8.4 2.2.8.5 2.2.8.6 2.2.8.7 2.2.8.9 2.2.9.1 2.2.9.2 2.2.9.2.1 2.2.9.3 2.2.9.4 2.2.9.6 2.2.9.7 2.2.9.9 2.3.0 2.3.1.1 2.4.0 2.4.1 2.4.1.2 2.4.1.3 2.4.1.4 2.4.1.5 2.4.1.6 2.4.2 2.4.2.1 2.4.3 2.4.3.1 2.4.3.2 2.4.3.3 2.4.3.4 2.4.4 2.4.4.1 2.4.4.2 2.4.4.3 2.5.0 2.5.1 2.5.1.1 2.5.1.2 2.5.1.3 2.5.1.4 2.5.2 2.5.2.1 2.5.2.2 2.5.2.3 2.5.2.4 2.5.3 2.5.3.1 2.5.3.2 2.5.3.3 trunk 0.2 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 0.4.4 0.4.6 0.4.7 0.4.8 0.4.9 0.5.3 0.5.4 1.0.0 1.0.1 1.0.4 1.1.0 1.1.1 1.1.2 1.11.6.3 2.0.0 2.0.3 2.0.4 2.0.4.3 2.0.5.1 2.0.5.2 2.0.5.3 2.0.5.3.1 2.0.5.4 2.0.5.4a 2.0.5.5 2.0.5.6 2.0.5.6.1 2.0.5.7 2.0.5.9a 2.0.6.2.1 2.0.6.2a 2.0.6.3 2.1.0 2.1.1 2.1.2.4 2.2.0 2.2.1.1 2.2.1.2 2.2.11 2.2.12 2.2.13.1 2.2.14 2.2.15.1 2.2.16 2.2.17
permalink-manager / permalink-manager.php
permalink-manager Last commit date
css 10 years ago inc 10 years ago js 10 years ago languages 10 years ago LICENSE.txt 10 years ago README.txt 10 years ago permalink-manager.php 10 years ago
permalink-manager.php
645 lines
1 <?php
2
3 /**
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.3.0
8 * Author: Maciej Bis
9 * Author URI: http://maciejbis.net/
10 * License: GPL-2.0+
11 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
12 * Text Domain: permalink-manager
13 * Domain Path: /languages
14 */
15
16 // If this file is called directly, abort.
17 if ( ! defined( 'WPINC' ) ) {
18 die;
19 }
20
21 // Define the directories used to load plugin files.
22 define( 'PERMALINK_MANAGER_PLUGIN_NAME', 'permalink-manager' );
23 define( 'PERMALINK_MANAGER_VERSION', '0.3.0' );
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' );
27 define( 'PERMALINK_MANAGER_MENU_PAGE', 'tools_page_permalink-manager' );
28 define( 'PERMALINK_MANAGER_OPTIONS_PAGE', PERMALINK_MANAGER_PLUGIN_NAME . '.php' );
29
30 class Permalink_Manager_Class {
31
32 protected $permalink_manager, $admin_page, $permalink_manager_options_page, $permalink_manager_options;
33
34 public function __construct() {
35
36 $this->permalink_manager_options = get_option('permalink-manager');
37
38 add_action( 'plugins_loaded', array($this, 'localize_me') );
39 add_action( 'init', array($this, 'flush_rewrite_rules') );
40 add_action( 'admin_init', array($this, 'bulk_actions') );
41 add_action( 'admin_menu', array($this, 'add_menu_page') );
42 add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugins_page_links') );
43
44 add_filter( 'rewrite_rules_array', array($this, 'custom_permalinks_rewrite_rules'), 999, 1);
45 add_filter( '_get_page_link', array($this, 'custom_permalinks'), 999, 2);
46 add_filter( 'post_link', array($this, 'custom_permalinks'), 999, 2);
47 add_filter( 'post_type_link', array($this, 'custom_permalinks'), 999, 2);
48
49 }
50
51 /**
52 * Localize this plugin
53 */
54 function localize_me() {
55 load_plugin_textdomain( 'permalink-manager', false, PERMALINK_MANAGER_DIR );
56 }
57
58 /**
59 * Add menu page and load CSS & JS.
60 */
61 function add_menu_page() {
62 add_management_page( __('Permalink Manager', 'permalink-manager'), __('Permalink Manager', 'permalink-manager'), 'manage_options', PERMALINK_MANAGER_OPTIONS_PAGE, array($this, 'list_slugs_admin_page') );
63
64 // Make sure thata the CSS and JS files are loaded only on plugin admin page.
65 add_action( 'admin_print_scripts-' . PERMALINK_MANAGER_MENU_PAGE, array($this, 'enqueue_styles' ) );
66 add_action( 'admin_print_scripts-' . PERMALINK_MANAGER_MENU_PAGE, array($this, 'enqueue_scripts' ) );
67 }
68
69 /**
70 * Display the table with slugs.
71 */
72 function slug_editor_html() {
73 global $wpdb;
74
75 $Permalink_Manager_Slug_Editor = new Permalink_Manager_Slug_Editor();
76 $Permalink_Manager_Slug_Editor->set_screen_option_fields($this->fields_arrays('screen_options'));
77 $Permalink_Manager_Slug_Editor->prepare_items($wpdb->posts);
78
79 ?>
80
81 <form id="permalinks-table" method="post">
82 <input type="hidden" name="tab" value="slug_editor" />
83 <?php echo $Permalink_Manager_Slug_Editor->display(); ?>
84 </form>
85 <?php
86 }
87
88 /**
89 * Mass replace options page.
90 */
91 function find_and_replace_html() {
92 $button = get_submit_button( __( 'Find & Replace', 'permalink-manager' ), 'primary', 'find-replace-button', false );
93
94 $return = "<form id=\"permalinks-table-find-replace\" method=\"post\">";
95 $return .= "<input type=\"hidden\" name=\"tab\" value=\"find_and_replace\" />";
96 $return .= "<table class=\"form-table\">";
97
98 foreach($this->fields_arrays('find_and_replace') as $field_name => $field_args) {
99 $return .= Permalink_Manager_Helper_Functions::generate_option_field($field_name, $field_args, 'find-replace');
100 }
101
102 $return .= "</table>{$button}";
103 $return .= "</form>";
104
105 echo $return;
106 }
107
108 /**
109 * Reset slugs page.
110 */
111 function regenerate_slugs_html() {
112 $button = get_submit_button( __( 'Regenerate', 'permalink-manager' ), 'primary', 'regenerate-button', false );
113
114 $return = "<form id=\"permalinks-table-regenerate\" method=\"post\">";
115 $return .= "<input type=\"hidden\" name=\"tab\" value=\"regenerate_slugs\" />";
116 $return .= "<table class=\"form-table\">";
117
118 foreach($this->fields_arrays('regenerate_slugs') as $field_name => $field_args) {
119 $return .= Permalink_Manager_Helper_Functions::generate_option_field($field_name, $field_args, 'regenerate_slugs');
120 }
121
122 $return .= "</table>{$button}";
123 $return .= "</form>";
124
125 echo $return;
126 }
127
128 /**
129 * Permalink Base Editor
130 */
131 function base_editor_html() {
132 global $wpdb, $wp_rewrite;
133
134 $Permalink_Manager_Base_Editor = new Permalink_Manager_Base_Editor();
135 $Permalink_Manager_Base_Editor->set_screen_option_fields($this->fields_arrays('screen_options'));
136 $Permalink_Manager_Base_Editor->prepare_items($wpdb->posts);
137
138 //echo '<pre>';
139 //print_r($wp_rewrite);
140 //echo '</pre>';
141
142 ?>
143
144 <form id="permalinks-base-table" method="post">
145 <input type="hidden" name="tab" value="base_editor" />
146 <?php echo $Permalink_Manager_Base_Editor->display(); ?>
147 </form>
148 <?php
149 }
150
151 /**
152 * Display the plugin dashboard.
153 */
154 function list_slugs_admin_page() {
155 global $wpdb;
156
157 // Check which tab is active now.
158 if(isset($_POST['tab'])) {
159 $active_tab = $_POST['tab'];
160 } else if(isset($_GET['tab'])) {
161 $active_tab = $_GET['tab'];
162 } else {
163 $active_tab = 'slug_editor';
164 }
165
166 // Tabs array with assigned functions used to display HTML content.
167 $tabs = array(
168 'slug_editor' => array(
169 'name' => __('Slug Editor', 'permalink-manager'),
170 'function' => 'slug_editor_html',
171 'description' => __('You can disable/enable selected post types from the table below using <strong>"Screen Options"</strong> (click on the upper-right button to show it) section above.', 'permalink-manager')
172 ),
173 'find_and_replace' => array(
174 'name' => __('Find and replace', 'permalink-manager'),
175 'function' => 'find_and_replace_html',
176 'warning' => (__('<strong>You are doing it at your own risk!</strong>', 'permalink-manager') . '<br />' . __('A backup of MySQL database before using this tool is highly recommended. The search & replace operation might be not revertible!', 'permalink-manager'))
177 ),
178 'regenerate_slugs' => array(
179 'name' => __('Regenerate slugs', 'permalink-manager'),
180 'function' => 'regenerate_slugs_html',
181 'warning' => (__('<strong>You are doing it at your own risk!</strong>', 'permalink-manager') . '<br />' . __('A backup of MySQL database before using this tool is highly recommended. The regenerate process of slugs might be not revertible!', 'permalink-manager'))
182 ),
183 'base_editor' => array(
184 'name' => __('Permalinks Base Editor', 'permalink-manager'),
185 'function' => 'base_editor_html',
186 'warning' => array(
187 sprintf(__('<strong>This is an experimental feature!</strong> Please report all the bugs & issues <a href="%s">here</a>.', 'permalink-manager'), 'https://wordpress.org/support/plugin/permalink-manager'),
188 __('Custom Post Types should have their own, unique front, eg. <em>products/%product%!</em>', 'permalink-manager'),
189 __('After you update & save the settings below, you need to flush the rewrite rules!', 'permalink-manager'),
190 ),
191 'description' => (sprintf( __('All the <a href="%s" target="_blank">Structure Tags</a> allowed are listed below. Please note that some of them can be used only for particular Post Types.', 'permalink-manager'), "https://codex.wordpress.org/Using_Permalinks#Structure_Tags") . "<br />" . Permalink_Manager_Helper_Functions::get_all_structure_tags())
192 ),
193 );
194
195 ?>
196 <div id="permalinks-table-wrap" class="wrap">
197
198 <?php
199 // Display alerts and another content if needed
200 echo apply_filters('permalink-manager-before-tabs','');
201 ?>
202
203 <div id="icon-themes" class="icon32"></div>
204 <h2 id="plugin-name-heading"><?php _e('Permalink Manager', 'permalink-manager'); ?> <a href="<?php echo PERMALINK_MANAGER_WEBSITE; ?>" target="_blank"><?php _e('by Maciej Bis', 'permalink-manager'); ?></a></h2>
205
206 <h2 id="permalink-manager-tabs-nav" class="nav-tab-wrapper">
207 <?php
208 foreach($tabs as $tab_id => $tab_properties) {
209 $active_class = ($active_tab === $tab_id) ? 'nav-tab-active nav-tab' : 'nav-tab';
210 echo '<a data-tab="' . $tab_id . '" href="' . admin_url('admin.php?page=' . PERMALINK_MANAGER_PLUGIN_NAME . '.php&tab=' . $tab_id) . '" class="' . $active_class . '">' . $tab_properties['name'] . '</a>';
211 } ?>
212 </h2>
213
214 <div id="permalink-manager-tabs">
215 <?php
216 foreach($tabs as $tab_id => $tab_properties) {
217 $active_show = ($active_tab === $tab_id) ? 'show' : '';
218
219 // Prepare warning & description texts
220 $warning = (isset($tab_properties['warning'])) ? $tab_properties['warning'] : '';
221 $description = (isset($tab_properties['description'])) ? $tab_properties['description'] : '';
222
223 if(is_array($warning)) {
224 $warning = "<ol>"; // Overwrite the variable
225 foreach($tab_properties['warning'] as $point) { $warning .= "<li>{$point}</li>"; }
226 $warning .= "</ol>";
227 }
228
229 if(is_array($description)) {
230 $description = "<ol>"; // Overwrite the variable
231 foreach($tab_properties['description'] as $point) { $description .= "<li>{$point}</li>"; }
232 $description .= "</ol>";
233 }
234
235 echo '<div data-tab="' . $tab_id . '" id="' . $tab_id . '" class="' . $active_show . '">';
236 echo ($warning) ? "<div class=\"warning alert\">" . wpautop($warning) . "</div>" : "";
237 echo (isset($tab_properties['description'])) ? "<div class=\"info alert\">" . wpautop($description) . "</div>" : "";
238 $this->$tab_properties['function']();
239 echo '</div>';
240 } ?>
241 </div>
242
243 <?php
244 // Display alerts and another content if needed
245 echo apply_filters('permalink-manager-after-tabs','');
246 ?>
247
248 </div>
249 <?php
250 }
251
252 /**
253 * Register the stylesheets for the Dashboard.
254 */
255 function enqueue_styles() {
256 wp_enqueue_style( PERMALINK_MANAGER_PLUGIN_NAME, PERMALINK_MANAGER_URL . '/css/permalink-manager-admin.css', array(), PERMALINK_MANAGER_VERSION, 'all' );
257 }
258
259 /**
260 * Register the JavaScript for the dashboard.
261 */
262 function enqueue_scripts() {
263 wp_enqueue_script( PERMALINK_MANAGER_PLUGIN_NAME, PERMALINK_MANAGER_URL . '/js/permalink-manager-admin.js', array( 'jquery' ), PERMALINK_MANAGER_VERSION, false );
264 }
265
266 /**
267 * Additional links on "Plugins" page
268 */
269 function plugins_page_links( $links ) {
270 $links[] = '<a href="' . esc_url( get_admin_url(null, "tools.php?page=" . PERMALINK_MANAGER_OPTIONS_PAGE) ) .'">' . __( 'Go To Permalink Manager', 'permalink-manager' ) . '</a>';
271 return $links;
272 }
273
274 /**
275 * Fields for "Screen Options"
276 */
277 function fields_arrays($array) {
278
279 // All registered post types array
280 $all_post_statuses_array = get_post_statuses();
281 $all_post_types = Permalink_Manager_Helper_Functions::get_post_types_array();
282
283 // Fields for "Screen Options"
284 $screen_options = array(
285 'post_types' => array(
286 'label' => __( 'Post Types', 'permalink-manager' ),
287 'type' => 'checkbox',
288 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
289 'default' => array('post', 'page')
290 ),
291 'post_statuses' => array(
292 'label' => __( 'Post Statuses', 'permalink-manager' ),
293 'type' => 'checkbox',
294 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
295 'default' => array('publish')
296 ),
297 'per_page' => array(
298 'label' => __( 'Per page', 'permalink-manager' ),
299 'type' => 'number',
300 'default' => 10
301 )
302 );
303
304 // Fields for "Find and replace"
305 $find_and_replace = array(
306 'old_string' => array(
307 'label' => __( 'Find ...', 'permalink-manager' ),
308 'type' => 'text',
309 ),
310 'new_string' => array(
311 'label' => __( 'Replace with ...', 'permalink-manager' ),
312 'type' => 'text',
313 ),
314 'post_types' => array(
315 'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
316 'type' => 'checkbox',
317 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
318 'default' => array('post', 'page')
319 ),
320 'post_statuses' => array(
321 'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
322 'type' => 'checkbox',
323 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
324 'default' => array('publish')
325 )
326 );
327
328 // Fields for "Regenerate slugs"
329 $regenerate_slugs = array(
330 'post_types' => array(
331 'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
332 'type' => 'checkbox',
333 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
334 'default' => array('post', 'page')
335 ),
336 'post_statuses' => array(
337 'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
338 'type' => 'checkbox',
339 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
340 'default' => array('publish')
341 )
342 );
343
344 return isset($array) ? ${$array} : array();
345
346 }
347
348 /**
349 * Bulk actions functions
350 */
351 function bulk_actions() {
352 global $wpdb;
353
354 $updated_slugs_count = 0;
355 $updated_array = array();
356 $alert_type = $alert_content = $errors = $main_content = '';
357
358 if (isset($_POST['update_all_slugs'])) {
359
360 $slugs = isset($_POST['slug']) ? $_POST['slug'] : array();
361
362 // Double check if the slugs and ids are stored in arrays
363 if (!is_array($slugs)) $slugs = explode(',', $slugs);
364
365 if (!empty($slugs)) {
366 foreach($slugs as $id => $new_slug) {
367 $this_post = get_post($id);
368
369 // Check if slug was changed
370 if($this_post->post_name != $new_slug) {
371 // Update slugs
372 Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, $id);
373
374 $updated_array[] = array('post_title' => get_the_title($id), 'old_slug' => $this_post->post_name, 'new_slug' => $new_slug);
375 $updated_slugs_count++;
376 }
377
378 // Reset slug
379 $slug = '';
380 }
381 }
382
383 } else if (isset($_POST['find-replace-button'])) {
384
385 $var['old_string'] = esc_sql($_POST['permalink-manager']['find-replace']['old_string']);
386 $var['new_string'] = esc_sql($_POST['permalink-manager']['find-replace']['new_string']);
387 $post_types_array = ($_POST['permalink-manager']['find-replace']['post_types']);
388 $post_statuses_array = ($_POST['permalink-manager']['find-replace']['post_statuses']);
389 $var['post_types'] = implode("', '", $post_types_array);
390 $var['post_statuses'] = implode("', '", $post_statuses_array);
391
392 // Check if any of variables is not empty
393 $find_and_replace_fields = $this->fields_arrays('find_and_replace');
394 foreach($var as $key => $val) {
395 if(empty($val)) $errors .= '<p>' . sprintf( __( '<strong>"%1s"</strong> field is empty!', 'permalink-manager' ), $find_and_replace_fields[$key]['label'] ) . '</p>';
396 }
397
398 // Save the rows before they are updated to an array
399 $posts_to_update = $wpdb->get_results("SELECT post_title, post_name, ID FROM {$wpdb->posts} WHERE post_status IN ('{$var['post_statuses']}') AND post_name LIKE '%{$var['old_string']}%' AND post_type IN ('{$var['post_types']}')", ARRAY_A);
400
401 // Now if the array is not empty use IDs from each subarray as a key
402 if($posts_to_update && empty($errors)) {
403 foreach ($posts_to_update as $row) {
404 // Get new slug
405 $old_slug = $row['post_name'];
406 $new_slug = str_replace($var['old_string'], $var['new_string'], $old_slug);
407
408 // Update slugs
409 Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, $row['ID']);
410
411 $updated_array[] = array('post_title' => $row['post_title'], 'old_slug' => $old_slug, 'new_slug' => $new_slug);
412 $updated_slugs_count++;
413
414 // Reset slug
415 $slug = '';
416 }
417 } else {
418 $alert_type = 'error';
419 $alert_content = $errors;
420 }
421
422 } else if (isset($_POST['regenerate-button'])) {
423
424 // Setup needed variables
425 $post_types_array = ($_POST['permalink-manager']['regenerate_slugs']['post_types']);
426 $post_statuses_array = ($_POST['permalink-manager']['regenerate_slugs']['post_statuses']);
427
428 // Reset query
429 $reset_query = new WP_Query( array( 'post_type' => $post_types_array, 'post_status' => $post_statuses_array, 'posts_per_page' => -1 ) );
430
431 // The Loop
432 if ( $reset_query->have_posts() ) {
433 while ( $reset_query->have_posts() ) {
434 $reset_query->the_post();
435 $this_post = get_post(get_the_ID());
436
437 $correct_slug = sanitize_title(get_the_title());
438 $old_slug = $this_post->post_name;
439 $new_slug = wp_unique_post_slug($correct_slug, get_the_ID(), get_post_status(get_the_ID()), get_post_type(get_the_ID()), null);
440
441 if($old_slug != $new_slug) {
442 $updated_slugs_count++;
443
444 Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, get_the_ID());
445 $updated_array[] = array('post_title' => get_the_title(), 'old_slug' => $old_slug, 'new_slug' => $new_slug);
446 }
447 }
448 }
449
450 // Restore original Post Data
451 wp_reset_postdata();
452
453 // Save Permalink Structures/Permalinks Bases
454 } else if (isset($_POST['save_permalink_structures'])) {
455 Permalink_Manager_Helper_Functions::save_option('base-editor', $_POST['permalink-manager']['base-editor']);
456
457 $alert_type = 'updated';
458 $alert_content = sprintf( __( '<a href="%s">Click here</a> to flush the rewrite rules (it is required to make the new permalinks working).', 'permalink-manager' ), admin_url('admin.php?page=' . PERMALINK_MANAGER_PLUGIN_NAME . '.php&flush_rewrite_rules=true&tab=base_editor'));
459 Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true);
460 return;
461 // Flush rewrite rules
462 } else if (isset($_POST['flush_rewrite_rules'])) {
463 $this->flush_rewrite_rules();
464 return;
465 }
466
467 /**
468 * Display results
469 */
470 if((isset($_POST['permalink-manager']) || isset($_POST['update_all_slugs'])) && !(isset($_POST['screen-options-apply']))) {
471 // Display errors or success message
472
473 // Check how many rows/slugs were affected
474 if($updated_slugs_count > 0) {
475 $alert_type = 'updated';
476 $alert_content = sprintf( _n( '<strong>%d</strong> slug were updated!', '<strong>%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' ';
477 $alert_content .= sprintf( __( '<a href="%s">Click here</a> to go to the list of updated slugs', 'permalink-manager' ), '#updated-list');
478 } else {
479 $alert_type = 'error';
480 $alert_content = ($alert_content) ? $alert_content : __( '<strong>No slugs</strong> were updated!', 'permalink-manager' );
481 }
482
483 Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true);
484
485 // Display summary after update
486 // Display only if there are any slugs updated
487 if ( $updated_slugs_count > 0 && $updated_array ) {
488 add_filter('permalink-manager-after-tabs', function( $arg ) use ( $alert_content, $alert_type, $errors, $updated_array, $main_content ) {
489
490 $header_footer = '<tr>';
491 $header_footer .= '<th class="column-primary">' . __('Title', 'permalink-manager') . '</th>';
492 $header_footer .= '<th>' . __('Old slug', 'permalink-manager') . '</th>';
493 $header_footer .= '<th>' . __('New slug', 'permalink-manager') . '</th>';
494 $header_footer .= '</tr>';
495
496 $updated_slugs_count = 0;
497 foreach($updated_array as $row) {
498 // Odd/even class
499 $updated_slugs_count++;
500 $alternate_class = ($updated_slugs_count % 2 == 1) ? ' class="alternate"' : '';
501
502 $main_content .= "<tr{$alternate_class}>";
503 $main_content .= '<td class="row-title column-primary" data-colname="' . __('Title', 'permalink-manager') . '">' . $row['post_title'] . '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __('Show more details', 'permalink-manager') . '</span></button></td>';
504 $main_content .= '<td data-colname="' . __('Old slug', 'permalink-manager') . '">' . $row['old_slug'] . '</td>';
505 $main_content .= '<td data-colname="' . __('New slug', 'permalink-manager') . '">' . $row['new_slug'] . '</td>';
506 $main_content .= '</tr>';
507 }
508
509 // Merge header, footer and content
510 $output = '<h3 id="updated-list">' . __('List of updated posts', 'permalink-manager') . '</h3>';
511 $output .= '<table class="widefat wp-list-table">';
512 $output .= "<thead>{$header_footer}</thead><tbody>{$main_content}</tbody><tfoot>{$header_footer}</tfoot>";
513 $output .= '</table>';
514
515 return $output ;
516
517 });
518 }
519 }
520 }
521
522 /**
523 * Change permalinks for posts, pages & custom post types
524 */
525 function custom_permalinks($permalink, $post) {
526 $post = (is_integer($post)) ? get_post($post) : $post;
527 $post_type = $post->post_type;
528 $permastruct = isset($this->permalink_manager_options['base-editor'][$post_type]) ? $this->permalink_manager_options['base-editor'][$post_type] : '';
529
530 // Ignore empty permastructures (do not change them)
531 if(empty($permastruct) || $post->post_status != 'publish') return $permalink;
532
533 // Get options
534 if($permastruct) {
535 $permalink = home_url() . "/" . trim($permastruct, '/');
536 }
537
538 /**
539 * Replace Structure Tags
540 */
541
542 // Get the date
543 $date = explode(" ",date('Y m d H i s', strtotime($post->post_date)));
544
545 // Get the category (if needed)
546 $category = '';
547 if ( strpos($permalink, '%category%') !== false ) {
548 $cats = get_the_category($post->ID);
549 if ( $cats ) {
550 usort($cats, '_usort_terms_by_ID'); // order by ID
551 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
552 $category_object = get_term( $category_object, 'category' );
553 $category = $category_object->slug;
554 if ( $parent = $category_object->parent )
555 $category = get_category_parents($parent, false, '/', true) . $category;
556 }
557 // show default category in permalinks, without having to assign it explicitly
558 if ( empty($category) ) {
559 $default_category = get_term( get_option( 'default_category' ), 'category' );
560 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
561 }
562 }
563
564 // Get the author (if needed)
565 $author = '';
566 if ( strpos($permalink, '%author%') !== false ) {
567 $authordata = get_userdata($post->post_author);
568 $author = $authordata->user_nicename;
569 }
570
571 // Fix for hierarchical CPT (start)
572 $full_slug = get_page_uri($post);
573 $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type);
574
575 // Do the replacement (post tag is removed now to enable support for hierarchical CPT)
576 $tags = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%category%', '%author%', $post_type_tag);
577 $replacements = array($date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->ID, $category, $author, '');
578
579 return str_replace($tags, $replacements, "{$permalink}{$full_slug}");
580 }
581
582 /**
583 * Add rewrite rules
584 */
585 function custom_permalinks_rewrite_rules($rules) {
586
587 global $wp_rewrite;
588
589 $new_rules = array();
590 $permastructures = $this->permalink_manager_options['base-editor'];
591
592 // Move post & page permastructures to the end of array
593 $page_p = $permastructures['page'];
594 $post_p = $permastructures['post'];
595 unset($permastructures['post'], $permastructures['page']);
596
597 $permastructures['page'] = $page_p;
598 $permastructures['post'] = $post_p;
599 $permastructures = array_reverse($permastructures);
600
601 foreach($permastructures as $post_type => $permastruct) {
602 // Ignore empty permastructures (do not add them)
603 if(empty($permastruct)) continue;
604
605 $new_rule = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $permastruct, EP_PERMALINK);
606 $rules = array_merge($new_rule, $rules);
607 }
608 return $rules;
609 }
610
611 /**
612 * Flush rewrite rules
613 */
614 function flush_rewrite_rules() {
615 if(isset($_REQUEST['flush_rewrite_rules'])) {
616 flush_rewrite_rules();
617
618 $alert_type = 'updated';
619 $alert_content = __( 'The rewrite rules are flushed!', 'permalink-manager' );
620 return Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true);
621 }
622 }
623
624 }
625
626 /**
627 * Begins execution of the plugin.
628 */
629 function run_permalink_manager() {
630
631 // Load plugin files.
632 if( is_admin() ) {
633 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-slug-editor.php';
634 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-base-editor.php';
635 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-screen-options.php';
636 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-helper-functions.php';
637
638 $Permalink_Manager_Class = new Permalink_Manager_Class();
639 $Permalink_Manager_Screen_Options = new Permalink_Manager_Screen_Options();
640 }
641
642 }
643
644 run_permalink_manager();
645