Make WordPress Core

Ticket #56689: 56689.2.diff

File 56689.2.diff, 2.7 KB (added by SergeyBiryukov, 2 years ago)
  • src/wp-includes/post.php

     
    57045704                }
    57055705        }
    57065706
    5707         $page_path     = rawurlencode( urldecode( $page_path ) );
    5708         $page_path     = str_replace( '%2F', '/', $page_path );
    5709         $page_path     = str_replace( '%20', ' ', $page_path );
    5710         $parts         = explode( '/', trim( $page_path, '/' ) );
    5711         $parts         = array_map( 'sanitize_title_for_query', $parts );
    5712         $escaped_parts = esc_sql( $parts );
     5707        $page_path = rawurlencode( urldecode( $page_path ) );
     5708        $page_path = str_replace( '%2F', '/', $page_path );
     5709        $page_path = str_replace( '%20', ' ', $page_path );
     5710        $parts     = explode( '/', trim( $page_path, '/' ) );
     5711        $parts     = array_map( 'sanitize_title_for_query', $parts );
    57135712
    5714         $in_string = "'" . implode( "','", $escaped_parts ) . "'";
    5715 
    57165713        if ( is_array( $post_type ) ) {
    57175714                $post_types = $post_type;
    57185715        } else {
     
    57195716                $post_types = array( $post_type, 'attachment' );
    57205717        }
    57215718
    5722         $post_types          = esc_sql( $post_types );
    5723         $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
    5724         $sql                 = "
    5725                 SELECT ID, post_name, post_parent, post_type
    5726                 FROM $wpdb->posts
    5727                 WHERE post_name IN ($in_string)
    5728                 AND post_type IN ($post_type_in_string)
    5729         ";
     5719        /*
     5720         * Perform a direct SQL query when called from 'pre_get_posts',
     5721         * to avoid an infinite loop. Otherwise, use WP_Query.
     5722         */
     5723        if ( doing_action( 'pre_get_posts' ) ) {
     5724                $escaped_parts = esc_sql( $parts );
     5725                $in_string     = "'" . implode( "','", $escaped_parts ) . "'";
    57305726
    5731         $pages = $wpdb->get_results( $sql, OBJECT_K );
     5727                $post_types          = esc_sql( $post_types );
     5728                $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
     5729                $sql                 = "
     5730                        SELECT ID, post_name, post_parent, post_type
     5731                        FROM $wpdb->posts
     5732                        WHERE post_name IN ($in_string)
     5733                        AND post_type IN ($post_type_in_string)
     5734                ";
    57325735
     5736                $pages = (array) $wpdb->get_results( $sql, OBJECT_K );
     5737        } else {
     5738                $args = array(
     5739                        'post_name__in'          => $parts,
     5740                        'post_type'              => $post_types,
     5741                        'post_status'            => 'all',
     5742                        'posts_per_page'         => -1,
     5743                        'update_post_term_cache' => false,
     5744                        'update_post_meta_cache' => false,
     5745                        'no_found_rows'          => true,
     5746                        'orderby'                => 'none',
     5747                );
     5748
     5749                $query = new WP_Query( $args );
     5750                $posts = $query->get_posts();
     5751                $pages = array();
     5752
     5753                foreach ( $posts as $post ) {
     5754                        $pages[ $post->ID ] = $post;
     5755                }
     5756        }
     5757
    57335758        $revparts = array_reverse( $parts );
    57345759
    57355760        $foundid = 0;
    5736         foreach ( (array) $pages as $page ) {
     5761        foreach ( $pages as $page ) {
    57375762                if ( $page->post_name == $revparts[0] ) {
    57385763                        $count = 0;
    57395764                        $p     = $page;