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-uri-functions-post.php
725 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Additional functions used in classes and another subclasses |
| 5 | */ |
| 6 | class Permalink_Manager_URI_Functions_Post extends Permalink_Manager_Class { |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action( 'admin_init', array($this, 'admin_init'), 999, 3); |
| 10 | |
| 11 | add_filter( '_get_page_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 12 | add_filter( 'page_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 13 | add_filter( 'post_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 14 | add_filter( 'post_type_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 15 | add_filter( 'attachment_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 16 | |
| 17 | add_filter( 'permalink-manager-uris', array($this, 'exclude_homepage'), 999, 2); |
| 18 | |
| 19 | /** |
| 20 | * URI Editor |
| 21 | */ |
| 22 | add_filter( 'get_sample_permalink_html', array($this, 'edit_uri_box'), 999, 5 ); |
| 23 | add_action( 'save_post', array($this, 'update_post_uri'), 999, 1); |
| 24 | add_action( 'edit_attachment', array($this, 'update_post_uri'), 999, 1 ); |
| 25 | add_action( 'wp_insert_post', array($this, 'new_post_uri'), 999, 1 ); |
| 26 | add_action( 'wp_trash_post', array($this, 'remove_post_uri'), 10, 1 ); |
| 27 | |
| 28 | add_action( 'quick_edit_custom_box', array($this, 'quick_edit_column_form'), 999, 3); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Init |
| 33 | */ |
| 34 | function admin_init() { |
| 35 | $post_types = get_post_types(array('public' => true), 'names', 'and'); |
| 36 | |
| 37 | // Add "URI Editor" to "Quick Edit" for all post_types |
| 38 | foreach($post_types as $post_type) { |
| 39 | // Check if post type is allowed |
| 40 | if(Permalink_Manager_Helper_Functions::is_disabled($post_type, 'post_type')) { continue; } |
| 41 | |
| 42 | $post_type = ($post_type == 'post' || $post_type == 'page') ? "{$post_type}s" : $post_type; |
| 43 | add_filter( "manage_{$post_type}_columns" , array($this, 'quick_edit_column') ); |
| 44 | add_filter( "manage_{$post_type}_custom_column" , array($this, 'quick_edit_column_content'), 10, 2 ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Change permalinks for posts, pages & custom post types |
| 50 | */ |
| 51 | function custom_post_permalinks($permalink, $post) { |
| 52 | global $wp_rewrite, $permalink_manager_uris, $permalink_manager_options; |
| 53 | |
| 54 | $post = (is_integer($post)) ? get_post($post) : $post; |
| 55 | |
| 56 | // Start with homepage URL |
| 57 | $home_url = trim(get_option('home'), "/"); |
| 58 | |
| 59 | // 1. Check if post type is allowed |
| 60 | if(!empty($post->post_type) && Permalink_Manager_Helper_Functions::is_disabled($post->post_type, 'post_type')) { return $permalink; } |
| 61 | |
| 62 | // 2A. Do not change permalink of frontpage |
| 63 | if(get_option('page_on_front') == $post->ID) { |
| 64 | return $permalink; |
| 65 | } |
| 66 | // 2B. Do not change permalink for drafts and future posts (+ remove trailing slash from them) |
| 67 | else if(in_array($post->post_status, array('draft', 'pending', 'auto-draft', 'future'))) { |
| 68 | return trim($permalink, "/"); |
| 69 | } |
| 70 | |
| 71 | // 3. Save the old permalink to separate variable |
| 72 | $old_permalink = $permalink; |
| 73 | |
| 74 | // 4. Filter only the posts with custom permalink assigned |
| 75 | if(isset($permalink_manager_uris[$post->ID])) { |
| 76 | // Apend the language code as a non-editable prefix (can be used also for another prefixes) |
| 77 | $prefix = apply_filters('permalink-manager-post-permalink-prefix', '', $post); |
| 78 | |
| 79 | // Encode URI? |
| 80 | if(!empty($permalink_manager_options['general']['decode_uris'])) { |
| 81 | $permalink = "{$home_url}/" . urldecode("/{$prefix}{$permalink_manager_uris[$post->ID]}"); |
| 82 | } else { |
| 83 | $permalink = "{$home_url}/" . Permalink_Manager_Helper_Functions::encode_uri("{$prefix}{$permalink_manager_uris[$post->ID]}"); |
| 84 | } |
| 85 | } else if($post->post_type == 'attachment' && $post->post_parent > 0 && $post->post_parent != $post->ID && !empty($permalink_manager_uris[$post->post_parent])) { |
| 86 | $permalink = "{$home_url}/{$permalink_manager_uris[$post->post_parent]}/attachment/{$post->post_name}"; |
| 87 | } else if(!empty($permalink_manager_options['general']['decode_uris'])) { |
| 88 | $permalink = "{$home_url}/" . urldecode("/{$permalink}"); |
| 89 | } |
| 90 | |
| 91 | // 5. Additional filter |
| 92 | $permalink = apply_filters('permalink_manager_filter_final_post_permalink', user_trailingslashit($permalink), $post, $old_permalink); |
| 93 | |
| 94 | return $permalink; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check if the provided slug is unique and then update it with SQL query. |
| 99 | */ |
| 100 | static function update_slug_by_id($slug, $id) { |
| 101 | global $wpdb; |
| 102 | |
| 103 | // Update slug and make it unique |
| 104 | $slug = (empty($slug)) ? sanitize_title(get_the_title($id)) : $slug; |
| 105 | $new_slug = wp_unique_post_slug($slug, $id, get_post_status($id), get_post_type($id), null); |
| 106 | $wpdb->query("UPDATE $wpdb->posts SET post_name = '$new_slug' WHERE ID = '$id'"); |
| 107 | |
| 108 | return $new_slug; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the active URI |
| 113 | */ |
| 114 | public static function get_post_uri($post_id, $native_uri = false) { |
| 115 | global $permalink_manager_uris; |
| 116 | |
| 117 | // Check if input is post object |
| 118 | $post_id = (isset($post_id->ID)) ? $post_id->ID : $post_id; |
| 119 | |
| 120 | $final_uri = (!empty($permalink_manager_uris[$post_id])) ? $permalink_manager_uris[$post_id] : self::get_default_post_uri($post_id, $native_uri); |
| 121 | return $final_uri; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the default (not overwritten by the user) or native URI (unfiltered) |
| 126 | */ |
| 127 | public static function get_default_post_uri($post, $native_uri = false) { |
| 128 | global $permalink_manager_options, $permalink_manager_uris, $permalink_manager_permastructs; |
| 129 | |
| 130 | // Load all bases & post |
| 131 | $post = is_object($post) ? $post : get_post($post); |
| 132 | |
| 133 | // Check if post ID is defined |
| 134 | if(empty($post->ID)) { return ''; } |
| 135 | $post_id = $post->ID; |
| 136 | $post_type = $post->post_type; |
| 137 | $post_name = (empty($post->post_name)) ? sanitize_title($post->post_title) : $post->post_name; |
| 138 | |
| 139 | // 1. Get the permastruct |
| 140 | if($post_type == 'attachment') { |
| 141 | $parent_page = ($post->post_parent > 0 && $post->post_parent != $post->ID) ? get_post($post->post_parent) : false; |
| 142 | $default_permastruct = ($parent_page) ? trim(get_page_uri($parent_page->ID), "/") . "/attachment" : ""; |
| 143 | |
| 144 | if($native_uri) { |
| 145 | $permastruct = $default_permastruct; |
| 146 | } else { |
| 147 | $permastruct = (!empty($permalink_manager_permastructs['post_types'][$post_type])) ? $permalink_manager_permastructs['post_types'][$post_type] : $default_permastruct; |
| 148 | } |
| 149 | } else { |
| 150 | $default_permastruct = Permalink_Manager_Helper_Functions::get_default_permastruct($post_type); |
| 151 | if($native_uri) { |
| 152 | $permastruct = $default_permastruct; |
| 153 | } else { |
| 154 | $permastruct = (isset($permalink_manager_permastructs['post_types'][$post_type])) ? $permalink_manager_permastructs['post_types'][$post_type] : $default_permastruct; |
| 155 | } |
| 156 | } |
| 157 | $default_base = (!empty($permastruct)) ? trim($permastruct, '/') : ""; |
| 158 | |
| 159 | // 2A. Get the date |
| 160 | $date = explode(" ", date('Y m d H i s', strtotime($post->post_date))); |
| 161 | |
| 162 | // 2B. Get the author (if needed) |
| 163 | $author = ''; |
| 164 | if(strpos($default_base, '%author%') !== false) { |
| 165 | $authordata = get_userdata($post->post_author); |
| 166 | $author = $authordata->user_nicename; |
| 167 | } |
| 168 | |
| 169 | // 3A. Fix for hierarchical CPT (start) |
| 170 | $full_slug = (is_post_type_hierarchical($post_type)) ? get_page_uri($post) : $post_name; |
| 171 | $full_slug = (empty($full_slug)) ? $post_name : $full_slug; |
| 172 | |
| 173 | // 3B. Allow filter the default slug |
| 174 | if(!$native_uri) { |
| 175 | $full_slug = ($native_uri) ? $full_slug : Permalink_Manager_Helper_Functions::force_custom_slugs($full_slug, $post); |
| 176 | $full_slug = apply_filters('permalink_manager_filter_default_post_slug', $full_slug, $post, $post_name); |
| 177 | } |
| 178 | $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type); |
| 179 | |
| 180 | // 3C. Get the standard tags and replace them with their values |
| 181 | $tags = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%author%'); |
| 182 | $tags_replacements = array($date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->ID, $author); |
| 183 | $default_uri = str_replace($tags, $tags_replacements, $default_base); |
| 184 | |
| 185 | // 3B. Check if any post tag is present in custom permastructure |
| 186 | $do_not_append_slug = apply_filters("permalink_manager_do_not_append_slug", false, $post_type, $post); |
| 187 | if($do_not_append_slug == false) { |
| 188 | $slug_tags = array($post_type_tag, '%postname%', '%postname_flat%'); |
| 189 | $slug_tags_replacement = array($full_slug, $full_slug, $post_name); |
| 190 | foreach($slug_tags as $tag) { |
| 191 | if(strpos($default_uri, $tag) !== false) { |
| 192 | $do_not_append_slug = true; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // 3B. Replace the post tags with slugs or rppend the slug if no post tag is defined |
| 199 | if(!empty($do_not_append_slug)) { |
| 200 | $default_uri = str_replace($slug_tags, $slug_tags_replacement, $default_uri); |
| 201 | } else { |
| 202 | $default_uri .= "/{$full_slug}"; |
| 203 | } |
| 204 | |
| 205 | // 3C. Replace taxonomies |
| 206 | $taxonomies = get_taxonomies(); |
| 207 | |
| 208 | if($taxonomies) { |
| 209 | foreach($taxonomies as $taxonomy) { |
| 210 | // 1. Reset $replacement |
| 211 | $replacement = $terms = $replacement_term = ""; |
| 212 | |
| 213 | // 2. Try to use Yoast SEO Primary Term |
| 214 | $replacement_term = $primary_term = Permalink_Manager_Helper_Functions::get_primary_term($post->ID, $taxonomy, false); |
| 215 | |
| 216 | // 3. Get the first assigned term to this taxonomy |
| 217 | if(empty($replacement_term)) { |
| 218 | $terms = wp_get_object_terms($post->ID, $taxonomy); |
| 219 | $replacement_term = (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) ? $terms[0] : ""; |
| 220 | $replacement_term = apply_filters('permalink_manager_filter_post_terms', $replacement_term, $post, $terms, $taxonomy, $native_uri); |
| 221 | } |
| 222 | |
| 223 | // 4A. Get permalink base from the term's custom URI |
| 224 | if(!empty($replacement_term->term_id) && strpos($default_uri, "%{$taxonomy}_custom_uri%") !== false && !empty($permalink_manager_uris["tax-{$replacement_term->term_id}"])) { |
| 225 | $replacement = $permalink_manager_uris["tax-{$replacement_term->term_id}"]; |
| 226 | } |
| 227 | // 4B. Hierarhcical taxonomy base |
| 228 | else if(!empty($replacement_term->term_id) && strpos($default_uri, "%{$taxonomy}_flat%") === false && is_taxonomy_hierarchical($taxonomy)) { |
| 229 | $ancestors = get_ancestors( $replacement_term->term_id, $taxonomy, 'taxonomy' ); |
| 230 | $hierarchical_slugs = array(); |
| 231 | |
| 232 | foreach((array) $ancestors as $ancestor) { |
| 233 | $ancestor_term = get_term($ancestor, $taxonomy); |
| 234 | $hierarchical_slugs[] = ($native_uri) ? $replacement_term->slug : Permalink_Manager_Helper_Functions::force_custom_slugs($ancestor_term->slug, $ancestor_term); |
| 235 | } |
| 236 | $hierarchical_slugs = array_reverse($hierarchical_slugs); |
| 237 | $replacement = implode('/', $hierarchical_slugs); |
| 238 | |
| 239 | // Append the term slug now |
| 240 | $last_term_slug = ($native_uri) ? $replacement_term->slug : Permalink_Manager_Helper_Functions::force_custom_slugs($replacement_term->slug, $replacement_term); |
| 241 | $replacement = "{$replacement}/{$last_term_slug}"; |
| 242 | } |
| 243 | // 4C. Force flat taxonomy base - get highgest level term (if %taxonomy_flat% tag is used) |
| 244 | else if(!$native_uri && strpos($default_uri, "%{$taxonomy}_flat%") !== false && !empty($terms) && empty($primary_term->slug)) { |
| 245 | foreach ($terms as $single_term) { |
| 246 | if ($single_term->parent == 0) { |
| 247 | $replacement = Permalink_Manager_Helper_Functions::force_custom_slugs($single_term->slug, $single_term); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | // 4D. Flat/non-hierarchical taxonomy base - get primary term (if set) or first term |
| 253 | else if(!empty($replacement_term->slug)) { |
| 254 | $replacement = ($native_uri) ? $replacement_term->slug : Permalink_Manager_Helper_Functions::force_custom_slugs($replacement_term->slug, $replacement_term); |
| 255 | } |
| 256 | |
| 257 | // 4. Do the replacement |
| 258 | $default_uri = (!empty($replacement)) ? str_replace(array("%{$taxonomy}%", "%{$taxonomy}_flat%", "%{$taxonomy}_custom_uri%"), $replacement, $default_uri) : $default_uri; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // 4. Clear the URI |
| 263 | $default_uri = preg_replace('/\s+/', '', $default_uri); |
| 264 | $default_uri = str_replace('//', '/', $default_uri); |
| 265 | $default_uri = trim($default_uri, "/"); |
| 266 | |
| 267 | return apply_filters('permalink_manager_filter_default_post_uri', $default_uri, $post->post_name, $post, $post_name, $native_uri); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * The homepage should not use URI |
| 272 | */ |
| 273 | function exclude_homepage($uris) { |
| 274 | // Find the homepage URI |
| 275 | $homepage_id = get_option('page_on_front'); |
| 276 | if(isset($uris[$homepage_id])) { unset($uris[$homepage_id]); } |
| 277 | |
| 278 | return $uris; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Find & replace (bulk action) |
| 283 | */ |
| 284 | public static function find_and_replace() { |
| 285 | global $wpdb, $permalink_manager_uris; |
| 286 | |
| 287 | // Check if post types & statuses are not empty |
| 288 | if(empty($_POST['post_types']) || empty($_POST['post_statuses'])) { return false; } |
| 289 | |
| 290 | // Get homepage URL and ensure that it ends with slash |
| 291 | $home_url = trim(get_option('home'), "/") . "/"; |
| 292 | |
| 293 | // Reset variables |
| 294 | $updated_slugs_count = 0; |
| 295 | $updated_array = array(); |
| 296 | $alert_type = $alert_content = $errors = ''; |
| 297 | |
| 298 | // Process the variables from $_POST object |
| 299 | $old_string = str_replace($home_url, '', esc_sql($_POST['old_string'])); |
| 300 | $new_string = str_replace($home_url, '', esc_sql($_POST['new_string'])); |
| 301 | |
| 302 | $post_types_array = ($_POST['post_types']); |
| 303 | $post_statuses_array = ($_POST['post_statuses']); |
| 304 | $post_types = implode("', '", $post_types_array); |
| 305 | $post_statuses = implode("', '", $post_statuses_array); |
| 306 | $mode = isset($_POST['mode']) ? $_POST['mode'] : 'custom_uris'; |
| 307 | |
| 308 | // Filter the posts by IDs |
| 309 | $where = ''; |
| 310 | if(!empty($_POST['ids'])) { |
| 311 | // Remove whitespaces and prepare array with IDs and/or ranges |
| 312 | $ids = esc_sql(preg_replace('/\s*/m', '', $_POST['ids'])); |
| 313 | preg_match_all("/([\d]+(?:-?[\d]+)?)/x", $ids, $groups); |
| 314 | |
| 315 | // Prepare the extra ID filters |
| 316 | $where .= "AND ("; |
| 317 | foreach($groups[0] as $group) { |
| 318 | $where .= ($group == reset($groups[0])) ? "" : " OR "; |
| 319 | // A. Single number |
| 320 | if(is_numeric($group)) { |
| 321 | $where .= "(ID = {$group})"; |
| 322 | } |
| 323 | // B. Range |
| 324 | else if(substr_count($group, '-')) { |
| 325 | $range_edges = explode("-", $group); |
| 326 | $where .= "(ID BETWEEN {$range_edges[0]} AND {$range_edges[1]})"; |
| 327 | } |
| 328 | } |
| 329 | $where .= ")"; |
| 330 | } |
| 331 | |
| 332 | // Get the rows before they are altered |
| 333 | $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}') {$where}", ARRAY_A); |
| 334 | |
| 335 | // Now if the array is not empty use IDs from each subarray as a key |
| 336 | if($posts_to_update && empty($errors)) { |
| 337 | foreach ($posts_to_update as $row) { |
| 338 | // Get default & native URL |
| 339 | $native_uri = self::get_default_post_uri($row['ID'], true); |
| 340 | $default_uri = self::get_default_post_uri($row['ID']); |
| 341 | $old_post_name = $row['post_name']; |
| 342 | $old_uri = (isset($permalink_manager_uris[$row['ID']])) ? $permalink_manager_uris[$row['ID']] : $default_uri; |
| 343 | |
| 344 | // Do replacement on slugs (non-REGEX) |
| 345 | if(preg_match("/^\/.+\/[a-z]*$/i", $old_string)) { |
| 346 | // Use $_POST['old_string'] directly here & fix double slashes problem |
| 347 | $pattern = "~" . stripslashes(trim(sanitize_text_field($_POST['old_string']), "/")) . "~"; |
| 348 | |
| 349 | $new_post_name = ($mode == 'slugs') ? preg_replace($pattern, $new_string, $old_post_name) : $old_post_name; |
| 350 | $new_uri = ($mode != 'slugs') ? preg_replace($pattern, $new_string, $old_uri) : $old_uri; |
| 351 | } else { |
| 352 | $new_post_name = ($mode == 'slugs') ? str_replace($old_string, $new_string, $old_post_name) : $old_post_name; // Post name is changed only in first mode |
| 353 | $new_uri = ($mode != 'slugs') ? str_replace($old_string, $new_string, $old_uri) : $old_uri; |
| 354 | } |
| 355 | |
| 356 | //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n"); |
| 357 | |
| 358 | // Check if native slug should be changed |
| 359 | if(($mode == 'slugs') && ($old_post_name != $new_post_name)) { |
| 360 | self::update_slug_by_id($new_post_name, $row['ID']); |
| 361 | } |
| 362 | |
| 363 | if(($old_uri != $new_uri) || ($old_post_name != $new_post_name) && !(empty($new_uri))) { |
| 364 | $permalink_manager_uris[$row['ID']] = $new_uri; |
| 365 | $updated_array[] = array('item_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); |
| 366 | $updated_slugs_count++; |
| 367 | } |
| 368 | |
| 369 | do_action('permalink-manager-updated-post-uri', $row['ID'], $new_uri, $old_uri, $native_uri, $default_uri); |
| 370 | } |
| 371 | |
| 372 | // Filter array before saving |
| 373 | $permalink_manager_uris = array_filter($permalink_manager_uris); |
| 374 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 375 | |
| 376 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 377 | wp_reset_postdata(); |
| 378 | } |
| 379 | |
| 380 | return ($output) ? $output : ""; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Regenerate slugs & bases (bulk action) |
| 385 | */ |
| 386 | static function regenerate_all_permalinks() { |
| 387 | global $wpdb, $permalink_manager_uris; |
| 388 | |
| 389 | // Check if post types & statuses are not empty |
| 390 | if(empty($_POST['post_types']) || empty($_POST['post_statuses'])) { return false; } |
| 391 | |
| 392 | // Process the variables from $_POST object |
| 393 | $updated_slugs_count = 0; |
| 394 | $updated_array = array(); |
| 395 | $alert_type = $alert_content = $errors = ''; |
| 396 | |
| 397 | $post_types_array = ($_POST['post_types']) ? ($_POST['post_types']) : ''; |
| 398 | $post_statuses_array = ($_POST['post_statuses']) ? $_POST['post_statuses'] : ''; |
| 399 | $post_types = implode("', '", $post_types_array); |
| 400 | $post_statuses = implode("', '", $post_statuses_array); |
| 401 | $mode = isset($_POST['mode']) ? $_POST['mode'] : 'custom_uris'; |
| 402 | |
| 403 | // Filter the posts by IDs |
| 404 | $where = ''; |
| 405 | if(!empty($_POST['ids'])) { |
| 406 | // Remove whitespaces and prepare array with IDs and/or ranges |
| 407 | $ids = esc_sql(preg_replace('/\s*/m', '', $_POST['ids'])); |
| 408 | preg_match_all("/([\d]+(?:-?[\d]+)?)/x", $ids, $groups); |
| 409 | |
| 410 | // Prepare the extra ID filters |
| 411 | $where .= "AND ("; |
| 412 | foreach($groups[0] as $group) { |
| 413 | $where .= ($group == reset($groups[0])) ? "" : " OR "; |
| 414 | // A. Single number |
| 415 | if(is_numeric($group)) { |
| 416 | $where .= "(ID = {$group})"; |
| 417 | } |
| 418 | // B. Range |
| 419 | else if(substr_count($group, '-')) { |
| 420 | $range_edges = explode("-", $group); |
| 421 | $where .= "(ID BETWEEN {$range_edges[0]} AND {$range_edges[1]})"; |
| 422 | } |
| 423 | } |
| 424 | $where .= ")"; |
| 425 | } |
| 426 | |
| 427 | // Get the rows before they are altered |
| 428 | $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}') {$where}", ARRAY_A); |
| 429 | |
| 430 | // Now if the array is not empty use IDs from each subarray as a key |
| 431 | if($posts_to_update && empty($errors)) { |
| 432 | foreach ($posts_to_update as $row) { |
| 433 | // Prevent server timeout |
| 434 | set_time_limit(0); |
| 435 | |
| 436 | // Get default & native URL |
| 437 | $native_uri = self::get_default_post_uri($row['ID'], true); |
| 438 | $default_uri = self::get_default_post_uri($row['ID']); |
| 439 | $old_post_name = $row['post_name']; |
| 440 | $old_uri = isset($permalink_manager_uris[$row['ID']]) ? trim($permalink_manager_uris[$row['ID']], "/") : $native_uri; |
| 441 | $correct_slug = sanitize_title($row['post_title']); |
| 442 | |
| 443 | // Process URI & slug |
| 444 | $new_slug = wp_unique_post_slug($correct_slug, $row['ID'], get_post_status($row['ID']), get_post_type($row['ID']), null); |
| 445 | $new_post_name = ($mode == 'slugs') ? $new_slug : $old_post_name; // Post name is changed only in first mode |
| 446 | |
| 447 | // Prepare the new URI |
| 448 | if($mode == 'slugs') { |
| 449 | $new_uri = $old_uri; |
| 450 | } else if($mode == 'native') { |
| 451 | $new_uri = $native_uri; |
| 452 | } else { |
| 453 | $new_uri = $default_uri; |
| 454 | } |
| 455 | |
| 456 | //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n"); |
| 457 | |
| 458 | // Check if native slug should be changed |
| 459 | if(($mode == 'slugs') && ($old_post_name != $new_post_name)) { |
| 460 | self::update_slug_by_id($new_post_name, $row['ID']); |
| 461 | } |
| 462 | |
| 463 | if(($old_uri != $new_uri) || ($old_post_name != $new_post_name)) { |
| 464 | $permalink_manager_uris[$row['ID']] = $new_uri; |
| 465 | $updated_array[] = array('item_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); |
| 466 | $updated_slugs_count++; |
| 467 | } |
| 468 | |
| 469 | do_action('permalink-manager-updated-post-uri', $row['ID'], $new_uri, $old_uri, $native_uri, $default_uri); |
| 470 | } |
| 471 | |
| 472 | // Filter array before saving |
| 473 | $permalink_manager_uris = array_filter($permalink_manager_uris); |
| 474 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 475 | |
| 476 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 477 | wp_reset_postdata(); |
| 478 | } |
| 479 | |
| 480 | return (!empty($output)) ? $output : ""; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Update all slugs & bases (bulk action) |
| 485 | */ |
| 486 | static public function update_all_permalinks() { |
| 487 | global $permalink_manager_uris; |
| 488 | |
| 489 | // Setup needed variables |
| 490 | $updated_slugs_count = 0; |
| 491 | $updated_array = array(); |
| 492 | |
| 493 | $old_uris = $permalink_manager_uris; |
| 494 | $new_uris = isset($_POST['uri']) ? $_POST['uri'] : array(); |
| 495 | |
| 496 | // Double check if the slugs and ids are stored in arrays |
| 497 | if (!is_array($new_uris)) $new_uris = explode(',', $new_uris); |
| 498 | |
| 499 | if (!empty($new_uris)) { |
| 500 | foreach($new_uris as $id => $new_uri) { |
| 501 | // Prevent server timeout |
| 502 | set_time_limit(0); |
| 503 | |
| 504 | // Prepare variables |
| 505 | $this_post = get_post($id); |
| 506 | $updated = ''; |
| 507 | |
| 508 | // Get default & native URL |
| 509 | $native_uri = self::get_default_post_uri($id, true); |
| 510 | $default_uri = self::get_default_post_uri($id); |
| 511 | |
| 512 | $old_uri = isset($old_uris[$id]) ? trim($old_uris[$id], "/") : $native_uri; |
| 513 | |
| 514 | // Process new values - empty entries will be treated as default values |
| 515 | $new_uri = preg_replace('/\s+/', '', $new_uri); |
| 516 | $new_uri = (!empty($new_uri)) ? trim($new_uri, "/") : $default_uri; |
| 517 | $new_slug = (strpos($new_uri, '/') !== false) ? substr($new_uri, strrpos($new_uri, '/') + 1) : $new_uri; |
| 518 | |
| 519 | //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri}\n"); |
| 520 | |
| 521 | if($new_uri != $old_uri) { |
| 522 | $old_uris[$id] = $new_uri; |
| 523 | $updated_array[] = array('item_title' => get_the_title($id), 'ID' => $id, 'old_uri' => $old_uri, 'new_uri' => $new_uri); |
| 524 | $updated_slugs_count++; |
| 525 | } |
| 526 | |
| 527 | do_action('permalink-manager-updated-post-uri', $id, $new_uri, $old_uri, $native_uri, $default_uri); |
| 528 | } |
| 529 | |
| 530 | // Filter array before saving & append the global |
| 531 | $old_uris = $permalink_manager_uris = array_filter($old_uris); |
| 532 | update_option('permalink-manager-uris', $old_uris); |
| 533 | |
| 534 | //print_r($permalink_manager_uris); |
| 535 | |
| 536 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 537 | } |
| 538 | |
| 539 | return ($output) ? $output : ""; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Allow to edit URIs from "Edit Post" admin pages |
| 544 | */ |
| 545 | function edit_uri_box($html, $id, $new_title, $new_slug, $post) { |
| 546 | global $permalink_manager_uris, $permalink_manager_options; |
| 547 | |
| 548 | // Detect auto drafts |
| 549 | $autosave = (!empty($new_title) && empty($new_slug)) ? true : false; |
| 550 | |
| 551 | // Do not do anything if new slug is empty or page is front-page |
| 552 | if(get_option('page_on_front') == $id) { return $html; } |
| 553 | |
| 554 | // Check if post type is disabled |
| 555 | if(Permalink_Manager_Helper_Functions::is_disabled($post->post_type, 'post_type')) { return $html; } |
| 556 | |
| 557 | $html = preg_replace("/(<strong>(.*)<\/strong>)(.*)/is", "$1 ", $html); |
| 558 | $default_uri = self::get_default_post_uri($id); |
| 559 | $native_uri = self::get_default_post_uri($id, true); |
| 560 | |
| 561 | // Make sure that home URL ends with slash |
| 562 | $home_url = trim(get_option('home'), "/") . "/"; |
| 563 | $prefix = apply_filters('permalink-manager-post-permalink-prefix', '', $post, true); |
| 564 | |
| 565 | // Do not change anything if post is not saved yet (display sample permalink instead) |
| 566 | if($autosave || empty($post->post_status)) { |
| 567 | $sample_permalink_url = $default_uri; |
| 568 | } else { |
| 569 | //$uri = $sample_permalink_url = (!empty($permalink_manager_uris[$id])) ? $permalink_manager_uris[$id] : $default_uri; |
| 570 | $uri = $sample_permalink_url = (!empty($permalink_manager_uris[$id])) ? $permalink_manager_uris[$id] : $native_uri; |
| 571 | } |
| 572 | |
| 573 | // Decode URI & allow to filter it |
| 574 | $sample_permalink_url = apply_filters('permalink_manager_filter_post_sample_permalink', urldecode($sample_permalink_url), $post); |
| 575 | |
| 576 | // Prepare the sample & default permalink |
| 577 | $sample_permalink = sprintf("{$home_url}{$prefix}<span class=\"editable\">%s</span>", str_replace("//", "/", $sample_permalink_url)); |
| 578 | |
| 579 | // Allow to filter the sample permalink URL |
| 580 | |
| 581 | // Append new HTML output |
| 582 | $html .= sprintf("<span class=\"sample-permalink-span\"><a href=\"%s\">%s</a></span> ", strip_tags($sample_permalink), $sample_permalink); |
| 583 | |
| 584 | if(!empty($uri)) { |
| 585 | $html .= Permalink_Manager_Admin_Functions::display_uri_box($post, $default_uri, $uri, $native_uri, "{$home_url}{$prefix}"); |
| 586 | } |
| 587 | |
| 588 | // Append hidden field with native slug |
| 589 | $html .= (!empty($post->post_name)) ? "<input id=\"new-post-slug\" value=\"{$post->post_name}\" autocomplete=\"off\" type=\"hidden\">" : ""; |
| 590 | |
| 591 | return $html; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * "Quick Edit" form |
| 596 | */ |
| 597 | function quick_edit_column($columns) { |
| 598 | return array_merge($columns, array('permalink-manager-col' => __( 'Current URI', 'permalink-manager'))); |
| 599 | } |
| 600 | |
| 601 | function quick_edit_column_content($column_name, $post_id) { |
| 602 | if($column_name != "permalink-manager-col") { return; } |
| 603 | |
| 604 | $html = self::get_post_uri($post_id); |
| 605 | echo $html; |
| 606 | } |
| 607 | |
| 608 | function quick_edit_column_form($column_name, $post_type, $taxonomy = '') { |
| 609 | if(!$taxonomy && $column_name == 'permalink-manager-col') { |
| 610 | echo Permalink_Manager_Admin_Functions::quick_edit_column_form(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Update URI when new post is added |
| 616 | */ |
| 617 | function new_post_uri($post_id) { |
| 618 | global $permalink_manager_uris, $permalink_manager_options, $permalink_manager_before_sections_html; |
| 619 | |
| 620 | // Do not trigger if post is a revision |
| 621 | if(wp_is_post_revision($post_id)) { return $post_id; } |
| 622 | |
| 623 | // Do not do anything if post is autosaved |
| 624 | if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } |
| 625 | |
| 626 | // Do not do anything on in "Bulk Edit" |
| 627 | if(!empty($_REQUEST['bulk_edit'])) { return $post_id; } |
| 628 | |
| 629 | // Hotfix |
| 630 | if(isset($_POST['custom_uri']) || isset($_POST['permalink-manager-quick-edit']) || isset($permalink_manager_uris[$post_id])) { return $post_id; } |
| 631 | |
| 632 | $post = get_post($post_id); |
| 633 | |
| 634 | // Check if post type is allowed |
| 635 | if(Permalink_Manager_Helper_Functions::is_disabled($post->post_type, 'post_type')) { return $post_id; }; |
| 636 | |
| 637 | // Hotfix for menu items & auto-drafts |
| 638 | if($post->post_type == 'nav_menu_item' || $post->post_status == 'auto-draft') { return $post_id; } |
| 639 | |
| 640 | $native_uri = self::get_default_post_uri($post_id, true); |
| 641 | $new_uri = self::get_default_post_uri($post_id); |
| 642 | $permalink_manager_uris[$post->ID] = $new_uri; |
| 643 | |
| 644 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 645 | |
| 646 | do_action('permalink-manager-new-post-uri', $post_id, $new_uri, $native_uri); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Update URI from "Edit Post" admin page |
| 651 | */ |
| 652 | function update_post_uri($post_id) { |
| 653 | global $permalink_manager_uris, $permalink_manager_options, $permalink_manager_before_sections_html; |
| 654 | |
| 655 | // Verify nonce at first |
| 656 | if(!isset($_POST['permalink-manager-nonce']) || !wp_verify_nonce($_POST['permalink-manager-nonce'], 'permalink-manager-edit-uri-box')) { return $post_id; } |
| 657 | |
| 658 | // Do not do anything if post is autosaved |
| 659 | if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } |
| 660 | |
| 661 | // Do not do anything on in "Bulk Edit" |
| 662 | if(!empty($_REQUEST['bulk_edit'])) { return $post_id; } |
| 663 | |
| 664 | // Do not do anything if the field with URI is not present |
| 665 | if(!isset($_POST['custom_uri'])) { return $post_id; } |
| 666 | |
| 667 | // Fix for revisions |
| 668 | $is_revision = wp_is_post_revision($post_id); |
| 669 | $post_id = ($is_revision) ? $is_revision : $post_id; |
| 670 | $post = get_post($post_id); |
| 671 | |
| 672 | // Check if post type is allowed |
| 673 | if(Permalink_Manager_Helper_Functions::is_disabled($post->post_type, 'post_type')) { return $post_id; }; |
| 674 | |
| 675 | // Hotfix for menu items |
| 676 | if($post->post_type == 'nav_menu_item') { return $post_id; } |
| 677 | |
| 678 | // Ignore auto-drafts & removed posts |
| 679 | if(in_array($post->post_status, array('auto-draft', 'trash'))) { return; } |
| 680 | |
| 681 | // Get auto-update URI setting (if empty use global setting) |
| 682 | $auto_update_uri_current = (!empty($_POST["auto_update_uri"])) ? intval($_POST["auto_update_uri"]) : 0; |
| 683 | $auto_update_uri = (!empty($_POST["auto_update_uri"])) ? $auto_update_uri_current : $permalink_manager_options["general"]["auto_update_uris"]; |
| 684 | |
| 685 | $default_uri = self::get_default_post_uri($post_id); |
| 686 | $native_uri = self::get_default_post_uri($post_id, true); |
| 687 | $old_uri = (isset($permalink_manager_uris[$post->ID])) ? $permalink_manager_uris[$post->ID] : $native_uri; |
| 688 | |
| 689 | // Use default URI if URI is cleared by user OR URI should be automatically updated |
| 690 | $new_uri = (($_POST['custom_uri'] == '') || $auto_update_uri == 1) ? $default_uri : Permalink_Manager_Helper_Functions::sanitize_title($_POST['custom_uri']); |
| 691 | |
| 692 | // Save or remove "Auto-update URI" settings |
| 693 | if(!empty($auto_update_uri_current)) { |
| 694 | update_post_meta($post_id, "auto_update_uri", $auto_update_uri_current); |
| 695 | } elseif(isset($_POST['auto_update_uri'])) { |
| 696 | delete_post_meta($post_id, "auto_update_uri"); |
| 697 | } |
| 698 | |
| 699 | // Save only changed URIs |
| 700 | if($new_uri != $old_uri) { |
| 701 | $permalink_manager_uris[$post->ID] = $new_uri; |
| 702 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 703 | } |
| 704 | |
| 705 | do_action('permalink-manager-updated-post-uri', $post_id, $new_uri, $old_uri, $native_uri, $default_uri, $single_update = true); |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Remove URI from options array after post is moved to the trash |
| 710 | */ |
| 711 | function remove_post_uri($post_id) { |
| 712 | global $permalink_manager_uris; |
| 713 | |
| 714 | // Check if the custom permalink is assigned to this post |
| 715 | if(isset($permalink_manager_uris[$post_id])) { |
| 716 | unset($permalink_manager_uris[$post_id]); |
| 717 | } |
| 718 | |
| 719 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 720 | } |
| 721 | |
| 722 | } |
| 723 | |
| 724 | ?> |
| 725 |