1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace LIN3S\WPSymfonyForm\Admin\Views; |
4
|
|
|
|
5
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\Storage; |
6
|
|
|
|
7
|
|
|
if (!class_exists('WP_List_Table')) { |
8
|
|
|
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Logs table. |
13
|
|
|
* |
14
|
|
|
* @author Beñat Espiña <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class LogsTable extends \WP_List_Table |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The storage. |
20
|
|
|
* |
21
|
|
|
* @var Storage |
22
|
|
|
*/ |
23
|
|
|
private $storage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The form type name. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $formType; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $formType The form type name |
36
|
|
|
* @param Storage $storage The storage |
37
|
|
|
*/ |
38
|
|
|
public function __construct($formType, Storage $storage) |
39
|
|
|
{ |
40
|
|
|
parent::__construct([ |
41
|
|
|
'singular' => __('Log', \WPSymfonyForm::TRANSLATION_DOMAIN), |
42
|
|
|
'plural' => __('Logs', \WPSymfonyForm::TRANSLATION_DOMAIN), |
43
|
|
|
'ajax' => false, |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$this->formType = $formType; |
47
|
|
|
$this->storage = $storage; |
48
|
|
|
add_action('admin_head', [$this, 'header']); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function no_items() |
55
|
|
|
{ |
56
|
|
|
_e('No emails sent yet', \WPSymfonyForm::TRANSLATION_DOMAIN); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function column_default($item, $column_name) |
63
|
|
|
{ |
64
|
|
|
return $item[$column_name]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function get_columns() |
71
|
|
|
{ |
72
|
|
|
$columns = []; |
73
|
|
|
foreach ($this->storage->properties() as $property) { |
74
|
|
|
$columns[$property] = __(ucfirst($property), \WPSymfonyForm::TRANSLATION_DOMAIN); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $columns; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function get_sortable_columns() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
'date' => ['date', true], |
87
|
|
|
'email' => ['email', true], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function prepare_items() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->_column_headers = $this->get_column_info(); |
97
|
|
|
|
98
|
|
|
$limit = $this->get_items_per_page('logs_per_page', 10); |
99
|
|
|
$offset = $this->get_pagenum(); |
100
|
|
|
$this->items = $this->storage->query(['formType' => $this->formType], $limit, $offset); |
101
|
|
|
$total = count($this->items); |
102
|
|
|
|
103
|
|
|
$this->set_pagination_args([ |
104
|
|
|
'total_items' => $total, |
105
|
|
|
'per_page' => $limit, |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Callback that customize the header of the table. |
111
|
|
|
*/ |
112
|
|
|
public function header() |
113
|
|
|
{ |
114
|
|
|
echo <<<EOL |
115
|
|
|
<style type="text/css"> |
116
|
|
|
.wp-list-table .column-date { |
117
|
|
|
width: 15%; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
.wp-list-table .column-email { |
121
|
|
|
width: 15%; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
.wp-list-table .column-comment { |
125
|
|
|
width: 30%; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
.wp-list-table .column-message { |
129
|
|
|
width: 30%; |
130
|
|
|
} |
131
|
|
|
</style> |
132
|
|
|
EOL; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.