/** * @covers \Pressbooks\Utility\multi_sort */ public function test_multi_sort() { $arr = [['foo' => 1, 'bar' => 'A'], ['foo' => 3, 'bar' => 'C'], ['foo' => 2, 'bar' => 'B']]; $res = \Pressbooks\Utility\multi_sort($arr, 'foo:desc'); $this->assertEquals('3', $res[0]['foo']); $this->assertEquals('1', $res[2]['foo']); $res = \Pressbooks\Utility\multi_sort($arr, 'bar:asc', 'foo:desc'); $this->assertEquals('A', $res[0]['bar']); $this->assertEquals('C', $res[2]['bar']); $res = \Pressbooks\Utility\multi_sort($arr); $this->assertFalse($res); }
/** * Get book data * Sort by featured DESC, title ASC * * @param PB_Catalog $catalog * * @return array */ function _books(PB_Catalog $catalog) { $books = $catalog->getAggregate(); foreach ($books as $key => $val) { // Deleted if ($val['deleted']) { unset($books[$key]); continue; } // Calculate cover height $books[$key]['cover_height'] = _cover_height($val['cover_url']['pb_cover_medium']); } return \Pressbooks\Utility\multi_sort($books, 'featured:desc', 'title:asc'); }
/** * Return a comma delimited string from an SQL array of tags, in alphabetical order. * * @param array $tags * * @return string */ static function tagsToString(array $tags) { $tags = \Pressbooks\Utility\multi_sort($tags, 'tag:asc'); $str = ''; foreach ($tags as $tag) { $str .= $tag['tag'] . ', '; } return rtrim($str, ', '); }
/** * REQUIRED! This is where you prepare your data for display. This method will * usually be used to query the database, sort and filter the data, and generally * get it ready to be displayed. At a minimum, we should set $this->items and * $this->set_pagination_args() */ function prepare_items() { // Define Columns $columns = $this->get_columns(); $hidden = $this->getHiddenColumns(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array($columns, $hidden, $sortable); // Get data, sort $data = $this->getItemsData(); $valid_cols = $this->get_sortable_columns(); $order = !empty($_REQUEST['order']) ? $_REQUEST['order'] : 'asc'; // If no order, default to asc if (isset($_REQUEST['orderby']) && isset($valid_cols[$_REQUEST['orderby']])) { $data = \Pressbooks\Utility\multi_sort($data, "{$_REQUEST['orderby']}:{$order}"); } else { $data = \Pressbooks\Utility\multi_sort($data, 'status:desc', 'title:asc'); // Default } // Pagination $per_page = 1000; $current_page = $this->get_pagenum(); $total_items = count($data); /* The WP_List_Table class does not handle pagination for us, so we need * to ensure that the data is trimmed to only the current page. We can use * array_slice() to */ $data = array_slice($data, ($current_page - 1) * $per_page, $per_page); /* REQUIRED. Now we can add our *sorted* data to the items property, where * it can be used by the rest of the class. */ $this->items = $data; /* REQUIRED. We also have to register our pagination options & calculations. */ $args = array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page)); $this->set_pagination_args($args); }