Ticket #39603: constant-time-slug-generation.diff
File constant-time-slug-generation.diff, 4.3 KB (added by , 8 years ago) |
---|
-
C:\Users\User\Documents\www\wp\wp-includes\
old new 3650 3650 } 3651 3651 3652 3652 /** 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 /** 3653 3677 * Computes a unique slug for the post, when given the desired slug and some post details. 3654 3678 * 3655 3679 * @since 2.8.0 … … 3690 3714 * @param string $slug The post slug. 3691 3715 */ 3692 3716 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { 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; 3717 $last_slug_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name LIKE %s ORDER BY id DESC LIMIT 1"; 3718 $slug = _get_unique_post_slug($slug, $last_slug_sql, array($wpdb->esc_like($slug) . '%')); 3700 3719 } 3701 3720 } elseif ( is_post_type_hierarchical( $post_type ) ) { 3702 3721 if ( 'nav_menu_item' == $post_type ) 3703 3722 return $slug; 3704 3705 3723 /* 3706 3724 * Page slugs must be unique within their own trees. Pages are in a separate 3707 3725 * namespace than posts so page slugs are allowed to overlap post slugs. 3708 3726 */ 3709 3727 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; 3710 3728 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); 3711 3729 3712 3730 /** 3713 3731 * Filters whether the post slug would make a bad hierarchical post slug. 3714 3732 * … … 3720 3738 * @param int $post_parent Post parent ID. 3721 3739 */ 3722 3740 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { 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; 3741 $last_slug_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name LIKE %s AND post_type IN ( %s, 'attachment' ) AND post_parent = %d ORDER BY id DESC LIMIT 1"; 3742 $slug = _get_unique_post_slug($slug, $last_slug_sql, array($wpdb->esc_like($slug) . '%', $post_type, $post_parent)); 3730 3743 } 3731 3744 } else { 3732 3745 // Post slugs must be unique across all posts. … … 3765 3778 * @param string $post_type Post type. 3766 3779 */ 3767 3780 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { 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; 3781 $last_slug_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name LIKE %s AND post_type = %s ORDER BY id DESC LIMIT 1"; 3782 $slug = _get_unique_post_slug($slug, $last_slug_sql, array($wpdb->esc_like($slug) . '%', $post_type)); 3775 3783 } 3776 3784 } 3777 3785