WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/30/2017 01:14:34 AM (2 years ago)
Author:
westonruter
Message:

Embeds: Cache oEmbeds in an oembed_cache custom post type instead of postmeta when there is no global $post.

Add processing of embeds to rich Text widget.

Props swissspidy, westonruter, ocean90, johnbillion.
See #40854, #39994, #40935.
Fixes #34115.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/oembed/WpEmbed.php

    r38762 r41651  
    162162    }
    163163
    164     public function test_shortcode_should_cache_data_in_post_meta_for_known_post() {
    165         $GLOBALS['post'] = $this->factory()->post->create_and_get();
     164    public function test_shortcode_should_get_cached_data_from_post_meta_for_known_post() {
     165        global $post;
     166
     167        $post = $this->factory()->post->create_and_get();
    166168        $url             = 'https://example.com/';
    167169        $expected        = '<b>Embedded content</b>';
    168170        $key_suffix      = md5( $url . serialize( wp_embed_defaults( $url ) ) );
    169171        $cachekey        = '_oembed_' . $key_suffix;
    170         $cachekey_time   = '_oembed_time_' . $key_suffix;
     172
     173        add_post_meta( $post->ID, $cachekey, $expected );
    171174
    172175        add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     
    174177        remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
    175178
     179        $actual_2 = $this->wp_embed->shortcode( array(), $url );
     180
     181        $cached = get_post_meta( $post->ID, $cachekey, true );
     182
     183        // Cleanup.
     184        unset( $post );
     185
    176186        $this->assertEquals( $expected, $actual );
    177 
    178         $this->assertEquals( $expected, get_post_meta( $GLOBALS['post']->ID, $cachekey, true ) );
    179         $this->assertNotEmpty( get_post_meta( $GLOBALS['post']->ID, $cachekey_time, true ) );
    180 
    181         // Result should be cached.
    182         $actual = $this->wp_embed->shortcode( array(), $url );
    183         $this->assertEquals( $expected, $actual );
    184     }
    185 
    186     public function test_shortcode_should_cache_failure_in_post_meta_for_known_post() {
    187         $GLOBALS['post'] = $this->factory()->post->create_and_get();
     187        $this->assertEquals( $expected, $actual_2 );
     188        $this->assertEquals( $expected, $cached );
     189    }
     190
     191    public function test_shortcode_should_get_cached_failure_from_post_meta_for_known_post() {
     192        global $post;
     193
     194        $post = $this->factory()->post->create_and_get();
    188195        $url             = 'https://example.com/';
    189196        $expected        = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
     
    192199        $cachekey_time   = '_oembed_time_' . $key_suffix;
    193200
     201        add_post_meta( $post->ID, $cachekey, '{{unknown}}' );
     202        add_post_meta( $post->ID, $cachekey_time, 0 );
     203
    194204        add_filter( 'pre_oembed_result', '__return_empty_string' );
    195205        $actual = $this->wp_embed->shortcode( array(), $url );
    196206        remove_filter( 'pre_oembed_result', '__return_empty_string' );
    197207
     208        // Result should be cached.
     209        $actual_2 = $this->wp_embed->shortcode( array(), $url );
     210
     211        $cached = get_post_meta( $post->ID, $cachekey, true );
     212        $cached_time = get_post_meta( $post->ID, $cachekey_time, true );
     213
     214        // Cleanup.
     215        unset( $post );
     216
    198217        $this->assertEquals( $expected, $actual );
    199 
    200         $this->assertEquals( '{{unknown}}', get_post_meta( $GLOBALS['post']->ID, $cachekey, true ) );
    201         $this->assertEmpty( get_post_meta( $GLOBALS['post']->ID, $cachekey_time, true ) );
     218        $this->assertEquals( '{{unknown}}', $cached );
     219        $this->assertEmpty( $cached_time );
     220        $this->assertEquals( $expected, $actual_2 );
     221    }
     222
     223    /**
     224     * @ticket 34115
     225     */
     226    public function test_shortcode_should_cache_data_in_custom_post() {
     227        $url        = 'https://example.com/';
     228        $expected   = '<b>Embedded content</b>';
     229        $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
     230
     231        add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     232        $actual = $this->wp_embed->shortcode( array(), $url );
     233        remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     234
     235        $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
     236        $post_content = get_post( $oembed_post_id )->post_content;
    202237
    203238        // Result should be cached.
    204         $actual = $this->wp_embed->shortcode( array(), $url );
     239        $actual_2 = $this->wp_embed->shortcode( array(), $url );
     240
     241        wp_delete_post( $oembed_post_id );
     242
     243        $this->assertNotNull( $oembed_post_id );
     244        $this->assertEquals( $expected, $post_content );
    205245        $this->assertEquals( $expected, $actual );
     246        $this->assertEquals( $expected, $actual_2 );
     247    }
     248
     249    /**
     250     * @ticket 34115
     251     */
     252    public function test_shortcode_should_cache_failure_in_custom_post() {
     253        $url        = 'https://example.com/';
     254        $expected   = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
     255        $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
     256
     257        add_filter( 'pre_oembed_result', '__return_empty_string' );
     258        $actual = $this->wp_embed->shortcode( array(), $url );
     259        remove_filter( 'pre_oembed_result', '__return_empty_string' );
     260
     261        $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
     262        $post_content = get_post( $oembed_post_id )->post_content;
     263
     264        // Result should be cached.
     265        $actual_2 = $this->wp_embed->shortcode( array(), $url );
     266
     267        wp_delete_post( $oembed_post_id );
     268
     269        $this->assertEquals( $expected, $actual );
     270        $this->assertEquals( $expected, $actual_2 );
     271        $this->assertNotNull( $oembed_post_id );
     272        $this->assertEquals( '{{unknown}}', $post_content );
    206273    }
    207274
Note: See TracChangeset for help on using the changeset viewer.