[wp-trac] [WordPress Trac] #38160: Attachment template hierarchy selects generic attachment.php before specific post type templates
WordPress Trac
noreply at wordpress.org
Sat Oct 21 17:29:12 UTC 2017
#38160: Attachment template hierarchy selects generic attachment.php before
specific post type templates
------------------------------+------------------------------
Reporter: johnbillion | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version:
Severity: normal | Resolution:
Keywords: needs-unit-tests | Focuses: template
------------------------------+------------------------------
Changes (by birgire):
* keywords: needs-patch needs-unit-tests => needs-unit-tests
Comment:
In [attachment:38160.diff] the {{{get_attachment_template()}}} and
{{{get_single_template()}}} are modified accordingly. There are various
ways to handle this, but I think this approach has the smallest code
change that I could think of.
I looked into creating a test, but stumbled upon a problem.
The plan is to create a theme under
{{{tests/phpunit/data/themedir1/38160}}} with files like
{{{attachment.php}}} and {{{single-attachment-foo.php}}} appart from
{{{index.php}}} and {{{style.css}}}.
Then switch to that theme with:
{{{
$theme = get_theme( '38160' );
$this->assertNotEmpty( $theme );
switch_theme( $theme['Template'], $theme['Stylesheet'] );
}}}
where we could add our test method to the {{{Tests_Admin_includesTheme}}}
class to be able to use the {{{setUp}}} there
[https://core.trac.wordpress.org/browser/tags/4.8.2/tests/phpunit/tests/admin/includesTheme.php#L7
src], or move the similar setup to another class.
Then create an attachment and go to that attachment page.
{{{
$attachment_id = self::factory()->attachment->create_object( 'foo.jpg', 0,
array(
'post_name' => 'foo',
'post_mime_type' => 'image/jpeg',
) );
$this->go_to( get_permalink( $attachment_id ) );
}}}
Then try using {{{get_attachment_template()}}} and
{{{get_single_template()}}} to verify the wanted results.
But here {{{locate_template()}}} only returned an empty string, because
the {{{TEMPLATEPATH}}} was stuck for the default theme
{{{tests/phpunit/includes/../data/themedir1/default}}}.
I checked the tests in {{{Tests_Theme::test_switch_theme()}}}:
https://core.trac.wordpress.org/browser/tags/4.8.2/tests/phpunit/tests/theme.php#L285
that uses {{{get_attachment_template()}}} and {{{get_single_template()}}},
but they are also only returning an empty string, because the
{{{TEMPLATEPATH}}} is also stuck there for the {{{default}}} theme.
Maybe I'm missing something here, but I will have to look into the tests
further.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/38160#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list