Skip to content
Merged
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
29 changes: 16 additions & 13 deletions docs/post-types/Creating-columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,38 @@ class PriceColumn extends Column
}

/**
* Populate column callback.
* Position a column before/after another.
*
* @return void
* @return array
*/
public function populate( int $post_id ): void
public function position(): array
{
echo '$' . get_post_meta( $post_id, '_price', true );
return $this->after( 'title' );
}

/**
* Set the column can be sorted.
* Populate column callback.
*
* @return boolean
* @return callable
*/
public function isSortable(): bool
public function populate(): callable
{
return true;
return function( int $post_id ) {
echo '$' . get_post_meta( $post_id, '_price', true );
};
}

/**
* Handle sorting the column by modifying the admin query.
*
* @param $query \WP_Query
* @return void
* @return callable
*/
public function sort(\WP_Query $query): void
public function sort(): callable
{
$query->set( 'meta_key', '_price' );
$query->set( 'orderby', 'meta_value_num' );
return function( \WP_Query $query ) {
$query->set( 'meta_key', '_price' );
$query->set( 'orderby', 'meta_value_num' );
};
}
}
```
Expand Down
15 changes: 6 additions & 9 deletions docs/post-types/Modifying-columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To modify a post types admin columns use the `column()` method. This method acce

## Adding Columns

To add columns to the admin edit screen pass an array of column slugs and labels to the `add()` method.
To add a column to the admin edit screen pass a column ket and label to the `label()` method.

```php
use PostTypes\PostType;
Expand All @@ -22,7 +22,7 @@ class Books extends PostType
public function columns( Columns $column ): Columns
{
// Add a new price column.
$columns->add( 'price', __( 'Price', 'my-text-domain' ) );
$columns->label( 'price', __( 'Price', 'my-text-domain' ) );

// Populate the price column with post meta.
$columns->populate( 'price', function( $post_id ) {
Expand Down Expand Up @@ -125,9 +125,9 @@ class Books extends PostType
}
```

## Column Order
## Column Positions

To rearrange columns pass an array of column slugs and position to the `order()` method. Only olumns you want to reorder need to be set, not all columns.
To rearrange columns pass an array of column slugs and position to the `position()` method. Only olumns you want to reorder need to be set, not all columns.


```php
Expand All @@ -145,11 +145,8 @@ class Books extends PostType
*/
public function columns( Columns $column ): Columns
{
// Order the new Rating and Genre columns.
$columns->order( [
'rating' => 2,
'genre' => 4,
] );
// Position the rating column after the title column.
$columns->position( 'rating', 'after', 'title' );

return $columns;
}
Expand Down
8 changes: 3 additions & 5 deletions docs/taxonomies/Modifying-columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Genres extends Taxonomy
public function columns( Columns $column ): Columns
{
// Add a new Popularity column.
$columns->add( 'popularity', __( 'Popularity', 'my-text-domain' ) );
$columns->label( 'popularity', __( 'Popularity', 'my-text-domain' ) );

// Populate the popularity column with term meta.
$columns->populate( 'popularity', function( $term_id ) {
Expand Down Expand Up @@ -147,10 +147,8 @@ class Genres extends Taxonomy
*/
public function columns( Columns $column ): Columns
{
// Order the new Popularity column.
$columns->order( [
'popularity' => 2,
] );
// Position the new Popularity column.
$columns->position( 'popularity', 'after', 'title' );

return $columns;
}
Expand Down
18 changes: 6 additions & 12 deletions examples/Genres.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@ public function posttypes(): array {
}

public function columns( Columns $columns ): Columns {
$columns->remove(['posts']);
$columns->remove( [ 'posts' ] );

$columns->add(
'popularity',
__( 'Popularity', 'post-types' ),
function( $term_id ) {
echo get_term_meta( $term_id, 'popularity', true );
}
);
$columns->label( 'popularity', __( 'Popularity', 'post-types' ) );

$columns->order( [
'popularity' => 2,
] );
$columns->populate( 'popularity', function( $term_id ) {
echo get_term_meta( $term_id, 'popularity', true );
} );

$columns->sortable( 'popularity', function( $query ) {
$columns->sort( 'popularity', function( $query ) {
$query->query_vars['orderby'] = 'meta_value';
$query->query_vars['meta_key'] = 'popularity';
} );
Expand Down
22 changes: 11 additions & 11 deletions examples/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public function label(): string {
return __( 'Price', 'post-types' );
}

public function order(): int {
return 2;
public function position(): array {
return $this->after( 'title' );
}

public function populate( int $post_id ): void {
echo '£' . get_post_meta( $post_id, 'price', true );
public function populate(): callable {
return function( int $post_id ) {
echo '£' . get_post_meta( $post_id, 'price', true );
};
}

public function isSortable(): bool {
return true;
}

public function sort( $query ): void {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'price');
public function sort(): callable {
return function( $query ) {
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'price' );
};
}
}
22 changes: 14 additions & 8 deletions examples/books.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,29 @@ public function columns( Columns $columns ): Columns {

$columns->column( new Price );

$columns->add( 'rating', __( 'Rating', 'post-types' ) );
$columns->label( 'rating', __( 'Rating', 'post-types' ) );

$columns->position( 'rating', 'after', 'price' );

$columns->populate( 'rating', function( $post_id ) {
echo get_post_meta( $post_id, 'rating', true );
} );

$columns->sortable( 'rating', function( $query ) {
$columns->sort( 'rating', function( $query ) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'rating');
} );

$columns->order( [
'price' => 4,
'rating' => 5,
'taxonomy-genre' => 2,
'tags' => 3,
] );
$columns->add( 'rating' )
->after( 'price' )
->label( __( 'Rating', 'post-types' ) )
->populate( function( $post_id ) {
echo get_post_meta( $post_id, 'rating', true );
} )
->sort( function( $query ) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'rating');
} );

return $columns;
}
Expand Down
40 changes: 25 additions & 15 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,52 @@ public function label(): string
/**
* Populate the column.
*
* @param integer $objectId
* @return void
* @return callable|null
*/
public function populate(int $objectId): void
public function populate(): ?callable
{
return;
return null;
}

/**
* Set the column order.
*
* @return integer|null
* @return array|null
*/
public function position(): ?array
{
return null;
}

/**
* Return the sort callback for the column.
*
* @return callable|null
*/
public function order(): ?int
public function sort(): ?callable
{
return null;
}

/**
* Handle sorting the column.
* Return the before position array structure.
*
* @param \WP_Query|\WP_Term_Query $query
* @return void
* @param string $reference
* @return array
*/
public function sort($query)
protected function before(string $reference): array
{
return;
return ['before', $reference];
}

/**
* Can the column be sorted.
* Return the after position array structure.
*
* @return boolean
* @param string $reference
* @return array
*/
public function isSortable(): bool
protected function after(string $reference): array
{
return false;
return ['after', $reference];
}
}
107 changes: 107 additions & 0 deletions src/ColumnBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace PostTypes;

class ColumnBuilder
{
/**
* Columns instance.
*
* @var Columns
*/
protected $columns;

/**
* Column key.
*
* @var string
*/
protected $key;

/**
* Constructor.
*
* @param Columns $columns
* @param string $key
*/
public function __construct(Columns $columns, string $key)
{
$this->columns = $columns;
$this->key = $key;
}

/**
* Set the label for the column.
*
* @param string $label
* @return ColumnBuilder
*/
public function label(string $label): ColumnBuilder
{
$this->columns->label($this->key, $label);

return $this;
}

/**
* Position a column.
*
* @param string $direction
* @param string $reference
* @return ColumnBuilder
*/
public function position(string $direction, string $reference): ColumnBuilder
{
$this->columns->position($this->key, $direction, $reference);

return $this;
}

/**
* Position a column after another.
*
* @param string $reference
* @return ColumnBuilder
*/
public function after(string $reference): ColumnBuilder
{
return $this->position('after', $reference);
}

/**
* Position a column before another.
*
* @param string $reference
* @return ColumnBuilder
*/
public function before(string $reference): ColumnBuilder
{
return $this->position('before', $reference);
}

/**
* Set columns populate callback.
*
* @param callable $callback
* @return ColumnBuilder
*/
public function populate(callable $callback): ColumnBuilder
{
$this->columns->populate($this->key, $callback);

return $this;
}

/**
* Set columns sort callback.
*
* @param callable $callback
* @return ColumnBuilder
*/
public function sort(callable $callback): ColumnBuilder
{
$this->columns->sort($this->key, $callback);

return $this;
}
}
Loading