Ticket #40245: 40245.diff
File 40245.diff, 5.1 KB (added by , 6 years ago) |
---|
-
src/wp-includes/default-filters.php
diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php index 66d64485d6..6821da6299 100644
add_filter( 'the_excerpt_embed', 'shortcode_unautop' ); 585 585 add_filter( 'the_excerpt_embed', 'wp_embed_excerpt_attachment' ); 586 586 587 587 add_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10, 3 ); 588 add_filter( 'oembed_dataparse', 'wp_filter_oembed_title_attribute', 20, 3 ); 588 589 add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 ); 589 590 add_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10, 3 ); 590 591 -
src/wp-includes/embed.php
diff --git src/wp-includes/embed.php src/wp-includes/embed.php index 7cce664548..1737c143cb 100644
function _oembed_create_xml( $data, $node = null ) { 779 779 return $node->asXML(); 780 780 } 781 781 782 /** 783 * Filters the given oEmbed HTML to make sure iframes have a title attribute. 784 * 785 * @since 5.2.0 786 * 787 * @param string $result The oEmbed HTML result. 788 * @param object $data A data object result from an oEmbed provider. 789 * @param string $url The URL of the content to be embedded. 790 * @return string The filtered oEmbed result. 791 */ 792 function wp_filter_oembed_title_attribute( $result, $data, $url ) { 793 if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) { 794 return $result; 795 } 796 797 $title = ! empty( $data->title ) ? $data->title : ''; 798 799 $pattern = '/title\=[\"|\\\']{1}([^\"\\\']*)[\"|\\\']{1}/i'; 800 $has_title_attr = preg_match( $pattern, $result, $matches ); 801 802 if ( $has_title_attr && ! empty( $matches[1] ) ) { 803 $title = $matches[1]; 804 } 805 806 /** 807 * Filters the title attribute of the given oEmbed HTML. 808 * 809 * @since 5.2.0 810 * 811 * @param string $title The title attribute. 812 * @param string $result The oEmbed HTML result. 813 * @param object $data A data object result from an oEmbed provider. 814 * @param string $url The URL of the content to be embedded. 815 */ 816 $title = apply_filters( 'oembed_title_attribute', $title, $result, $data, $url ); 817 818 if ( '' === $title ) { 819 return $result; 820 } 821 822 if ( $has_title_attr ) { 823 $result = preg_replace( $pattern, 'title="' . esc_attr( $title ) . '"', $result ); 824 } else { 825 return str_ireplace( '<iframe ', sprintf( '<iframe title="%s" ', esc_attr( $title ) ), $result ); 826 } 827 828 return $result; 829 } 830 782 831 /** 783 832 * Filters the given oEmbed HTML. 784 833 * -
new file tests/phpunit/tests/oembed/filterTitleAttributes.php
diff --git tests/phpunit/tests/oembed/filterTitleAttributes.php tests/phpunit/tests/oembed/filterTitleAttributes.php new file mode 100644 index 0000000000..f720ee5ee3
- + 1 <?php 2 3 /** 4 * @group oembed 5 */ 6 class Tests_Filter_oEmbed_Title_Attribute extends WP_UnitTestCase { 7 function data_filter_oembed_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 src="" title="Hello World"></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 src="" title="Foo"></iframe>', 60 ), 61 ); 62 } 63 64 /** 65 * @dataProvider data_filter_oembed_title_attribute 66 */ 67 function test_oembed_title_attribute( $html, $oembed_data, $url, $expected ) { 68 $actual = wp_filter_oembed_title_attribute( $html, (object) $oembed_data, $url ); 69 70 $this->assertSame( $expected, $actual ); 71 } 72 73 function test_filter_oembed_title_attribute() { 74 add_filter( 'oembed_title_attribute', array( $this, '_filter_oembed_title_attribute' ) ); 75 76 $actual = wp_filter_oembed_title_attribute( 77 '<iframe src="" title="Foo"></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_title_attribute', array( $this, '_filter_oembed_title_attribute' ) ); 86 87 $this->assertSame( '<iframe src="" title="Baz"></iframe>', $actual ); 88 } 89 90 function _filter_oembed_title_attribute() { 91 return 'Baz'; 92 } 93 }