| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Hooks; |
| 4 |
|
| 5 |
use FL\Assistant\Data\Transformers\NotationsTransformer; |
| 6 |
use FL\Assistant\Data\Repository\NotationsRepository; |
| 7 |
use FL\Assistant\Data\Repository\LabelsRepository; |
| 8 |
use FL\Assistant\Data\Transformers\LabelsTransformer; |
| 9 |
|
| 10 |
class AdminColumns { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
add_action( 'init', [ $this, 'enqueue_custom_admin_columns' ], PHP_INT_MAX ); |
| 14 |
} |
| 15 |
|
| 16 |
public function enqueue_custom_admin_columns() { |
| 17 |
$types = get_post_types( [ 'public' => true ], 'objects' ); |
| 18 |
|
| 19 |
foreach ( $types as $type => $info ) { |
| 20 |
add_filter( "manage_{$type}_posts_columns", [ $this, 'add_columns' ] ); |
| 21 |
add_action( "manage_{$type}_posts_custom_column", [ $this, 'render_column' ], 10, 2 ); |
| 22 |
add_filter( "default_hidden_columns", [ $this, 'defualt_hidden_columns' ], 10, 2 ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public function add_columns( $columns ) { |
| 27 |
$columns['fl_assistant'] = __( 'Labels', 'assistant' ); |
| 28 |
return $columns; |
| 29 |
} |
| 30 |
|
| 31 |
public function render_column( $column, $post_id ) { |
| 32 |
$vars = '--fl-asst-red: #FF5335; --fl-asst-blue: #1BADF8; --fl-asst-green: #00D281; --fl-asst-yellow: #FFD000; --fl-asst-orange: #FF9500; --fl-asst-purple: #CC73E1; --fl-asst-pink: #FF2968;'; |
| 33 |
$styles = "{$vars} padding: 2px 5px; display: inline-flex; flex: 0 0 auto; border-radius: 3px; border: 1px solid #d2d2d2; align-items: center; color: black; background: white; margin: 2px 5px 3px; margin-left:0; font-size: 12px;"; |
| 34 |
$dot = 'width: 10px; height: 10px; flex: 0 0 10px; border-radius: 7px; background: blue; margin-right: 5px;'; |
| 35 |
|
| 36 |
if ( 'fl_assistant' === $column ) { |
| 37 |
$transformer = new NotationsTransformer; |
| 38 |
$repository = new NotationsRepository( $transformer ); |
| 39 |
$labels = $repository->get_labels( 'post', $post_id ); |
| 40 |
|
| 41 |
$labels_repo = new LabelsRepository; |
| 42 |
$labels_transformer = new LabelsTransformer( $labels_repo ); |
| 43 |
$terms = $labels_repo->query( [ 'hide_empty' => false ] )->get_terms(); |
| 44 |
|
| 45 |
// Assemble term meta dataset |
| 46 |
$term_meta = []; |
| 47 |
foreach ( $terms as $key => $term ) { |
| 48 |
$item = call_user_func( $labels_transformer, $term ); |
| 49 |
$term_meta[ $item['id'] ] = $item; |
| 50 |
} |
| 51 |
|
| 52 |
// Output labels |
| 53 |
foreach ( $labels as $label ) { |
| 54 |
$term = $term_meta[ $label['label_id'] ]; |
| 55 |
if ( isset( $term ) ) { |
| 56 |
print "<span style='{$styles}'><span style='{$dot}; background-color: {$term['color']}'></span>{$term['label']}</span>"; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Make our custom column hidden by default. |
| 64 |
*/ |
| 65 |
public function defualt_hidden_columns( $hidden, $screen ) { |
| 66 |
$hidden[] = 'fl_assistant'; |
| 67 |
return $hidden; |
| 68 |
} |
| 69 |
} |
| 70 |
|