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

Adds support for value => label definitions in the admin filter options #107

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 31 additions & 10 deletions src/class-extended-cpt-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,14 @@ public function filters() {

$selected = wp_unslash( get_query_var( $filter_key ) );

$use_key = false;

foreach ( $filter['options'] as $k => $v ) {
if ( ! is_numeric( $k ) ) {
$use_key = true;
break;
}
}
$filter['options'] = $this->parse_filter_options( $filter['options'] );

# Output the dropdown:
?>
<select name="<?php echo esc_attr( $filter_key ); ?>" id="filter_<?php echo esc_attr( $filter_key ); ?>">
<option value=""><?php echo esc_html( $filter['title'] ); ?></option>
<?php
foreach ( $filter['options'] as $k => $v ) {
$key = ( $use_key ? $k : $v );
foreach ( $filter['options'] as $key => $v ) {
?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $selected, $key ); ?>><?php echo esc_html( $v ); ?></option>
<?php } ?>
Expand Down Expand Up @@ -1221,4 +1213,33 @@ protected function p2p_connection_exists( string $connection ) : bool {
return $this->connection_exists[ $connection ];
}


/**
* Parses filter options into value => label pairs.
*
* @param array $options Filter options.
*
* @return array Filter options single dimension array.
*/
public function parse_filter_options( $options ) {
$result = [];

foreach ( $options as $k => $v ) {
// Check if $v is an array of key, and value properties
if ( is_array( $v ) ) {
$value = $v['value'] ?? ( $v['label'] ?? null );
$label = $v['label'] ?? $value;

if ( ! is_null( $value ) ) {
$result[ $value ] = $label;
}
} else {
$key = ! is_numeric( $k ) ? $k : $v;
$result[ $key ] = $v;
}
}

return $result;
}

}
1 change: 0 additions & 1 deletion tests/phpunit/extended-cpts-test-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@ public function tearDown() {
// reset
set_current_screen( 'front' );
}

}
114 changes: 114 additions & 0 deletions tests/phpunit/test-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
class Extended_CPT_Functions_Test extends Extended_CPT_Test_Admin
{

/**
* @dataProvider filterOptions
*/
public function testParseFilterOptions($test, $expected)
{
$admin = new Extended_CPT_Admin(new Extended_CPT('test'));
$result = $admin->parse_filter_options($test);
// $ecptAdminMock = $this->getMockBuilder(Extended_CPT_Admin::class)
// ->disableOriginalConstructor()
// ->getMock();

// $result = $ecptAdminMock->parse_filter_options($test);

$this->assertEquals($result, $expected);
}

public function filterOptions()
{
return [
[
'test' => [],
'expected' => []
],
[
'test' => [
'Option One',
'Option Two'
],
'expected' => [
'Option One' => 'Option One',
'Option Two' => 'Option Two'
]
],
[
'test' => [
1 => 'Option One',
2 => 'Option Two'
],
'expected' => [
'Option One' => 'Option One',
'Option Two' => 'Option Two'
]
],
[
'test' => [
'1' => 'Option One',
'2' => 'Option Two'
],
'expected' => [
'Option One' => 'Option One',
'Option Two' => 'Option Two'
]
],
[
'test' => [
[
'value' => 0,
'label' => 'Option One'
],
[
'value' => 1,
'label' => 'Option Two'
],
],
'expected' => [
0 => 'Option One',
1 => 'Option Two'
]
],
[
'test' => [
[
'value' => 'Option One',
],
[
'label' => 'Option Two'
],
],
'expected' => [
'Option One' => 'Option One',
'Option Two' => 'Option Two'
]
],
[
'test' => [
[
'Option One',
],
[
'Option Two'
],
'Option Three',
],
'expected' => [
'Option Three' => 'Option Three',
]
],
[
'test' => [
'value' => 'Option One',
'label' => 'Option Two'
],
'expected' => [
'value' => 'Option One',
'label' => 'Option Two'
]
],
];
}
}