/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/FileListTable.php


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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
declare( strict_types );

namespace 
Automattic\WooCommerce\Internal\Admin\Logging\FileV2;

use 
Automattic\WooCommerce\Internal\Admin\Logging\PageController;

use 
WP_List_Table;

/**
 * FileListTable class.
 */
class FileListTable extends WP_List_Table {
    
/**
     * The user option key for saving the preferred number of files displayed per page.
     *
     * @const string
     */
    
public const PER_PAGE_USER_OPTION_KEY 'woocommerce_logging_file_list_per_page';

    
/**
     * Instance of FileController.
     *
     * @var FileController
     */
    
private $file_controller;

    
/**
     * Instance of PageController.
     *
     * @var PageController
     */
    
private $page_controller;

    
/**
     * FileListTable class.
     *
     * @param FileController $file_controller Instance of FileController.
     * @param PageController $page_controller Instance of PageController.
     */
    
public function __constructFileController $file_controllerPageController $page_controller ) {
        
$this->file_controller $file_controller;
        
$this->page_controller $page_controller;

        
parent::__construct(
            array(
                
'singular' => 'log-file',
                
'plural'   => 'log-files',
                
'ajax'     => false,
            )
        );
    }

    
/**
     * Render message when there are no items.
     *
     * @return void
     */
    
public function no_items(): void {
        
esc_html_e'No log files found.''woocommerce' );
    }

    
/**
     * Retrieves the list of bulk actions available for this table.
     *
     * @return array
     */
    
protected function get_bulk_actions(): array {
        return array(
            
'export' => esc_html__'Download''woocommerce' ),
            
'delete' => esc_html__'Delete permanently''woocommerce' ),
        );
    }

    
/**
     * Get the existing log sources for the filter dropdown.
     *
     * @return array
     */
    
protected function get_sources_list(): array {
        
$sources $this->file_controller->get_file_sources();
        if ( 
is_wp_error$sources ) ) {
            return array();
        }

        
sort$sources );

        return 
$sources;
    }

    
/**
     * Displays extra controls between bulk actions and pagination.
     *
     * @param string $which The location of the tablenav being rendered. 'top' or 'bottom'.
     *
     * @return void
     */
    
protected function extra_tablenav$which ): void {
        
$all_sources $this->get_sources_list();

        
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Recommended
        
$current_source File::sanitize_sourcewp_unslash$_GET['source'] ?? '' ) );

        
?>
        <div class="alignleft actions">
            <?php if ( 'top' === $which ) : ?>
                <label for="filter-by-source" class="screen-reader-text"><?php esc_html_e'Filter by log source''woocommerce' ); ?></label>
                <select name="source" id="filter-by-source">
                    <option<?php selected$current_source'' ); ?> value=""><?php esc_html_e'All sources''woocommerce' ); ?></option>
                    <?php foreach ( $all_sources as $source ) : ?>
                        <option<?php selected$current_source$source ); ?> value="<?php echo esc_attr$source ); ?>">
                            <?php echo esc_html$source ); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <?php
                submit_button
(
                    
__'Filter''woocommerce' ),
                    
'',
                    
'filter_action',
                    
false,
                    array(
                        
'id' => 'logs-filter-submit',
                    )
                );
                
?>
            <?php endif; ?>
        </div>
        <?php
    
}

    
/**
     * Set up the column header info.
     *
     * @return void
     */
    
public function prepare_column_headers(): void {
        
$this->_column_headers = array(
            
$this->get_columns(),
            
get_hidden_columns$this->screen ),
            
$this->get_sortable_columns(),
            
$this->get_primary_column(),
        );
    }

    
/**
     * Prepares the list of items for displaying.
     *
     * @return void
     */
    
public function prepare_items(): void {
        
$per_page $this->get_items_per_page(
            
self::PER_PAGE_USER_OPTION_KEY,
            
$this->get_per_page_default()
        );

        
$defaults  = array(
            
'per_page' => $per_page,
            
'offset'   => ( $this->get_pagenum() - ) * $per_page,
        );
        
$file_args wp_parse_args(
            
$this->page_controller->get_query_params( array( 'order''orderby''source' ) ),
            
$defaults
        
);

        
$total_items $this->file_controller->get_files$file_argstrue );
        if ( 
is_wp_error$total_items ) ) {
            
printf(
                
'<div class="notice notice-warning"><p>%s</p></div>',
                
esc_html$total_items->get_error_message() )
            );

            return;
        }

        
