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