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

Fix PHP notice that showed when orderby query parameter was an array (With Unit tests) #6439

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/wp-admin/includes/class-wp-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,18 @@ public function search_box( $text, $input_id ) {
$input_id = $input_id . '-search-input';

if ( ! empty( $_REQUEST['orderby'] ) ) {
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
if ( is_array( $_REQUEST['orderby'] ) ) {
foreach ( $_REQUEST['orderby'] as $key => $value ) {
/*
* Orderby can be either an associative array or non-associative array.
* In the latter case, this makes sure the key is a string before calling esc_attr().
*/
$key = (string) $key;
echo '<input type="hidden" name="orderby[' . esc_attr( $key ) . ']" value="' . esc_attr( $value ) . '" />';
}
} else {
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
}
}
if ( ! empty( $_REQUEST['order'] ) ) {
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
Expand Down Expand Up @@ -1868,4 +1879,4 @@ public function _js_vars() {

printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );
}
}
}
Rajinsharwar marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions tests/phpunit/tests/admin/wpListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,39 @@ public function data_compat_fields() {
),
);
}

/**
* Tests that "search_box()" works correctly with an array of orderby.
*
* @ticket 59494
*
*/
Rajinsharwar marked this conversation as resolved.
Show resolved Hide resolved
public function test_search_box_working_with_array_of_orderby() {
$_REQUEST['s'] = 'search term';
$_REQUEST['orderby'] = array(
'menu_order' => 'ASC',
'title' => 'ASC',
);

$output = get_echo( array( $this->list_table, 'search_box' ), array( 'foo Label', 59494 ) );

$expected_html1 = '<input type="hidden" name="orderby[menu_order]" value="ASC" />';
$expected_html2 = '<input type="hidden" name="orderby[title]" value="ASC" />';

$this->assertStringContainsString( $expected_html1, $output );
$this->assertStringContainsString( $expected_html2, $output );

// Test with one 'orderby' element.
$_REQUEST['orderby'] = array(
'title' => 'ASC',
);

$output = get_echo( array( $this->list_table, 'search_box' ), array( 'foo Label', 59494 ) );

$expected_html1 = '<input type="hidden" name="orderby[menu_order]" value="ASC" />';
$expected_html2 = '<input type="hidden" name="orderby[title]" value="ASC" />';

$this->assertStringNotContainsString( $expected_html1, $output );
$this->assertStringContainsString( $expected_html2, $output );
}
Rajinsharwar marked this conversation as resolved.
Show resolved Hide resolved
}
Loading