| 3653 | * Gets a unique post slug using a supplied SQL query for getting the latest appropriate slug. |
| 3654 | * |
| 3655 | * @global wpdb $wpdb WordPress database abstraction object. |
| 3656 | * |
| 3657 | * @param string $slug The desired slug (post_name). |
| 3658 | * @param string $last_slug_sql An SQL query returning the latest appropriate slug. |
| 3659 | * @param array $prep_args Parameters for the SQL query for using in $wpdb->prepare() call. |
| 3660 | */ |
| 3661 | function _get_unique_post_slug( $slug, $last_slug_sql, $prep_args ) { |
| 3662 | global $wpdb; |
| 3663 | |
| 3664 | $suffix = 2; |
| 3665 | $last_slug_prep = $wpdb->prepare($last_slug_sql, $prep_args); |
| 3666 | |
| 3667 | $last_slug = $wpdb->get_var($last_slug_prep); |
| 3668 | $last_suffix = substr($last_slug, strlen($slug) + 1); |
| 3669 | |
| 3670 | if ($last_suffix) |
| 3671 | $suffix = $last_suffix + 1; |
| 3672 | |
| 3673 | $slug = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-{$suffix}"; |
| 3674 | return $slug; |
| 3675 | } |
| 3676 | |
| 3677 | /** |
3693 | | $suffix = 2; |
3694 | | do { |
3695 | | $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
3696 | | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); |
3697 | | $suffix++; |
3698 | | } while ( $post_name_check ); |
3699 | | $slug = $alt_post_name; |
| 3718 | $last_slug_sql = "SELECT post_name FROM $wpdb->posts WHERE (post_name = %s OR post_name RLIKE %s) AND ID != %d ORDER BY LENGTH(post_name) DESC, post_name DESC LIMIT 1"; |
| 3719 | $slug = _get_unique_post_slug($slug, $last_slug_sql, array($slug, $wpdb->esc_like("^{$slug}-[0-9]+\$"), $post_ID)); |
3723 | | $suffix = 2; |
3724 | | do { |
3725 | | $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
3726 | | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); |
3727 | | $suffix++; |
3728 | | } while ( $post_name_check ); |
3729 | | $slug = $alt_post_name; |
| 3742 | $last_slug_sql = "SELECT post_name FROM $wpdb->posts WHERE (post_name = %s OR post_name RLIKE %s) AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d ORDER BY LENGTH(post_name) DESC, post_name DESC LIMIT 1"; |
| 3743 | $slug = _get_unique_post_slug($slug, $last_slug_sql, array($slug, $wpdb->esc_like("^{$slug}-[0-9]+\$"), $post_type, $post_ID, $post_parent)); |
3768 | | $suffix = 2; |
3769 | | do { |
3770 | | $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
3771 | | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
3772 | | $suffix++; |
3773 | | } while ( $post_name_check ); |
3774 | | $slug = $alt_post_name; |
| 3782 | $last_slug_sql = "SELECT post_name FROM $wpdb->posts WHERE (post_name = %s OR post_name RLIKE %s) AND post_type = %s AND ID != %d ORDER BY LENGTH(post_name) DESC, post_name DESC LIMIT 1"; |
| 3783 | $slug = _get_unique_post_slug($slug, $last_slug_sql, array($slug, $wpdb->esc_like("^{$slug}-[0-9]+\$"), $post_type, $post_ID)); |