PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 2.0.5.3
Permalink Manager Lite v2.0.5.3
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 8 years ago permalink-manager-admin-functions.php 8 years ago permalink-manager-core-functions.php 8 years ago permalink-manager-helper-functions.php 8 years ago permalink-manager-third-parties.php 8 years ago permalink-manager-uri-functions-post.php 8 years ago
permalink-manager-actions.php
360 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'), 999 );
9 add_action( 'admin_init', array($this, 'extra_actions') );
10 }
11
12 /**
13 * Actions
14 */
15 public function trigger_action() {
16 global $permalink_manager_before_sections_html, $permalink_manager_after_sections_html;
17
18 // 1. Check if the form was submitted
19 if(empty($_POST)) { return; }
20
21 $actions_map = array(
22 'uri_editor' => array('function' => 'update_all_permalinks', 'display_uri_table' => true),
23 'regenerate' => array('function' => 'regenerate_all_permalinks', 'display_uri_table' => true),
24 'find_and_replace' => array('function' => 'find_and_replace', 'display_uri_table' => true),
25 'permalink_manager_options' => array('function' => 'save_settings'),
26 'permalink_manager_permastructs' => array('function' => 'save_permastructures'),
27 'flush_sitemaps' => array('function' => 'save_permastructures'),
28 'import' => array('function' => 'import_custom_permalinks_uris'),
29 );
30 // Clear URIs & reset settings & permastructs also should be added here.
31
32 // 2. Find the action
33 foreach($actions_map as $action => $map) {
34 if(isset($_POST[$action]) && wp_verify_nonce($_POST[$action], 'permalink-manager')) {
35 $output = call_user_func(array($this, $map['function']));
36
37 // Get list of updated URIs
38 if(!empty($map['display_uri_table'])) {
39 $updated_slugs_count = (isset($output['updated_count']) && $output['updated_count'] > 0) ? $output['updated_count'] : false;
40 $updated_slugs_array = ($updated_slugs_count) ? $output['updated'] : '';
41 }
42
43 // Trigger only one function
44 break;
45 }
46 }
47
48 // 3. Display the slugs table (and append the globals)
49 if(isset($updated_slugs_count)) {
50 if($updated_slugs_count > 0) {
51 $updated_title = __('List of updated items', 'bis');
52 $alert_content = sprintf( _n( '<strong>%d</strong> slug was updated!', '<strong>%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' ';
53 $alert_content .= sprintf( __( '<a %s>Click here</a> to go to the list of updated slugs', 'permalink-manager' ), "href=\"#updated-list\" title=\"{$updated_title}\" class=\"thickbox\"");
54
55 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message($alert_content, 'updated');
56 $permalink_manager_after_sections_html .= Permalink_Manager_Admin_Functions::display_updated_slugs($updated_slugs_array);
57 } else {
58 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( '<strong>No slugs</strong> were updated!', 'permalink-manager' ), 'error');
59 }
60 }
61 }
62
63 /**
64 * Save settings
65 */
66 public static function save_settings($field = false, $value = false) {
67 global $permalink_manager_options, $permalink_manager_before_sections_html;
68
69 // Info: The settings array is used also by "Screen Options"
70 $new_options = $permalink_manager_options;
71 //$new_options = array();
72
73 // Save only selected field/sections
74 if($field && $value) {
75 $new_options[$field] = $value;
76 } else {
77 $post_fields = $_POST;
78
79 foreach($post_fields as $option_name => $option_value) {
80 $new_options[$option_name] = $option_value;
81 }
82 }
83
84 // Sanitize & override the global with new settings
85 $new_options = Permalink_Manager_Helper_Functions::sanitize_array($new_options);
86 $permalink_manager_options = $new_options = array_filter($new_options);
87
88 // Save the settings in database
89 update_option('permalink-manager', $new_options);
90
91 // Display the message
92 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'The settings are saved!', 'permalink-manager' ), 'updated');
93 }
94
95 /**
96 * Additional actions
97 */
98 public static function extra_actions() {
99 if(isset($_GET['flush_sitemaps'])) {
100 self::flush_sitemaps();
101 } else if(isset($_GET['clear-permalink-manager-uris'])) {
102 self::clear_all_uris();
103 } else if(!empty($_REQUEST['remove-uri'])) {
104 $uri_key = sanitize_text_field($_REQUEST['remove-uri']);
105 self::force_clear_single_element_uris_and_redirects($uri_key);
106 } else if(!empty($_POST['screen-options-apply'])) {
107 self::save_screen_options();
108 }
109 }
110
111 /**
112 * Save "Screen Options"
113 */
114 public static function save_screen_options() {
115 check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
116
117 // The values will be sanitized inside the function
118 self::save_settings('screen-options', $_POST['screen-options']);
119 }
120
121 /**
122 * Save permastructures
123 */
124 public static function save_permastructures() {
125 global $permalink_manager_permastructs;
126
127 $post_fields = $_POST;
128 $new_options = array();
129
130 foreach($post_fields as $option_name => $option_value) {
131 $new_options[$option_name] = $option_value;
132 }
133
134 // Trim the trailing slashes & remove empty permastructures
135 $new_options = Permalink_Manager_Helper_Functions::multidimensional_array_map('untrailingslashit', $new_options);
136 foreach($new_options as $group_name => $group) {
137 if(is_array($group)) {
138 foreach($group as $element => $permastruct) {
139 // Trim slashes
140 $permastruct = trim($permastruct, "/");
141
142 // Do not store default permastructures
143 // $default_permastruct = ($group_name == 'post_types') ? Permalink_Manager_Helper_Functions::get_default_permastruct($element, true) : "";
144 // if($permastruct == $default_permastruct) { unset($group[$element]); }
145 }
146 // Do not store empty permastructures
147 // $new_options[$group_name] = array_filter($group);
148 } else {
149 unset($new_options[$group_name]);
150 }
151 }
152
153 // Override the global with settings
154 // $permalink_manager_permastructs = $new_options = $new_options;
155 $permalink_manager_permastructs = $new_options;
156
157 // Save the settings in database
158 update_option('permalink-manager-permastructs', $new_options);
159 }
160
161 /**
162 * Clear URIs
163 */
164 public static function clear_all_uris() {
165 global $permalink_manager_uris, $permalink_manager_redirects, $wpdb, $permalink_manager_before_sections_html;
166
167 // Check if array with custom URIs exists
168 if(empty($permalink_manager_uris)) { return; }
169
170 // Count removed URIs & redirects
171 $removed_uris = 0;
172 $removed_redirects = 0;
173
174 foreach($permalink_manager_uris as $element_id => $uri) {
175 $count = self::clear_single_element_uris_and_redirects($element_id, true);
176
177 $removed_uris = (!empty($count[0])) ? $count[0] + $removed_uris : $removed_uris;
178 $removed_redirects = (!empty($count[1])) ? $count[1] + $removed_redirects : $removed_redirects;
179 }
180
181 // Save cleared URIs & Redirects
182 if($removed_uris > 0 || $removed_redirects > 0) {
183 update_option('permalink-manager-uris', array_filter($permalink_manager_uris));
184 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
185
186 $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');
187 } else {
188 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'No Custom URIs or Custom Redirects were removed!', 'permalink-manager' ), 'error');
189 }
190 }
191
192 /**
193 * Check if the post/term uses the same URI for both permalink & custom redirects
194 */
195 public static function clear_single_element_duplicated_redirect($element_id, $count_removed = false) {
196 global $permalink_manager_uris, $permalink_manager_redirects;
197
198 if(!empty($permalink_manager_uris[$element_id]) && !empty($permalink_manager_redirects[$element_id])) {
199 $custom_uri = $permalink_manager_uris[$element_id];
200
201 if(in_array($custom_uri, $permalink_manager_redirects[$element_id])) {
202 $duplicated_redirect_id = array_search($custom_uri, $permalink_manager_redirects[$element_id]);
203 unset($permalink_manager_redirects[$element_id][$duplicated_redirect_id]);
204 }
205 }
206
207 // Check if function should only return the counts or update
208 if($count_removed) {
209 return (isset($duplicated_redirect_id)) ? 1 : 0;
210 } else if(isset($duplicated_redirect_id)) {
211 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
212 return true;
213 }
214 }
215
216 /**
217 * Remove unused custom URI & redirects for deleted post or term
218 */
219 public static function clear_single_element_uris_and_redirects($element_id, $count_removed = false) {
220 global $wpdb, $permalink_manager_uris, $permalink_manager_redirects;
221
222 // Count removed URIs & redirects
223 $removed_uris = 0;
224 $removed_redirects = 0;
225
226 // 1. Check if element exists
227 if(is_numeric($element_id)) {
228 $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'");
229
230 // Remove custom URIs for removed, auto-draft posts or disabled post types
231 $remove = (!empty($post_type)) ? Permalink_Manager_Helper_Functions::is_disabled($post_type, 'post_type') : true;
232 } else if(strpos($element_id, 'tax-') !== false) {
233 $term_id = preg_replace("/[^0-9]/", "", $element_id);
234 $taxonomy = $wpdb->get_var($wpdb->prepare("SELECT t.taxonomy FROM $wpdb->term_taxonomy AS t WHERE t.term_id = %s LIMIT 1", $term_id));
235
236 // Remove custom URIs for removed terms or disabled taxonomies
237 $remove = (!empty($taxonomy)) ? Permalink_Manager_Helper_Functions::is_disabled($taxonomy) : true;
238 }
239
240 // 2A. Remove ALL unused custom permalinks & redirects
241 if(!empty($remove)) {
242 // Remove URI
243 if(!empty($permalink_manager_uris[$element_id])) {
244 $removed_uris = 1;
245 unset($permalink_manager_uris[$element_id]);
246 }
247
248 // Remove all custom redirects
249 if(!empty($permalink_manager_redirects[$element_id]) && is_array($permalink_manager_redirects[$element_id])) {
250 $removed_redirects = count($permalink_manager_redirects[$element_id]);
251 unset($permalink_manager_redirects[$element_id]);;
252 }
253 }
254 // 2B. Check if the post/term uses the same URI for both permalink & custom redirects
255 else {
256 $removed_redirect = self::clear_single_element_duplicated_redirect($element_id, true);
257 $removed_redirects = (!empty($removed_redirect)) ? 1 : 0;
258 }
259
260 // Check if function should only return the counts or update
261 if($count_removed) {
262 return array($removed_uris, $removed_redirects);
263 } else if(!empty($removed_uris) || !empty($removed_redirects)) {
264 update_option('permalink-manager-uris', array_filter($permalink_manager_uris));
265 update_option('permalink-manager-redirects', array_filter($permalink_manager_redirects));
266 return true;
267 }
268 }
269
270 /**
271 * Remove custom URI & redirects for any requested post or term
272 */
273 public static function force_clear_single_element_uris_and_redirects($uri_key) {
274 global $permalink_manager_uris, $permalink_manager_redirects, $permalink_manager_before_sections_html;
275
276 // Check if custom URI is set
277 if(isset($permalink_manager_uris[$uri_key])) {
278 $uri = $permalink_manager_uris[$uri_key];
279
280 unset($permalink_manager_uris[$uri_key]);
281 update_option('permalink-manager-uris', $permalink_manager_uris);
282
283 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(sprintf(__( 'URI "%s" was removed successfully!', 'permalink-manager' ), $uri), 'updated');
284 $updated = true;
285 }
286
287 // Check if custom redirects are set
288 if(isset($permalink_manager_redirects[$uri_key])) {
289 unset($permalink_manager_redirects[$uri_key]);
290 update_option('permalink-manager-redirects', $permalink_manager_redirects);
291
292 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'Broken redirects were removed successfully!', 'permalink-manager' ), 'updated');
293 $updated = true;
294 }
295
296 if(empty($updated)) {
297 $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');
298 }
299 }
300
301 /**
302 * "Find and replace" in "Tools"
303 */
304 function find_and_replace() {
305 // Check if posts or terms should be updated
306 if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') {
307 return Permalink_Manager_URI_Functions_Tax::find_and_replace();
308 } else {
309 return Permalink_Manager_URI_Functions_Post::find_and_replace();
310 }
311 }
312
313 /**
314 * Regenerate all permalinks in "Tools"
315 */
316 function regenerate_all_permalinks() {
317 // Check if posts or terms should be updated
318 if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') {
319 return Permalink_Manager_URI_Functions_Tax::regenerate_all_permalinks();
320 } else {
321 return Permalink_Manager_URI_Functions_Post::regenerate_all_permalinks();
322 }
323 }
324
325 /**
326 * Update all permalinks in "Permalink Editor"
327 */
328 function update_all_permalinks() {
329 // Check if posts or terms should be updated
330 if(!empty($_POST['content_type']) && $_POST['content_type'] == 'taxonomies') {
331 return Permalink_Manager_URI_Functions_Tax::update_all_permalinks();
332 } else {
333 return Permalink_Manager_URI_Functions_Post::update_all_permalinks();
334 }
335 }
336
337 /**
338 * Clear sitemaps cache
339 */
340 function flush_sitemaps($types = array()) {
341 global $permalink_manager_before_sections_html;
342
343 if(class_exists('WPSEO_Sitemaps_Cache')) {
344 $sitemaps = WPSEO_Sitemaps_Cache::clear($types);
345
346 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'Sitemaps were updated!', 'permalink-manager' ), 'updated');
347 }
348 }
349
350 /**
351 * Import old URIs from "Custom Permalinks" (Pro)
352 */
353 function import_custom_permalinks_uris() {
354 Permalink_Manager_Third_Parties::import_custom_permalinks_uris();
355 }
356
357 }
358
359 ?>
360