Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#30319 closed defect (bug) (fixed)

Twenty Fifteen: Incorrect use of sprintf() and _x() in template-tags.php

Reported by: aprea's profile aprea Owned by:
Milestone: 4.1 Priority: normal
Severity: normal Version: 4.1
Component: Bundled Theme Keywords: has-patch
Focuses: Cc:

Description

The current /inc/template-tags.php has several incorrect uses of sprintf() and _x, see example below:

if ( is_attachment() && wp_attachment_is_image() ) {
	// Retrieve attachment metadata.
	$metadata = wp_get_attachment_metadata();

	printf( '<span class="full-size-link">%1$s<a href="%2$s">%3$s &times; %4$s</a></span>',
		sprintf( _x( '<span class="screen-reader-text">Full size</span>', 'Used before full size attachment link.', 'twentyfifteen' ) ),
		esc_url( wp_get_attachment_url() ),
		$metadata['width'],
		$metadata['height']
	);
}

Two problems here:

  1. sprintf() is only passing 1 argument.
  2. _x() includes HTML markup in the replacement text.

Attachments (2)

30319.patch (2.8 KB) - added by aprea 9 years ago.
30319-removes-sprintf.patch (3.2 KB) - added by aprea 9 years ago.
Removes the sprintf()s and places the screen reader text wrapper directly in the first format string.

Download all attachments as: .zip

Change History (10)

@aprea
9 years ago

#1 @aprea
9 years ago

  • Keywords has-patch added

#2 @aprea
9 years ago

  • Component changed from Themes to Bundled Theme

#3 @obenland
9 years ago

If the screen reader text wrapper would be placed in the first format string, sprintf() could be removed all together.

#4 @obenland
9 years ago

Also: Nice find aprea, way to go with your first ticket!

#5 @aprea
9 years ago

Thanks obenland!

I was speaking to Dion about this last night on Slack, he suggested that it was perhaps done this way to improve code readability.

I've attached another patch that removes the sprintf()s and places the screen reader text wrapper directly in the first format string as you suggested.

@aprea
9 years ago

Removes the sprintf()s and places the screen reader text wrapper directly in the first format string.

#6 @iandstewart
9 years ago

  • Resolution set to fixed
  • Status changed from new to closed

Fixed in r30312.

Nice work, @aprea!

#7 @dd32
9 years ago

  • Milestone changed from Awaiting Review to 4.1

FWIW, @aprea, I was thinking something like this, not using the extra sprintf:

printf( '<span class="full-size-link">%1$s<a href="%2$s">%3$s &times; %4$s</a></span>',
		'<span class="screen-reader-text">' . _x( 'Full size', 'Used before full size attachment link.', 'twentyfifteen' ) . '</span>',
		esc_url( wp_get_attachment_url() ),
		$metadata['width'],
		$metadata['height']
	);

Of course, it's fine-as-is in r30312 :)

#8 @aprea
9 years ago

Oh right, of course! I don't think my brain was working at 100% capacity last night ;)

Note: See TracTickets for help on using tickets.