permalink-manager-actions.php
9 years ago
permalink-manager-admin-functions.php
9 years ago
permalink-manager-helper-functions.php
9 years ago
permalink-manager-uri-functions-post.php
9 years ago
permalink-manager-uri-functions-post.php
512 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_filter( 'get_sample_permalink_html', array($this, 'edit_uri_box'), 999, 5 ); |
| 10 | add_action( 'post_submitbox_misc_actions', array($this, 'display_post_link'), 99); |
| 11 | |
| 12 | add_filter( '_get_page_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 13 | add_filter( 'page_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 14 | add_filter( 'post_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 15 | add_filter( 'post_type_link', array($this, 'custom_post_permalinks'), 999, 2); |
| 16 | |
| 17 | add_filter( 'permalink-manager-uris', array($this, 'exclude_homepage'), 999, 2); |
| 18 | add_action( 'save_post', array($this, 'update_post_uri'), 10, 3 ); |
| 19 | add_action( 'wp_trash_post', array($this, 'remove_post_uri'), 10, 3 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Change permalinks for posts, pages & custom post types |
| 24 | */ |
| 25 | function custom_post_permalinks($permalink, $post) { |
| 26 | global $wp_rewrite, $permalink_manager_uris, $sitepress_settings; |
| 27 | |
| 28 | $post = (is_integer($post)) ? get_post($post) : $post; |
| 29 | $post_type = $post->post_type; |
| 30 | |
| 31 | // 1A. Do not change permalink of frontpage |
| 32 | if(get_option('page_on_front') == $post->ID) { |
| 33 | return $permalink; |
| 34 | } |
| 35 | // 1B. Do not change permalink for drafts and future posts (+ remove trailing slash from them) |
| 36 | else if(in_array($post->post_status, array('draft', 'pending', 'auto-draft', 'future'))) { |
| 37 | return trim($permalink, "/"); |
| 38 | } |
| 39 | |
| 40 | // 2. Apend the language code as a non-editable prefix |
| 41 | if(isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 1) { |
| 42 | $post_lang_details = apply_filters('wpml_post_language_details', NULL, $post->ID); |
| 43 | $language_code = (!empty($post_lang_details['language_code'])) ? "{$post_lang_details['language_code']}/" : ''; |
| 44 | |
| 45 | // Hide language code if "Use directory for default language" option is enabled |
| 46 | $default_language = Permalink_Manager_Helper_Functions::get_language(); |
| 47 | if(isset($sitepress_settings['urls']['directory_for_default_language']) && ($sitepress_settings['urls']['directory_for_default_language'] == 0) && ($default_language == $post_lang_details['language_code'])) { |
| 48 | $language_code = ""; |
| 49 | } |
| 50 | } else { |
| 51 | $language_code = ""; |
| 52 | } |
| 53 | |
| 54 | // 3. Filter only the posts with custom permalink assigned |
| 55 | if(isset($permalink_manager_uris[$post->ID])) { $permalink = get_option('home') . "/{$language_code}{$permalink_manager_uris[$post->ID]}"; } |
| 56 | |
| 57 | // 4. Additional filter |
| 58 | $permalink = apply_filters('permalink_manager_filter_final_post_permalink', user_trailingslashit($permalink), $post); |
| 59 | |
| 60 | return $permalink; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if the provided slug is unique and then update it with SQL query. |
| 65 | */ |
| 66 | static function update_slug_by_id($slug, $id) { |
| 67 | global $wpdb; |
| 68 | |
| 69 | // Update slug and make it unique |
| 70 | $slug = (empty($slug)) ? sanitize_title(get_the_title($id)) : $slug; |
| 71 | $new_slug = wp_unique_post_slug($slug, $id, get_post_status($id), get_post_type($id), null); |
| 72 | $wpdb->query("UPDATE $wpdb->posts SET post_name = '$new_slug' WHERE ID = '$id'"); |
| 73 | |
| 74 | return $new_slug; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the active URI |
| 79 | */ |
| 80 | public static function get_post_uri($post_id, $native_uri = false) { |
| 81 | global $permalink_manager_uris; |
| 82 | |
| 83 | // Check if input is post object |
| 84 | $post_id = (isset($post_id->ID)) ? $post_id->ID : $post_id; |
| 85 | |
| 86 | $final_uri = (!empty($permalink_manager_uris[$post_id])) ? $permalink_manager_uris[$post_id] : self::get_default_post_uri($post_id, $native_uri); |
| 87 | return $final_uri; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the default (not overwritten by the user) or native URI (unfiltered) |
| 92 | */ |
| 93 | public static function get_default_post_uri($post, $native_uri = false) { |
| 94 | global $permalink_manager_options, $permalink_manager_uris, $permalink_manager_permastructs, $sitepress_settings; |
| 95 | |
| 96 | // Load all bases & post |
| 97 | $post = is_object($post) ? $post : get_post($post); |
| 98 | |
| 99 | // Check if post ID is defined |
| 100 | if(empty($post->ID)) { return ''; } |
| 101 | $post_id = $post->ID; |
| 102 | $post_type = $post->post_type; |
| 103 | $post_name = (empty($post->post_name)) ? sanitize_title($post->post_title) : $post->post_name; |
| 104 | |
| 105 | // Get the permastruct |
| 106 | $default_permastruct = Permalink_Manager_Helper_Functions::get_default_permastruct($post_type); |
| 107 | if($native_uri) { |
| 108 | $permastruct = $default_permastruct; |
| 109 | } else { |
| 110 | $permastruct = (!empty($permalink_manager_permastructs['post_types'][$post_type])) ? $permalink_manager_permastructs['post_types'][$post_type] : $default_permastruct; |
| 111 | } |
| 112 | |
| 113 | $default_base = (!empty($permastruct)) ? trim($permastruct, '/') : ""; |
| 114 | |
| 115 | // 1A. Get the date |
| 116 | $date = explode(" ", date('Y m d H i s', strtotime($post->post_date))); |
| 117 | |
| 118 | // 1B. Get the author (if needed) |
| 119 | $author = ''; |
| 120 | if ( strpos($default_base, '%author%') !== false ) { |
| 121 | $authordata = get_userdata($post->post_author); |
| 122 | $author = $authordata->user_nicename; |
| 123 | } |
| 124 | |
| 125 | // 2. Fix for hierarchical CPT (start) |
| 126 | $full_slug = ($native_uri == false) ? apply_filters('permalink_manager_filter_default_post_slug', get_page_uri($post), $post, $post_name) : get_page_uri($post); |
| 127 | $full_slug = (empty($full_slug)) ? $post_name : $full_slug; |
| 128 | $post_type_tag = Permalink_Manager_Helper_Functions::get_post_tag($post_type); |
| 129 | |
| 130 | // 3A. Do the replacement (post tag is removed now to enable support for hierarchical CPT) |
| 131 | $tags = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%author%', $post_type_tag); |
| 132 | $replacements = array($date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->ID, $author, ''); |
| 133 | $default_uri = str_replace($tags, $replacements, "{$default_base}/{$full_slug}"); |
| 134 | |
| 135 | // 3B. Replace taxonomies |
| 136 | $taxonomies = get_taxonomies(); |
| 137 | |
| 138 | if($taxonomies) { |
| 139 | foreach($taxonomies as $taxonomy) { |
| 140 | // A. Try to use Yoast SEO Primary Term |
| 141 | $replacement = Permalink_Manager_Helper_Functions::get_primary_term($post->ID, $taxonomy); |
| 142 | |
| 143 | // B. Get the first assigned term to this taxonomy |
| 144 | if(empty($replacement)) { |
| 145 | $terms = wp_get_object_terms($post->ID, $taxonomy); |
| 146 | $replacement = (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) ? $terms[0]->slug : ""; |
| 147 | } |
| 148 | |
| 149 | // Do the replacement |
| 150 | $default_uri = (!empty($replacement)) ? str_replace("%{$taxonomy}%", $replacement, $default_uri) : $default_uri; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $default_uri = preg_replace('/\s+/', '', $default_uri); |
| 155 | $default_uri = str_replace('//', '/', $default_uri); |
| 156 | $default_uri = trim($default_uri, "/"); |
| 157 | |
| 158 | return $default_uri; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * The homepage should not use URI |
| 163 | */ |
| 164 | function exclude_homepage($uris) { |
| 165 | // Find the homepage URI |
| 166 | $homepage_id = get_option('page_on_front'); |
| 167 | if(isset($uris[$homepage_id])) { unset($uris[$homepage_id]); } |
| 168 | |
| 169 | return $uris; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Find & replace (bulk action) |
| 174 | */ |
| 175 | public static function find_and_replace() { |
| 176 | global $wpdb, $permalink_manager_uris; |
| 177 | |
| 178 | // Check if post types & statuses are not empty |
| 179 | if(empty($_POST['post_types']) || empty($_POST['post_statuses'])) { return false; } |
| 180 | |
| 181 | // Get homepage URL and ensure that it ends with slash |
| 182 | $home_url = trim(get_option('home'), "/") . "/"; |
| 183 | |
| 184 | // Reset variables |
| 185 | $updated_slugs_count = 0; |
| 186 | $updated_array = array(); |
| 187 | $alert_type = $alert_content = $errors = ''; |
| 188 | |
| 189 | // Prepare default variables from $_POST object |
| 190 | $old_string = str_replace($home_url, '', esc_sql($_POST['old_string'])); |
| 191 | $new_string = str_replace($home_url, '', esc_sql($_POST['new_string'])); |
| 192 | $mode = isset($_POST['mode']) ? $_POST['mode'] : array('both'); |
| 193 | $post_types_array = ($_POST['post_types']); |
| 194 | $post_statuses_array = ($_POST['post_statuses']); |
| 195 | $post_types = implode("', '", $post_types_array); |
| 196 | $post_statuses = implode("', '", $post_statuses_array); |
| 197 | |
| 198 | // Save the rows before they are updated to an array |
| 199 | $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); |
| 200 | |
| 201 | // Now if the array is not empty use IDs from each subarray as a key |
| 202 | if($posts_to_update && empty($errors)) { |
| 203 | foreach ($posts_to_update as $row) { |
| 204 | |
| 205 | // Prepare variables |
| 206 | $old_post_name = $row['post_name']; |
| 207 | $native_uri = self::get_default_post_uri($row['ID'], true); |
| 208 | $default_uri = self::get_default_post_uri($row['ID']); |
| 209 | $old_uri = (isset($permalink_manager_uris[$row['ID']])) ? $permalink_manager_uris[$row['ID']] : $default_uri; |
| 210 | $old_slug = (strpos($old_uri, '/') !== false) ? substr($old_uri, strrpos($old_uri, '/') + 1) : $old_uri; |
| 211 | |
| 212 | // Do replacement on slugs (non-REGEX) |
| 213 | if(preg_match("/^\/.+\/[a-z]*$/i", $old_string)) { |
| 214 | // Use $_POST['old_string'] directly here & fix double slashes problem |
| 215 | $pattern = "~" . stripslashes(trim($_POST['old_string'], "/")) . "~"; |
| 216 | |
| 217 | $new_post_name = (in_array($mode, array('post_names'))) ? preg_replace($pattern, $new_string, $old_post_name) : $old_post_name; |
| 218 | $new_uri = preg_replace($pattern, $new_string, $old_uri); |
| 219 | } |
| 220 | else { |
| 221 | $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 |
| 222 | $new_uri = str_replace($old_string, $new_string, $old_uri); |
| 223 | } |
| 224 | |
| 225 | //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n"); |
| 226 | |
| 227 | // Check if native slug should be changed |
| 228 | if(in_array($mode, array('post_names')) && ($old_post_name != $new_post_name)) { |
| 229 | self::update_slug_by_id($new_post_name, $row['ID']); |
| 230 | } |
| 231 | |
| 232 | if(($old_uri != $new_uri) || ($old_post_name != $new_post_name) && !(empty($new_uri))) { |
| 233 | $permalink_manager_uris[$row['ID']] = $new_uri; |
| 234 | $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); |
| 235 | $updated_slugs_count++; |
| 236 | } |
| 237 | |
| 238 | // Do not store default values |
| 239 | if(isset($permalink_manager_uris[$row['ID']]) && ($new_uri == $native_uri)) { |
| 240 | unset($permalink_manager_uris[$row['ID']]); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Filter array before saving |
| 245 | $permalink_manager_uris = array_filter($permalink_manager_uris); |
| 246 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 247 | |
| 248 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 249 | wp_reset_postdata(); |
| 250 | } |
| 251 | |
| 252 | return ($output) ? $output : ""; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Regenerate slugs & bases (bulk action) |
| 257 | */ |
| 258 | static function regenerate_all_permalinks() { |
| 259 | global $wpdb, $permalink_manager_uris; |
| 260 | |
| 261 | // Check if post types & statuses are not empty |
| 262 | if(empty($_POST['post_types']) || empty($_POST['post_statuses'])) { return false; } |
| 263 | |
| 264 | // Setup needed variables |
| 265 | $updated_slugs_count = 0; |
| 266 | $updated_array = array(); |
| 267 | $alert_type = $alert_content = $errors = ''; |
| 268 | |
| 269 | $post_types_array = ($_POST['post_types']) ? ($_POST['post_types']) : ''; |
| 270 | $post_statuses_array = ($_POST['post_statuses']) ? $_POST['post_statuses'] : ''; |
| 271 | $post_types = implode("', '", $post_types_array); |
| 272 | $post_statuses = implode("', '", $post_statuses_array); |
| 273 | $mode = isset($_POST['mode']) ? $_POST['mode'] : 'both'; |
| 274 | |
| 275 | // Save the rows before they are updated to an array |
| 276 | $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); |
| 277 | |
| 278 | // Now if the array is not empty use IDs from each subarray as a key |
| 279 | if($posts_to_update && empty($errors)) { |
| 280 | foreach ($posts_to_update as $row) { |
| 281 | $updated = 0; |
| 282 | |
| 283 | // Prepare variables |
| 284 | $old_post_name = $row['post_name']; |
| 285 | $native_uri = self::get_default_post_uri($row['ID'], true); |
| 286 | $default_uri = self::get_default_post_uri($row['ID']); |
| 287 | $old_uri = isset($permalink_manager_uris[$row['ID']]) ? trim($permalink_manager_uris[$row['ID']], "/") : $native_uri; |
| 288 | $old_slug = (strpos($old_uri, '/') !== false) ? substr($old_uri, strrpos($old_uri, '/') + 1) : $old_uri; |
| 289 | $correct_slug = sanitize_title($row['post_title']); |
| 290 | |
| 291 | // Process URI & slug |
| 292 | $new_slug = wp_unique_post_slug($correct_slug, $row['ID'], get_post_status($row['ID']), get_post_type($row['ID']), null); |
| 293 | $new_post_name = (in_array($mode, array('post_names'))) ? $new_slug : $old_post_name; // Post name is changed only in first mode |
| 294 | $new_uri = (in_array($mode, array('both'))) ? $default_uri : str_replace($old_slug, $new_slug, $old_uri); |
| 295 | |
| 296 | //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri} \n"); |
| 297 | |
| 298 | // Check if native slug should be changed |
| 299 | if(in_array($mode, array('post_names')) && ($old_post_name != $new_post_name)) { |
| 300 | self::update_slug_by_id($new_post_name, $row['ID']); |
| 301 | } |
| 302 | |
| 303 | if(($old_uri != $new_uri) || ($old_post_name != $new_post_name)) { |
| 304 | $permalink_manager_uris[$row['ID']] = $new_uri; |
| 305 | $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); |
| 306 | $updated_slugs_count++; |
| 307 | } |
| 308 | |
| 309 | // Do not store default values |
| 310 | if(isset($permalink_manager_uris[$row['ID']]) && ($new_uri == $native_uri)) { |
| 311 | unset($permalink_manager_uris[$row['ID']]); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Filter array before saving |
| 316 | $permalink_manager_uris = array_filter($permalink_manager_uris); |
| 317 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 318 | |
| 319 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 320 | wp_reset_postdata(); |
| 321 | } |
| 322 | |
| 323 | return (!empty($output)) ? $output : ""; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Update all slugs & bases (bulk action) |
| 328 | */ |
| 329 | static public function update_all_permalinks() { |
| 330 | global $permalink_manager_uris; |
| 331 | |
| 332 | // Setup needed variables |
| 333 | $updated_slugs_count = 0; |
| 334 | $updated_array = array(); |
| 335 | |
| 336 | $old_uris = $permalink_manager_uris; |
| 337 | $new_uris = isset($_POST['uri']) ? $_POST['uri'] : array(); |
| 338 | |
| 339 | // Double check if the slugs and ids are stored in arrays |
| 340 | if (!is_array($new_uris)) $new_uris = explode(',', $new_uris); |
| 341 | |
| 342 | if (!empty($new_uris)) { |
| 343 | foreach($new_uris as $id => $new_uri) { |
| 344 | // Prepare variables |
| 345 | $this_post = get_post($id); |
| 346 | $updated = ''; |
| 347 | |
| 348 | // Get default & native URL |
| 349 | $native_uri = self::get_default_post_uri($id, true); |
| 350 | $default_uri = self::get_default_post_uri($id); |
| 351 | |
| 352 | $old_uri = isset($old_uris[$id]) ? trim($old_uris[$id], "/") : $native_uri; |
| 353 | |
| 354 | // Process new values - empty entries will be treated as default values |
| 355 | $new_uri = preg_replace('/\s+/', '', $new_uri); |
| 356 | $new_uri = (!empty($new_uri)) ? trim($new_uri, "/") : $default_uri; |
| 357 | $new_slug = (strpos($new_uri, '/') !== false) ? substr($new_uri, strrpos($new_uri, '/') + 1) : $new_uri; |
| 358 | |
| 359 | //print_r("{$old_uri} - {$new_uri} - {$native_uri} - {$default_uri}\n"); |
| 360 | |
| 361 | if($new_uri != $old_uri) { |
| 362 | $old_uris[$id] = $new_uri; |
| 363 | $updated_array[] = array('post_title' => get_the_title($id), 'ID' => $id, 'old_uri' => $old_uri, 'new_uri' => $new_uri); |
| 364 | $updated_slugs_count++; |
| 365 | } |
| 366 | |
| 367 | // Do not store native URIs |
| 368 | if($new_uri == $native_uri) { |
| 369 | unset($old_uris[$id]); |
| 370 | } |
| 371 | |
| 372 | } |
| 373 | |
| 374 | // Filter array before saving & append the global |
| 375 | $old_uris = $permalink_manager_uris = array_filter($old_uris); |
| 376 | update_option('permalink-manager-uris', $old_uris); |
| 377 | |
| 378 | //print_r($permalink_manager_uris); |
| 379 | |
| 380 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 381 | } |
| 382 | |
| 383 | return ($output) ? $output : ""; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Allow to edit URIs from "Edit Post" admin pages |
| 388 | */ |
| 389 | function edit_uri_box($html, $id, $new_title, $new_slug, $post) { |
| 390 | global $permalink_manager_uris, $sitepress_settings; |
| 391 | |
| 392 | // Detect auto drafts |
| 393 | $autosave = (!empty($new_title) && empty($new_slug)) ? true : false; |
| 394 | |
| 395 | // Get the post object |
| 396 | $post = get_post($id); |
| 397 | |
| 398 | // Do not do anything if new slug is empty or page is front-page |
| 399 | if( get_option('page_on_front') == $id) { return $html; } |
| 400 | |
| 401 | $html = preg_replace("/(<strong>(.*)<\/strong>)(.*)/is", "$1 ", $html); |
| 402 | $default_uri = self::get_default_post_uri($id); |
| 403 | |
| 404 | // Make sure that home URL ends with slash |
| 405 | $home_url = trim(get_option('home'), "/") . "/"; |
| 406 | |
| 407 | // WPML - apend the language code as a non-editable prefix |
| 408 | if(isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 1) { |
| 409 | $post_lang_details = apply_filters('wpml_post_language_details', NULL, $id); |
| 410 | $language_code = (!empty($post_lang_details['language_code'])) ? $post_lang_details['language_code'] : ''; |
| 411 | |
| 412 | // Last instance - use language paramater from &_GET array |
| 413 | $language_code = (empty($language_code) && !empty($_GET['lang'])) ? $_GET['lang'] : $language_code; |
| 414 | |
| 415 | // Hide language code if "Use directory for default language" option is enabled |
| 416 | $default_language = Permalink_Manager_Helper_Functions::get_language(); |
| 417 | if(isset($sitepress_settings['urls']['directory_for_default_language']) && ($sitepress_settings['urls']['directory_for_default_language'] == 0) && ($default_language == $language_code)) { |
| 418 | $language_code = ''; |
| 419 | } |
| 420 | } else { |
| 421 | $language_code = ""; |
| 422 | } |
| 423 | |
| 424 | // Append slash to the end of language code if it is not empty |
| 425 | $language_code .= ($language_code) ? "/" : ""; |
| 426 | |
| 427 | // Do not change anything if post is not saved yet (display sample permalink instead) |
| 428 | if($autosave || empty($post->post_status)) { |
| 429 | $sample_permalink = $home_url . str_replace("//", "/", trim("{$language_code}{$default_uri}", "/")); |
| 430 | |
| 431 | $html .= "<span><a href=\"{$sample_permalink}\">{$sample_permalink}</a></span>"; |
| 432 | } else { |
| 433 | $uri = (!empty($permalink_manager_uris[$id])) ? $permalink_manager_uris[$id] : $default_uri; |
| 434 | $html .= "{$home_url}{$language_code} <span id=\"editable-post-name\"><input type='text' value='{$uri}' name='custom_uri'/></span>"; |
| 435 | } |
| 436 | |
| 437 | return $html; |
| 438 | } |
| 439 | |
| 440 | function display_post_link($post) { |
| 441 | global $permalink_manager_uris; |
| 442 | |
| 443 | // Do not display the link on drafts & scheduled posts |
| 444 | if(in_array($post->post_status, array('draft', 'pending', 'auto-draft', 'future'))) { return; } |
| 445 | |
| 446 | $button_text = __('Go to the post', 'permalink-manager'); |
| 447 | $permalink = get_permalink($post->ID); |
| 448 | |
| 449 | $html = "<div class=\"misc-pub-section permalink-manager-link\" id=\"permalink-manager\">"; |
| 450 | $html .= "<span class=\"image yoast-logo svg bad\"></span><span class=\"score-text\">" . PERMALINK_MANAGER_PLUGIN_NAME . ": <a href=\"{$permalink}\" target=\"_blank\">{$button_text}</a></div>"; |
| 451 | |
| 452 | echo $html; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Update URI from "Edit Post" admin page |
| 457 | */ |
| 458 | function update_post_uri($post_id, $post, $update) { |
| 459 | global $permalink_manager_uris, $permalink_manager_options, $permalink_manager_before_sections_html; |
| 460 | |
| 461 | // Fix for revisions |
| 462 | $is_revision = wp_is_post_revision($post_id); |
| 463 | $post_id = ($is_revision) ? $is_revision : $post_id; |
| 464 | $post = get_post($post_id); |
| 465 | |
| 466 | // Ignore auto-drafts & removed posts |
| 467 | if(in_array($post->post_status, array('auto-draft', 'trash')) || empty($post->post_name) || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) { return; } |
| 468 | |
| 469 | $default_uri = self::get_default_post_uri($post_id); |
| 470 | $native_uri = self::get_default_post_uri($post_id, true); |
| 471 | $old_uri = (isset($permalink_manager_uris[$post->ID])) ? $permalink_manager_uris[$post->ID] : $native_uri; |
| 472 | |
| 473 | // Use default URI if URI is cleared by user |
| 474 | $new_uri = (!empty($_POST['custom_uri'])) ? trim($_POST['custom_uri'], "/") : $default_uri; |
| 475 | |
| 476 | // Do not store default values |
| 477 | if(isset($permalink_manager_uris[$post->ID]) && ($new_uri == $native_uri)) { |
| 478 | unset($permalink_manager_uris[$post->ID]); |
| 479 | } |
| 480 | // Save only changed URIs |
| 481 | else if (($new_uri != $native_uri) && ($new_uri != $old_uri)) { |
| 482 | $permalink_manager_uris[$post->ID] = $new_uri; |
| 483 | } |
| 484 | |
| 485 | if(!empty($permalink_manager_options['miscellaneous']['debug_mode'])) { |
| 486 | $debug_data = "<pre>" . print_r($post, true) . "</pre><br />"; |
| 487 | $debug_data .= "Update: " . print_r($update, true); |
| 488 | $debug_data .= "<br/>Native URI: <em>{$native_uri}</em><br/>Defualt URI: <em>{$default_uri}</em><br/>New URI: <em>{$new_uri}</em><br/>"; |
| 489 | wp_die($debug_data); |
| 490 | } |
| 491 | |
| 492 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Remove URI from options array after post is moved to the trash |
| 497 | */ |
| 498 | function remove_post_uri($post_id) { |
| 499 | global $permalink_manager_uris; |
| 500 | |
| 501 | // Check if the custom permalink is assigned to this post |
| 502 | if(isset($permalink_manager_uris[$post_id])) { |
| 503 | unset($permalink_manager_uris[$post_id]); |
| 504 | } |
| 505 | |
| 506 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 507 | } |
| 508 | |
| 509 | } |
| 510 | |
| 511 | ?> |
| 512 |