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