Make WordPress Core

Ticket #36711: 36711.4.patch

File 36711.4.patch, 2.9 KB (added by spacedmonkey, 9 years ago)
  • src/wp-includes/post.php

     
    42414241        $in_string = "'" . implode( "','", $parts ) . "'";
    42424242
    42434243        if ( is_array( $post_type ) ) {
     4244                $last_changed = wp_cache_get( 'last_changed', 'posts' );
     4245                if ( ! $last_changed ) {
     4246                        $last_changed = microtime();
     4247                        wp_cache_set( 'last_changed', $last_changed, 'posts' );
     4248                }
     4249                $cache_key  = md5( serialize( $post_type ) ) . ':' . sanitize_key( $page_path ) . ':' . $last_changed;
    42444250                $post_types = $post_type;
    42454251        } else {
    42464252                $post_types = array( $post_type, 'attachment' );
     4253                $cache_key  = $post_type . ':' . sanitize_key( $page_path );
    42474254        }
    42484255
     4256        if ( $cache = wp_cache_get( $cache_key, 'get_page_by_path' ) ) {
     4257           return get_post( $cache, $output );
     4258        }
     4259
    42494260        $post_types = esc_sql( $post_types );
    42504261        $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
     4262
    42514263        $sql = "
    42524264                SELECT ID, post_name, post_parent, post_type
    42534265                FROM $wpdb->posts
     
    42844296                        }
    42854297                }
    42864298        }
     4299   
     4300        wp_cache_set( $cache_key, $foundid, 'posts' );
    42874301
    42884302        if ( $foundid ) {
    42894303                return get_post( $foundid, $output );
     
    56545668
    56555669        wp_cache_delete( 'wp_get_archives', 'general' );
    56565670
     5671        $page_path = get_page_uri( $post->ID );
     5672        wp_cache_delete( $post->post_type . ':' . sanitize_key( $page_path ), 'get_page_by_path' );
     5673
    56575674        /**
    56585675         * Fires immediately after the given post's cache is cleaned.
    56595676         *
  • tests/phpunit/tests/post.php

     
    565565                $this->assertEquals( $other_att, get_page_by_path( 'some-other-page' ) );
    566566        }
    567567
     568
     569        /**
     570         * @ticket 36711
     571         */
     572        function test_get_page_by_path_cache() {
     573                global $wpdb;
     574
     575                $page       = self::factory()->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) );
     576                $wpdb->update( $wpdb->posts, array( 'post_name' => 'some-page' ), array( 'ID' => $page->ID ) );
     577                clean_post_cache( $page->ID );
     578
     579                $page = get_post( $page->ID );
     580
     581                $this->assertEquals( 'some-page', $page->post_name );
     582                $this->assertEquals( $page, get_page_by_path( 'some-page' ) );
     583
     584                $wpdb->update( $wpdb->posts, array( 'post_name' => 'another-url' ), array( 'ID' => $page->ID ) );
     585                clean_post_cache( $page->ID );
     586
     587                $page = get_post( $page->ID );
     588                // Test changing url
     589                $this->assertEquals( 'another-url', $page->post_name );
     590                $this->assertEquals( $page, get_page_by_path( 'another-url' ) );
     591
     592                $num_queries = $wpdb->num_queries;
     593                $this->assertEquals( $page, get_page_by_path( 'another-url' ) );
     594                // Test caching
     595                $this->assertEquals( $num_queries, $wpdb->num_queries );
     596
     597        }
     598
    568599        function test_wp_publish_post() {
    569600                $draft_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
    570601