Ticket #18302: 18302.diff
File 18302.diff, 9.8 KB (added by , 8 years ago) |
---|
-
src/wp-includes/link-template.php
4021 4021 */ 4022 4022 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 4023 4023 } 4024 4025 /** 4026 * Retrieve the URL of a file in the theme. 4027 * 4028 * Searches in the stylesheet directory before the template directory so themes 4029 * which inherit from a parent theme can just override one file. 4030 * 4031 * @since 4.7.0 4032 * 4033 * @param string $file Optional. File to search for in the stylesheet directory. 4034 * @return string The URL of the file. 4035 */ 4036 function get_theme_file_uri( $file = '' ) { 4037 $file = ltrim( $file, '/' ); 4038 4039 if ( empty( $file ) ) { 4040 $url = get_stylesheet_directory_uri(); 4041 } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { 4042 $url = get_stylesheet_directory_uri() . '/' . $file; 4043 } else { 4044 $url = get_template_directory_uri() . '/' . $file; 4045 } 4046 4047 /** 4048 * Filter the URL to a file in the theme. 4049 * 4050 * @since 4.7.0 4051 * 4052 * @param string $url The file URL. 4053 * @param string $file The requested file to search for. 4054 */ 4055 return apply_filters( 'theme_file_uri', $url, $file ); 4056 } 4057 4058 /** 4059 * Retrieve the URL of a file in the parent theme. 4060 * 4061 * @since 4.7.0 4062 * 4063 * @param string $file Optional. File to return the URL for in the template directory. 4064 * @return string The URL of the file. 4065 */ 4066 function get_parent_theme_file_uri( $file = '' ) { 4067 $file = ltrim( $file, '/' ); 4068 4069 if ( empty( $file ) ) { 4070 $url = get_template_directory_uri(); 4071 } else { 4072 $url = get_template_directory_uri() . '/' . $file; 4073 } 4074 4075 /** 4076 * Filter the URL to a file in the parent theme. 4077 * 4078 * @since 4.7.0 4079 * 4080 * @param string $url The file URL. 4081 * @param string $file The requested file to search for. 4082 */ 4083 return apply_filters( 'parent_theme_file_uri', $url, $file ); 4084 } 4085 4086 /** 4087 * Retrieve the path of a file in the theme. 4088 * 4089 * Searches in the stylesheet directory before the template directory so themes 4090 * which inherit from a parent theme can just override one file. 4091 * 4092 * @since 4.7.0 4093 * 4094 * @param string $file Optional. File to search for in the stylesheet directory. 4095 * @return string The path of the file. 4096 */ 4097 function get_theme_file( $file = '' ) { 4098 $file = ltrim( $file, '/' ); 4099 4100 if ( empty( $file ) ) { 4101 $path = get_stylesheet_directory(); 4102 } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { 4103 $path = get_stylesheet_directory() . '/' . $file; 4104 } else { 4105 $path = get_template_directory() . '/' . $file; 4106 } 4107 4108 /** 4109 * Filter the path to a file in the theme. 4110 * 4111 * @since 4.7.0 4112 * 4113 * @param string $path The file path. 4114 * @param string $file The requested file to search for. 4115 */ 4116 return apply_filters( 'theme_file_path', $path, $file ); 4117 } 4118 4119 /** 4120 * Retrieve the path of a file in the parent theme. 4121 * 4122 * @since 4.7.0 4123 * 4124 * @param string $file Optional. File to return the path for in the template directory. 4125 * @return string The path of the file. 4126 */ 4127 function get_parent_theme_file( $file = '' ) { 4128 $file = ltrim( $file, '/' ); 4129 4130 if ( empty( $file ) ) { 4131 $path = get_template_directory(); 4132 } else { 4133 $path = get_template_directory() . '/' . $file; 4134 } 4135 4136 /** 4137 * Filter the path to a file in the parent theme. 4138 * 4139 * @since 4.7.0 4140 * 4141 * @param string $path The file path. 4142 * @param string $file The requested file to search for. 4143 */ 4144 return apply_filters( 'parent_theme_file_path', $path, $file ); 4145 } -
tests/phpunit/tests/link/themeFile.php
1 <?php 2 /** 3 * @group link 4 */ 5 class Test_Theme_File extends WP_UnitTestCase { 6 7 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 8 symlink( DIR_TESTDATA . '/theme-file-parent', WP_CONTENT_DIR . '/themes/theme-file-parent' ); 9 symlink( DIR_TESTDATA . '/theme-file-child', WP_CONTENT_DIR . '/themes/theme-file-child' ); 10 } 11 12 public static function wpTearDownAfterClass() { 13 unlink( WP_CONTENT_DIR . '/themes/theme-file-parent' ); 14 unlink( WP_CONTENT_DIR . '/themes/theme-file-child' ); 15 } 16 17 /** 18 * @ticket 18302 19 * 20 * @dataProvider data_theme_files 21 */ 22 public function test_theme_file_uri_with_parent_theme( $file, $expected_theme, $existence ) { 23 switch_theme( 'theme-file-parent' ); 24 25 // Ensure the returned URL always uses the parent theme: 26 $this->assertSame( content_url( "themes/theme-file-parent/{$file}" ), get_theme_file_uri( $file ) ); 27 $this->assertSame( content_url( "themes/theme-file-parent/{$file}" ), get_parent_theme_file_uri( $file ) ); 28 } 29 30 /** 31 * @ticket 18302 32 * 33 * @dataProvider data_theme_files 34 */ 35 public function test_theme_file_uri_with_child_theme( $file, $expected_theme, $existence ) { 36 switch_theme( 'theme-file-child' ); 37 38 // Ensure the returned URL uses the expected theme: 39 $this->assertSame( content_url( "themes/{$expected_theme}/{$file}" ), get_theme_file_uri( $file ) ); 40 41 // Ensure the returned URL always uses the parent theme: 42 $this->assertSame( content_url( "themes/theme-file-parent/{$file}" ), get_parent_theme_file_uri( $file ) ); 43 } 44 45 /** 46 * @ticket 18302 47 * 48 * @dataProvider data_theme_files 49 */ 50 public function test_theme_file_path_with_parent_theme( $file, $expected_theme, $existence ) { 51 switch_theme( 'theme-file-parent' ); 52 53 // Ensure the returned path always uses the parent theme: 54 $this->assertSame( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}", get_theme_file( $file ) ); 55 $this->assertSame( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}", get_parent_theme_file( $file ) ); 56 } 57 58 /** 59 * @ticket 18302 60 * 61 * @dataProvider data_theme_files 62 */ 63 public function test_theme_file_path_with_child_theme( $file, $expected_theme, $existence ) { 64 switch_theme( 'theme-file-child' ); 65 66 // Ensure the returned path uses the expected theme: 67 $this->assertSame( WP_CONTENT_DIR . "/themes/{$expected_theme}/{$file}", get_theme_file( $file ) ); 68 69 // Ensure the returned path always uses the parent theme: 70 $this->assertSame( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}", get_parent_theme_file( $file ) ); 71 } 72 73 /** 74 * Test the tests. 75 * 76 * @ticket 18302 77 * 78 * @dataProvider data_theme_files 79 */ 80 public function test_theme_file_existance( $file, $expected_theme, $existence ) { 81 82 if ( in_array( 'theme-file-child', $existence, true ) ) { 83 $this->assertFileExists( WP_CONTENT_DIR . "/themes/theme-file-child/{$file}" ); 84 } else { 85 $this->assertFileNotExists( WP_CONTENT_DIR . "/themes/theme-file-child/{$file}" ); 86 } 87 88 if ( in_array( 'theme-file-parent', $existence, true ) ) { 89 $this->assertFileExists( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}" ); 90 } else { 91 $this->assertFileNotExists( WP_CONTENT_DIR . "/themes/theme-file-parent/{$file}" ); 92 } 93 94 } 95 96 /** 97 * @ticket 18302 98 * 99 * @dataProvider data_theme_files 100 */ 101 public function test_theme_file_uri_returns_valid_uri( $file, $expected_theme, $existence ) { 102 $uri = get_theme_file_uri( $file ); 103 $parent_uri = get_parent_theme_file_uri( $file ); 104 105 $this->assertSame( esc_url_raw( $uri ), $uri ); 106 $this->assertSame( esc_url_raw( $parent_uri ), $parent_uri ); 107 } 108 109 public function data_theme_files() { 110 $parent = 'theme-file-parent'; 111 $child = 'theme-file-child'; 112 113 return array( 114 array( 115 'parent-only.php', 116 $parent, 117 array( 118 $parent, 119 ), 120 ), 121 array( 122 'child-only.php', 123 $child, 124 array( 125 $child, 126 ), 127 ), 128 array( 129 'parent-and-child.php', 130 $child, 131 array( 132 $parent, 133 $child, 134 ), 135 ), 136 array( 137 'neither.php', 138 $parent, 139 array( 140 ), 141 ), 142 ); 143 } 144 145 } -
tests/phpunit/data/theme-file-child/child-only.php
1 <!-- child only --> 2 No newline at end of file -
tests/phpunit/data/theme-file-child/parent-and-child.php
1 <!-- parent and child --> 2 No newline at end of file -
tests/phpunit/data/theme-file-child/style.css
1 /* 2 Theme Name: Child Theme 3 Template: theme-file-parent 4 */ -
tests/phpunit/data/theme-file-parent/parent-and-child.php
1 <!-- parent and child --> 2 No newline at end of file -
tests/phpunit/data/theme-file-parent/parent-only.php
1 <!-- parent only --> 2 No newline at end of file -
tests/phpunit/data/theme-file-parent/style.css
1 /* 2 Theme Name: Parent Theme 3 */