Make WordPress Core

Ticket #42310: 42310.diff

File 42310.diff, 3.6 KB (added by dlh, 7 years ago)
  • src/wp-admin/includes/ajax-actions.php

     
    30343034        $parsed = false;
    30353035        $wp_embed->return_false_on_fail = true;
    30363036
     3037        if ( 0 === $post_id ) {
     3038                /*
     3039                 * Refresh oEmbeds cached outside of posts that are past their TTL.
     3040                 * Posts are excluded because they have separate logic for refreshing
     3041                 * their post meta caches. See WP_Embed::cache_oembed().
     3042                 */
     3043                $wp_embed->usecache = false;
     3044        }
     3045
    30373046        if ( is_ssl() && 0 === strpos( $url, 'http://' ) ) {
    30383047                // Admin is ssl and the user pasted non-ssl URL.
    30393048                // Check if the provider supports ssl embeds and use that for the preview.
  • src/wp-includes/class-wp-embed.php

     
    284284                                kses_remove_filters();
    285285                        }
    286286
    287                         wp_insert_post( wp_slash( array(
    288                                 'post_name'    => $key_suffix,
    289                                 'post_content' => $html ? $html : '{{unknown}}',
    290                                 'post_status'  => 'publish',
    291                                 'post_type'    => 'oembed_cache',
    292                         ) ) );
     287                        $insert_post_args = array(
     288                                'post_name' => $key_suffix,
     289                                'post_status' => 'publish',
     290                                'post_type' => 'oembed_cache',
     291                        );
    293292
     293                        if ( $html ) {
     294                                if ( $cached_post_id ) {
     295                                        wp_update_post( wp_slash( array(
     296                                                'ID' => $cached_post_id,
     297                                                'post_content' => $html,
     298                                        ) ) );
     299                                } else {
     300                                        wp_insert_post( wp_slash( array_merge(
     301                                                $insert_post_args,
     302                                                array(
     303                                                        'post_content' => $html,
     304                                                )
     305                                        ) ) );
     306                                }
     307                        } elseif ( ! $cache ) {
     308                                wp_insert_post( wp_slash( array_merge(
     309                                        $insert_post_args,
     310                                        array(
     311                                                'post_content' => '{{unknown}}',
     312                                        )
     313                                ) ) );
     314                        }
     315
    294316                        if ( $has_kses ) {
    295317                                kses_init_filters();
    296318                        }
  • tests/phpunit/tests/oembed/WpEmbed.php

     
    272272                $this->assertEquals( '{{unknown}}', $post_content );
    273273        }
    274274
     275        public function test_shortcode_should_update_custom_post() {
     276                add_filter( 'oembed_ttl', '__return_zero' );
     277
     278                $url        = 'https://example.com/';
     279                $embedded   = '<b>Embedded content</b>';
     280                $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
     281
     282                add_filter( 'pre_oembed_result', '__return_empty_string' );
     283                $this->wp_embed->shortcode( array(), $url );
     284                remove_filter( 'pre_oembed_result', '__return_empty_string' );
     285
     286                $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
     287
     288                $this->assertSame( '{{unknown}}', get_post( $oembed_post_id )->post_content );
     289
     290                $previous_usecache = $this->wp_embed->usecache;
     291                $this->wp_embed->usecache = false;
     292
     293                // The update cannot be empty because empty responses won't overwrite the cache.
     294                add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     295                $this->wp_embed->shortcode( array(), $url );
     296                remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
     297
     298                $this->assertSame( $embedded, get_post( $oembed_post_id )->post_content );
     299
     300                $this->wp_embed->usecache = $previous_usecache;
     301                remove_filter( 'oembed_ttl', '__return_zero' );
     302        }
     303
    275304        public function test_shortcode_should_get_url_from_src_attribute() {
    276305                $url    = 'http://example.com/embed/foo';
    277306                $actual = $this->wp_embed->shortcode( array( 'src' => $url ) );