Ticket #40245: 40245.2.diff
File 40245.2.diff, 5.3 KB (added by , 6 years ago) |
---|
-
src/wp-includes/default-filters.php
573 573 add_filter( 'the_excerpt_embed', 'wp_embed_excerpt_attachment' ); 574 574 575 575 add_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10, 3 ); 576 add_filter( 'oembed_dataparse', 'wp_filter_oembed_iframe_title_attribute', 20, 3 ); 576 577 add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 ); 577 578 add_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10, 3 ); 578 579 -
src/wp-includes/embed.php
781 781 } 782 782 783 783 /** 784 * Filters the given oEmbed HTML to make sure iframes have a title attribute. 785 * 786 * @since 5.2.0 787 * 788 * @param string $result The oEmbed HTML result. 789 * @param object $data A data object result from an oEmbed provider. 790 * @param string $url The URL of the content to be embedded. 791 * @return string The filtered oEmbed result. 792 */ 793 function wp_filter_oembed_iframe_title_attribute( $result, $data, $url ) { 794 if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) { 795 return $result; 796 } 797 798 $title = ! empty( $data->title ) ? $data->title : ''; 799 800 $pattern = '`<iframe[^>]*?title=(\\\\\'|\\\\"|[\'"])([^>]*?)\1`i'; 801 $has_title_attr = preg_match( $pattern, $result, $matches ); 802 803 if ( $has_title_attr && ! empty( $matches[2] ) ) { 804 $title = $matches[2]; 805 } 806 807 /** 808 * Filters the title attribute of the given oEmbed HTML iframe. 809 * 810 * @since 5.2.0 811 * 812 * @param string $title The title attribute. 813 * @param string $result The oEmbed HTML result. 814 * @param object $data A data object result from an oEmbed provider. 815 * @param string $url The URL of the content to be embedded. 816 */ 817 $title = apply_filters( 'oembed_iframe_title_attribute', $title, $result, $data, $url ); 818 819 if ( '' === $title ) { 820 return $result; 821 } 822 823 if ( $has_title_attr ) { 824 // Remove the old title, $matches[1]: quote, $matches[2]: title attribute value. 825 $result = str_replace( ' title=' . $matches[1] . $matches[2] . $matches[1], '', $result ); 826 } 827 828 return str_ireplace( '<iframe ', sprintf( '<iframe title="%s" ', esc_attr( $title ) ), $result ); 829 } 830 831 832 /** 784 833 * Filters the given oEmbed HTML. 785 834 * 786 835 * If the `$url` isn't on the trusted providers list, -
tests/phpunit/tests/oembed/filterTitleAttributes.php
1 <?php 2 3 /** 4 * @group oembed 5 */ 6 class Tests_Filter_oEmbed_Iframe_Title_Attribute extends WP_UnitTestCase { 7 function data_filter_oembed_iframe_title_attribute() { 8 return array( 9 array( 10 '<p>Foo</p><iframe src=""></iframe><b>Bar</b>', 11 array( 12 'type' => 'rich', 13 ), 14 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 15 '<p>Foo</p><iframe src=""></iframe><b>Bar</b>', 16 ), 17 array( 18 '<p>Foo</p><iframe src="" title="Hello World"></iframe><b>Bar</b>', 19 array( 20 'type' => 'rich', 21 ), 22 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 23 '<p>Foo</p><iframe title="Hello World" src=""></iframe><b>Bar</b>', 24 ), 25 array( 26 '<p>Foo</p>', 27 array( 28 'type' => 'rich', 29 'title' => 'Hello World', 30 ), 31 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 32 '<p>Foo</p>', 33 ), 34 array( 35 '<p title="Foo">Bar</p>', 36 array( 37 'type' => 'rich', 38 'title' => 'Hello World', 39 ), 40 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 41 '<p title="Foo">Bar</p>', 42 ), 43 array( 44 '<p>Foo</p><iframe src=""></iframe><b>Bar</b>', 45 array( 46 'type' => 'rich', 47 'title' => 'Hello World', 48 ), 49 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 50 '<p>Foo</p><iframe title="Hello World" src=""></iframe><b>Bar</b>', 51 ), 52 array( 53 '<iframe src="" title="Foo"></iframe>', 54 array( 55 'type' => 'rich', 56 'title' => 'Bar', 57 ), 58 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 59 '<iframe title="Foo" src=""></iframe>', 60 ), 61 ); 62 } 63 64 /** 65 * @dataProvider data_filter_oembed_iframe_title_attribute 66 */ 67 function test_oembed_iframe_title_attribute( $html, $oembed_data, $url, $expected ) { 68 $actual = wp_filter_oembed_iframe_title_attribute( $html, (object) $oembed_data, $url ); 69 70 $this->assertSame( $expected, $actual ); 71 } 72 73 function test_filter_oembed_iframe_title_attribute() { 74 add_filter( 'oembed_iframe_title_attribute', array( $this, '_filter_oembed_iframe_title_attribute' ) ); 75 76 $actual = wp_filter_oembed_iframe_title_attribute( 77 '<iframe title="Foo" src=""></iframe>', 78 (object) array( 79 'type' => 'rich', 80 'title' => 'Bar', 81 ), 82 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' 83 ); 84 85 remove_filter( 'oembed_iframe_title_attribute', array( $this, '_filter_oembed_iframe_title_attribute' ) ); 86 87 $this->assertSame( '<iframe title="Baz" src=""></iframe>', $actual ); 88 } 89 90 function _filter_oembed_iframe_title_attribute() { 91 return 'Baz'; 92 } 93 }