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