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

BuddyPress: Uncaught ValueErrror from printf-style messages. #1701

Open
dd32 opened this issue Feb 14, 2025 · 1 comment
Open

BuddyPress: Uncaught ValueErrror from printf-style messages. #1701

dd32 opened this issue Feb 14, 2025 · 1 comment

Comments

@dd32
Copy link
Contributor

dd32 commented Feb 14, 2025

Bug Report

Similar to #1508, the BuddyPress connector (And it appears others, by reading code) are still affected by that..

Uncaught ValueError: Unknown format specifier "D" in wp-content/plugins/stream/classes/class-log.php:127
Stack trace:
#0 wp-content/plugins/stream/classes/class-log.php(127): vsprintf('Unmarked activi...', Array)
#1 [internal function]: WP_Stream\Log->log('buddypress', 'Unmarked activi...', Array, 9894139, 'forums', 'unspammed', 0)
#2 wp-content/plugins/stream/classes/class-connector.php(178): call_user_func_array(Array, Array)
#3 wp-content/plugins/stream/connectors/class-connector-buddypress.php(685): WP_Stream\Connector->log('Unmarked activi...', Array, 123456, 'forums', 'unspammed')

Expected Behavior

The $message passed to Log::log() should be a printf-ready string, instead it's often containing prepared data.

Actual Behavior

$this->log(
sprintf(
/* translators: %s: an activity title (e.g. "Update") */
__( 'Unmarked activity "%s" as spam', 'stream' ),
wp_strip_all_tags( $activity->action )
),
array(
'id' => $activity->id,
'item_id' => $activity->item_id,
'type' => $activity->type,
'author' => $activity->user_id,
),

In the above case, vsprintf( 'Unmarked activity "something 5% Do something"', [ 'id' => ?, 'item_id' => ??, ... ] ) is called, which is obviously wrong.

It appears that a number of connectors are calling log() and passing context to the vsprintf args key, instead of args that should be interpolated into the message.

Here's an example of a correct usage:

/* translators: %1$s: a user display name, %2$s: a user role (e.g. "Jane Doe", "subscriber") */
$message = _x(
'New user account created for %1$s (%2$s)',
'1: User display name, 2: User role',
'stream'
);
$user_to_log = $current_user->ID;
}
$this->log(
$message,
array(
'display_name' => ( $registered_user->display_name ) ? $registered_user->display_name : $registered_user->user_login,
'roles' => implode( ', ', $this->get_role_labels( $user_id ) ),
),

Here's another example of an incorrect usage (That happens to pass, as the first key of the $args happens to be the one wanted)

$this->log(
/* translators: %s: a username (e.g. "administrator") */
__( 'Comment flooding by %s detected and prevented', 'stream' ),
compact( 'user_name', 'user_id', 'time_lastcomment', 'time_newcomment' ),
null,
'comments',
'flood'
);

This is a little more prevalent in the plugin than I expected, but appears to mostly not be an issue as it's rare for the $message to contain a % character.

@dd32
Copy link
Contributor Author

dd32 commented Feb 14, 2025

Personally speaking, I'd drop printf support in the Log::log( $message ) arg and require all connectors/uses perform the sprintf at that point in time, converting the $args into a "Message context" parameter instead. That however prevents the filters from being able to filter the saved message by altering the passed data 🤷

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

No branches or pull requests

1 participant