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
667 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.3 |
| 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.3' ); |
| 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 | $function_name = $tab_properties['function']; |
| 246 | $this->$function_name(); |
| 247 | echo '</div>'; |
| 248 | } ?> |
| 249 | </div> |
| 250 | |
| 251 | <?php |
| 252 | // Display alerts and another content if needed |
| 253 | echo apply_filters('permalink-manager-after-tabs',''); |
| 254 | ?> |
| 255 | |
| 256 | </div> |
| 257 | <?php |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Register the stylesheets for the Dashboard. |
| 262 | */ |
| 263 | function enqueue_styles() { |
| 264 | wp_enqueue_style( PERMALINK_MANAGER_PLUGIN_NAME, PERMALINK_MANAGER_URL . '/css/permalink-manager-admin.css', array(), PERMALINK_MANAGER_VERSION, 'all' ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Register the JavaScript for the dashboard. |
| 269 | */ |
| 270 | function enqueue_scripts() { |
| 271 | wp_enqueue_script( PERMALINK_MANAGER_PLUGIN_NAME, PERMALINK_MANAGER_URL . '/js/permalink-manager-admin.js', array( 'jquery' ), PERMALINK_MANAGER_VERSION, false ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Additional links on "Plugins" page |
| 276 | */ |
| 277 | function plugins_page_links( $links ) { |
| 278 | $links[] = '<a href="' . esc_url( get_admin_url(null, "tools.php?page=" . PERMALINK_MANAGER_OPTIONS_PAGE) ) .'">' . __( 'Go To Permalink Manager', 'permalink-manager' ) . '</a>'; |
| 279 | return $links; |
| 280 | } |
| 281 | |
| 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( |
| 314 | 'old_string' => array( |
| 315 | 'label' => __( 'Find ...', 'permalink-manager' ), |
| 316 | 'type' => 'text', |
| 317 | ), |
| 318 | 'new_string' => array( |
| 319 | 'label' => __( 'Replace with ...', 'permalink-manager' ), |
| 320 | 'type' => 'text', |
| 321 | ), |
| 322 | 'post_types' => array( |
| 323 | 'label' => __( 'Post Types that should be affected', 'permalink-manager' ), |
| 324 | 'type' => 'checkbox', |
| 325 | 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types), |
| 326 | 'default' => array('post', 'page') |
| 327 | ), |
| 328 | 'post_statuses' => array( |
| 329 | 'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ), |
| 330 | 'type' => 'checkbox', |
| 331 | 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array), |
| 332 | 'default' => array('publish') |
| 333 | ) |
| 334 | ); |
| 335 | |
| 336 | // Fields for "Regenerate slugs" |
| 337 | $regenerate_slugs = array( |
| 338 | 'post_types' => array( |
| 339 | 'label' => __( 'Post Types that should be affected', 'permalink-manager' ), |
| 340 | 'type' => 'checkbox', |
| 341 | 'choices' => array_merge(array('all' => '<strong>' . __('All Post Types', 'permalink-manager') . '</strong>'), $all_post_types), |
| 342 | 'default' => array('post', 'page') |
| 343 | ), |
| 344 | 'post_statuses' => array( |
| 345 | 'label' => __( 'Post Statuses that should be affected', 'permalink-manager' ), |
| 346 | 'type' => 'checkbox', |
| 347 | 'choices' => array_merge(array('all' => '<strong>' . __('All Post Statuses', 'permalink-manager') . '</strong>'), $all_post_statuses_array), |
| 348 | 'default' => array('publish') |
| 349 | ) |
| 350 | ); |
| 351 | |
| 352 | return isset($array) ? ${$array} : array(); |
| 353 | |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Bulk actions functions |
| 358 | */ |
| 359 | function bulk_actions() { |
| 360 | global $wpdb; |
| 361 | |
| 362 | $updated_slugs_count = 0; |
| 363 | $updated_array = array(); |
| 364 | $alert_type = $alert_content = $errors = $main_content = ''; |
| 365 | |
| 366 | if (isset($_POST['update_all_slugs'])) { |
| 367 | |
| 368 | $slugs = isset($_POST['slug']) ? $_POST['slug'] : array(); |
| 369 | |
| 370 | // Double check if the slugs and ids are stored in arrays |
| 371 | if (!is_array($slugs)) $slugs = explode(',', $slugs); |
| 372 | |
| 373 | if (!empty($slugs)) { |
| 374 | foreach($slugs as $id => $new_slug) { |
| 375 | $this_post = get_post($id); |
| 376 | |
| 377 | // Check if slug was changed |
| 378 | if($this_post->post_name != $new_slug) { |
| 379 | // Update slugs |
| 380 | Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, $id); |
| 381 | |
| 382 | $updated_array[] = array('post_title' => get_the_title($id), 'old_slug' => $this_post->post_name, 'new_slug' => $new_slug); |
| 383 | $updated_slugs_count++; |
| 384 | } |
| 385 | |
| 386 | // Reset slug |
| 387 | $slug = ''; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | } else if (isset($_POST['find-replace-button'])) { |
| 392 | |
| 393 | $var['old_string'] = esc_sql($_POST['permalink-manager']['find-replace']['old_string']); |
| 394 | $var['new_string'] = esc_sql($_POST['permalink-manager']['find-replace']['new_string']); |
| 395 | $post_types_array = ($_POST['permalink-manager']['find-replace']['post_types']); |
| 396 | $post_statuses_array = ($_POST['permalink-manager']['find-replace']['post_statuses']); |
| 397 | $var['post_types'] = implode("', '", $post_types_array); |
| 398 | $var['post_statuses'] = implode("', '", $post_statuses_array); |
| 399 | |
| 400 | // Check if any of variables is not empty |
| 401 | $find_and_replace_fields = $this->fields_arrays('find_and_replace'); |
| 402 | foreach($var as $key => $val) { |
| 403 | if(empty($val)) $errors .= '<p>' . sprintf( __( '<strong>"%1s"</strong> field is empty!', 'permalink-manager' ), $find_and_replace_fields[$key]['label'] ) . '</p>'; |
| 404 | } |
| 405 | |
| 406 | // Save the rows before they are updated to an array |
| 407 | $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); |
| 408 | |
| 409 | // Now if the array is not empty use IDs from each subarray as a key |
| 410 | if($posts_to_update && empty($errors)) { |
| 411 | foreach ($posts_to_update as $row) { |
| 412 | // Get new slug |
| 413 | $old_slug = $row['post_name']; |
| 414 | $new_slug = str_replace($var['old_string'], $var['new_string'], $old_slug); |
| 415 | |
| 416 | // Update slugs |
| 417 | Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, $row['ID']); |
| 418 | |
| 419 | $updated_array[] = array('post_title' => $row['post_title'], 'old_slug' => $old_slug, 'new_slug' => $new_slug); |
| 420 | $updated_slugs_count++; |
| 421 | |
| 422 | // Reset slug |
| 423 | $slug = ''; |
| 424 | } |
| 425 | } else { |
| 426 | $alert_type = 'error'; |
| 427 | $alert_content = $errors; |
| 428 | } |
| 429 | |
| 430 | } else if (isset($_POST['regenerate-button'])) { |
| 431 | |
| 432 | // Setup needed variables |
| 433 | $post_types_array = ($_POST['permalink-manager']['regenerate_slugs']['post_types']); |
| 434 | $post_statuses_array = ($_POST['permalink-manager']['regenerate_slugs']['post_statuses']); |
| 435 | |
| 436 | // Reset query |
| 437 | $reset_query = new WP_Query( array( 'post_type' => $post_types_array, 'post_status' => $post_statuses_array, 'posts_per_page' => -1 ) ); |
| 438 | |
| 439 | // The Loop |
| 440 | if ( $reset_query->have_posts() ) { |
| 441 | while ( $reset_query->have_posts() ) { |
| 442 | $reset_query->the_post(); |
| 443 | $this_post = get_post(get_the_ID()); |
| 444 | |
| 445 | $correct_slug = sanitize_title(get_the_title()); |
| 446 | $old_slug = $this_post->post_name; |
| 447 | $new_slug = wp_unique_post_slug($correct_slug, get_the_ID(), get_post_status(get_the_ID()), get_post_type(get_the_ID()), null); |
| 448 | |
| 449 | if($old_slug != $new_slug) { |
| 450 | $updated_slugs_count++; |
| 451 | |
| 452 | Permalink_Manager_Helper_Functions::update_slug_by_id($new_slug, get_the_ID()); |
| 453 | $updated_array[] = array('post_title' => get_the_title(), 'old_slug' => $old_slug, 'new_slug' => $new_slug); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Restore original Post Data |
| 459 | wp_reset_postdata(); |
| 460 | |
| 461 | // Save Permalink Structures/Permalinks Bases |
| 462 | } else if (isset($_POST['save_permalink_structures'])) { |
| 463 | Permalink_Manager_Helper_Functions::save_option('base-editor', $_POST['permalink-manager']['base-editor']); |
| 464 | |
| 465 | $alert_type = 'updated'; |
| 466 | $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')); |
| 467 | Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true); |
| 468 | return; |
| 469 | // Flush rewrite rules |
| 470 | } else if (isset($_POST['flush_rewrite_rules'])) { |
| 471 | $this->flush_rewrite_rules(); |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Display results |
| 477 | */ |
| 478 | if((isset($_POST['permalink-manager']) || isset($_POST['update_all_slugs'])) && !(isset($_POST['screen-options-apply']))) { |
| 479 | // Display errors or success message |
| 480 | |
| 481 | // Check how many rows/slugs were affected |
| 482 | if($updated_slugs_count > 0) { |
| 483 | $alert_type = 'updated'; |
| 484 | $alert_content = sprintf( _n( '<strong>%d</strong> slug were updated!', '<strong>%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' '; |
| 485 | $alert_content .= sprintf( __( '<a href="%s">Click here</a> to go to the list of updated slugs', 'permalink-manager' ), '#updated-list'); |
| 486 | } else { |
| 487 | $alert_type = 'error'; |
| 488 | $alert_content = ($alert_content) ? $alert_content : __( '<strong>No slugs</strong> were updated!', 'permalink-manager' ); |
| 489 | } |
| 490 | |
| 491 | Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true); |
| 492 | |
| 493 | // Display summary after update |
| 494 | // Display only if there are any slugs updated |
| 495 | if ( $updated_slugs_count > 0 && $updated_array ) { |
| 496 | add_filter('permalink-manager-after-tabs', function( $arg ) use ( $alert_content, $alert_type, $errors, $updated_array, $main_content ) { |
| 497 | |
| 498 | $header_footer = '<tr>'; |
| 499 | $header_footer .= '<th class="column-primary">' . __('Title', 'permalink-manager') . '</th>'; |
| 500 | $header_footer .= '<th>' . __('Old slug', 'permalink-manager') . '</th>'; |
| 501 | $header_footer .= '<th>' . __('New slug', 'permalink-manager') . '</th>'; |
| 502 | $header_footer .= '</tr>'; |
| 503 | |
| 504 | $updated_slugs_count = 0; |
| 505 | foreach($updated_array as $row) { |
| 506 | // Odd/even class |
| 507 | $updated_slugs_count++; |
| 508 | $alternate_class = ($updated_slugs_count % 2 == 1) ? ' class="alternate"' : ''; |
| 509 | |
| 510 | $main_content .= "<tr{$alternate_class}>"; |
| 511 | $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>'; |
| 512 | $main_content .= '<td data-colname="' . __('Old slug', 'permalink-manager') . '">' . $row['old_slug'] . '</td>'; |
| 513 | $main_content .= '<td data-colname="' . __('New slug', 'permalink-manager') . '">' . $row['new_slug'] . '</td>'; |
| 514 | $main_content .= '</tr>'; |
| 515 | } |
| 516 | |
| 517 | // Merge header, footer and content |
| 518 | $output = '<h3 id="updated-list">' . __('List of updated posts', 'permalink-manager') . '</h3>'; |
| 519 | $output .= '<table class="widefat wp-list-table">'; |
| 520 | $output .= "<thead>{$header_footer}</thead><tbody>{$main_content}</tbody><tfoot>{$header_footer}</tfoot>"; |
| 521 | $output .= '</table>'; |
| 522 | |
| 523 | return $output ; |
| 524 | |
| 525 | }); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Change permalinks for posts, pages & custom post types |
| 532 | */ |
| 533 | function custom_permalinks($permalink, $post) { |
| 534 | $post = (is_integer($post)) ? get_post($post) : $post; |
| 535 | $post_type = $post->post_type; |
| 536 | $permastruct = isset($this->permalink_manager_options['base-editor'][$post_type]) ? $this->permalink_manager_options['base-editor'][$post_type] : ''; |
| 537 | |
| 538 | // Ignore empty permastructures (do not change them) |
| 539 | if(empty($permastruct) || $post->post_status != 'publish') return $permalink; |
| 540 | |
| 541 | // Get options |
| 542 | if($permastruct) { |
| 543 | $permalink = home_url() . "/" . trim($permastruct, '/'); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Replace Structure Tags |
| 548 | */ |
| 549 | |
| 550 | // Get the date |
| 551 | $date = explode(" ",date('Y m d H i s', strtotime($post->post_date))); |
| 552 | |
| 553 | // Get the category (if needed) |
| 554 | $category = ''; |
| 555 | if ( strpos($permalink, '%category%') !== false ) { |
| 556 | $cats = get_the_category($post->ID); |
| 557 | if ( $cats ) { |
| 558 | usort($cats, '_usort_terms_by_ID'); // order by ID |
| 559 | $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); |
| 560 | $category_object = get_term( $category_object, 'category' ); |
| 561 | $category = $category_object->slug; |
| 562 | if ( $parent = $category_object->parent ) |
| 563 | $category = get_category_parents($parent, false, '/', true) . $category; |
| 564 | } |
| 565 | // show default category in permalinks, without having to assign it explicitly |
| 566 | if ( empty($category) ) { |
| 567 | $default_category = get_term( get_option( 'default_category' ), 'category' ); |
| 568 | $category = is_wp_error( $default_category ) ? '' : $default_category->slug; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // Get the author (if needed) |
| 573 | $author = ''; |
| 574 | if ( strpos($permalink, '%author%') !== false ) { |
| 575 | $authordata = get_userdata($post->post_author); |
| 576 | $author = $authordata->user_nicename; |
| 577 | } |
| 578 | |
| 579 | // Fix for hierarchical CPT (start) |
| 580 | $full_slug = get_page_uri($post); |
| 581 | $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type); |
| 582 | |
| 583 | // Do the replacement (post tag is removed now to enable support for hierarchical CPT) |
| 584 | $tags = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%category%', '%author%', $post_type_tag); |
| 585 | $replacements = array($date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->ID, $category, $author, ''); |
| 586 | |
| 587 | return str_replace($tags, $replacements, "{$permalink}{$full_slug}"); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Add rewrite rules |
| 592 | */ |
| 593 | function custom_cpt_rewrite_rules($rules) { |
| 594 | |
| 595 | global $wp_rewrite; |
| 596 | |
| 597 | $new_rules = array(); |
| 598 | $permastructures = $this->permalink_manager_options['base-editor']; |
| 599 | |
| 600 | // Rewrite rules for Posts & Pages are defined in different filters |
| 601 | unset($permastructures['post'], $permastructures['page']); |
| 602 | |
| 603 | foreach($permastructures as $post_type => $permastruct) { |
| 604 | // Ignore empty permastructures (do not add them) |
| 605 | if(empty($permastruct)) continue; |
| 606 | |
| 607 | $new_rule = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $permastruct, EP_PERMALINK); |
| 608 | $rules = array_merge($new_rule, $rules); |
| 609 | } |
| 610 | return $rules; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Post Rewrite Rules |
| 615 | */ |
| 616 | function custom_post_rewrite_rules($rules) { |
| 617 | global $wp_rewrite; |
| 618 | if(isset($this->permalink_manager_options['base-editor']['post'])) { |
| 619 | $rules = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $this->permalink_manager_options['base-editor']['post'], EP_PERMALINK); |
| 620 | } |
| 621 | return $rules; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Page Rewrite Rules |
| 626 | */ |
| 627 | function custom_page_rewrite_rules($rules) { |
| 628 | global $wp_rewrite; |
| 629 | if(isset($this->permalink_manager_options['base-editor']['page'])) { |
| 630 | $rules = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . $this->permalink_manager_options['base-editor']['page'], EP_PERMALINK); |
| 631 | } |
| 632 | return $rules; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Flush rewrite rules |
| 637 | */ |
| 638 | function flush_rewrite_rules() { |
| 639 | if(isset($_REQUEST['flush_rewrite_rules'])) { |
| 640 | flush_rewrite_rules(); |
| 641 | |
| 642 | $alert_type = 'updated'; |
| 643 | $alert_content = __( 'The rewrite rules are flushed!', 'permalink-manager' ); |
| 644 | return Permalink_Manager_Helper_Functions::display_alert($alert_content, $alert_type, true); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Begins execution of the plugin. |
| 652 | */ |
| 653 | function run_permalink_manager() { |
| 654 | |
| 655 | // Load plugin files. |
| 656 | require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-slug-editor.php'; |
| 657 | require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-base-editor.php'; |
| 658 | require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-screen-options.php'; |
| 659 | require_once PERMALINK_MANAGER_DIR . '/inc/permalink-manager-helper-functions.php'; |
| 660 | |
| 661 | $Permalink_Manager_Class = new Permalink_Manager_Class(); |
| 662 | $Permalink_Manager_Screen_Options = new Permalink_Manager_Screen_Options(); |
| 663 | |
| 664 | } |
| 665 | |
| 666 | run_permalink_manager(); |
| 667 |