PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 0.5.3
Permalink Manager Lite v0.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-uri-actions.php
permalink-manager / includes / core Last commit date
permalink-manager-admin-functions.php 9 years ago permalink-manager-helper-functions.php 9 years ago permalink-manager-post-uri-functions.php 9 years ago permalink-manager-uri-actions.php 9 years ago
permalink-manager-uri-actions.php
351 lines
1 <?php
2
3 /**
4 * Register all action hooks
5 */
6 class Permalink_Manager_Uri_Actions extends Permalink_Manager_Class {
7
8 public function __construct() {
9 add_action('admin_init', array($this, 'trigger_action'), 0);
10
11 add_action( 'save_post', array($this, 'update_single_uri'), 10, 3 );
12 add_action( 'wp_trash_post', array($this, 'remove_single_uri'), 10, 3 );
13 }
14
15 /**
16 * Trigger the specific action
17 */
18 function trigger_action() {
19 global $permalink_manager_before_sections_html, $permalink_manager_after_sections_html;
20
21 // Triggered in "Permalink Editor" section
22 if(isset($_POST['slug_editor']) && wp_verify_nonce($_POST['slug_editor'], 'uri_actions')) {
23 $updated_list = $this->posts_update_all_permalinks();
24
25 $updated_slugs_count = (isset($updated_list['updated_count']) && $updated_list['updated_count'] > 0) ? $updated_list['updated_count'] : false;
26 $updated_slugs_array = ($updated_slugs_count) ? $updated_list['updated'] : '';
27 }
28 // Triggered in "Regenerate/Rest" section
29 else if(isset($_POST['regenerate_posts']) && wp_verify_nonce($_POST['regenerate_posts'], 'uri_actions')) {
30 $updated_list = $this->posts_regenerate_all_permalinks();
31
32 $updated_slugs_count = (isset($updated_list['updated_count']) && $updated_list['updated_count'] > 0) ? $updated_list['updated_count'] : false;
33 $updated_slugs_array = ($updated_slugs_count) ? $updated_list['updated'] : '';
34 }
35 // Triggered in "Find and Replace" section
36 else if(isset($_POST['find_and_replace']) && wp_verify_nonce($_POST['find_and_replace'], 'uri_actions')) {
37 $updated_list = $this->posts_find_and_replace();
38
39 $updated_slugs_count = (isset($updated_list['updated_count']) && $updated_list['updated_count'] > 0) ? $updated_list['updated_count'] : false;
40 $updated_slugs_array = ($updated_slugs_count) ? $updated_list['updated'] : '';
41 }
42
43 // 2. Display the slugs table (and append the globals)
44 if(isset($updated_slugs_count)) {
45 if($updated_slugs_count > 0) {
46 $alert_content = sprintf( _n( '<strong>%d</strong> slug was updated!', '<strong>%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' ';
47 $alert_content .= sprintf( __( '<a href="%s">Click here</a> to go to the list of updated slugs', 'permalink-manager' ), '#updated-list');
48
49 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message($alert_content, 'updated');
50 $permalink_manager_after_sections_html .= Permalink_Manager_Admin_Functions::display_updated_slugs($updated_slugs_array);
51 } else {
52 $alert_content = __( '<strong>No slugs</strong> were updated!', 'permalink-manager' );
53 $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message($alert_content, 'error');
54 }
55 }
56 }
57
58 /**
59 * Find & replace (bulk action)
60 */
61 static function posts_find_and_replace() {
62 global $wpdb, $permalink_manager_uris;
63
64 // Reset variables
65 $updated_slugs_count = 0;
66 $updated_array = array();
67 $alert_type = $alert_content = $errors = '';
68
69 // Prepare default variables from $_POST object
70 $old_string = esc_sql($_POST['old_string']);
71 $new_string = esc_sql($_POST['new_string']);
72 $mode = isset($_POST['mode']) ? $_POST['mode'] : array('both');
73 $post_types_array = ($_POST['post_types']);
74 $post_statuses_array = ($_POST['post_statuses']);
75 $post_types = implode("', '", $post_types_array);
76 $post_statuses = implode("', '", $post_statuses_array);
77
78 // Save the rows before they are updated to an array
79 $posts_to_update = $wpdb->get_results("SELECT post_title, post_name, ID FROM {$wpdb->posts} WHERE post_status IN ('{$post_statuses}') AND post_type IN ('{$post_types}')", ARRAY_A);
80
81 // Now if the array is not empty use IDs from each subarray as a key
82 if($posts_to_update && empty($errors)) {
83 foreach ($posts_to_update as $row) {
84
85 // Prepare variables
86 $old_post_name = $row['post_name'];
87 $native_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($row['ID'], true);
88 $default_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($row['ID']);
89 $old_uri = (isset($permalink_manager_uris[$row['ID']])) ? $permalink_manager_uris[$row['ID']] : $default_uri;
90 $old_slug = (strpos($old_uri, '/') !== false) ? substr($old_uri, strrpos($old_uri, '/') + 1) : $old_uri;
91 $old_base = (strpos($old_uri, '/') !== false) ? substr($old_uri, 0, strrpos( $old_uri, '/') ) : '';
92
93 // Process URI & slug
94 $new_slug = str_replace($old_string, $new_string, $old_slug);
95 $new_base = str_replace($old_string, $new_string, $old_base);
96 $new_uri = (in_array($mode, array('both'))) ? trim("{$new_base}/{$new_slug}", "/") : trim("{$old_base}/{$new_slug}", "/");
97 $new_post_name = (in_array($mode, array('post_names'))) ? str_replace($old_string, $new_string, $old_post_name) : $old_post_name; // Post name is changed only in first mode
98
99 //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n");
100
101 // Check if native slug should be changed
102 if(in_array($mode, array('post_names')) && ($old_post_name != $new_post_name)) {
103 Permalink_Manager_Post_URI_Functions::update_slug_by_id($new_post_name, $row['ID']);
104 }
105
106 if(($old_uri != $new_uri) || ($old_post_name != $new_post_name)) {
107 $permalink_manager_uris[$row['ID']] = $new_uri;
108 $updated_array[] = array('post_title' => $row['post_title'], 'ID' => $row['ID'], 'old_uri' => $old_uri, 'new_uri' => $new_uri, 'old_slug' => $old_post_name, 'new_slug' => $new_post_name);
109 $updated_slugs_count++;
110 }
111
112 // Do not store default values
113 if(isset($permalink_manager_uris[$row['ID']]) && ($new_uri == $native_uri)) {
114 unset($permalink_manager_uris[$row['ID']]);
115 }
116 }
117
118 // Filter array before saving
119 $permalink_manager_uris = array_filter($permalink_manager_uris);
120 update_option('permalink-manager-uris', $permalink_manager_uris);
121
122 $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count);
123 wp_reset_postdata();
124 }
125
126 return ($output) ? $output : "";
127 }
128
129 /**
130 * Regenerate slugs & bases (bulk action)
131 */
132 static function posts_regenerate_all_permalinks() {
133 global $wpdb, $permalink_manager_uris, $permalink_manager_permastructs;
134
135 // Setup needed variables
136 $updated_slugs_count = 0;
137 $updated_array = array();
138 $alert_type = $alert_content = $errors = '';
139
140 // Check if post types & statuses are not empty
141 if(empty($_POST['post_types']) || empty($_POST['post_statuses'])) { return false; }
142
143 $post_types_array = ($_POST['post_types']) ? ($_POST['post_types']) : '';
144 $post_statuses_array = ($_POST['post_statuses']) ? $_POST['post_statuses'] : '';
145 $post_types = implode("', '", $post_types_array);
146 $post_statuses = implode("', '", $post_statuses_array);
147 $mode = isset($_POST['mode']) ? $_POST['mode'] : 'both';
148
149 // Save the rows before they are updated to an array
150 $posts_to_update = $wpdb->get_results("SELECT post_title, post_name, post_type, ID FROM {$wpdb->posts} WHERE post_status IN ('{$post_statuses}') AND post_type IN ('{$post_types}')", ARRAY_A);
151
152 // Now if the array is not empty use IDs from each subarray as a key
153 if($posts_to_update && empty($errors)) {
154 foreach ($posts_to_update as $row) {
155 $updated = 0;
156
157 // Prepare variables
158 $old_post_name = $row['post_name'];
159 $native_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($row['ID'], true);
160 $default_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($row['ID']);
161 $old_uri = isset($permalink_manager_uris[$row['ID']]) ? trim($permalink_manager_uris[$row['ID']], "/") : $native_uri;
162 $old_slug = (strpos($old_uri, '/') !== false) ? substr($old_uri, strrpos($old_uri, '/') + 1) : $old_uri;
163 $correct_slug = sanitize_title($row['post_title']);
164
165 // Process URI & slug
166 $new_slug = wp_unique_post_slug($correct_slug, $row['ID'], get_post_status($row['ID']), get_post_type($row['ID']), null);
167 $new_post_name = (in_array($mode, array('post_names'))) ? $new_slug : $old_post_name; // Post name is changed only in first mode
168 $new_uri = (in_array($mode, array('both'))) ? $default_uri : str_replace($old_slug, $new_slug, $old_uri);
169
170 //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n");
171
172 // Check if native slug should be changed
173 if(in_array($mode, array('post_names')) && ($old_post_name != $new_post_name)) {
174 Permalink_Manager_Post_URI_Functions::update_slug_by_id($new_post_name, $row['ID']);
175 }
176
177 if(($old_uri != $new_uri) || ($old_post_name != $new_post_name)) {
178 $permalink_manager_uris[$row['ID']] = $new_uri;
179 $updated_array[] = array('post_title' => $row['post_title'], 'ID' => $row['ID'], 'old_uri' => $old_uri, 'new_uri' => $new_uri, 'old_slug' => $old_post_name, 'new_slug' => $new_post_name);
180 $updated_slugs_count++;
181 }
182
183 // Do not store default values
184 if(isset($permalink_manager_uris[$row['ID']]) && ($new_uri == $native_uri)) {
185 unset($permalink_manager_uris[$row['ID']]);
186 }
187 }
188
189 // Filter array before saving
190 $permalink_manager_uris = array_filter($permalink_manager_uris);
191 update_option('permalink-manager-uris', $permalink_manager_uris);
192
193 $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count);
194 wp_reset_postdata();
195 }
196
197 return (!empty($output)) ? $output : "";
198 }
199
200 /**
201 * Update all slugs & bases (bulk action)
202 */
203 public function posts_update_all_permalinks() {
204 global $permalink_manager_uris;
205
206 // Setup needed variables
207 $updated_slugs_count = 0;
208 $updated_array = array();
209
210 $old_uris = $permalink_manager_uris;
211 $new_uris = isset($_POST['uri']) ? $_POST['uri'] : array();
212
213 // Double check if the slugs and ids are stored in arrays
214 if (!is_array($new_uris)) $new_uris = explode(',', $new_uris);
215
216 if (!empty($new_uris)) {
217 foreach($new_uris as $id => $new_uri) {
218 // Prepare variables
219 $this_post = get_post($id);
220 $updated = '';
221
222 // Get default & native URL
223 $native_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($id, true);
224 $default_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($id);
225
226 $old_uri = isset($old_uris[$id]) ? trim($old_uris[$id], "/") : $native_uri;
227
228 // Process new values - empty entries will be treated as default values
229 $new_uri = preg_replace('/\s+/', '', $new_uri);
230 $new_uri = (!empty($new_uri)) ? trim($new_uri, "/") : $default_uri;
231 $new_slug = (strpos($new_uri, '/') !== false) ? substr($new_uri, strrpos($new_uri, '/') + 1) : $new_uri;
232
233 //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n");
234
235 // Do not store native URIs
236 if($new_uri == $native_uri) {
237 unset($old_uris[$id]);
238 }
239
240 if($new_uri != $old_uri) {
241 $old_uris[$id] = $new_uri;
242 $updated_array[] = array('post_title' => get_the_title($id), 'ID' => $id, 'old_uri' => $old_uri, 'new_uri' => $new_uri);
243 $updated_slugs_count++;
244 }
245
246 }
247
248 // Filter array before saving & append the global
249 $old_uris = $permalink_manager_uris = array_filter($old_uris);
250 update_option('permalink-manager-uris', $old_uris);
251
252 $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count);
253 }
254
255 return ($output) ? $output : "";
256 }
257
258 /**
259 * Remove URI from options array after post is moved to the trash
260 */
261 function clear_uris($post_id) {
262 $uris = $this->permalink_manager_uris;
263
264 foreach($uris as $post_id => $uri) {
265 $post_status = get_post_status($post_id);
266 if(in_array($post_status, array('auto-draft', 'trash', ''))) {
267 unset($uris[$post_id]);
268 }
269 }
270
271 update_option('permalink-manager-uris', $uris);
272 }
273
274 /**
275 * Update permastructs
276 */
277 static function update_permastructs() {
278 // Setup needed variables
279 $alert_type = $alert_content = $errors = '';
280 $permastructs = get_option('permalink-manager-permastructs', array());
281 $new_permastructs = array_filter($_POST['permalink-manager']['custom-permastructs']);
282
283 foreach($new_permastructs as $post_type => $new_permstruct) {
284 $default_permastruct = Permalink_Manager_Helper_Functions::get_default_permastruct($post_type, true);
285 $permastructs[$post_type] = trim(preg_replace('/\s+/', '', $new_permstruct), "/");
286
287 // Do not save default permastructs
288 if($default_permastruct == $new_permstruct) {
289 unset($permastructs[$post_type]);
290 }
291 }
292
293 update_option('permalink-manager-permastructs', $permastructs);
294
295 return "";
296 }
297
298 /**
299 * Update URI from "Edit Post" admin page
300 */
301 function update_single_uri($post_id, $post, $update) {
302 global $permalink_manager_uris;
303
304 // Ignore trashed items
305 if($post->post_status == 'trash') return;
306
307 // Fix for revisions
308 $is_revision = wp_is_post_revision($post_id);
309 $post_id = ($is_revision) ? $is_revision : $post_id;
310 $post = get_post($post_id);
311
312 $native_uri = Permalink_Manager_Post_URI_Functions::get_default_post_uri($post, true);
313 $old_uri = (isset($permalink_manager_uris[$post->ID])) ? $permalink_manager_uris[$post->ID] : $native_uri;
314 $new_uri = '';
315
316 // Check if user changed URI (available after post is saved)
317 if(isset($_POST['custom_uri'])) {
318 $new_uri = trim($_POST['custom_uri'], "/");
319 }
320
321 // A little hack (if user removes whole URI from input) ...
322 $new_uri = ($new_uri) ? $new_uri : Permalink_Manager_Post_URI_Functions::get_post_uri($post);
323
324 // Do not store default values
325 if(isset($permalink_manager_uris[$post->ID]) && ($new_uri == $native_uri)) {
326 unset($permalink_manager_uris[$post->ID]);
327 }
328 // Save only changed URIs
329 else if (($new_uri != $native_uri) && ($new_uri != $old_uri)) {
330 $permalink_manager_uris[$post->ID] = $new_uri;
331 }
332
333 update_option('permalink-manager-uris', $permalink_manager_uris);
334 }
335
336 /**
337 * Remove URI from options array after post is moved to the trash
338 */
339 function remove_single_uri($post_id) {
340 global $permalink_manager_uris;
341
342 // Check if the custom permalink is assigned to this post
343 if(isset($permalink_manager_uris[$post_id])) {
344 unset($permalink_manager_uris[$post_id]);
345 }
346
347 update_option('permalink-manager-uris', $permalink_manager_uris);
348 }
349
350 }
351