permalink-manager-debug.php
3 months ago
permalink-manager-permastructures.php
1 month ago
permalink-manager-settings.php
3 months ago
permalink-manager-tools.php
3 months ago
permalink-manager-ui-elements.php
3 months ago
permalink-manager-uri-editor-post.php
1 month ago
permalink-manager-uri-editor.php
3 months ago
permalink-manager-tools.php
339 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | /** |
| 6 | * Display the page where the slugs could be regenerated or replaced |
| 7 | */ |
| 8 | class Permalink_Manager_Tools { |
| 9 | |
| 10 | public function __construct() { |
| 11 | add_filter( 'permalink_manager_sections', array( $this, 'add_admin_section' ), 1 ); |
| 12 | add_action( 'plugins_loaded', array( $this, 'init' ) ); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Register hooks |
| 17 | */ |
| 18 | public function init() { |
| 19 | add_filter( 'permalink_manager_tools_fields', array( $this, 'add_preview_mode_toggle' ), 50, 2 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Add a new section to the Permalink Manager UI |
| 24 | * |
| 25 | * @param array $admin_sections |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function add_admin_section( $admin_sections ) { |
| 30 | $admin_sections['tools'] = array( |
| 31 | 'name' => __( 'Tools', 'permalink-manager' ), |
| 32 | 'subsections' => array( |
| 33 | 'duplicates' => array( |
| 34 | 'name' => __( 'Permalink Duplicates', 'permalink-manager' ), |
| 35 | 'function' => array( 'class' => 'Permalink_Manager_Tools', 'method' => 'duplicates_output' ) |
| 36 | ), |
| 37 | 'find_and_replace' => array( |
| 38 | 'name' => __( 'Find & Replace', 'permalink-manager' ), |
| 39 | 'function' => array( 'class' => 'Permalink_Manager_Tools', 'method' => 'find_and_replace_output' ) |
| 40 | ), |
| 41 | 'regenerate_slugs' => array( |
| 42 | 'name' => __( 'Regenerate/Reset', 'permalink-manager' ), |
| 43 | 'function' => array( 'class' => 'Permalink_Manager_Tools', 'method' => 'regenerate_slugs_output' ) |
| 44 | ), |
| 45 | 'stop_words' => array( |
| 46 | 'name' => __( 'Stop Words', 'permalink-manager' ), |
| 47 | 'function' => array( 'class' => 'Permalink_Manager_UI_Elements', 'method' => 'pro_text' ) |
| 48 | ), |
| 49 | 'import' => array( |
| 50 | 'name' => __( 'Custom Permalinks', 'permalink-manager' ), |
| 51 | 'function' => array( 'class' => 'Permalink_Manager_UI_Elements', 'method' => 'pro_text' ) |
| 52 | ) |
| 53 | ) |
| 54 | ); |
| 55 | |
| 56 | return $admin_sections; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Display a warning message before the user changes the permalinks mode to "Native slugs" |
| 61 | * |
| 62 | * @return string |
| 63 | */ |
| 64 | public function display_instructions() { |
| 65 | return sprintf( '<p><strong>%s</strong>', __( 'A MySQL backup is highly recommended before using "<em>Native slugs</em>" mode!', 'permalink-manager' ) ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Display a list of all duplicated URIs and redirects |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function duplicates_output() { |
| 74 | // Get the duplicates & another variables |
| 75 | $all_duplicates = Permalink_Manager_Admin_Functions::get_all_duplicates(); |
| 76 | |
| 77 | $button_url = add_query_arg( array( |
| 78 | 'section' => 'tools', |
| 79 | 'subsection' => 'duplicates', |
| 80 | 'clear-permalink-manager-uris' => 1, |
| 81 | 'permalink-manager-nonce' => wp_create_nonce( 'permalink-manager' ) |
| 82 | ), Permalink_Manager_Admin_Functions::get_admin_url() ); |
| 83 | |
| 84 | $html = sprintf( "<h3>%s</h3>", __( "List of duplicated permalinks", "permalink-manager" ) ); |
| 85 | $html .= wpautop( sprintf( "<a class=\"button button-primary\" href=\"%s\">%s</a>", $button_url, __( 'Fix custom permalinks & redirects', 'permalink-manager' ) ) ); |
| 86 | |
| 87 | if ( ! empty( $all_duplicates ) ) { |
| 88 | foreach ( $all_duplicates as $uri => $duplicates ) { |
| 89 | $html .= "<div class=\"permalink-manager postbox permalink-manager-duplicate-box\">"; |
| 90 | $html .= sprintf( '<h4 class="heading"><a href="%1$s" target="_blank">%1$s <span class="dashicons dashicons-external"></span></a></h4>', Permalink_Manager_Core_Functions::control_trailing_slashes( home_url( $uri ) ) ); |
| 91 | $html .= "<table>"; |
| 92 | |
| 93 | foreach ( $duplicates as $item_id ) { |
| 94 | $html .= "<tr>"; |
| 95 | |
| 96 | // Detect duplicate type |
| 97 | preg_match( "/(redirect-([\d]+)_)?(?:(tax-)?([\d]*))/", $item_id, $parts ); |
| 98 | |
| 99 | $is_extra_redirect = ( ! empty( $parts[1] ) ) ? true : false; |
| 100 | $duplicate_type = ( $is_extra_redirect ) ? __( 'Extra Redirect', 'permalink-manager' ) : __( 'Custom permalink', 'permalink-manager' ); |
| 101 | $detected_id = $parts[4]; |
| 102 | // $detected_index = $parts[2]; |
| 103 | $detected_term = ( ! empty( $parts[3] ) ) ? true : false; |
| 104 | $remove_link = ( $is_extra_redirect ) ? sprintf( " <a href=\"%s\"><span class=\"dashicons dashicons-trash\"></span> %s</a>", admin_url( "tools.php?page=permalink-manager§ion=tools&subsection=duplicates&remove-redirect={$item_id}" ), __( 'Remove Redirect', 'permalink-manager' ) ) : ""; |
| 105 | |
| 106 | // Get term |
| 107 | if ( $detected_term && ! empty( $detected_id ) ) { |
| 108 | $term = get_term( $detected_id ); |
| 109 | if ( ! empty( $term->name ) ) { |
| 110 | $title = $term->name; |
| 111 | $edit_label = "<span class=\"dashicons dashicons-edit\"></span>" . __( "Edit term", "permalink-manager" ); |
| 112 | $edit_link = get_edit_tag_link( $term->term_id, $term->taxonomy ); |
| 113 | } else { |
| 114 | $title = __( "(Removed term)", "permalink-manager" ); |
| 115 | $edit_label = "<span class=\"dashicons dashicons-trash\"></span>" . __( "Remove broken URI", "permalink-manager" ); |
| 116 | $edit_link = admin_url( "tools.php?page=permalink-manager§ion=tools&subsection=duplicates&remove-uri=tax-{$detected_id}" ); |
| 117 | } |
| 118 | } // Get post |
| 119 | else if ( ! empty( $detected_id ) ) { |
| 120 | $post = get_post( $detected_id ); |
| 121 | if ( ! empty( $post->post_title ) && post_type_exists( $post->post_type ) ) { |
| 122 | $title = $post->post_title; |
| 123 | $edit_label = "<span class=\"dashicons dashicons-edit\"></span>" . __( "Edit post", "permalink-manager" ); |
| 124 | $edit_link = get_edit_post_link( $post->ID ); |
| 125 | } else { |
| 126 | $title = __( "(Removed post)", "permalink-manager" ); |
| 127 | $edit_label = "<span class=\"dashicons dashicons-trash\"></span>" . __( "Remove broken URI", "permalink-manager" ); |
| 128 | $edit_link = admin_url( "tools.php?page=permalink-manager§ion=tools&subsection=duplicates&remove-uri={$detected_id}" ); |
| 129 | } |
| 130 | } else { |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | $html .= sprintf( '<td><a href="%1$s">%2$s</a>%3$s</td><td>%4$s</td><td class="actions"><a href="%1$s">%5$s</a>%6$s</td>', $edit_link, $title, " <small>#{$detected_id}</small>", $duplicate_type, $edit_label, $remove_link ); |
| 135 | $html .= "</tr>"; |
| 136 | } |
| 137 | $html .= "</table>"; |
| 138 | $html .= "</div>"; |
| 139 | } |
| 140 | } else { |
| 141 | $html .= sprintf( "<p class=\"alert notice-success notice\">%s</p>", __( 'Congratulations! No duplicated URIs or Redirects found!', 'permalink-manager' ) ); |
| 142 | } |
| 143 | |
| 144 | return $html; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Generate a form for "Tools -> Find & replace" tool |
| 149 | * |
| 150 | * @return string |
| 151 | */ |
| 152 | public function find_and_replace_output() { |
| 153 | // Get all registered post types array & statuses |
| 154 | $all_post_statuses_array = Permalink_Manager_Helper_Functions::get_post_statuses(); |
| 155 | $all_post_types = Permalink_Manager_Helper_Functions::get_post_types_array(); |
| 156 | $all_taxonomies = Permalink_Manager_Helper_Functions::get_taxonomies_array(); |
| 157 | |
| 158 | $fields = apply_filters( 'permalink_manager_tools_fields', array( |
| 159 | 'old_string' => array( |
| 160 | 'label' => __( 'Find ...', 'permalink-manager' ), |
| 161 | 'type' => 'text', |
| 162 | 'container' => 'row', |
| 163 | 'input_class' => 'widefat' |
| 164 | ), |
| 165 | 'new_string' => array( |
| 166 | 'label' => __( 'Replace with ...', 'permalink-manager' ), |
| 167 | 'type' => 'text', |
| 168 | 'container' => 'row', |
| 169 | 'input_class' => 'widefat' |
| 170 | ), |
| 171 | 'mode' => array( |
| 172 | 'label' => __( 'Mode', 'permalink-manager' ), |
| 173 | 'type' => 'select', |
| 174 | 'container' => 'row', |
| 175 | 'choices' => array( |
| 176 | 'custom_uris' => __( 'Custom permalinks', 'permalink-manager' ), |
| 177 | 'slugs' => __( 'Native slugs', 'permalink-manager' ) |
| 178 | ), |
| 179 | ), |
| 180 | 'content_type' => array( |
| 181 | 'label' => __( 'Select content type', 'permalink-manager' ), |
| 182 | 'type' => 'select', |
| 183 | 'disabled' => true, |
| 184 | 'pro' => true, |
| 185 | 'container' => 'row', |
| 186 | 'default' => 'post_types', |
| 187 | 'choices' => array( |
| 188 | 'post_types' => __( 'Post types', 'permalink-manager' ), |
| 189 | 'taxonomies' => __( 'Taxonomies', 'permalink-manager' ) |
| 190 | ), |
| 191 | ), |
| 192 | 'post_types' => array( |
| 193 | 'label' => __( 'Select post types', 'permalink-manager' ), |
| 194 | 'type' => 'checkbox', |
| 195 | 'container' => 'row', |
| 196 | 'default' => array( 'post', 'page' ), |
| 197 | 'choices' => $all_post_types, |
| 198 | 'select_all' => '', |
| 199 | 'unselect_all' => '', |
| 200 | ), |
| 201 | 'taxonomies' => array( |
| 202 | 'label' => __( 'Select taxonomies', 'permalink-manager' ), |
| 203 | 'type' => 'checkbox', |
| 204 | 'container' => 'row', |
| 205 | 'container_class' => 'hidden', |
| 206 | 'default' => array( 'category', 'post_tag' ), |
| 207 | 'choices' => $all_taxonomies, |
| 208 | 'pro' => true, |
| 209 | 'select_all' => '', |
| 210 | 'unselect_all' => '', |
| 211 | ), |
| 212 | 'post_statuses' => array( |
| 213 | 'label' => __( 'Select post statuses', 'permalink-manager' ), |
| 214 | 'type' => 'checkbox', |
| 215 | 'container' => 'row', |
| 216 | 'default' => array( 'publish' ), |
| 217 | 'choices' => $all_post_statuses_array, |
| 218 | 'select_all' => '', |
| 219 | 'unselect_all' => '', |
| 220 | ), |
| 221 | 'ids' => array( |
| 222 | 'label' => __( 'Select IDs', 'permalink-manager' ), |
| 223 | 'type' => 'text', |
| 224 | 'container' => 'row', |
| 225 | //'disabled' => true, |
| 226 | 'description' => __( 'To narrow the above filters you can type the post IDs (or ranges) here. E.g. <strong>1-8, 10, 25</strong>.', 'permalink-manager' ), |
| 227 | //'pro' => true, |
| 228 | 'input_class' => 'widefat' |
| 229 | ) |
| 230 | ), 'find_and_replace' ); |
| 231 | |
| 232 | $sidebar = '<h3>' . __( 'Important notices', 'permalink-manager' ) . '</h3>'; |
| 233 | $sidebar .= self::display_instructions(); |
| 234 | |
| 235 | return Permalink_Manager_UI_Elements::get_the_form( $fields, 'columns-3', array( 'text' => __( 'Find and replace', 'permalink-manager' ), 'class' => 'primary margin-top' ), $sidebar, array( 'action' => 'permalink-manager', 'name' => 'find_and_replace' ), true, 'form-ajax' ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Generate a form for "Tools -> Regenerate/reset" tool |
| 240 | * |
| 241 | * @return string |
| 242 | */ |
| 243 | public function regenerate_slugs_output() { |
| 244 | // Get all registered post types array & statuses |
| 245 | $all_post_statuses_array = Permalink_Manager_Helper_Functions::get_post_statuses(); |
| 246 | $all_post_types = Permalink_Manager_Helper_Functions::get_post_types_array(); |
| 247 | $all_taxonomies = Permalink_Manager_Helper_Functions::get_taxonomies_array(); |
| 248 | |
| 249 | $fields = apply_filters( 'permalink_manager_tools_fields', array( |
| 250 | 'mode' => array( |
| 251 | 'label' => __( 'Mode', 'permalink-manager' ), |
| 252 | 'type' => 'select', |
| 253 | 'container' => 'row', |
| 254 | 'choices' => array( |
| 255 | 'custom_uris' => __( 'Regenerate custom permalinks', 'permalink-manager' ), |
| 256 | 'slugs' => __( 'Regenerate native slugs', 'permalink-manager' ), |
| 257 | 'native' => __( 'Use original URLs as custom permalinks', 'permalink-manager' ) |
| 258 | ), |
| 259 | ), |
| 260 | 'content_type' => array( |
| 261 | 'label' => __( 'Select content type', 'permalink-manager' ), |
| 262 | 'type' => 'select', |
| 263 | 'disabled' => true, |
| 264 | 'pro' => true, |
| 265 | 'container' => 'row', |
| 266 | 'default' => 'post_types', |
| 267 | 'choices' => array( |
| 268 | 'post_types' => __( 'Post types', 'permalink-manager' ), |
| 269 | 'taxonomies' => __( 'Taxonomies', 'permalink-manager' ) |
| 270 | ), |
| 271 | ), |
| 272 | 'post_types' => array( |
| 273 | 'label' => __( 'Select post types', 'permalink-manager' ), |
| 274 | 'type' => 'checkbox', |
| 275 | 'container' => 'row', |
| 276 | 'default' => array( 'post', 'page' ), |
| 277 | 'choices' => $all_post_types, |
| 278 | 'select_all' => '', |
| 279 | 'unselect_all' => '', |
| 280 | ), |
| 281 | 'taxonomies' => array( |
| 282 | 'label' => __( 'Select taxonomies', 'permalink-manager' ), |
| 283 | 'type' => 'checkbox', |
| 284 | 'container' => 'row', |
| 285 | 'container_class' => 'hidden', |
| 286 | 'default' => array( 'category', 'post_tag' ), |
| 287 | 'choices' => $all_taxonomies, |
| 288 | 'pro' => true, |
| 289 | 'select_all' => '', |
| 290 | 'unselect_all' => '', |
| 291 | ), |
| 292 | 'post_statuses' => array( |
| 293 | 'label' => __( 'Select post statuses', 'permalink-manager' ), |
| 294 | 'type' => 'checkbox', |
| 295 | 'container' => 'row', |
| 296 | 'default' => array( 'publish' ), |
| 297 | 'choices' => $all_post_statuses_array, |
| 298 | 'select_all' => '', |
| 299 | 'unselect_all' => '', |
| 300 | ), |
| 301 | 'ids' => array( |
| 302 | 'label' => __( 'Select IDs', 'permalink-manager' ), |
| 303 | 'type' => 'text', |
| 304 | 'container' => 'row', |
| 305 | //'disabled' => true, |
| 306 | 'description' => __( 'To narrow the above filters you can type the post IDs (or ranges) here. E.g. <strong>1-8, 10, 25</strong>.', 'permalink-manager' ), |
| 307 | //'pro' => true, |
| 308 | 'input_class' => 'widefat' |
| 309 | ) |
| 310 | ), 'regenerate' ); |
| 311 | |
| 312 | $sidebar = '<h3>' . __( 'Important notices', 'permalink-manager' ) . '</h3>'; |
| 313 | $sidebar .= self::display_instructions(); |
| 314 | |
| 315 | return Permalink_Manager_UI_Elements::get_the_form( $fields, 'columns-3', array( 'text' => __( 'Regenerate', 'permalink-manager' ), 'class' => 'primary margin-top' ), $sidebar, array( 'action' => 'permalink-manager', 'name' => 'regenerate' ), true, 'form-ajax' ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Add "Preview mode" toggle to the end of options list in "Regenerate/rest" and "Find & replace" |
| 320 | * |
| 321 | * @param $fields |
| 322 | * @param $tool_name |
| 323 | * |
| 324 | * @return array |
| 325 | */ |
| 326 | public function add_preview_mode_toggle( $fields, $tool_name ) { |
| 327 | if ( is_array( $fields ) && in_array( $tool_name, array( 'regenerate', 'find_and_replace' ) ) ) { |
| 328 | $fields['preview_mode'] = array( |
| 329 | 'label' => __( 'Preview mode', 'permalink-manager' ), |
| 330 | 'type' => 'single_checkbox', |
| 331 | 'container' => 'row', |
| 332 | 'description' => __( 'Enable this option if you want to review the changes in "read mode" before saving them in the database.', 'permalink-manager' ) |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | return $fields; |
| 337 | } |
| 338 | } |
| 339 |