summaryrefslogtreecommitdiff
path: root/src/Columns.php
blob: 51f0cd78cec0164343afe4c587e27db368b5337f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php

namespace PostTypes;

/**
 * Columns
 *
 * Used to help manage a post types columns in the admin table
 */
class Columns
{
    /**
     * Holds an array of all the defined columns.
     *
     * @var array
     */
    public $items = [];

    /**
     * An array of columns to add.
     *
     * @var array
     */
    public $add = [];

    /**
     * An array of columns to hide.
     *
     * @var array
     */
    public $hide = [];

    /**
     * An array of columns to reposition.
     *
     * @var array
     */
    public $positions = [];

    /**
     * An array of custom populate callbacks.
     *
     * @var array
     */
    public $populate = [];

    /**
     * An array of columns that are sortable.
     *
     * @var array
     */
    public $sortable = [];

    /**
     * Set the all columns
     * @param array $columns an array of all the columns to replace
     */
    public function set($columns)
    {
        $this->items = $columns;
    }

    /**
     * Add a new column
     * @param string  $column   the slug of the column
     * @param string  $label    the label for the column
     */
    public function add($columns, $label = null)
    {

        if (!is_array($columns)) {
            $columns = [$columns => $label];
        }

        foreach ($columns as $column => $label) {
            if (is_null($label)) {
                $label = str_replace(['_', '-'], ' ', ucfirst($column));
            }

            $this->add[$column] = $label;
        }

        return $this;
    }

    /**
     * Add a column to hide
     * @param  string $column the slug of the column to hdie
     */
    public function hide($columns)
    {
        if (!is_array($columns)) {
            $columns = [$columns];
        }

        foreach ($columns as $column) {
            $this->hide[] = $column;
        }

        return $this;
    }

    /**
     * Set a custom callback to populate a column
     * @param  string $column   the column slug
     * @param  mixed  $callback callback function
     */
    public function populate($column, $callback)
    {
        $this->populate[$column] = $callback;

        return $this;
    }

    /**
     * Define the postion for a columns
     * @param  string  $columns  an array of columns
     */
    public function order($columns)
    {
        foreach ($columns as $column => $position) {
            $this->positions[$column] = $position;
        }

        return $this;
    }

    /**
     * Set columns that are sortable
     * @param  string  $column     the slug of the column
     * @param  string  $meta_value the meta_value to orderby
     * @param  boolean $is_num     whether to order by string/number
     */
    public function sortable($sortable)
    {
        foreach ($sortable as $column => $options) {
            $this->sortable[$column] = $options;
        }

        return $this;
    }

    /**
     * Check if an orderby field is a custom sort option.
     * @param  string  $orderby  the orderby value from query params
     */
    public function isSortable($orderby)
    {
        if (is_string($orderby) && array_key_exists($orderby, $this->sortable)) {
            return true;
        }

        foreach ($this->sortable as $column => $options) {
            if (is_string($options) && $options === $orderby) {
                return true;
            }
            if (is_array($options) && isset($options[0]) && $options[0] === $orderby) {
                return true;
            }
        }

        return false;
    }

    /**
     * Get meta key for an orderby.
     * @param  string  $orderby  the orderby value from query params
     */
    public function sortableMeta($orderby)
    {
        if (array_key_exists($orderby, $this->sortable)) {
            return $this->sortable[$orderby];
        }

        foreach ($this->sortable as $column => $options) {
            if (is_string($options) && $options === $orderby) {
                return $options;
            }
            if (is_array($options) && isset($options[0]) && $options[0] === $orderby) {
                return $options;
            }
        }

        return '';
    }

    /**
     * Modify the columns for the object
     * @param  array  $columns WordPress default columns
     * @return array           The modified columns
     */
    public function modifyColumns($columns)
    {
        // if user defined set columns, return those
        if (!empty($this->items)) {
            return $this->items;
        }

        // add additional columns
        if (!empty($this->add)) {
            foreach ($this->add as $key => $label) {
                $columns[$key] = $label;
            }
        }

        // unset hidden columns
        if (!empty($this->hide)) {
            foreach ($this->hide as $key) {
                unset($columns[$key]);
            }
        }

        // if user has made added custom columns
        if (!empty($this->positions)) {
            foreach ($this->positions as $key => $position) {
                // find index of the element in the array
                $index = array_search($key, array_keys($columns));
                // retrieve the element in the array of columns
                $item = array_slice($columns, $index, 1);
                // remove item from the array
                unset($columns[$key]);

                // split columns array into two at the desired position
                $start = array_slice($columns, 0, $position, true);
                $end = array_slice($columns, $position, count($columns) - 1, true);

                // insert column into position
                $columns = $start + $item + $end;
            }
        }

        return $columns;
    }
}