Skip to content

Commit

Permalink
Merge pull request #362 from thomasgriffin/feature/simplify-table-sor…
Browse files Browse the repository at this point in the history
…ting

Simplify sorting of table data
  • Loading branch information
GaryJones committed Apr 27, 2015
2 parents 10bee0c + d01ed79 commit 43b9384
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,8 @@ class TGMPA_List_Table extends WP_List_Table {
public function __construct() {
$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );

add_filter( 'tgmpa_plugin_table_items', array( $this, 'sort_table_items' ) );

parent::__construct(
array(
'singular' => 'plugin',
Expand Down Expand Up @@ -1356,33 +1358,32 @@ protected function _gather_plugin_data() {
$i++;
}

// Sort plugins by Required/Recommended type and by alphabetical listing within each type.
$resort = array();
$req = array();
$rec = array();
return $table_data;

// Grab all the plugin types.
foreach ( $table_data as $plugin ) {
$resort[] = $plugin['type'];
}
}

// Sort each plugin by type.
foreach ( $resort as $type ) {
if ( 'Required' === $type ) {
$req[] = $type;
}
else {
$rec[] = $type;
}
/**
* Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
*
* @since 2.5.0
*
* @param array $items
*
* @return array
*/
public function sort_table_items( $items ) {

$type = array();
$name = array();

foreach ( $items as $i => $plugin ) {
$type[ $i ] = $plugin['type'];
$name[ $i ] = $plugin['sanitized_plugin'];
}

// Sort alphabetically each plugin type array, merge them and then sort in reverse (lists Required plugins first).
sort( $req );
sort( $rec );
array_merge( $resort, $req, $rec );
array_multisort( $resort, SORT_DESC, $table_data );
array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );

return $table_data;
return $items;

}

Expand Down Expand Up @@ -1858,7 +1859,7 @@ public function prepare_items() {
$this->process_bulk_actions();

// Store all of our plugin data into $items array so WP_List_Table can use it.
$this->items = $this->_gather_plugin_data();
$this->items = apply_filters( 'tgmpa_plugin_table_items', $this->_gather_plugin_data() );

}

Expand Down

0 comments on commit 43b9384

Please sign in to comment.