-
Notifications
You must be signed in to change notification settings - Fork 116
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
Fix: column value getting overridden when multiple custom columns are added #1185
Conversation
Hi @kidunot89, @dero, @kasparsd 👋 Any chance to get a review on this? |
classes/class-list-table.php
Outdated
@@ -396,6 +396,12 @@ public function column_default( $item, $column_name ) { | |||
|
|||
if ( ! empty( $inserted_columns ) && is_array( $inserted_columns ) ) { | |||
foreach ( $inserted_columns as $column_title ) { | |||
|
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.
@kasparsd or @kidunot89 Correct me if I'm wrong here, but to me this seems like the problem is that the loop needs to exit if there is a value found and also that the default gets set if there isn't a value coming from the filters.
So the whole code block would change to:
if ( ! empty( $inserted_columns ) && is_array( $inserted_columns ) ) {
foreach ( $inserted_columns as $column_title ) {
/**
* If column title inserted via wp_stream_register_column_defaults ($column_title) exists
* among columns registered with get_columns ($column_name) and there is an action associated
* with this column, do the action
*
* Also, note that the action name must include the $column_title registered
* with wp_stream_register_column_defaults
*/
if ( $column_title === $column_name && has_filter( "wp_stream_insert_column_default_{$column_title}" ) ) {
/**
* Allows for the addition of content under a specified column.
*
* @param object $record Contents of the row
*
* @return string
*/
$out = apply_filters( "wp_stream_insert_column_default_{$column_title}", $column_name, $record );
break;
}
}
}
// Set default value.
if ( empty( $out ) ) {
$out = $column_name;
}
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.
This should work as expected, I will update the PR
One question - If any column value is actually empty
(for an example case where that column is not relevant for that log type) that gets set to $column_name
everywhere, That is because of the last // Set default value.
section. Is this an expected behavior or can that be removed?
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.
I’m not sure what the intended default behavior is. Is there a test for this @kidunot89
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.
@derekherman @Nikschavan I don't believe so, so I've added some more changes. This should be all ready after somebody else reviews it.
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.
@Nikschavan I'm sorry it took so long to get this reviewed 😞.
classes/class-list-table.php
Outdated
if ( empty( $out ) ) { | ||
$out = $column_name; | ||
} | ||
|
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.
As @derekherman stated above the loop has to be exited once the value is found or it will just overwrite itself with the default value.
@Nikschavan You can replace the whole conditional started on Line 397 with @derekherman suggestion above and that should work.
ebeb46b
to
1a9ec17
Compare
1a9ec17
to
1232b34
Compare
if ( $column_title === $column_name ) { | ||
/** | ||
* Allows for the addition of content under a specified column. | ||
* | ||
* @param object $record Contents of the row | ||
* @param string $out Column content. | ||
* @param object $record Record with row content. | ||
* @param string $column_name Column name. | ||
* | ||
* @return string | ||
*/ | ||
$out = apply_filters( "wp_stream_insert_column_default_{$column_title}", $column_name, $record ); | ||
} else { | ||
$out = $column_name; | ||
$out = apply_filters( "wp_stream_insert_column_default_{$column_title}", $out, $record, $column_name ); |
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.
While this was on the right track, the default filter is never called when creating a key => value pair of column(s).
So when filtering a new column, the $column_title
could be Record ID
, which is not a filterable value in L418.
Really this should have been:
if ( $column_title === $column_name ) { | |
/** | |
* Allows for the addition of content under a specified column. | |
* | |
* @param object $record Contents of the row | |
* @param string $out Column content. | |
* @param object $record Record with row content. | |
* @param string $column_name Column name. | |
* | |
* @return string | |
*/ | |
$out = apply_filters( "wp_stream_insert_column_default_{$column_title}", $column_name, $record ); | |
} else { | |
$out = $column_name; | |
$out = apply_filters( "wp_stream_insert_column_default_{$column_title}", $out, $record, $column_name ); | |
foreach ( $inserted_columns as $column_key => $column_title ) { | |
@@ -404,21 +405,20 @@ public function column_default( $item, $column_name ) { | |
* Also, note that the action name must include the $column_title registered | |
* with wp_stream_register_column_defaults | |
*/ | |
if ( $column_title === $column_key ) { | |
/** | |
* Allows for the addition of content under a specified column. | |
* | |
* @param string $out Column content. | |
* @param object $record Record with row content. | |
* @param string $column_name Column name. | |
* | |
* @return string | |
*/ | |
$out = apply_filters( "wp_stream_insert_column_default_{$column_key}", $out, $record, $column_name ); |
Fixes #1184.
The statement
$out = $column_name;
would get called even after the correct value is set to the variable for the first column.I have changed this so that the default value will be used only if the variable
out
is still unchanged so that it retains the correct column value for all the columns.Checklist
contributing.md
).Release Changelog
Release Checklist
master
branch.readme.txt
.stream.php
.Stable tag
inreadme.txt
.classes/class-plugin.php
.