Skip to content

Commit

Permalink
The previous code only worked if the function was called directly, no…
Browse files Browse the repository at this point in the history
…t if do_action was called. This commit creates a new filter to get the resulting credits links and the wraps the old function around it to echo as was the previous behavior
  • Loading branch information
mlaetitia committed Jan 31, 2024
1 parent 846410d commit f077e72
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions colophon.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@

if ( ! function_exists( 'team51_credits' ) ) :


/**
* A colophon-generating method for WordPress Special Projects Sites.
* A wrapper to the colophon generating method for WordPress Special Projects Sites.
* Echoes a the resulting credit links
*
* Usage: team51_credits( 'separator= | ' );
*
* @param array{separator?: string, wpcom?: string, pressable?: string} $args The Args passed to the function.
*
* @return void|string
* @return void
*/
function team51_credits( $args = array() ) {
echo apply_filters( 'team51_credits_filter', '', $args ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
* A colophon-generating method for WordPress Special Projects Sites.
*
* Usage: team51_credits( 'separator= | ' );
*
* @param array{separator?: string, wpcom?: string, pressable?: string} $args The Args passed to the function.
*
* @return string the resulting credits link
*/
function team51_credits_filter( $str = '', $args = array() ) {
$args = wp_parse_args(
$args,
array(
Expand All @@ -29,7 +44,6 @@ function team51_credits( $args = array() ) {
'wpcom' => sprintf( __( 'Proudly powered by %s.', 'team51' ), 'WordPress' ),
/* translators: %s: Pressable. */
'pressable' => sprintf( __( 'Hosted by %s.', 'team51' ), 'Pressable' ),
'return_output' => false,
)
);

Expand Down Expand Up @@ -89,18 +103,14 @@ function team51_credits( $args = array() ) {
*/
$credit_links = apply_filters( 'team51_credit_links', $credit_links, $args );

$output = implode(
return implode(
esc_html( $args['separator'] ),
$credit_links //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, this cant be escaped as it runs through a filter
);

if ( $args['return_output'] ) {
return $output;
}

echo $output; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, this cant be escaped as it runs through a filter
}

add_action( 'team51_credits', 'team51_credits', 10, 1 );
add_filter( 'team51_credits_filter', 'team51_credits_filter', 10, 2 );
endif;

if ( ! function_exists( 'team51_credits_shortcode' ) ) :
Expand Down

0 comments on commit f077e72

Please sign in to comment.