PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 0.3.1
Permalink Manager Lite v0.3.1
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
663 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.1
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.1' );
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( 'page_rewrite_rules', array($this, 'custom_page_rewrite_rules'), 999, 1);
45 add_filter( 'post_rewrite_rules', array($this, 'custom_post_rewrite_rules'), 999, 1);
46 add_filter( 'rewrite_rules_array', array($this, 'custom_cpt_rewrite_rules'), 999, 1);
47 add_filter( '_get_page_link', array($this, 'custom_permalinks'), 999, 2);
48 add_filter( 'post_link', array($this, 'custom_permalinks'), 999, 2);
49 add_filter( 'post_type_link', array($this, 'custom_permalinks'), 999, 2);
50
51 }
52
53 /**
54 * Localize this plugin
55 */
56 function localize_me() {
57 load_plugin_textdomain( 'permalink-manager', false, PERMALINK_MANAGER_DIR );
58 }
59
60 /**
61 * Add menu page and load CSS & JS.
62 */
63 function add_menu_page() {
64 add_management_page( __('Permalink Manager', 'permalink-manager'), __('Permalink Manager', 'permalink-manager'), 'manage_options', PERMALINK_MANAGER_OPTIONS_PAGE, array($this, 'list_slugs_admin_page') );
65
66 // Make sure thata the CSS and JS files are loaded only on plugin admin page.
67 add_action( 'admin_print_scripts-' . PERMALINK_MANAGER_MENU_PAGE, array($this, 'enqueue_styles' ) );
68 add_action( 'admin_print_scripts-' . PERMALINK_MANAGER_MENU_PAGE, array($this, 'enqueue_scripts' ) );
69 }
70
71 /**
72 * Display the table with slugs.
73 */
74 function slug_editor_html() {
75 global $wpdb;
76
77 $Permalink_Manager_Slug_Editor = new Permalink_Manager_Slug_Editor();
78 $Permalink_Manager_Slug_Editor->set_screen_option_fields($this->fields_arrays('screen_options'));
79 $Permalink_Manager_Slug_Editor->prepare_items($wpdb->posts);
80
81 ?>
82
83 <form id="permalinks-table" method="post">
84 <input type="hidden" name="tab" value="slug_editor" />
85 <?php echo $Permalink_Manager_Slug_Editor->display(); ?>
86 </form>
87 <?php
88 }
89
90 /**
91 * Mass replace options page.
92 */
93 function find_and_replace_html() {
94 $button = get_submit_button( __( 'Find & Replace', 'permalink-manager' ), 'primary', 'find-replace-button', false );
95
96 $return = "<form id=\"permalinks-table-find-replace\" method=\"post\">";
97 $return .= "<input type=\"hidden\" name=\"tab\" value=\"find_and_replace\" />";
98 $return .= "<table class=\"form-table\">";
99
100 foreach($this->fields_arrays('find_and_replace') as $field_name => $field_args) {
101 $return .= Permalink_Manager_Helper_Functions::generate_option_field($field_name, $field_args, 'find-replace');
102 }
103
104 $return .= "</table>{$button}";
105 $return .= "</form>";
106
107 echo $return;
108 }
109
110 /**
111 * Reset slugs page.
112 */
113 function regenerate_slugs_html() {
114 $button = get_submit_button( __( 'Regenerate', 'permalink-manager' ), 'primary', 'regenerate-button', false );
115
116 $return = "<form id=\"permalinks-table-regenerate\" method=\"post\">";
117 $return .= "<input type=\"hidden\" name=\"tab\" value=\"regenerate_slugs\" />";
118 $return .= "<table class=\"form-table\">";
119
120 foreach($this->fields_arrays('regenerate_slugs') as $field_name => $field_args) {
121 $return .= Permalink_Manager_Helper_Functions::generate_option_field($field_name, $field_args, 'regenerate_slugs');
122 }
123
124 $return .= "</table>{$button}";
125 $return .= "</form>";
126
127 echo $return;
128 }
129
130 /**
131 * Permalink Base Editor
132 */
133 function base_editor_html() {
134 global $wpdb, $wp_rewrite;
135
136 $Permalink_Manager_Base_Editor = new Permalink_Manager_Base_Editor();
137 $Permalink_Manager_Base_Editor->set_screen_option_fields($this->fields_arrays('screen_options'));
138 $Permalink_Manager_Base_Editor->prepare_items($wpdb->posts);
139
140 //echo '<pre>';
141 //print_r($wp_rewrite);
142 //echo '</pre>';
143
144 ?>
145
146 <form id="permalinks-base-table" method="post">
147 <input type="hidden" name="tab" value="base_editor" />
148 <?php echo $Permalink_Manager_Base_Editor->display(); ?>
149 </form>
150 <?php
151 }
152
153 /**
154 * Display the plugin dashboard.
155 */
156 function list_slugs_admin_page() {
157 global $wpdb;
158
159 // Check which tab is active now.
160 if(isset($_POST['tab'])) {
161 $active_tab = $_POST['tab'];
162 } else if(isset($_GET['tab'])) {
163 $active_tab = $_GET['tab'];
164 } else {
165 $active_tab = 'slug_editor';
166 }
167
168 // Tabs array with assigned functions used to display HTML content.
169 $tabs = array(
170 'slug_editor' => array(
171 'name' => __('Slug Editor', 'permalink-manager'),
172 'function' => 'slug_editor_html',
173 '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')
174 ),
175 'find_and_replace' => array(
176 'name' => __('Find and replace', 'permalink-manager'),
177 'function' => 'find_and_replace_html',
178 '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'))
179 ),
180 'regenerate_slugs' => array(
181 'name' => __('Regenerate slugs', 'permalink-manager'),
182 'function' => 'regenerate_slugs_html',
183 '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'))
184 ),
185 'base_editor' => array(
186 'name' => __('Permalinks Base Editor', 'permalink-manager'),
187 'function' => 'base_editor_html',
188 'warning' => array(
189 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'),
190 __('Custom Post Types should have their own, unique front, eg. <em>products/%product%!</em>', 'permalink-manager'),
191 __('After you update & save the settings below, you need to flush the rewrite rules!', 'permalink-manager'),
192 ),
193 '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())
194 ),
195 );
196
197 ?>
198 <div id="permalinks-table-wrap" class="wrap">
199
200 <?php
201 // Display alerts and another content if needed
202 echo apply_filters('permalink-manager-before-tabs','');
203 ?>
204
205 <div id="icon-themes" class="icon32"></div>
206 <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>
207
208 <h2 id="permalink-manager-tabs-nav" class="nav-tab-wrapper">
209 <?php
210 foreach($tabs as $tab_id => $tab_properties) {
211 $active_class = ($active_tab === $tab_id) ? 'nav-tab-active nav-tab' : 'nav-tab';
212 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>';
213 } ?>
214 </h2>
215
216 <div id="permalink-manager-tabs">
217 <?php
218 foreach($tabs as $tab_id => $tab_properties) {
219 $active_show = ($active_tab === $tab_id) ? 'show' : '';
220
221 // Prepare warning & description texts
222 $warning = (isset($tab_properties['warning'])) ? $tab_properties['warning'] : '';
223 $description = (isset($tab_properties['description'])) ? $tab_properties['description'] : '';
224
225 if(is_array($warning)) {
226 $warning = "<ol>"; // Overwrite the variable
227 foreach($tab_properties['warning'] as $point) { $warning .= "<li>{$point}</li>"; }
228 $warning .= "</ol>";
229 }
230
231 if(is_array($description)) {
232 $description = "<ol>"; // Overwrite the variable
233 foreach($tab_properties['description'] as $point) { $description .= "<li>{$point}</li>"; }
234 $description .= "</ol>";
235 }
236
237 echo '<div data-tab="' . $tab_id . '" id="' . $tab_id . '" class="' . $active_show . '">';
238 echo ($warning) ? "<div class=\"warning alert\">" . wpautop($warning) . "</div>" : "";
239 echo (isset($tab_properties['description'])) ? "<div class=\"info alert\">" . wpautop($description) . "</div>" : "";
240 $this->$tab_properties['function']();
241 echo '</div>';
242 } ?>
243 </div>
244
245 <?php
246 // Display alerts and another content if needed
247 echo apply_filters('permalink-manager-after-tabs','');
248 ?>
249
250 </div>
251 <?php
252 }
253
254 /**
255 * Register the stylesheets for the Dashboard.
256 */
257 function enqueue_styles() {
258 wp_enqueue_style( PERMALINK_MANAGER_PLUGIN_NAME, PERMALINK_MANAGER_URL . '/css/permalink-manager-admin.css', array(), PERMALINK_MANAGER_VERSION, 'all' );
259 }
260
261 /**
262 * Register the JavaScript for the dashboard.
263 */
264 function enqueue_scripts() {
265 wp_enqueue_script( PERMALINK_MANAGER_PLUGIN_NAME, PERMALINK_MANAGER_URL . '/js/permalink-manager-admin.js', array( 'jquery' ), PERMALINK_MANAGER_VERSION, false );
266 }
267
268 /**
269 * Additional links on "Plugins" page
270 */
271 function plugins_page_links( $links ) {
272 $links[] = '<a href="' . esc_url( get_admin_url(null, "tools.php?page=" . PERMALINK_MANAGER_OPTIONS_PAGE) ) .'">' . __( 'Go To Permalink Manager', 'permalink-manager' ) . '</a>';
273 return $links;
274 }
275
276 /**
277 * Fields for "Screen Options"
278 */
279 function fields_arrays($array) {
280
281 // All registered post types array
282 $all_post_statuses_array = get_post_statuses();
283 $all_post_types = Permalink_Manager_Helper_Functions::get_post_types_array();
284
285 // Fields for "Screen Options"
286 $screen_options = array(
287 'post_types' => array(
288 'label' => __( 'Post Types', 'permalink-manager' ),
289 'type' => 'checkbox',
290 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
291 'default' => array('post', 'page')
292 ),
293 'post_statuses' => array(
294 'label' => __( 'Post Statuses', 'permalink-manager' ),
295 'type' => 'checkbox',
296 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
297 'default' => array('publish')
298 ),
299 'per_page' => array(
300 'label' => __( 'Per page', 'permalink-manager' ),
301 'type' => 'number',
302 'default' => 10
303 )
304 );
305
306 // Fields for "Find and replace"
307 $find_and_replace = array(
308 'old_string' => array(
309 'label' => __( 'Find ...', 'permalink-manager' ),
310 'type' => 'text',
311 ),
312 'new_string' => array(
313 'label' => __( 'Replace with ...', 'permalink-manager' ),
314 'type' => 'text',
315 ),
316 'post_types' => array(
317 'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
318 'type' => 'checkbox',
319 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
320 'default' => array('post', 'page')
321 ),
322 'post_statuses' => array(
323 'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
324 'type' => 'checkbox',
325 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
326 'default' => array('publish')
327 )
328 );
329
330 // Fields for "Regenerate slugs"
331 $regenerate_slugs = array(
332 'post_types' => array(
333 'label' => __( 'Post Types that should be affected', 'permalink-manager' ),
334 'type' => 'checkbox',
335 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types),
336 'default' => array('post', 'page')
337 ),
338 'post_statuses' => array(
339 'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ),
340 'type' => 'checkbox',
341 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array),
342 'default' => array('publish')
343 )
344 );
345
346 return isset($array) ? ${$array} : array();
347
348 }
349
350 /**
351 * Bulk actions functions
352 */
353 function bulk_actions() {
354 global $wpdb;
355
356 $updated_slugs_count = 0;
357 $updated_array = array();
358 $alert_type = $alert_content = $errors = $main_content = '';
359
360 if (isset($_POST['update_all_slugs'])) {
361
362 $slugs = isset($_POST['slug']) ? $_POST['slug'] : array();
363
364 // Double check if the slugs and ids are stored in arrays
365 if (!is_array($slugs)) $slugs = explode(',', $slugs);
366
367 if (!empty($slugs)) {
368 foreach($slugs as $id => $new_slug) {
369 $this_post = get_post($id);
370
371 // Check if slug was changed
372 if($this_post->post_name != $new_slug) {
373 // Update slugs
374 Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, $id);
375
376 $updated_array[] = array('post_title' => get_the_title($id), 'old_slug' => $this_post->post_name, 'new_slug' => $new_slug);
377 $updated_slugs_count++;
378 }
379
380 // Reset slug
381 $slug = '';
382 }
383 }
384
385 } else if (isset($_POST['find-replace-button'])) {
386
387 $var['old_string'] = esc_sql($_POST['permalink-manager']['find-replace']['old_string']);
388 $var['new_string'] = esc_sql($_POST['permalink-manager']['find-replace']['new_string']);
389 $post_types_array = ($_POST['permalink-manager']['find-replace']['post_types']);
390 $post_statuses_array = ($_POST['permalink-manager']['find-replace']['post_statuses']);
391 $var['post_types'] = implode("', '", $post_types_array);
392 $var['post_statuses'] = implode("', '", $post_statuses_array);
393
394 // Check if any of variables is not empty
395 $find_and_replace_fields = $this->fields_arrays('find_and_replace');
396 foreach($var as $key => $val) {
397 if(empty($val)) $errors .= '<p>' . sprintf( __( '<strong>"%1s"</strong> field is empty!', 'permalink-manager' ), $find_and_replace_fields[$key]['label'] ) . '</p>';
398 }
399
400 // Save the rows before they are updated to an array
401 $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);
402
403 // Now if the array is not empty use IDs from each subarray as a key
404 if($posts_to_update && empty($errors)) {
405 foreach ($posts_to_update as $row) {
406 // Get new slug
407 $old_slug = $row['post_name'];
408 $new_slug = str_replace($var['old_string'], $var['new_string'], $old_slug);
409
410 // Update slugs
411 Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, $row['ID']);
412
413 $updated_array[] = array('post_title' => $row['post_title'], 'old_slug' => $old_slug, 'new_slug' => $new_slug);
414 $updated_slugs_count++;
415
416 // Reset slug
417 $slug = '';
418 }
419 } else {
420 $alert_type = 'error';
421 $alert_content = $errors;
422 }
423
424 } else if (isset($_POST['regenerate-button'])) {
425
426 // Setup needed variables
427 $post_types_array = ($_POST['permalink-manager']['regenerate_slugs']['post_types']);
428 $post_statuses_array = ($_POST['permalink-manager']['regenerate_slugs']['post_statuses']);
429
430 // Reset query
431 $reset_query = new WP_Query( array( 'post_type' => $post_types_array, 'post_status' => $post_statuses_array, 'posts_per_page' => -1 ) );
432
433 // The Loop
434 if ( $reset_query->have_posts() ) {
435 while ( $reset_query->have_posts() ) {
436 $reset_query->the_post();
437 $this_post = get_post(get_the_ID());
438
439 $correct_slug = sanitize_title(get_the_title());
440 $old_slug = $this_post->post_name;
441 $new_slug = wp_unique_post_slug($correct_slug, get_the_ID(), get_post_status(get_the_ID()), get_post_type(get_the_ID()), null);
442
443 if($old_slug != $new_slug) {
444 $updated_slugs_count++;
445
446 Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, get_the_ID());
447 $updated_array[] = array('post_title' => get_the_title(), 'old_slug' => $old_slug, 'new_slug' => $new_slug);
448 }
449 }
450 }
451
452 // Restore original Post Data
453 wp_reset_postdata();
454
455 // Save Permalink Structures/Permalinks Bases
456 } else if (isset($_POST['save_permalink_structures'])) {
457 Permalink_Manager_Helper_Functions::save_option('base-editor', $_POST['permalink-manager']['base-editor']);
458
459 $alert_type = 'updated';
460 $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'));
461 Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true);
462 return;
463 // Flush rewrite rules
464 } else if (isset($_POST['flush_rewrite_rules'])) {
465 $this->flush_rewrite_rules();
466 return;
467 }
468
469 /**
470 * Display results
471 */
472 if((isset($_POST['permalink-manager']) || isset($_POST['update_all_slugs'])) && !(isset($_POST['screen-options-apply']))) {
473 // Display errors or success message
474
475 // Check how many rows/slugs were affected
476 if($updated_slugs_count > 0) {
477 $alert_type = 'updated';
478 $alert_content = sprintf( _n( '<strong>%d</strong> slug were updated!', '<strong>%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' ';
479 $alert_content .= sprintf( __( '<a href="%s">Click here</a> to go to the list of updated slugs', 'permalink-manager' ), '#updated-list');
480 } else {
481 $alert_type = 'error';
482 $alert_content = ($alert_content) ? $alert_content : __( '<strong>No slugs</strong> were updated!', 'permalink-manager' );
483 }
484
485 Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true);
486
487 // Display summary after update
488 // Display only if there are any slugs updated
489 if ( $updated_slugs_count > 0 && $updated_array ) {
490 add_filter('permalink-manager-after-tabs', function( $arg ) use ( $alert_content, $alert_type, $errors, $updated_array, $main_content ) {
491
492 $header_footer = '<tr>';
493 $header_footer .= '<th class="column-primary">' . __('Title', 'permalink-manager') . '</th>';
494 $header_footer .= '<th>' . __('Old slug', 'permalink-manager') . '</th>';
495 $header_footer .= '<th>' . __('New slug', 'permalink-manager') . '</th>';
496 $header_footer .= '</tr>';
497
498 $updated_slugs_count = 0;
499 foreach($updated_array as $row) {
500 // Odd/even class
501 $updated_slugs_count++;
502 $alternate_class = ($updated_slugs_count % 2 == 1) ? ' class="alternate"' : '';
503
504 $main_content .= "<tr{$alternate_class}>";
505 $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>';
506 $main_content .= '<td data-colname="' . __('Old slug', 'permalink-manager') . '">' . $row['old_slug'] . '</td>';
507 $main_content .= '<td data-colname="' . __('New slug', 'permalink-manager') . '">' . $row['new_slug'] . '</td>';
508 $main_content .= '</tr>';
509 }
510
511 // Merge header, footer and content
512 $output = '<h3 id="updated-list">' . __('List of updated posts', 'permalink-manager') . '</h3>';
513 $output .= '<table class="widefat wp-list-table">';
514 $output .= "<thead>{$header_footer}</thead><tbody>{$main_content}</tbody><tfoot>{$header_footer}</tfoot>";
515 $output .= '</table>';
516
517 return $output ;
518
519 });
520 }
521 }
522 }
523
524 /**
525 * Change permalinks for posts, pages & custom post types
526 */
527 function custom_permalinks($permalink, $post) {
528 $post = (is_integer($post)) ? get_post($post) : $post;
529 $post_type = $post->post_type;
530 $permastruct = isset($this->permalink_manager_options['base-editor'][$post_type]) ? $this->permalink_manager_options['base-editor'][$post_type] : '';
531
532 // Ignore empty permastructures (do not change them)
533 if(empty($permastruct) || $post->post_status != 'publish') return $permalink;
534
535 // Get options
536 if($permastruct) {
537 $permalink = home_url() . "/" . trim($permastruct, '/');
538 }
539
540 /**
541 * Replace Structure Tags
542 */
543
544 // Get the date
545 $date = explode(" ",date('Y m d H i s', strtotime($post->post_date)));
546
547 // Get the category (if needed)
548 $category = '';
549 if ( strpos($permalink, '%category%') !== false ) {
550 $cats = get_the_category($post->ID);
551 if ( $cats ) {
552 usort($cats, '_usort_terms_by_ID'); // order by ID
553 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
554 $category_object = get_term( $category_object, 'category' );
555 $category = $category_object->slug;
556 if ( $parent = $category_object->parent )
557 $category = get_category_parents($parent, false, '/', true) . $category;
558 }
559 // show default category in permalinks, without having to assign it explicitly
560 if ( empty($category) ) {
561 $default_category = get_term( get_option( 'default_category' ), 'category' );
562 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
563 }
564 }
565
566 // Get the author (if needed)
567 $author = '';
568 if ( strpos($permalink, '%author%') !== false ) {
569 $authordata = get_userdata($post->post_author);
570 $author = $authordata->user_nicename;
571 }
572
573 // Fix for hierarchical CPT (start)
574 $full_slug = get_page_uri($post);
575 $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type);
576
577 // Do the replacement (post tag is removed now to enable support for hierarchical CPT)
578 $tags = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%category%', '%author%', $post_type_tag);
579 $replacements = array($date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->ID, $category, $author, '');
580
581 return str_replace($tags, $replacements, "{$permalink}{$full_slug}");
582 }
583
584 /**
585 * Add rewrite rules
586 */
587 function custom_cpt_rewrite_rules($rules) {
588
589 global $wp_rewrite;
590
591 $new_rules = array();
592 $permastructures = $this->permalink_manager_options['base-editor'];
593
594 // Rewrite rules for Posts & Pages are defined in different filters
595 unset($permastructures['post'], $permastructures['page']);
596
597 foreach($permastructures as $post_type => $permastruct) {
598 // Ignore empty permastructures (do not add them)
599 if(empty($permastruct)) continue;
600
601 $new_rule = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $permastruct, EP_PERMALINK);
602 $rules = array_merge($new_rule, $rules);
603 }
604 return $rules;
605 }
606
607 /**
608 * Post Rewrite Rules
609 */
610 function custom_post_rewrite_rules($rules) {
611 global $wp_rewrite;
612 if(isset($this->permalink_manager_options['base-editor']['post'])) {
613 $rules = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $this->permalink_manager_options['base-editor']['post'], EP_PERMALINK);
614 }
615 return $rules;
616 }
617
618 /**
619 * Page Rewrite Rules
620 */
621 function custom_page_rewrite_rules($rules) {
622 global $wp_rewrite;
623 if(isset($this->permalink_manager_options['base-editor']['page'])) {
624 $rules = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $this->permalink_manager_options['base-editor']['page'], EP_PERMALINK);
625 }
626 return $rules;
627 }
628
629 /**
630 * Flush rewrite rules
631 */
632 function flush_rewrite_rules() {
633 if(isset($_REQUEST['flush_rewrite_rules'])) {
634 flush_rewrite_rules();
635
636 $alert_type = 'updated';
637 $alert_content = __( 'The rewrite rules are flushed!', 'permalink-manager' );
638 return Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true);
639 }
640 }
641
642 }
643
644 /**
645 * Begins execution of the plugin.
646 */
647 function run_permalink_manager() {
648
649 // Load plugin files.
650 if( is_admin() ) {
651 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-slug-editor.php';
652 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-base-editor.php';
653 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-screen-options.php';
654 require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-helper-functions.php';
655
656 $Permalink_Manager_Class = new Permalink_Manager_Class();
657 $Permalink_Manager_Screen_Options = new Permalink_Manager_Screen_Options();
658 }
659
660 }
661
662 run_permalink_manager();
663