Completed
Pull Request — master (#2)
by Beñat
02:52
created

LogsTable::get_columns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 8.

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.

Loading history...
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
     * Constructor.
27
     *
28
     * @param Storage $storage The storage
29
     */
30
    public function __construct(Storage $storage)
31
    {
32
        parent::__construct([
33
            'singular' => __('Log', \WPSymfonyForm::TRANSLATION_DOMAIN),
34
            'plural'   => __('Logs', \WPSymfonyForm::TRANSLATION_DOMAIN),
35
            'ajax'     => false,
36
        ]);
37
38
        $this->storage = $storage;
39
        add_action('admin_head', [$this, 'header']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function no_items()
46
    {
47
        _e('No emails sent yet', \WPSymfonyForm::TRANSLATION_DOMAIN);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function column_default($item, $column_name)
54
    {
55
        return $item[$column_name];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function get_columns()
62
    {
63
        $columns = [];
64
        foreach ($this->storage->properties() as $property) {
65
            $columns[$property] = __(ucfirst($property), \WPSymfonyForm::TRANSLATION_DOMAIN);
66
        }
67
68
        return $columns;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function get_sortable_columns()
75
    {
76
        return [
77
            'date'  => ['date', true],
78
            'email' => ['email', true],
79
        ];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function prepare_items()
86
    {
87
        $this->_column_headers = $this->get_column_info();
88
89
        $limit = $this->get_items_per_page('logs_per_page', 10);
90
        $offset = $this->get_pagenum();
91
        $total = $this->storage->size();
92
93
        $this->set_pagination_args([
94
            'total_items' => $total,
95
            'per_page'    => $limit,
96
        ]);
97
98
        $this->items = $this->storage->get($limit, $offset);
99
    }
100
101
    /**
102
     * Callback that customize the header of the table.
103
     */
104
    public function header()
105
    {
106
        echo <<<EOL
107
<style type="text/css">
108
    .wp-list-table .column-date {
109
        width: 15%;
110
    }
111
112
    .wp-list-table .column-email {
113
        width: 15%;
114
    }
115
116
    .wp-list-table .column-comment {
117
        width: 30%;
118
    }
119
    
120
    .wp-list-table .column-message {
121
        width: 30%;
122
    }
123
</style>
124
EOL;
125
    }
126
}
127