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