1
|
|
|
<?php |
2
|
|
|
namespace Gwa\Wordpress\Zero\Module; |
3
|
|
|
|
4
|
|
|
use Gwa\Wordpress\WpBridge\Traits\WpBridgeTrait; |
5
|
|
|
use Gwa\Wordpress\Zero\Module\AbstractThemeModule; |
6
|
|
|
|
7
|
|
|
class IdColumnsModule extends AbstractThemeModule |
8
|
|
|
{ |
9
|
|
|
use WpBridgeTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Add id columns to column posts |
13
|
|
|
* |
14
|
|
|
* @param array $defaults |
15
|
|
|
*/ |
16
|
|
|
public function addColumnId($defaults) |
17
|
|
|
{ |
18
|
|
|
$defaults['date column-id'] = __('ID'); |
19
|
|
|
return $defaults; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Add id columns to custom column post |
24
|
|
|
* |
25
|
|
|
* @param string $columnName |
26
|
|
|
* @param string $id |
27
|
|
|
*/ |
28
|
|
|
public function addColumnIdValue($columnName, $id) |
29
|
|
|
{ |
30
|
|
|
if ($columnName === 'date column-id') { |
31
|
|
|
echo $id; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return the ID for the column |
37
|
|
|
*/ |
38
|
|
|
public function addColumnReturnValue($value, $columName, $id) |
39
|
|
|
{ |
40
|
|
|
if ($columName === 'date column-id') { |
41
|
|
|
$value = $id; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adds a id column on all admin pages |
49
|
|
|
*/ |
50
|
|
|
public function addIdColumn() |
51
|
|
|
{ |
52
|
|
View Code Duplication |
foreach (get_taxonomies() as $taxonomy) { |
|
|
|
|
53
|
|
|
$this->getWpBridge()->addAction("manage_edit-${taxonomy}_columns", [$this, 'addColumnId']); |
54
|
|
|
$this->getWpBridge()->addFilter("manage_${taxonomy}_custom_column", [$this, 'addColumnReturnValue'], 10, 3); |
55
|
|
|
$this->getWpBridge()->addFilter("manage_edit-${taxonomy}_sortable_columns", [$this, 'addColumnId']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
foreach (get_post_types() as $ptype) { |
|
|
|
|
59
|
|
|
$this->getWpBridge()->addAction("manage_edit-${ptype}_columns", [$this, 'addColumnId']); |
60
|
|
|
$this->getWpBridge()->addFilter("manage_${ptype}_posts_custom_column", [$this, 'addColumnIdValue'], 10, 3); |
61
|
|
|
$this->getWpBridge()->addFilter("manage_edit-${ptype}_sortable_columns", [$this, 'addColumnId']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->getWpBridge()->addAction('manage_media_custom_column', [$this, 'addColumnIdValue'], 10, 2); |
65
|
|
|
$this->getWpBridge()->addAction('manage_link_custom_column', [$this, 'addColumnId'], 10, 2); |
66
|
|
|
$this->getWpBridge()->addAction('manage_edit-link-categories_columns', [$this, 'addColumnId']); |
67
|
|
|
$this->getWpBridge()->addAction('manage_users_columns', [$this, 'addColumnId']); |
68
|
|
|
$this->getWpBridge()->addAction('manage_edit-comments_columns', [$this, 'addColumnId']); |
69
|
|
|
$this->getWpBridge()->addAction('manage_comments_custom_column', [$this, 'addColumnIdValue'], 10, 2); |
70
|
|
|
|
71
|
|
|
$this->getWpBridge()->addFilter('manage_media_columns', [$this, 'addColumnId']); |
72
|
|
|
$this->getWpBridge()->addFilter('manage_link-manager_columns', [$this, 'addColumnId']); |
73
|
|
|
$this->getWpBridge()->addFilter('manage_link_categories_custom_column', [$this, 'addColumnReturnValue'], 10, 3); |
74
|
|
|
$this->getWpBridge()->addFilter('manage_users_custom_column', [$this, 'addColumnReturnValue'], 10, 3); |
75
|
|
|
$this->getWpBridge()->addFilter('manage_edit-comments_sortable_columns', [$this, 'addColumnId']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getActionMap() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
[ |
82
|
1 |
|
'hooks' => 'admin_init', |
83
|
1 |
|
'class' => $this, |
84
|
1 |
|
'method' => 'addIdColumn', |
85
|
1 |
|
'prio' => 199, |
86
|
1 |
|
'args' => 1, |
87
|
1 |
|
], |
88
|
1 |
|
]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.