Make WordPress Core

Ticket #56489: 56489.diff

File 56489.diff, 2.5 KB (added by david.binda, 3 years ago)
  • src/wp-includes/class-wp-embed.php

     
    248248                $cache      = '';
    249249                $cache_time = 0;
    250250
    251                 $cached_post_id = $this->find_oembed_post_id( $key_suffix );
    252 
    253251                if ( $post_ID ) {
    254252                        $cache      = get_post_meta( $post_ID, $cachekey, true );
    255253                        $cache_time = get_post_meta( $post_ID, $cachekey_time, true );
     
    257255                        if ( ! $cache_time ) {
    258256                                $cache_time = 0;
    259257                        }
    260                 } elseif ( $cached_post_id ) {
    261                         $cached_post = get_post( $cached_post_id );
     258                } else {
     259                        $cached_post_id = $this->find_oembed_post_id( $key_suffix );
     260                        if ( $cached_post_id ) {
     261                                $cached_post = get_post( $cached_post_id );
    262262
    263                         $cache      = $cached_post->post_content;
    264                         $cache_time = strtotime( $cached_post->post_modified_gmt );
     263                                $cache      = $cached_post->post_content;
     264                                $cache_time = strtotime( $cached_post->post_modified_gmt );
     265                        }
    265266                }
    266267
    267268                $cached_recently = ( time() - $cache_time ) < $ttl;
  • tests/phpunit/tests/oembed/WpEmbed.php

     
    377377                $this->wp_embed->linkifunknown = false;
    378378                $this->assertSame( $url, $this->wp_embed->maybe_make_link( $url ) );
    379379        }
     380
     381        /**
     382         * Test that there is no extra oembed_cache query in case global post is set.
     383         *
     384         * @ticket 56489
     385         */
     386        public function test_shortcode_no_extra_queries_for_known_post() {
     387                global $post, $wpdb;
     388
     389                $post          = $this->factory()->post->create_and_get();
     390                $url           = 'https://example.com/';
     391                $expected      = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
     392                $key_suffix    = md5( $url . serialize( wp_embed_defaults( $url ) ) );
     393                $cachekey      = '_oembed_' . $key_suffix;
     394                $cachekey_time = '_oembed_time_' . $key_suffix;
     395
     396                add_post_meta( $post->ID, $cachekey, '{{unknown}}' );
     397                add_post_meta( $post->ID, $cachekey_time, 0 );
     398
     399                add_filter( 'pre_oembed_result', '__return_empty_string' );
     400                $actual = $this->wp_embed->shortcode( array(), $url );
     401                remove_filter( 'pre_oembed_result', '__return_empty_string' );
     402
     403                $num_queries = $wpdb->num_queries;
     404
     405                // Result should be cached.
     406                $actual_2 = $this->wp_embed->shortcode( array(), $url );
     407
     408                // Cleanup.
     409                unset( $post );
     410
     411                $this->assertSame( $num_queries, $wpdb->num_queries );
     412        }
    380413}