Skip to content
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

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from

Conversation

gin0115
Copy link
Collaborator

@gin0115 gin0115 commented Jul 4, 2023

Wraps the outputted links in <p class="colophon__wrapper">...</p>

mentions #5

@gin0115 gin0115 linked an issue Jul 4, 2023 that may be closed by this pull request
Copy link
Contributor

@georgestephanis georgestephanis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hard opposed to this, as that could add extra markup -- that is a block-level element -- across a lot of sites.

What I'd be open to is passing the string through a filter before echoing it, so you can add a wrapper on a specific site if you'd like via add_filter()

@georgestephanis
Copy link
Contributor

This is meant to be a drop in replacement for existing markup across all sites. Shoving an extra block-level wrapper around it could cause unintended visual breakage on some sites that already have Colophon in place.

…on. Includes an additional fitler, which allows the whole rendered string to be controlled programaiticaly
…mpty span tag. Also added an additional param to the filter to pass the rendered links to the filter
@gin0115
Copy link
Collaborator Author

gin0115 commented Jul 7, 2023

Additional changes have been made.

passing wrapper='' will see the links wrapped in an empty span tag
<span>.....</span>

<!-- wp:shortcode -->
[team51-credits wrapper="" /]
<!-- /wp:shortcode -->


<!-- Renders as -->
<span>LINKS</span>

Passing a value will see them wrapped in a span with the passed values as the class wrapper='colophon__wrapper
<span class="colophon__wrapper">...</span>

<!-- wp:shortcode -->
[team51-credits wrapper="bar" /]
<!-- /wp:shortcode -->


<!-- Renders as -->
<span class="bar">LINKS</span>

There is also a new filter

An additional filter has been added which is used to modify the final output of the colophon. This can be used to add additional markup around the colophon, or to modify the output of the colophon itself. It can be done like so:

/**
 * Wraps the colophon in a div with a class of `colophon`.
 * 
 * @param $output (string) The output of the colophon.
 * @param $links (string) The unwrapped links string.
 * @param $args (array) The arguments passed to the colophon.
 * @return (string) The output of the colophon, wrapped in a div.
 */
function PREFIX_team51_wrap_colophon( $output, $links, $args ) {
  return '<div class="colophon">' . $output . '</div>';
}
add_filter( 'team51_credits_render', 'PREFIX_team51_wrap_colophon', 10, 3 );
<!-- wp:shortcode -->
[team51-credits wrapper="foo" /]
<!-- /wp:shortcode -->


<!-- Renders as -->
<div class="colophon"><span class="foo">LINKS</span></div>

You can also choose to wrap the links in any tags desired:

/**
 * Wraps the colophon in a div with a class of `colophon`.
 * 
 * @param $output (string) The output of the colophon.
 * @param $links (string) The unwrapped links string.
 * @param $args (array) The arguments passed to the colophon.
 * @return (string) The output of the colophon, wrapped in a div.
 */
function PREFIX_team51_colophon_as_paragraph( $output, $links, $args ) {
  return '<div class="some-class"><p id="colophon-wrapper">' . $links . '</p></div>';
}
add_filter( 'team51_credits_render', 'PREFIX_team51_colophon_as_paragraph', 10, 3 );
<!-- wp:shortcode -->
[team51-credits wrapper="ignore-me" /]
<!-- /wp:shortcode -->


<!-- Renders as -->
<div class="some-class"><p id="colophon-wrapper">LINKS</p></div>

The above would output the same, even if wrapper="" was not passed. [team51-credits /]

Copy link
Collaborator Author

@gin0115 gin0115 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has now been changed, please see the comments in the rest of this PR

@gin0115
Copy link
Collaborator Author

gin0115 commented Dec 19, 2023

To clarify above

These changes will not see any changes to existing sites that are using it. You can now pass the wrapper arg

<!-- wp:shortcode -->
[team51-credits wrapper="bar" /]
<!-- /wp:shortcode -->


<!-- Renders as -->
<span class="bar">LINKS</span>

You can also use the filters

