Skip to content

Commit

Permalink
Merge pull request #55 from bookwyrm/fix-column-mismatch-broken-sort
Browse files Browse the repository at this point in the history
Fix column mismatch broken sort
  • Loading branch information
jjgrainger authored Apr 12, 2020
2 parents cb1bb46 + e3d8720 commit 627a236
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,50 @@ public function sortable($sortable)
return $this;
}

/**
* Check if an orderby field is a custom sort option.
* @param string $orderby the orderby value from query params
*/
public function isSortable($orderby)
{
if (array_key_exists($orderby, $this->sortable)) {
return true;
}

foreach ($this->sortable as $column => $options) {
if (is_string($options) && $options === $orderby) {
return true;
}
if (is_array($options) && isset($options[0]) && $options[0] === $orderby) {
return true;
}
}

return false;
}

/**
* Get meta key for an orderby.
* @param string $orderby the orderby value from query params
*/
public function sortableMeta($orderby)
{
if (array_key_exists($orderby, $this->sortable)) {
return $this->sortable[$orderby];
}

foreach ($this->sortable as $column => $options) {
if (is_string($options) && $options === $orderby) {
return $options;
}
if (is_array($options) && isset($options[0]) && $options[0] === $orderby) {
return $options;
}
}

return '';
}

/**
* Modify the columns for the object
* @param array $columns WordPress default columns
Expand Down
4 changes: 2 additions & 2 deletions src/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ public function sortSortableColumns($query)
$orderby = $query->get('orderby');

// if the sorting a custom column
if (array_key_exists($orderby, $this->columns()->sortable)) {
if ($this->columns()->isSortable($orderby)) {
// get the custom column options
$meta = $this->columns()->sortable[$orderby];
$meta = $this->columns()->sortableMeta($orderby);

// determine type of ordering
if (is_string($meta)) {
Expand Down

0 comments on commit 627a236

Please sign in to comment.