$total_pages ceil$total_items $per_page );
        
$items       $this->file_controller->get_files$file_args );

        
$this->items $items;

        
$this->set_pagination_args(
            array(
                
'per_page'    => $per_page,
                
'total_items' => $total_items,
                
'total_pages' => $total_pages,
            )
        );
    }

    
/**
     * Gets a list of columns.
     *
     * @return array
     */
    
public function get_columns(): array {
        
$columns = array(
            
'cb'       => '<input type="checkbox" />',
            
'source'   => esc_html__'Source''woocommerce' ),
            
'created'  => esc_html__'Date created''woocommerce' ),
            
'modified' => esc_html__'Date modified''woocommerce' ),
            
'size'     => esc_html__'File size''woocommerce' ),
        );

        return 
$columns;
    }

    
/**
     * Gets a list of sortable columns.
     *
     * @return array
     */
    
protected function get_sortable_columns(): array {
        
$sortable = array(
            
'source'   => array( 'source' ),
            
'created'  => array( 'created' ),
            
'modified' => array( 'modified'true ),
            
'size'     => array( 'size' ),
        );

        return 
$sortable;
    }

    
/**
     * Render the checkbox column.
     *
     * @param File $item The current log file being rendered.
     *
     * @return string
     */
    
public function column_cb$item ): string {
        
ob_start();
        
?>
        <input
            id="cb-select-<?php echo esc_attr$item->get_file_id() ); ?>"
            type="checkbox"
            name="file_id[]"
            value="<?php echo esc_attr$item->get_file_id() ); ?>"
        />
        <label for="cb-select-<?php echo esc_attr$item->get_file_id() ); ?>">
            <span class="screen-reader-text">
                <?php
                printf
(
                    
// translators: 1. a date, 2. a slug-style name for a file.
                    
esc_html__'Select the %1$s log file for %2$s''woocommerce' ),
                    
esc_htmlgmdateget_option'date_format' ), $item->get_created_timestamp() ) ),
                    
esc_html$item->get_source() )
                );
                
?>
            </span>
        </label>
        <?php
        
return ob_get_clean();
    }

    
/**
     * Render the source column.
     *
     * @param File $item The current log file being rendered.
     *
     * @return string
     */
    
public function column_source$item ): string {
        
$log_file        $item->get_file_id();
        
$single_file_url add_query_arg(
            array(
                
'view'    => 'single_file',
                
'file_id' => $log_file,
            ),
            
$this->page_controller->get_logs_tab_url()
        );
        
$rotation        '';
        if ( ! 
is_null$item->get_rotation() ) ) {
            
$rotation sprintf(
                
' &ndash; <span class="post-state">%d</span>',
                
$item->get_rotation()
            );
        }

        return 
sprintf(
            
'<a class="row-title" href="%1$s">%2$s</a>%3$s',
            
esc_url$single_file_url ),
            
esc_html$item->get_source() ),
            
$rotation
        
);
    }

    
/**
     * Render the created column.
     *
     * @param File $item The current log file being rendered.
     *
     * @return string
     */
    
public function column_created$item ): string {
        
$timestamp $item->get_created_timestamp();

        return 
gmdate'Y-m-d'$timestamp );
    }

    
/**
     * Render the modified column.
     *
     * @param File $item The current log file being rendered.
     *
     * @return string
     */
    
public function column_modified$item ): string {
        
$timestamp $item->get_modified_timestamp();

        return 
gmdate'Y-m-d H:i:s'$timestamp );
    }

    
/**
     * Render the size column.
     *
     * @param File $item The current log file being rendered.
     *
     * @return string
     */
    
public function column_size$item ): string {
        
$size $item->get_file_size();

        return 
size_format$size );
    }

    
/**
     * Helper to get the default value for the per_page arg.
     *
     * @return int
     */
    
public function get_per_page_default(): int {
        return 
$this->file_controller::DEFAULTS_GET_FILES['per_page'];
    }
}