@@ -20,7 +20,7 @@
*
* @return void
*/
function team51_credits( $args = array() ) {
function team51_credits( $args = array() ): void {
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

(Don't say things like this, it reminds many of us of how long we've been around, I still carry around some PHP4 scars, ha!)

Copy link
Contributor

@georgestephanis georgestephanis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like we're trying to do both customization (with the new broad filter on the output) and configuration (with the new wrapper parameter) -- rather than choosing one. I'm not sure I agree with the rationale of adding two ways in to accomplish this, versus choosing a method and committing to it.

Very open to input or context as I've been somewhat out of the loop on this.

colophon.php Outdated
$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';
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

			$pairs['wrapper'] = 'colophon__wrapper';
			if ( $attributes['wrapper'] ) {
				$pairs['wrapper'] = esc_attr( $attributes['wrapper'] );
			}

would be a direct rewriting, however as $pairs and $attributes both get passed through shortcode_atts() -- so long as the default is being added to $pairs, I believe that shortcode_atts() should handle the splicing in of whatever's in $attributes -- so I'd prefer to just modify it in $attributes if it's empty -- to let the default pass through, giving us something closer to --

			$pairs['wrapper'] = 'colophon__wrapper';
			if ( empty( $attributes['wrapper'] ) ) {
				unset( $attributes['wrapper'] );
			}

or

Suggested change
$pairs['wrapper'] = '' !== $attributes['wrapper'] ? esc_attr( $attributes['wrapper'] ) : 'colophon__wrapper';
$pairs['wrapper'] = 'colophon__wrapper';
if ( empty( $attributes['wrapper'] ) ) {
$attributes['wrapper'] = 'colophon__wrapper';
}

depending on what we'd like it to look like if anyone tries to hook into the subsequent shortcode_atts_{$shortcode} filter inside shortcode_atts() or the like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading how this is then handled inside the new team51_credits(); I'm not sure I've got the gist right in my suggestions. If someone passes in a blank string, some remarks above indicate that colophon_wrapper will be used -- however the team51_credits has a contingency for dropping the class attribute from the wrapper span when passed in as empty -- which the shortcode prevents from happening as-is. I'd like to check on the intent of expectations here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 span to you being able to wrap it in a div or as a table. The same with the links, the filter just allows you to add additional tracking params or remove them.

Was not asked for in the brief but felt they added additional flexibility

colophon.php Outdated
team51_credits( $atts );
return ob_get_clean();
team51_credits( $attributes );
return ob_get_clean() ?: ''; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the only scenario where ob_get_clean() returns false -- or a non-string -- is when out buffering isn't active? And as we're starting it two lines earlier, I don't think that's a risk.

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?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
Removed

colophon.php Outdated
Comment on lines 97 to 99
$wrapped_template = '' !== $args['wrapper']
? '<span class="%1$s">%2$s</span>'
: '<span>%2$s</span>';
Copy link
Contributor

Choose a reason for hiding this comment

The 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
$wrapped_template = '' !== $args['wrapper']
? '<span class="%1$s">%2$s</span>'
: '<span>%2$s</span>';
$wrapped_template = '<span>%2$s</span>';
// Otherwise, include it as a class name...
if ( $args['wrapper'] ) {
$wrapped_template = '<span class="%1$s">%2$s</span>';
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

@gin0115
Copy link
Collaborator Author

gin0115 commented Apr 25, 2024

Hey @georgestephanis

This has been a while, I have implemented all the changes above including some additional ones.

I have removed the filter which allowed for the filtering of the final output and updated the docs to reflect this.

Did you want me to close this and open a new PR?

In a nutshell, this now allows you to wrap the links in <span> tags with optional wrappers.
By default there is no

or s added, unless included (so backwards compatible) and these are all editable via the block which has been included.
image

@gin0115
Copy link
Collaborator Author

gin0115 commented Apr 25, 2024

Suspect we are better closing this as the conflict on readme, isnt present in the code and is as a result of further changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add <p> tag around shortcode output
2 participants