Make WordPress Core

Ticket #39603: constant-time-slug-generation-2.diff

File constant-time-slug-generation-2.diff, 4.7 KB (added by bisyarin, 8 years ago)

The Second Patch of performance improving

  • C:\Users\User\Documents\wordpress\develop.svn.wordpress.org\src\wp-includes\

    old new  
    36503650}
    36513651
    36523652/**
     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 */
     3661function _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/**
    36533678 * Computes a unique slug for the post, when given the desired slug and some post details.
    36543679 *
    36553680 * @since 2.8.0
     
    36903715                 * @param string $slug     The post slug.
    36913716                 */
    36923717                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;
     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));
    37003720                }
    37013721        } elseif ( is_post_type_hierarchical( $post_type ) ) {
    37023722                if ( 'nav_menu_item' == $post_type )
    37033723                        return $slug;
    3704 
    37053724                /*
    37063725                 * Page slugs must be unique within their own trees. Pages are in a separate
    37073726                 * namespace than posts so page slugs are allowed to overlap post slugs.
    37083727                 */
    37093728                $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";
    37103729                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
    3711 
     3730               
    37123731                /**
    37133732                 * Filters whether the post slug would make a bad hierarchical post slug.
    37143733                 *
     
    37203739                 * @param int    $post_parent Post parent ID.
    37213740                 */
    37223741                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;
     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));
    37303744                }
    37313745        } else {
    37323746                // Post slugs must be unique across all posts.
     
    37653779                 * @param string $post_type Post type.
    37663780                 */
    37673781                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;
     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));
    37753784                }
    37763785        }
    37773786