-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added p tag with colophon__wrapper as the class around links #13
base: trunk
Are you sure you want to change the base?
Changes from 5 commits
36df3e1
a8edc62
1a83ee5
32c3408
9c0904c
3f27aa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,7 +20,7 @@ | |||||||||||||||||
* | ||||||||||||||||||
* @return void | ||||||||||||||||||
*/ | ||||||||||||||||||
function team51_credits( $args = array() ) { | ||||||||||||||||||
function team51_credits( $args = array() ): void { | ||||||||||||||||||
$args = wp_parse_args( | ||||||||||||||||||
$args, | ||||||||||||||||||
array( | ||||||||||||||||||
|
@@ -84,13 +84,38 @@ function team51_credits( $args = array() ) { | |||||||||||||||||
* | ||||||||||||||||||
* @param array $credit_links The associative array of credit links. | ||||||||||||||||||
* @param array $args The parsed arguments used by `team51_credits()`. | ||||||||||||||||||
* | ||||||||||||||||||
* @return array The filtered associative array of credit links. | ||||||||||||||||||
*/ | ||||||||||||||||||
$credit_links = apply_filters( 'team51_credit_links', $credit_links, $args ); | ||||||||||||||||||
|
||||||||||||||||||
echo implode( | ||||||||||||||||||
esc_html( $args['separator'] ), | ||||||||||||||||||
$credit_links //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, this cant be escaped as it runs through a filter | ||||||||||||||||||
); | ||||||||||||||||||
$links = implode( esc_html( $args['separator'] ), $credit_links ); | ||||||||||||||||||
|
||||||||||||||||||
// If we have an args['wrapper'] set, wrap the output in that. | ||||||||||||||||||
if ( array_key_exists( 'wrapper', $args ) ) { | ||||||||||||||||||
// If there is no class defined, use an empty span. | ||||||||||||||||||
$wrapped_template = '' !== $args['wrapper'] | ||||||||||||||||||
? '<span class="%1$s">%2$s</span>' | ||||||||||||||||||
: '<span>%2$s</span>'; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd far sooner see this written out, rather than doing assignments, comparisons, and ternaries on a single line.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has been removed as the args are passed through the shortcode handler by default. |
||||||||||||||||||
|
||||||||||||||||||
$string = sprintf( $wrapped_template, esc_attr( $args['wrapper'] ), $links ); | ||||||||||||||||||
} else { | ||||||||||||||||||
// Otherwise, just return the links. | ||||||||||||||||||
$string = $links; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Filters the output string. | ||||||||||||||||||
* | ||||||||||||||||||
* @param string $string The output string. | ||||||||||||||||||
* @param string $links The unwrapped links string. | ||||||||||||||||||
* @param array<string, mixed> $args The parsed arguments used by `team51_credits()`. | ||||||||||||||||||
* | ||||||||||||||||||
* @return string The filtered output string. | ||||||||||||||||||
*/ | ||||||||||||||||||
$string = apply_filters( 'team51_credits_render', $string, $links, $args ); | ||||||||||||||||||
|
||||||||||||||||||
echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, this cant be escaped as its filtered. | ||||||||||||||||||
} | ||||||||||||||||||
add_action( 'team51_credits', 'team51_credits', 10, 1 ); | ||||||||||||||||||
endif; | ||||||||||||||||||
|
@@ -102,11 +127,11 @@ function team51_credits( $args = array() ) { | |||||||||||||||||
* | ||||||||||||||||||
* Can also be used in the Shortcode block. | ||||||||||||||||||
* | ||||||||||||||||||
* @param array{separator?: string, wpcom?: string, pressable?: string} $atts The Args passed to the function. | ||||||||||||||||||
* @param array{separator?: string, wpcom?: string, pressable?: string} $attributes The Args passed to the function. | ||||||||||||||||||
* | ||||||||||||||||||
* @return string | ||||||||||||||||||
*/ | ||||||||||||||||||
function team51_credits_shortcode( $atts ) { | ||||||||||||||||||
function team51_credits_shortcode( array $attributes = array() ): string { | ||||||||||||||||||
$pairs = array( | ||||||||||||||||||
'separator' => ' ', | ||||||||||||||||||
/* translators: %s: WordPress. */ | ||||||||||||||||||
|
@@ -115,11 +140,15 @@ function team51_credits_shortcode( $atts ) { | |||||||||||||||||
'pressable' => sprintf( __( 'Hosted by %s.', 'team51' ), 'Pressable' ), | ||||||||||||||||||
); | ||||||||||||||||||
|
||||||||||||||||||
$atts = shortcode_atts( $pairs, $atts, 'team51-credits' ); | ||||||||||||||||||
// Add the wrapper if set. | ||||||||||||||||||
if ( is_array( $attributes ) && array_key_exists( 'wrapper', $attributes ) ) { | ||||||||||||||||||
$pairs['wrapper'] = '' !== $attributes['wrapper'] ? esc_attr( $attributes['wrapper'] ) : 'colophon__wrapper'; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For legibility and ability to understand what's going on here at a glance, I'd far sooner see this broken out to multiple lines rather than mixing ternaries and assignments and conditionals on one line.
would be a direct rewriting, however as
or
Suggested change
depending on what we'd like it to look like if anyone tries to hook into the subsequent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading how this is then handled inside the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So with the above, if you do not pass wrapper as an attribute the span should not be added (this means existing sites do not see any changes. Adding the default wrapper class to $pairs sees the wrapped added. So this is why we check if it exists, then its blank we add the fallback, else we use what's passed. There are 3 possible combinations here. You are correct that the check if the wrapp is empty and not rendering the class is pointless as you cant pass an empty class. So i can remove that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as for the filters, these just allow you to change it from being wrapped in a Was not asked for in the brief but felt they added additional flexibility |
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
$attributes = shortcode_atts( $pairs, $attributes, 'team51-credits' ); | ||||||||||||||||||
ob_start(); | ||||||||||||||||||
team51_credits( $atts ); | ||||||||||||||||||
return ob_get_clean(); | ||||||||||||||||||
team51_credits( $attributes ); | ||||||||||||||||||
return ob_get_clean() ?: ''; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that the only scenario where Is there a scenario where we're expecting it would return null or false or something falsey that we need to convert to an empty string? (And if so, I wonder if casting to a string may be simpler?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's just a habit of mine, ide flags it as a possibility and i just account for it. |
||||||||||||||||||
} | ||||||||||||||||||
add_action( | ||||||||||||||||||
'init', | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fine with the return type declaration here but just acknowledging that mandates php7.
Which I don't think should be a problem, my brain's just always touchy on making sure such things are explicit, having survived the PHP 5.2 and 5.3 support wars back in the day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function only echos, there is no return, so if someone were to add a return and not change the definition, it would prompt them in development to look at why/what.