permalink-manager-actions.php
6 years ago
permalink-manager-admin-functions.php
6 years ago
permalink-manager-core-functions.php
6 years ago
permalink-manager-debug.php
6 years ago
permalink-manager-gutenberg.php
6 years ago
permalink-manager-helper-functions.php
6 years ago
permalink-manager-third-parties.php
6 years ago
permalink-manager-uri-functions-post.php
6 years ago
permalink-manager-actions.php
609 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Additional hooks for "Permalink Manager Pro" |
| 4 | */ |
| 5 | class Permalink_Manager_Actions extends Permalink_Manager_Class { |
| 6 | |
| 7 | public function __construct() { |
| 8 | add_action('admin_init', array($this, 'trigger_action'), 9); |
| 9 | add_action('admin_init', array($this, 'extra_actions')); |
| 10 | |
| 11 | // Ajax-based functions |
| 12 | add_action('wp_ajax_pm_bulk_tools', array($this, 'pm_bulk_tools')); |
| 13 | add_action('wp_ajax_pm_save_permalink', array($this, 'pm_save_permalink')); |
| 14 | |
| 15 | add_action('clean_permalinks_event', array($this, 'clean_permalinks_hook')); |
| 16 | add_action('init', array($this, 'clean_permalinks_cronjob')); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Actions |
| 21 | */ |
| 22 | public function trigger_action() { |
| 23 | global $permalink_manager_after_sections_html; |
| 24 | |
| 25 | // 1. Check if the form was submitted |
| 26 | if(empty($_POST)) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | // 2. Do nothing if search query is not empty |
| 31 | if(isset($_REQUEST['search-submit']) || isset($_REQUEST['months-filter-button'])) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | $actions_map = array( |
| 36 | 'uri_editor' => array('function' => 'update_all_permalinks', 'display_uri_table' => true), |
| 37 | //'regenerate' => array('function' => 'regenerate_all_permalinks', 'display_uri_table' => true), |
| 38 | //'find_and_replace' => array('function' => 'find_and_replace', 'display_uri_table' => true), |
| 39 | 'permalink_manager_options' => array('function' => 'save_settings'), |
| 40 | 'permalink_manager_permastructs' => array('function' => 'save_permastructures'), |
| 41 | 'flush_sitemaps' => array('function' => 'flush_sitemaps'), |
| 42 | 'import' => array('function' => 'import_custom_permalinks_uris'), |
| 43 | ); |
| 44 | |
| 45 | // 3. Find the action |
| 46 | foreach($actions_map as $action => $map) { |
| 47 | if(isset($_POST[$action]) && wp_verify_nonce($_POST[$action], 'permalink-manager')) { |
| 48 | // Execute the function |
| 49 | $output = call_user_func(array($this, $map['function'])); |
| 50 | |
| 51 | // Get list of updated URIs |
| 52 | if(!empty($map['display_uri_table'])) { |
| 53 | $updated_slugs_count = (isset($output['updated_count']) && $output['updated_count'] > 0) ? $output['updated_count'] : false; |
| 54 | $updated_slugs_array = ($updated_slugs_count) ? $output['updated'] : ''; |
| 55 | } |
| 56 | |
| 57 | // Trigger only one function |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // 4. Display the slugs table (and append the globals) |
| 63 | if(isset($updated_slugs_count)) { |
| 64 | $permalink_manager_after_sections_html .= Permalink_Manager_Admin_Functions::display_updated_slugs($updated_slugs_array); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Save settings |
| 70 | */ |
| 71 | public static function save_settings($field = false, $value = false, $display_alert = true) { |
| 72 | global $permalink_manager_options, $permalink_manager_before_sections_html; |
| 73 | |
| 74 | // Info: The settings array is used also by "Screen Options" |
| 75 | $new_options = $permalink_manager_options; |
| 76 | //$new_options = array(); |
| 77 | |
| 78 | // Save only selected field/sections |
| 79 | if($field && $value) { |
| 80 | $new_options[$field] = $value; |
| 81 | } else { |
| 82 | $post_fields = $_POST; |
| 83 | |
| 84 | foreach($post_fields as $option_name => $option_value) { |
| 85 | $new_options[$option_name] = $option_value; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Sanitize & override the global with new settings |
| 90 | $new_options = Permalink_Manager_Helper_Functions::sanitize_array($new_options); |
| 91 | $permalink_manager_options = $new_options = array_filter($new_options); |
| 92 | |
| 93 | // Save the settings in database |
| 94 | update_option('permalink-manager', $new_options); |
| 95 | |
| 96 | // Display the message |
| 97 | $permalink_manager_before_sections_html .= ($display_alert) ? Permalink_Manager_Admin_Functions::get_alert_message(__( 'The settings are saved!', 'permalink-manager' ), 'updated') : ""; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Trigger bulk tools via AJAX |
| 102 | */ |
| 103 | function pm_bulk_tools() { |
| 104 | global $sitepress, $wp_filter; |
| 105 | |
| 106 | // Define variables |
| 107 | $return = array('alert' => Permalink_Manager_Admin_Functions::get_alert_message(__( '<strong>No slugs</strong> were updated!', 'permalink-manager' ), 'error updated_slugs')); |
| 108 | |
| 109 | // Get the name of the function |
| 110 | if(isset($_POST['regenerate']) && wp_verify_nonce($_POST['regenerate'], 'permalink-manager')) { |
| 111 | $function_name = 'regenerate_all_permalinks'; |
| 112 | $uniq_id = $_POST['pm_session_id']; |
| 113 | } else if(isset($_POST['find_and_replace']) && wp_verify_nonce($_POST['find_and_replace'], 'permalink-manager') && !empty($_POST['old_string']) && !empty($_POST['new_string'])) { |
| 114 | $function_name = 'find_and_replace'; |
| 115 | $uniq_id = $_POST['pm_session_id']; |
| 116 | } |
| 117 | |
| 118 | // Check if both strings are set for "Find and replace" tool |
| 119 | if(!empty($function_name)) { |
| 120 | // Hotfix for WPML (start) |
| 121 | if($sitepress) { |
| 122 | remove_filter('get_terms_args', array($sitepress, 'get_terms_args_filter'), 10); |
| 123 | remove_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1); |
| 124 | remove_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10); |
| 125 | remove_filter('get_pages', array($sitepress, 'get_pages_adjust_ids'), 1); |
| 126 | } |
| 127 | |
| 128 | // Get the mode |
| 129 | $mode = isset($_POST['mode']) ? $_POST['mode'] : 'custom_uris'; |
| 130 | |
| 131 | // Get the content type |
| 132 | if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') { |
| 133 | $class_name = 'Permalink_Manager_URI_Functions_Tax'; |
| 134 | } else { |
| 135 | $class_name = 'Permalink_Manager_URI_Functions_Post'; |
| 136 | } |
| 137 | |
| 138 | // Get items (try to get them from transient) |
| 139 | $items = get_transient("pm_{$uniq_id}"); |
| 140 | $progress = get_transient("pm_{$uniq_id}_progress"); |
| 141 | |
| 142 | $first_chunk = true; |
| 143 | $chunk_size = 50; |
| 144 | |
| 145 | if(empty($items)) { |
| 146 | $items = $class_name::get_items(); |
| 147 | |
| 148 | // Set stats (to display the progress) |
| 149 | $total = count($items); |
| 150 | |
| 151 | // Split items array into chunks and save them to transient |
| 152 | $items = array_chunk($items, $chunk_size); |
| 153 | |
| 154 | set_transient("pm_{$uniq_id}", $items, 300); |
| 155 | set_transient("pm_{$uniq_id}_progress", 0, 300); |
| 156 | } |
| 157 | |
| 158 | // Get homepage URL and ensure that it ends with slash |
| 159 | $home_url = Permalink_Manager_Helper_Functions::get_permalink_base() . "/"; |
| 160 | |
| 161 | // Process the variables from $_POST object |
| 162 | $old_string = (!empty($_POST['old_string'])) ? str_replace($home_url, '', esc_sql($_POST['old_string'])) : ''; |
| 163 | $new_string = (!empty($_POST['old_string'])) ? str_replace($home_url, '', esc_sql($_POST['new_string'])) : ''; |
| 164 | |
| 165 | // Process only one subarray |
| 166 | if(!empty($items[0])) { |
| 167 | $chunk = $items[0]; |
| 168 | |
| 169 | // Remove from array & update the transient |
| 170 | unset($items[0]); |
| 171 | $items = array_values($items); |
| 172 | set_transient("pm_{$uniq_id}", $items, 300); |
| 173 | |
| 174 | // Check if posts or terms should be updated |
| 175 | if($function_name == 'find_and_replace') { |
| 176 | $output = $class_name::find_and_replace($chunk, $mode, $old_string, $new_string); |
| 177 | } else { |
| 178 | $output = $class_name::regenerate_all_permalinks($chunk, $mode); |
| 179 | } |
| 180 | |
| 181 | if(!empty($output['updated_count'])) { |
| 182 | $return = array_merge($return, (array) Permalink_Manager_Admin_Functions::display_updated_slugs($output['updated'], true, $first_chunk)); |
| 183 | $return['updated_count'] = $output['updated_count']; |
| 184 | } |
| 185 | |
| 186 | // Send total number of processed items with a first chunk |
| 187 | if(!empty($total)) { |
| 188 | $return['total'] = $total; |
| 189 | } |
| 190 | |
| 191 | // Check if there are any chunks left |
| 192 | if(count($items) > 0) { |
| 193 | // Update progress |
| 194 | $progress += $chunk_size; |
| 195 | set_transient("pm_{$uniq_id}_progress", $progress, 300); |
| 196 | |
| 197 | $return['left_chunks'] = true; |
| 198 | $return['progress'] = $progress; |
| 199 | } else { |
| 200 | delete_transient("pm_{$uniq_id}"); |
| 201 | delete_transient("pm_{$uniq_id}_progress"); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Hotfix for WPML (end) |
| 206 | if($sitepress) { |
| 207 | add_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10, 4); |
| 208 | add_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1, 1); |
| 209 | add_filter('get_terms_args', array($sitepress, 'get_terms_args_filter'), 10, 2); |
| 210 | add_filter('get_pages', array($sitepress, 'get_pages_adjust_ids'), 1, 2); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | wp_send_json($return); |
| 215 | die(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Save permalink via AJAX |
| 220 | */ |
| 221 | public function pm_save_permalink() { |
| 222 | $element_id = (!empty($_POST['permalink-manager-edit-uri-element-id'])) ? sanitize_text_field($_POST['permalink-manager-edit-uri-element-id']) : ''; |
| 223 | |
| 224 | if(!empty($element_id) && is_numeric($element_id)) { |
| 225 | Permalink_Manager_URI_Functions_Post::update_post_uri($element_id); |
| 226 | |
| 227 | // Reload URI Editor & clean post cache |
| 228 | clean_post_cache($element_id); |
| 229 | $element = get_post($element_id); |
| 230 | $html = Permalink_Manager_Admin_Functions::display_uri_box($element, true); |
| 231 | |
| 232 | echo $html; |
| 233 | die(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Update all permalinks in "Permalink Editor" |
| 239 | */ |
| 240 | function update_all_permalinks() { |
| 241 | // Check if posts or terms should be updated |
| 242 | if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') { |
| 243 | return Permalink_Manager_URI_Functions_Tax::update_all_permalinks(); |
| 244 | } else { |
| 245 | return Permalink_Manager_URI_Functions_Post::update_all_permalinks(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Additional actions |
| 251 | */ |
| 252 | public static function extra_actions() { |
| 253 | if(isset($_GET['flush_sitemaps'])) { |
| 254 | self::flush_sitemaps(); |
| 255 | } else if(isset($_GET['clear-permalink-manager-uris'])) { |
| 256 | self::clear_all_uris(); |
| 257 | } else if(!empty($_REQUEST['remove-uri'])) { |
| 258 | $uri_key = sanitize_text_field($_REQUEST['remove-uri']); |
| 259 | self::force_clear_single_element_uris_and_redirects($uri_key); |
| 260 | } else if(!empty($_REQUEST['remove-redirect'])) { |
| 261 | $redirect_key = sanitize_text_field($_REQUEST['remove-redirect']); |
| 262 | self::force_clear_single_redirect($redirect_key); |
| 263 | } else if(!empty($_POST['screen-options-apply'])) { |
| 264 | self::save_screen_options(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Save "Screen Options" |
| 270 | */ |
| 271 | public static function save_screen_options() { |
| 272 | check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' ); |
| 273 | |
| 274 | // The values will be sanitized inside the function |
| 275 | self::save_settings('screen-options', $_POST['screen-options']); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Save permastructures |
| 280 | */ |
| 281 | public static function save_permastructures() { |
| 282 | global $permalink_manager_permastructs; |
| 283 | |
| 284 | $post_fields = $_POST; |
| 285 | $permastructure_options = $permastructures = array(); |
| 286 | |
| 287 | foreach($post_fields as $option_name => $option_value) { |
| 288 | $new_options[$option_name] = $option_value; |
| 289 | } |
| 290 | |
| 291 | // Split permastructure & permastructure settings |
| 292 | if(!empty($new_options['post_types'])) { |
| 293 | $permastructures['post_types'] = $new_options['post_types']; |
| 294 | } |
| 295 | |
| 296 | if(!empty($new_options['taxonomies'])) { |
| 297 | $permastructures['taxonomies'] = $new_options['taxonomies']; |
| 298 | } |
| 299 | |
| 300 | if(!empty($new_options['permastructure-settings'])) { |
| 301 | $permastructure_options = $new_options['permastructure-settings']; |
| 302 | } |
| 303 | |
| 304 | // A. Permastructures |
| 305 | if(!empty($new_options['post_types']) || !empty($new_options['taxonomies'])) { |
| 306 | $permastructures = Permalink_Manager_Helper_Functions::multidimensional_array_map('untrailingslashit', $permastructures); |
| 307 | |
| 308 | // Override the global with settings |
| 309 | $permalink_manager_permastructs = $permastructures; |
| 310 | |
| 311 | // Save the settings in database |
| 312 | update_option('permalink-manager-permastructs', $permastructures); |
| 313 | } |
| 314 | |
| 315 | // B. Permastructure settings |
| 316 | if(!empty($permastructure_options)) { |
| 317 | self::save_settings('permastructure-settings', $permastructure_options); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Clear URIs |
| 323 | */ |
| 324 | public static function clear_all_uris() { |
| 325 | global $permalink_manager_uris, $permalink_manager_redirects, $wpdb, $permalink_manager_before_sections_html; |
| 326 | |
| 327 | // Check if array with custom URIs exists |
| 328 | if(empty($permalink_manager_uris)) { return; } |
| 329 | |
| 330 | // Count removed URIs & redirects |
| 331 | $removed_uris = 0; |
| 332 | $removed_redirects = 0; |
| 333 | |
| 334 | // Get all element IDs |
| 335 | $element_ids = array_merge(array_keys((array) $permalink_manager_uris), array_keys((array) $permalink_manager_redirects)); |
| 336 | |
| 337 | // 1. Remove unused custom URI & redirects for deleted post or term |
| 338 | foreach($element_ids as $element_id) { |
| 339 | $count = self::clear_single_element_uris_and_redirects($element_id, true); |
| 340 | |
| 341 | $removed_uris = (!empty($count[0])) ? $count[0] + $removed_uris : $removed_uris; |
| 342 | $removed_redirects = (!empty($count[1])) ? $count[1] + $removed_redirects : $removed_redirects; |
| 343 | } |
| 344 | |
| 345 | // 2. Keep only a single redirect (make it unique) |
| 346 | $removed_redirects += self::clear_redirects_array(true); |
| 347 | |
| 348 | // 3. Optional method to keep the permalinks unique |
| 349 | if(apply_filters('permalink_manager_fix_uri_duplicates', false) == true) { |
| 350 | self::fix_uri_duplicates(); |
| 351 | } |
| 352 | |
| 353 | // Save cleared URIs & Redirects |
| 354 | if($removed_uris > 0 || $removed_redirects > 0) { |
| 355 | update_option('permalink-manager-uris', array_filter($permalink_manager_uris)); |
| 356 | update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects)); |
| 357 | |
| 358 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(sprintf(__( '%d Custom URIs and %d Custom Redirects were removed!', 'permalink-manager' ), $removed_uris, $removed_redirects), 'updated updated_slugs'); |
| 359 | } else { |
| 360 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'No Custom URIs or Custom Redirects were removed!', 'permalink-manager' ), 'error updated_slugs'); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Check if the post/term uses the same URI for both permalink & custom redirects |
| 366 | */ |
| 367 | public static function clear_single_element_duplicated_redirect($element_id, $count_removed = false, $uri = null) { |
| 368 | global $permalink_manager_uris, $permalink_manager_redirects; |
| 369 | |
| 370 | $custom_uri = (empty($uri) && !empty($permalink_manager_uris[$element_id])) ? $permalink_manager_uris[$element_id] : $uri; |
| 371 | |
| 372 | if($custom_uri && !empty($permalink_manager_redirects[$element_id]) && in_array($custom_uri, $permalink_manager_redirects[$element_id])) { |
| 373 | $duplicated_redirect_id = array_search($custom_uri, $permalink_manager_redirects[$element_id]); |
| 374 | unset($permalink_manager_redirects[$element_id][$duplicated_redirect_id]); |
| 375 | } |
| 376 | |
| 377 | // Check if function should only return the counts or update |
| 378 | if($count_removed) { |
| 379 | return (isset($duplicated_redirect_id)) ? 1 : 0; |
| 380 | } else if(isset($duplicated_redirect_id)) { |
| 381 | update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects)); |
| 382 | return true; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Remove unused custom URI & redirects for deleted post or term |
| 388 | */ |
| 389 | public static function clear_single_element_uris_and_redirects($element_id, $count_removed = false) { |
| 390 | global $wpdb, $permalink_manager_uris, $permalink_manager_redirects; |
| 391 | |
| 392 | // Count removed URIs & redirects |
| 393 | $removed_uris = 0; |
| 394 | $removed_redirects = 0; |
| 395 | |
| 396 | // Only admin users can remove the broken URIs for removed post types & taxonomies |
| 397 | $check_if_exists = (is_admin()) ? true : false; |
| 398 | |
| 399 | // 1. Check if element exists |
| 400 | if(strpos($element_id, 'tax-') !== false) { |
| 401 | $term_id = preg_replace("/[^0-9]/", "", $element_id); |
| 402 | $taxonomy = $wpdb->get_var($wpdb->prepare("SELECT t.taxonomy FROM $wpdb->term_taxonomy AS t WHERE t.term_id = %s LIMIT 1", $term_id)); |
| 403 | |
| 404 | // Remove custom URIs for removed terms or disabled taxonomies |
| 405 | $remove = (!empty($taxonomy)) ? Permalink_Manager_Helper_Functions::is_disabled($taxonomy, 'taxonomy', $check_if_exists) : true; |
| 406 | } else if(is_numeric($element_id)) { |
| 407 | $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->prefix}posts WHERE ID = {$element_id} AND post_status NOT IN ('auto-draft', 'trash') AND post_type != 'nav_menu_item'"); |
| 408 | |
| 409 | // Remove custom URIs for removed, auto-draft posts or disabled post types |
| 410 | $remove = (!empty($post_type)) ? Permalink_Manager_Helper_Functions::is_disabled($post_type, 'post_type', $check_if_exists) : true; |
| 411 | |
| 412 | // Remove custom URIs for attachments redirected with Yoast's SEO Premium |
| 413 | $yoast_permalink_options = (class_exists('WPSEO_Premium')) ? get_option('wpseo_permalinks') : array(); |
| 414 | |
| 415 | if(!empty($yoast_permalink_options['redirectattachment']) && $post_type == 'attachment') { |
| 416 | $attachment_parent = $wpdb->get_var("SELECT post_parent FROM {$wpdb->prefix}posts WHERE ID = {$element_id} AND post_type = 'attachment'"); |
| 417 | if(!empty($attachment_parent)) { |
| 418 | $remove = true; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // 2A. Remove ALL unused custom permalinks & redirects |
| 424 | if(!empty($remove)) { |
| 425 | // Remove URI |
| 426 | if(!empty($permalink_manager_uris[$element_id])) { |
| 427 | $removed_uris = 1; |
| 428 | unset($permalink_manager_uris[$element_id]); |
| 429 | } |
| 430 | |
| 431 | // Remove all custom redirects |
| 432 | if(!empty($permalink_manager_redirects[$element_id]) && is_array($permalink_manager_redirects[$element_id])) { |
| 433 | $removed_redirects = count($permalink_manager_redirects[$element_id]); |
| 434 | unset($permalink_manager_redirects[$element_id]);; |
| 435 | } |
| 436 | } |
| 437 | // 2B. Check if the post/term uses the same URI for both permalink & custom redirects |
| 438 | else { |
| 439 | $removed_redirect = self::clear_single_element_duplicated_redirect($element_id, true); |
| 440 | $removed_redirects = (!empty($removed_redirect)) ? 1 : 0; |
| 441 | } |
| 442 | |
| 443 | // Check if function should only return the counts or update |
| 444 | if($count_removed) { |
| 445 | return array($removed_uris, $removed_redirects); |
| 446 | } else if(!empty($removed_uris) || !empty($removed_redirects)) { |
| 447 | update_option('permalink-manager-uris', array_filter($permalink_manager_uris)); |
| 448 | update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects)); |
| 449 | return true; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Make the redirects unique |
| 455 | */ |
| 456 | public static function clear_redirects_array($count_removed = false) { |
| 457 | global $permalink_manager_redirects; |
| 458 | |
| 459 | $removed_redirects = 0; |
| 460 | |
| 461 | $all_redirect_duplicates = Permalink_Manager_Helper_Functions::get_all_duplicates(true); |
| 462 | |
| 463 | foreach($all_redirect_duplicates as $single_redirect_duplicate) { |
| 464 | $last_element = reset($single_redirect_duplicate); |
| 465 | |
| 466 | foreach($single_redirect_duplicate as $redirect_key) { |
| 467 | // Keep a single redirect |
| 468 | if($last_element == $redirect_key) { continue; } |
| 469 | preg_match("/redirect-([\d]+)_((?:tax-)?(?:[\d]+))/", $redirect_key, $ids); |
| 470 | |
| 471 | if(!empty($ids[2]) && !empty($permalink_manager_redirects[$ids[2]][$ids[1]])) { |
| 472 | $removed_redirects++; |
| 473 | unset($permalink_manager_redirects[$ids[2]][$ids[1]]); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // Check if function should only return the counts or update |
| 479 | if($count_removed) { |
| 480 | return $removed_redirects; |
| 481 | } else if(isset($duplicated_redirect_id)) { |
| 482 | update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects)); |
| 483 | return true; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Remove custom URI & redirects for any requested post or term |
| 489 | */ |
| 490 | public static function force_clear_single_element_uris_and_redirects($uri_key) { |
| 491 | global $permalink_manager_uris, $permalink_manager_redirects, $permalink_manager_before_sections_html; |
| 492 | |
| 493 | // Check if custom URI is set |
| 494 | if(isset($permalink_manager_uris[$uri_key])) { |
| 495 | $uri = $permalink_manager_uris[$uri_key]; |
| 496 | |
| 497 | unset($permalink_manager_uris[$uri_key]); |
| 498 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 499 | |
| 500 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(sprintf(__( 'URI "%s" was removed successfully!', 'permalink-manager' ), $uri), 'updated'); |
| 501 | $updated = true; |
| 502 | } |
| 503 | |
| 504 | // Check if custom redirects are set |
| 505 | if(isset($permalink_manager_redirects[$uri_key])) { |
| 506 | unset($permalink_manager_redirects[$uri_key]); |
| 507 | update_option('permalink-manager-redirects', $permalink_manager_redirects); |
| 508 | |
| 509 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'Broken redirects were removed successfully!', 'permalink-manager' ), 'updated'); |
| 510 | $updated = true; |
| 511 | } |
| 512 | |
| 513 | if(empty($updated)) { |
| 514 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'URI and/or custom redirects does not exist or were already removed!', 'permalink-manager' ), 'error'); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | public static function force_clear_single_redirect($redirect_key) { |
| 519 | global $permalink_manager_redirects, $permalink_manager_before_sections_html; |
| 520 | |
| 521 | preg_match("/redirect-([\d]+)_((?:tax-)?(?:[\d]+))/", $redirect_key, $ids); |
| 522 | |
| 523 | if(!empty($permalink_manager_redirects[$ids[2]][$ids[1]])) { |
| 524 | unset($permalink_manager_redirects[$ids[2]][$ids[1]]); |
| 525 | |
| 526 | update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects)); |
| 527 | |
| 528 | $permalink_manager_before_sections_html = Permalink_Manager_Admin_Functions::get_alert_message(__( 'The redirect was removed successfully!', 'permalink-manager' ), 'updated'); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Keep the permalinks unique |
| 534 | */ |
| 535 | public static function fix_uri_duplicates() { |
| 536 | global $permalink_manager_uris; |
| 537 | |
| 538 | $duplicates = array_count_values($permalink_manager_uris); |
| 539 | |
| 540 | foreach($duplicates as $uri => $count) { |
| 541 | if($count == 1) { continue; } |
| 542 | |
| 543 | $ids = array_keys($permalink_manager_uris, $uri); |
| 544 | foreach($ids as $index => $id) { |
| 545 | if($index > 0) { |
| 546 | $permalink_manager_uris[$id] = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $index . '$2', $uri); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Clear sitemaps cache |
| 556 | */ |
| 557 | function flush_sitemaps($types = array()) { |
| 558 | global $permalink_manager_before_sections_html; |
| 559 | |
| 560 | if(class_exists('WPSEO_Sitemaps_Cache')) { |
| 561 | $sitemaps = WPSEO_Sitemaps_Cache::clear($types); |
| 562 | |
| 563 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'Sitemaps were updated!', 'permalink-manager' ), 'updated'); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Import old URIs from "Custom Permalinks" (Pro) |
| 569 | */ |
| 570 | function import_custom_permalinks_uris() { |
| 571 | Permalink_Manager_Third_Parties::import_custom_permalinks_uris(); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * "Automatically remove duplicates" (if enabled) in background |
| 576 | */ |
| 577 | function clean_permalinks_hook() { |
| 578 | global $permalink_manager_uris, $permalink_manager_redirects; |
| 579 | |
| 580 | // Backup the custom URIs |
| 581 | if(is_array($permalink_manager_uris)) { |
| 582 | update_option('permalink-manager-uris_backup', $permalink_manager_uris); |
| 583 | } |
| 584 | // Backup the custom redirects |
| 585 | if(is_array($permalink_manager_redirects)) { |
| 586 | update_option('permalink-manager-redirects_backup', $permalink_manager_redirects); |
| 587 | } |
| 588 | |
| 589 | self::clear_all_uris(); |
| 590 | } |
| 591 | |
| 592 | function clean_permalinks_cronjob() { |
| 593 | global $permalink_manager_options; |
| 594 | |
| 595 | $event_name = 'clean_permalinks_event'; |
| 596 | |
| 597 | // Set-up the "Automatically remove duplicates" function that runs in background once a day |
| 598 | if(!empty($permalink_manager_options['general']['auto_remove_duplicates'])) { |
| 599 | if(!wp_next_scheduled($event_name)) { |
| 600 | wp_schedule_event(time(), 'daily', $event_name); |
| 601 | } |
| 602 | } else if(wp_next_scheduled($event_name)) { |
| 603 | $event_timestamp = wp_next_scheduled($event_name); |
| 604 | wp_unschedule_event($event_timestamp, $event_name); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | } |
| 609 |