PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Pro / 2.2.9.7
Permalink Manager Pro v2.2.9.7
2.5.4 2.5.3.4 2.2.18 2.2.19.2 2.2.19.3 2.2.19.3.1 2.2.2 2.2.20 2.2.20.1 2.2.20.3 2.2.4 2.2.5 2.2.6 2.2.7.2 2.2.7.3 2.2.7.5 2.2.7.6 2.2.8.4 2.2.8.5 2.2.8.6 2.2.8.7 2.2.8.9 2.2.9.1 2.2.9.2 2.2.9.2.1 2.2.9.3 2.2.9.4 2.2.9.6 2.2.9.7 2.2.9.9 2.3.0 2.3.1.1 2.4.0 2.4.1 2.4.1.2 2.4.1.3 2.4.1.4 2.4.1.5 2.4.1.6 2.4.2 2.4.2.1 2.4.3 2.4.3.1 2.4.3.2 2.4.3.3 2.4.3.4 2.4.4 2.4.4.1 2.4.4.2 2.4.4.3 2.5.0 2.5.1 2.5.1.1 2.5.1.2 2.5.1.3 2.5.1.4 2.5.2 2.5.2.1 2.5.2.2 2.5.2.3 2.5.2.4 2.5.3 2.5.3.1 2.5.3.2 2.5.3.3 trunk 0.2 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 0.4.4 0.4.6 0.4.7 0.4.8 0.4.9 0.5.3 0.5.4 1.0.0 1.0.1 1.0.4 1.1.0 1.1.1 1.1.2 1.11.6.3 2.0.0 2.0.3 2.0.4 2.0.4.3 2.0.5.1 2.0.5.2 2.0.5.3 2.0.5.3.1 2.0.5.4 2.0.5.4a 2.0.5.5 2.0.5.6 2.0.5.6.1 2.0.5.7 2.0.5.9a 2.0.6.2.1 2.0.6.2a 2.0.6.3 2.1.0 2.1.1 2.1.2.4 2.2.0 2.2.1.1 2.2.1.2 2.2.11 2.2.12 2.2.13.1 2.2.14 2.2.15.1 2.2.16 2.2.17
permalink-manager / includes / core / permalink-manager-actions.php
permalink-manager / includes / core Last commit date
permalink-manager-actions.php 5 years ago permalink-manager-admin-functions.php 5 years ago permalink-manager-core-functions.php 5 years ago permalink-manager-debug.php 5 years ago permalink-manager-gutenberg.php 5 years ago permalink-manager-helper-functions.php 5 years ago permalink-manager-language-plugins.php 5 years ago permalink-manager-third-parties.php 5 years ago permalink-manager-uri-functions-post.php 5 years ago permalink-manager-uri-functions.php 5 years ago
permalink-manager-actions.php
705 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 if(is_admin()) {
13 add_action('wp_ajax_pm_bulk_tools', array($this, 'pm_bulk_tools'));
14 add_action('wp_ajax_pm_save_permalink', array($this, 'pm_save_permalink'));
15 add_action('wp_ajax_pm_detect_duplicates', array($this, 'ajax_detect_duplicates') );
16 }
17
18 add_action('clean_permalinks_event', array($this, 'clean_permalinks_hook'));
19 add_action('init', array($this, 'clean_permalinks_cronjob'));
20 }
21
22 /**
23 * Actions
24 */
25 public function trigger_action() {
26 global $permalink_manager_after_sections_html;
27
28 // 1. Check if the form was submitted
29 if(empty($_POST)) {
30 return;
31 }
32
33 // 2. Do nothing if search query is not empty
34 if(isset($_REQUEST['search-submit']) || isset($_REQUEST['months-filter-button'])) {
35 return;
36 }
37
38 $actions_map = array(
39 'uri_editor' => array('function' => 'update_all_permalinks', 'display_uri_table' => true),
40 'permalink_manager_options' => array('function' => 'save_settings'),
41 'permalink_manager_permastructs' => array('function' => 'save_permastructures'),
42 'flush_sitemaps' => array('function' => 'flush_sitemaps'),
43 'import' => array('function' => 'import_custom_permalinks_uris'),
44 );
45
46 // 3. Find the action
47 foreach($actions_map as $action => $map) {
48 if(isset($_POST[$action]) && wp_verify_nonce($_POST[$action], 'permalink-manager')) {
49 // Execute the function
50 $output = call_user_func(array($this, $map['function']));
51
52 // Get list of updated URIs
53 if(!empty($map['display_uri_table'])) {
54 $updated_slugs_count = (isset($output['updated_count']) && $output['updated_count'] > 0) ? $output['updated_count'] : false;
55 $updated_slugs_array = ($updated_slugs_count) ? $output['updated'] : '';
56 }
57
58 // Trigger only one function
59 break;
60 }
61 }
62
63 // 4. Display the slugs table (and append the globals)
64 if(isset($updated_slugs_count)) {
65 $permalink_manager_after_sections_html .= Permalink_Manager_Admin_Functions::display_updated_slugs($updated_slugs_array);
66 }
67 }
68
69 /**
70 * Save settings
71 */
72 public static function save_settings($field = false, $value = false, $display_alert = true) {
73 global $permalink_manager_options, $permalink_manager_before_sections_html;
74
75 // Info: The settings array is used also by "Screen Options"
76 $new_options = $permalink_manager_options;
77 //$new_options = array();
78
79 // Save only selected field/sections
80 if($field && $value) {
81 $new_options[$field] = $value;
82 } else {
83 $post_fields = $_POST;
84
85 foreach($post_fields as $option_name => $option_value) {
86 $new_options[$option_name] = $option_value;
87 }
88 }
89
90 // Allow only white-listed option groups
91 foreach($new_options as $group => $group_options) {
92 if(!in_array($group, array('licence', 'screen-options', 'general', 'permastructure-settings', 'stop-words'))) {
93 unset($new_options[$group]);
94 }
95 }
96
97 // Sanitize & override the global with new settings
98 $new_options = Permalink_Manager_Helper_Functions::sanitize_array($new_options);
99 $permalink_manager_options = $new_options = array_filter($new_options);
100
101 // Save the settings in database
102 update_option('permalink-manager', $new_options);
103
104 // Display the message
105 $permalink_manager_before_sections_html .= ($display_alert) ? Permalink_Manager_Admin_Functions::get_alert_message(__( 'The settings are saved!', 'permalink-manager' ), 'updated') : "";
106 }
107
108 /**
109 * Trigger bulk tools via AJAX
110 */
111 function pm_bulk_tools() {
112 global $sitepress, $wp_filter, $wpdb;
113
114 // Define variables
115 $return = array('alert' => Permalink_Manager_Admin_Functions::get_alert_message(__( '<strong>No slugs</strong> were updated!', 'permalink-manager' ), 'error updated_slugs'));
116
117 // Get the name of the function
118 if(isset($_POST['regenerate']) && wp_verify_nonce($_POST['regenerate'], 'permalink-manager')) {
119 $function_name = 'regenerate_all_permalinks';
120 $uniq_id = $_POST['pm_session_id'];
121 } else if(isset($_POST['find_and_replace']) && wp_verify_nonce($_POST['find_and_replace'], 'permalink-manager') && !empty($_POST['old_string']) && !empty($_POST['new_string'])) {
122 $function_name = 'find_and_replace';
123 $uniq_id = $_POST['pm_session_id'];
124 }
125
126 // Check if both strings are set for "Find and replace" tool
127 if(!empty($function_name)) {
128 // Hotfix for WPML (start)
129 if($sitepress) {
130 remove_filter('get_terms_args', array($sitepress, 'get_terms_args_filter'), 10);
131 remove_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1);
132 remove_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10);
133 remove_filter('get_pages', array($sitepress, 'get_pages_adjust_ids'), 1);
134 }
135
136 // Get the mode
137 $mode = isset($_POST['mode']) ? $_POST['mode'] : 'custom_uris';
138
139 // Get the content type
140 if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') {
141 $class_name = 'Permalink_Manager_URI_Functions_Tax';
142 } else {
143 $class_name = 'Permalink_Manager_URI_Functions_Post';
144 }
145
146 // Get items (try to get them from transient)
147 $items = get_transient("pm_{$uniq_id}");
148 $progress = get_transient("pm_{$uniq_id}_progress");
149
150 $first_chunk = true;
151 $chunk_size = apply_filters('permalink_manager_chunk_size', 50);
152
153 if(empty($items)) {
154 $items = $class_name::get_items();
155
156 if(!empty($items)) {
157 // Set stats (to display the progress)
158 $total = count($items);
159
160 // Split items array into chunks and save them to transient
161 $items = array_chunk($items, $chunk_size);
162
163 set_transient("pm_{$uniq_id}_progress", 0, 300);
164 set_transient("pm_{$uniq_id}", $items, 300);
165
166 // Check for MySQL errors
167 if(!empty($wpdb->last_error)) {
168 printf('%s (%sMB)', $wpdb->last_error, strlen(serialize($items)) / 1000000);
169 http_response_code(500);
170 die();
171 }
172 }
173 }
174
175 // Get homepage URL and ensure that it ends with slash
176 $home_url = Permalink_Manager_Helper_Functions::get_permalink_base() . "/";
177
178 // Process the variables from $_POST object
179 $old_string = (!empty($_POST['old_string'])) ? str_replace($home_url, '', esc_sql($_POST['old_string'])) : '';
180 $new_string = (!empty($_POST['old_string'])) ? str_replace($home_url, '', esc_sql($_POST['new_string'])) : '';
181
182 // Process only one subarray
183 if(!empty($items[0])) {
184 $chunk = array_shift($items);
185 set_transient("pm_{$uniq_id}", $items, 300);
186
187 // Check if posts or terms should be updated
188 if($function_name == 'find_and_replace') {
189 $output = $class_name::find_and_replace($chunk, $mode, $old_string, $new_string);
190 } else {
191 $output = $class_name::regenerate_all_permalinks($chunk, $mode);
192 }
193
194 if(!empty($output['updated_count'])) {
195 $return = array_merge($return, (array) Permalink_Manager_Admin_Functions::display_updated_slugs($output['updated'], true, $first_chunk));
196 $return['updated_count'] = $output['updated_count'];
197 }
198
199 // Send total number of processed items with a first chunk
200 if(!empty($total)) {
201 $return['total'] = $total;
202 }
203
204 // Check if there are any chunks left
205 if(count($items) > 0) {
206 // Update progress
207 $progress += $chunk_size;
208 set_transient("pm_{$uniq_id}_progress", $progress, 300);
209
210 $return['left_chunks'] = true;
211 $return['progress'] = $progress;
212 } else {
213 delete_transient("pm_{$uniq_id}");
214 delete_transient("pm_{$uniq_id}_progress");
215 }
216 }
217
218 // Hotfix for WPML (end)
219 if($sitepress) {
220 add_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10, 4);
221 add_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1, 1);
222 add_filter('get_terms_args', array($sitepress, 'get_terms_args_filter'), 10, 2);
223 add_filter('get_pages', array($sitepress, 'get_pages_adjust_ids'), 1, 2);
224 }
225 }
226
227 wp_send_json($return);
228 die();
229 }
230
231 /**
232 * Save permalink via AJAX
233 */
234 public function pm_save_permalink() {
235 $element_id = (!empty($_POST['permalink-manager-edit-uri-element-id'])) ? sanitize_text_field($_POST['permalink-manager-edit-uri-element-id']) : '';
236
237 if(!empty($element_id) && is_numeric($element_id)) {
238 Permalink_Manager_URI_Functions_Post::update_post_uri($element_id);
239
240 // Reload URI Editor & clean post cache
241 clean_post_cache($element_id);
242 $element = get_post($element_id);
243 $html = Permalink_Manager_Admin_Functions::display_uri_box($element, true);
244
245 echo $html;
246 die();
247 }
248 }
249
250 /**
251 * Update all permalinks in "Permalink Editor"
252 */
253 function update_all_permalinks() {
254 // Check if posts or terms should be updated
255 if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') {
256 return Permalink_Manager_URI_Functions_Tax::update_all_permalinks();
257 } else {
258 return Permalink_Manager_URI_Functions_Post::update_all_permalinks();
259 }
260 }
261
262 /**
263 * Additional actions
264 */
265 public static function extra_actions() {
266 if(isset($_GET['flush_sitemaps'])) {
267 self::flush_sitemaps();
268 } else if(isset($_GET['clear-permalink-manager-uris'])) {
269 self::clear_all_uris();
270 } else if(isset($_GET['remove-permalink-manager-settings'])) {
271 $option_name = sanitize_text_field($_GET['remove-permalink-manager-settings']);
272 self::remove_plugin_data($option_name);
273 } else if(!empty($_REQUEST['remove-uri'])) {
274 $uri_key = sanitize_text_field($_REQUEST['remove-uri']);
275 self::force_clear_single_element_uris_and_redirects($uri_key);
276 } else if(!empty($_REQUEST['remove-redirect'])) {
277 $redirect_key = sanitize_text_field($_REQUEST['remove-redirect']);
278 self::force_clear_single_redirect($redirect_key);
279 } else if(!empty($_POST['screen-options-apply'])) {
280 self::save_screen_options();
281 }
282 }
283
284 /**
285 * Save "Screen Options"
286 */
287 public static function save_screen_options() {
288 check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
289
290 // The values will be sanitized inside the function
291 self::save_settings('screen-options', $_POST['screen-options']);
292 }
293
294 /**
295 * Save permastructures
296 */
297 public static function save_permastructures() {
298 global $permalink_manager_permastructs;
299
300 $post_fields = $_POST;
301 $permastructure_options = $permastructures = array();
302 $permastructure_types = array('post_types', 'taxonomies');
303
304 // Split permastructures & sanitize them
305 foreach($permastructure_types as $type) {
306 if(empty($_POST[$type]) || !is_array($_POST[$type])) { continue; }
307
308 $permastructures[$type] = $_POST[$type];
309
310 foreach($permastructures[$type] as &$single_permastructure) {
311 $single_permastructure = Permalink_Manager_Helper_Functions::sanitize_title($single_permastructure, true, false, false);
312 $single_permastructure = trim($single_permastructure, '\/ ');
313 }
314 }
315
316 if(!empty($_POST['permastructure-settings'])) {
317 $permastructure_options = $_POST['permastructure-settings'];
318 }
319
320 // A. Permastructures
321 if(!empty($permastructures['post_types']) || !empty($permastructures['taxonomies'])) {
322 // Override the global with settings
323 $permalink_manager_permastructs = $permastructures;
324
325 // Save the settings in database
326 update_option('permalink-manager-permastructs', $permastructures);
327 }
328
329 // B. Permastructure settings
330 if(!empty($permastructure_options)) {
331 self::save_settings('permastructure-settings', $permastructure_options);
332 }
333 }
334
335 /**
336 * Clear URIs
337 */
338 public static function clear_all_uris() {
339 global $permalink_manager_uris, $permalink_manager_redirects, $wpdb, $permalink_manager_before_sections_html;
340
341 // Check if array with custom URIs exists
342 if(empty($permalink_manager_uris)) { return; }
343
344 // Count removed URIs & redirects
345 $removed_uris = 0;
346 $removed_redirects = 0;
347
348 // Get all element IDs
349 $element_ids = array_merge(array_keys((array) $permalink_manager_uris), array_keys((array) $permalink_manager_redirects));
350
351 // 1. Remove unused custom URI & redirects for deleted post or term
352 foreach($element_ids as $element_id) {
353 $count = self::clear_single_element_uris_and_redirects($element_id, true);
354
355 $removed_uris = (!empty($count[0])) ? $count[0] + $removed_uris : $removed_uris;
356 $removed_redirects = (!empty($count[1])) ? $count[1] + $removed_redirects : $removed_redirects;
357 }
358
359 // 2. Keep only a single redirect (make it unique)
360 $removed_redirects += self::clear_redirects_array(true);
361
362 // 3. Optional method to keep the permalinks unique
363 if(apply_filters('permalink_manager_fix_uri_duplicates', false) == true) {
364 self::fix_uri_duplicates();
365 }
366
367 // 4. Remove items without keys
368 /*if(!empty($permalink_manager_uris[null])) {
369 unset($permalink_manager_uris[null]);
370 }*/
371
372 // Save cleared URIs & Redirects
373 if($removed_uris > 0 || $removed_redirects > 0) {
374 update_option('permalink-manager-uris', array_filter($permalink_manager_uris));
375 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
376
377 $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');
378 } else {
379 $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');
380 }
381 }
382
383 /**
384 * Remove plugin data
385 */
386 public static function remove_plugin_data($field_name) {
387 global $permalink_manager, $permalink_manager_before_sections_html;
388
389 // Make sure that the user is allowed to remove the plugin data
390 if(!current_user_can('manage_options')) {
391 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'You are not allowed to remove Permalink Manager data!', 'permalink-manager' ), 'error updated_slugs');
392 }
393
394 switch($field_name) {
395 case 'uris' :
396 $option_name = 'permalink-manager-uris';
397 $alert = __('Custom permalinks', 'permalink-manager');
398 break;
399 case 'redirects' :
400 $option_name = 'permalink-manager-redirects';
401 $alert = __('Custom redirects', 'permalink-manager');
402 break;
403 case 'external-redirects' :
404 $option_name = 'permalink-manager-external-redirects';
405 $alert = __('External redirects', 'permalink-manager');
406 break;
407 case 'permastructs' :
408 $option_name = 'permalink-manager-permastructs';
409 $alert = __('Permastructure settings', 'permalink-manager');
410 break;
411 case 'settings' :
412 $option_name = 'permalink-manager';
413 $alert = __('Permastructure settings', 'permalink-manager');
414 break;
415 }
416
417 if(!empty($option_name)) {
418 // Remove the option from DB
419 delete_option($option_name);
420
421 // Reload globals
422 $permalink_manager->get_options_and_globals();
423
424 $alert_message = sprintf(__('%s were removed!', 'permalink-manager'), $alert);
425 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message($alert_message, 'updated updated_slugs');
426 }
427 }
428
429 /**
430 * Check if the post/term uses the same URI for both permalink & custom redirects
431 */
432 public static function clear_single_element_duplicated_redirect($element_id, $count_removed = false, $uri = null) {
433 global $permalink_manager_uris, $permalink_manager_redirects;
434
435 $custom_uri = (empty($uri) && !empty($permalink_manager_uris[$element_id])) ? $permalink_manager_uris[$element_id] : $uri;
436
437 if($custom_uri && !empty($permalink_manager_redirects[$element_id]) && in_array($custom_uri, $permalink_manager_redirects[$element_id])) {
438 $duplicated_redirect_id = array_search($custom_uri, $permalink_manager_redirects[$element_id]);
439 unset($permalink_manager_redirects[$element_id][$duplicated_redirect_id]);
440 }
441
442 // Check if function should only return the counts or update
443 if($count_removed) {
444 return (isset($duplicated_redirect_id)) ? 1 : 0;
445 } else if(isset($duplicated_redirect_id)) {
446 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
447 return true;
448 }
449 }
450
451 /**
452 * Remove unused custom URI & redirects for deleted post or term
453 */
454 public static function clear_single_element_uris_and_redirects($element_id, $count_removed = false) {
455 global $wpdb, $permalink_manager_uris, $permalink_manager_redirects;
456
457 // Count removed URIs & redirects
458 $removed_uris = 0;
459 $removed_redirects = 0;
460
461 // Only admin users can remove the broken URIs for removed post types & taxonomies
462 $check_if_exists = (is_admin()) ? true : false;
463
464 // 1. Check if element exists
465 if(strpos($element_id, 'tax-') !== false) {
466 $term_id = preg_replace("/[^0-9]/", "", $element_id);
467 $taxonomy = $wpdb->get_var($wpdb->prepare("SELECT t.taxonomy FROM $wpdb->term_taxonomy AS t WHERE t.term_id = %s LIMIT 1", $term_id));
468
469 // Remove custom URIs for removed terms or disabled taxonomies
470 $remove = (!empty($taxonomy)) ? Permalink_Manager_Helper_Functions::is_disabled($taxonomy, 'taxonomy', $check_if_exists) : true;
471 } else if(is_numeric($element_id)) {
472 $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'");
473
474 // Remove custom URIs for removed, auto-draft posts or disabled post types
475 $remove = (!empty($post_type)) ? Permalink_Manager_Helper_Functions::is_disabled($post_type, 'post_type', $check_if_exists) : true;
476
477 // Remove custom URIs for attachments redirected with Yoast's SEO Premium
478 $yoast_permalink_options = (class_exists('WPSEO_Premium')) ? get_option('wpseo_permalinks') : array();
479
480 if(!empty($yoast_permalink_options['redirectattachment']) && $post_type == 'attachment') {
481 $attachment_parent = $wpdb->get_var("SELECT post_parent FROM {$wpdb->prefix}posts WHERE ID = {$element_id} AND post_type = 'attachment'");
482 if(!empty($attachment_parent)) {
483 $remove = true;
484 }
485 }
486 }
487
488 // 2A. Remove ALL unused custom permalinks & redirects
489 if(!empty($remove)) {
490 // Remove URI
491 if(!empty($permalink_manager_uris[$element_id])) {
492 $removed_uris = 1;
493 unset($permalink_manager_uris[$element_id]);
494 }
495
496 // Remove all custom redirects
497 if(!empty($permalink_manager_redirects[$element_id]) && is_array($permalink_manager_redirects[$element_id])) {
498 $removed_redirects = count($permalink_manager_redirects[$element_id]);
499 unset($permalink_manager_redirects[$element_id]);;
500 }
501 }
502 // 2B. Check if the post/term uses the same URI for both permalink & custom redirects
503 else {
504 $removed_redirect = self::clear_single_element_duplicated_redirect($element_id, true);
505 $removed_redirects = (!empty($removed_redirect)) ? 1 : 0;
506 }
507
508 // Check if function should only return the counts or update
509 if($count_removed) {
510 return array($removed_uris, $removed_redirects);
511 } else if(!empty($removed_uris) || !empty($removed_redirects)) {
512 update_option('permalink-manager-uris', array_filter($permalink_manager_uris));
513 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
514 return true;
515 }
516 }
517
518 /**
519 * Make the redirects unique
520 */
521 public static function clear_redirects_array($count_removed = false) {
522 global $permalink_manager_redirects;
523
524 $removed_redirects = 0;
525
526 $all_redirect_duplicates = Permalink_Manager_Helper_Functions::get_all_duplicates(true);
527
528 foreach($all_redirect_duplicates as $single_redirect_duplicate) {
529 $last_element = reset($single_redirect_duplicate);
530
531 foreach($single_redirect_duplicate as $redirect_key) {
532 // Keep a single redirect
533 if($last_element == $redirect_key) { continue; }
534 preg_match("/redirect-([\d]+)_((?:tax-)?(?:[\d]+))/", $redirect_key, $ids);
535
536 if(!empty($ids[2]) && !empty($permalink_manager_redirects[$ids[2]][$ids[1]])) {
537 $removed_redirects++;
538 unset($permalink_manager_redirects[$ids[2]][$ids[1]]);
539 }
540 }
541 }
542
543 // Check if function should only return the counts or update
544 if($count_removed) {
545 return $removed_redirects;
546 } else if(isset($duplicated_redirect_id)) {
547 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
548 return true;
549 }
550 }
551
552 /**
553 * Remove custom URI & redirects for any requested post or term
554 */
555 public static function force_clear_single_element_uris_and_redirects($uri_key) {
556 global $permalink_manager_uris, $permalink_manager_redirects, $permalink_manager_before_sections_html;
557
558 // Check if custom URI is set
559 if(isset($permalink_manager_uris[$uri_key])) {
560 $uri = $permalink_manager_uris[$uri_key];
561
562 unset($permalink_manager_uris[$uri_key]);
563 update_option('permalink-manager-uris', $permalink_manager_uris);
564
565 $updated = Permalink_Manager_Admin_Functions::get_alert_message(sprintf(__( 'URI "%s" was removed successfully!', 'permalink-manager' ), $uri), 'updated');
566 }
567
568 // Check if custom redirects are set
569 if(isset($permalink_manager_redirects[$uri_key])) {
570 unset($permalink_manager_redirects[$uri_key]);
571 update_option('permalink-manager-redirects', $permalink_manager_redirects);
572
573 $updated = Permalink_Manager_Admin_Functions::get_alert_message(__( 'Broken redirects were removed successfully!', 'permalink-manager' ), 'updated');
574 }
575
576 if(empty($updated)) {
577 $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');
578 } else {
579 // Display the alert in admin panel
580 if(!empty($permalink_manager_before_sections_html) && is_admin()) {
581 $permalink_manager_before_sections_html .= $updated;
582 }
583 return true;
584 }
585 }
586
587 public static function force_clear_single_redirect($redirect_key) {
588 global $permalink_manager_redirects, $permalink_manager_before_sections_html;
589
590 preg_match("/redirect-([\d]+)_((?:tax-)?(?:[\d]+))/", $redirect_key, $ids);
591
592 if(!empty($permalink_manager_redirects[$ids[2]][$ids[1]])) {
593 unset($permalink_manager_redirects[$ids[2]][$ids[1]]);
594
595 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
596
597 $permalink_manager_before_sections_html = Permalink_Manager_Admin_Functions::get_alert_message(__( 'The redirect was removed successfully!', 'permalink-manager' ), 'updated');
598 }
599 }
600
601 /**
602 * Keep the permalinks unique
603 */
604 public static function fix_uri_duplicates() {
605 global $permalink_manager_uris;
606
607 $duplicates = array_count_values($permalink_manager_uris);
608
609 foreach($duplicates as $uri => $count) {
610 if($count == 1) { continue; }
611
612 $ids = array_keys($permalink_manager_uris, $uri);
613 foreach($ids as $index => $id) {
614 if($index > 0) {
615 $permalink_manager_uris[$id] = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $index . '$2', $uri);
616 }
617 }
618 }
619
620 update_option('permalink-manager-uris', $permalink_manager_uris);
621 }
622
623 /**
624 * Check if URI was used before
625 */
626 function ajax_detect_duplicates($uri = null, $element_id = null) {
627 $duplicate_alert = __("URI is already in use, please select another one!", "permalink-manager");
628
629 if(!empty($_REQUEST['custom_uris'])) {
630 // Sanitize the array
631 $custom_uris = Permalink_Manager_Helper_Functions::sanitize_array($_REQUEST['custom_uris']);
632 $duplicates_array = array();
633
634 // Check each URI
635 foreach($custom_uris as $element_id => $uri) {
636 $duplicates_array[$element_id] = Permalink_Manager_Helper_Functions::is_uri_duplicated($uri, $element_id) ? $duplicate_alert : 0;
637 }
638
639 // Convert the output to JSON and stop the function
640 echo json_encode($duplicates_array);
641 } else if(!empty($_REQUEST['custom_uri']) && !empty($_REQUEST['element_id'])) {
642 $is_duplicated = Permalink_Manager_Helper_Functions::is_uri_duplicated($uri, $element_id);
643
644 echo ($is_duplicated) ? $duplicate_alert : 0;
645 }
646
647 die();
648 }
649
650 /**
651 * Clear sitemaps cache
652 */
653 function flush_sitemaps($types = array()) {
654 global $permalink_manager_before_sections_html;
655
656 if(class_exists('WPSEO_Sitemaps_Cache')) {
657 $sitemaps = WPSEO_Sitemaps_Cache::clear($types);
658
659 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'Sitemaps were updated!', 'permalink-manager' ), 'updated');
660 }
661 }
662
663 /**
664 * Import old URIs from "Custom Permalinks" (Pro)
665 */
666 function import_custom_permalinks_uris() {
667 Permalink_Manager_Third_Parties::import_custom_permalinks_uris();
668 }
669
670 /**
671 * "Automatically remove duplicates" (if enabled) in background
672 */
673 function clean_permalinks_hook() {
674 global $permalink_manager_uris, $permalink_manager_redirects;
675
676 // Backup the custom URIs
677 if(is_array($permalink_manager_uris)) {
678 update_option('permalink-manager-uris_backup', $permalink_manager_uris, false);
679 }
680 // Backup the custom redirects
681 if(is_array($permalink_manager_redirects)) {
682 update_option('permalink-manager-redirects_backup', $permalink_manager_redirects, false);
683 }
684
685 self::clear_all_uris();
686 }
687
688 function clean_permalinks_cronjob() {
689 global $permalink_manager_options;
690
691 $event_name = 'clean_permalinks_event';
692
693 // Set-up the "Automatically remove duplicates" function that runs in background once a day
694 if(!empty($permalink_manager_options['general']['auto_remove_duplicates']) && $permalink_manager_options['general']['auto_remove_duplicates'] == 2) {
695 if(!wp_next_scheduled($event_name)) {
696 wp_schedule_event(time(), 'daily', $event_name);
697 }
698 } else if(wp_next_scheduled($event_name)) {
699 $event_timestamp = wp_next_scheduled($event_name);
700 wp_unschedule_event($event_timestamp, $event_name);
701 }
702 }
703
704 }
705