Make WordPress Core

Ticket #39603: filling-gaps-slug-generation.diff

File filling-gaps-slug-generation.diff, 5.2 KB (added by bisyarin, 9 years ago)

Patch for post.php for faster slug generation with filling gaps approach

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

    old new  
    36503650}
    36513651
    36523652/**
     3653 * @param       string  Slug name without suffix
     3654 * @param       array   Naturally sorted array
     3655 */
     3656function _get_first_available_slug($base_slug, $arr) {
     3657        $base_slug = _truncate_post_slug($base_slug);
     3658
     3659        $suffix = 2; // if this function was called, then the base name has been taken
     3660                                 // (that is $base_slug is the first item of the array)
     3661       
     3662        for ($i = 1; $i < count($arr); $i++) {
     3663                $suffix = $i + 1;
     3664               
     3665                $actual_suffix = substr($arr[$i], strlen($base_slug) + 1);
     3666       
     3667                if ($suffix != $actual_suffix)
     3668                        break;
     3669                elseif ($i == count($arr) - 1)
     3670                        $suffix++;
     3671        }
     3672       
     3673        return _truncate_post_slug( $base_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-{$suffix}";
     3674}
     3675
     3676/**
     3677 * Gets a unique post slug using a supplied SQL query for getting the latest appropriate slug.
     3678 *
     3679 * @global wpdb  $wpdb                  WordPress database abstraction object.
     3680 *
     3681 * @param string $slug                  The desired slug (post_name).
     3682 * @param string $similar_slugs_sql     An SQL query returning the latest appropriate slug.
     3683 * @param array  $prep_args             Parameters for the SQL query for using in $wpdb->prepare() call.
     3684 */
     3685function _get_unique_post_slug( $slug, $similar_slugs_sql, $prep_args ) {
     3686        global $wpdb;
     3687       
     3688        $similar_slugs_prep = $wpdb->prepare($similar_slugs_sql, $prep_args);
     3689        $slugs = $wpdb->get_col($similar_slugs_prep);
     3690        $slug = _get_first_available_slug($slug, $slugs);
     3691       
     3692        return $slug;
     3693}
     3694
     3695/**
    36533696 * Computes a unique slug for the post, when given the desired slug and some post details.
    36543697 *
    36553698 * @since 2.8.0
     
    36903733                 * @param string $slug     The post slug.
    36913734                 */
    36923735                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;
     3736                        $similar_slugs_sql = "SELECT post_name FROM $wpdb->posts WHERE (post_name = %s OR post_name RLIKE %s) AND ID != %d ORDER BY LENGTH(post_name), post_name";
     3737                        $slug = _get_unique_post_slug($slug, $similar_slugs_sql, array($slug, $wpdb->esc_like("^{$slug}-[0-9]+\$"), $post_ID));
    37003738                }
    37013739        } elseif ( is_post_type_hierarchical( $post_type ) ) {
    37023740                if ( 'nav_menu_item' == $post_type )
    37033741                        return $slug;
    3704 
    37053742                /*
    37063743                 * Page slugs must be unique within their own trees. Pages are in a separate
    37073744                 * namespace than posts so page slugs are allowed to overlap post slugs.
    37083745                 */
    37093746                $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";
    37103747                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
    3711 
     3748               
    37123749                /**
    37133750                 * Filters whether the post slug would make a bad hierarchical post slug.
    37143751                 *
     
    37203757                 * @param int    $post_parent Post parent ID.
    37213758                 */
    37223759                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;
     3760                        $similar_slugs_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), post_name";
     3761                        $slug = _get_unique_post_slug($slug, $similar_slugs_sql, array($slug, $wpdb->esc_like("^{$slug}-[0-9]+\$"), $post_type, $post_ID, $post_parent));
    37303762                }
    37313763        } else {
    37323764                // Post slugs must be unique across all posts.
     
    37653797                 * @param string $post_type Post type.
    37663798                 */
    37673799                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;
     3800                        $similar_slugs_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), post_name";
     3801                        $slug = _get_unique_post_slug($slug, $similar_slugs_sql, array($slug, $wpdb->esc_like("^{$slug}-[0-9]+\$"), $post_type, $post_ID));
    37753802                }
    37763803        }
    37773804