File.php
2 years ago
FileController.php
1 year ago
FileExporter.php
2 years ago
FileListTable.php
2 years ago
SearchListTable.php
2 years ago
FileListTable.php
335 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Admin\Logging\PageController; |
| 7 | |
| 8 | use WP_List_Table; |
| 9 | |
| 10 | /** |
| 11 | * FileListTable class. |
| 12 | */ |
| 13 | class FileListTable extends WP_List_Table { |
| 14 | /** |
| 15 | * The user option key for saving the preferred number of files displayed per page. |
| 16 | * |
| 17 | * @const string |
| 18 | */ |
| 19 | public const PER_PAGE_USER_OPTION_KEY = 'woocommerce_logging_file_list_per_page'; |
| 20 | |
| 21 | /** |
| 22 | * Instance of FileController. |
| 23 | * |
| 24 | * @var FileController |
| 25 | */ |
| 26 | private $file_controller; |
| 27 | |
| 28 | /** |
| 29 | * Instance of PageController. |
| 30 | * |
| 31 | * @var PageController |
| 32 | */ |
| 33 | private $page_controller; |
| 34 | |
| 35 | /** |
| 36 | * FileListTable class. |
| 37 | * |
| 38 | * @param FileController $file_controller Instance of FileController. |
| 39 | * @param PageController $page_controller Instance of PageController. |
| 40 | */ |
| 41 | public function __construct( FileController $file_controller, PageController $page_controller ) { |
| 42 | $this->file_controller = $file_controller; |
| 43 | $this->page_controller = $page_controller; |
| 44 | |
| 45 | parent::__construct( |
| 46 | array( |
| 47 | 'singular' => 'log-file', |
| 48 | 'plural' => 'log-files', |
| 49 | 'ajax' => false, |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Render message when there are no items. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function no_items(): void { |
| 60 | esc_html_e( 'No log files found.', 'woocommerce' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Retrieves the list of bulk actions available for this table. |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | protected function get_bulk_actions(): array { |
| 69 | return array( |
| 70 | 'export' => esc_html__( 'Download', 'woocommerce' ), |
| 71 | 'delete' => esc_html__( 'Delete permanently', 'woocommerce' ), |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the existing log sources for the filter dropdown. |
| 77 | * |
| 78 | * @return array |
| 79 | */ |
| 80 | protected function get_sources_list(): array { |
| 81 | $sources = $this->file_controller->get_file_sources(); |
| 82 | if ( is_wp_error( $sources ) ) { |
| 83 | return array(); |
| 84 | } |
| 85 | |
| 86 | sort( $sources ); |
| 87 | |
| 88 | return $sources; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Displays extra controls between bulk actions and pagination. |
| 93 | * |
| 94 | * @param string $which The location of the tablenav being rendered. 'top' or 'bottom'. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | protected function extra_tablenav( $which ): void { |
| 99 | $all_sources = $this->get_sources_list(); |
| 100 | |
| 101 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Recommended |
| 102 | $current_source = File::sanitize_source( wp_unslash( $_GET['source'] ?? '' ) ); |
| 103 | |
| 104 | ?> |
| 105 | <div class="alignleft actions"> |
| 106 | <?php if ( 'top' === $which ) : ?> |
| 107 | <label for="filter-by-source" class="screen-reader-text"><?php esc_html_e( 'Filter by log source', 'woocommerce' ); ?></label> |
| 108 | <select name="source" id="filter-by-source"> |
| 109 | <option<?php selected( $current_source, '' ); ?> value=""><?php esc_html_e( 'All sources', 'woocommerce' ); ?></option> |
| 110 | <?php foreach ( $all_sources as $source ) : ?> |
| 111 | <option<?php selected( $current_source, $source ); ?> value="<?php echo esc_attr( $source ); ?>"> |
| 112 | <?php echo esc_html( $source ); ?> |
| 113 | </option> |
| 114 | <?php endforeach; ?> |
| 115 | </select> |
| 116 | <?php |
| 117 | submit_button( |
| 118 | __( 'Filter', 'woocommerce' ), |
| 119 | '', |
| 120 | 'filter_action', |
| 121 | false, |
| 122 | array( |
| 123 | 'id' => 'logs-filter-submit', |
| 124 | ) |
| 125 | ); |
| 126 | ?> |
| 127 | <?php endif; ?> |
| 128 | </div> |
| 129 | <?php |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Set up the column header info. |
| 134 | * |
| 135 | * @return void |
| 136 | */ |
| 137 | public function prepare_column_headers(): void { |
| 138 | $this->_column_headers = array( |
| 139 | $this->get_columns(), |
| 140 | get_hidden_columns( $this->screen ), |
| 141 | $this->get_sortable_columns(), |
| 142 | $this->get_primary_column(), |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Prepares the list of items for displaying. |
| 148 | * |
| 149 | * @return void |
| 150 | */ |
| 151 | public function prepare_items(): void { |
| 152 | $per_page = $this->get_items_per_page( |
| 153 | self::PER_PAGE_USER_OPTION_KEY, |
| 154 | $this->get_per_page_default() |
| 155 | ); |
| 156 | |
| 157 | $defaults = array( |
| 158 | 'per_page' => $per_page, |
| 159 | 'offset' => ( $this->get_pagenum() - 1 ) * $per_page, |
| 160 | ); |
| 161 | $file_args = wp_parse_args( |
| 162 | $this->page_controller->get_query_params( array( 'order', 'orderby', 'source' ) ), |
| 163 | $defaults |
| 164 | ); |
| 165 | |
| 166 | $total_items = $this->file_controller->get_files( $file_args, true ); |
| 167 | if ( is_wp_error( $total_items ) ) { |
| 168 | printf( |
| 169 | '<div class="notice notice-warning"><p>%s</p></div>', |
| 170 | esc_html( $total_items->get_error_message() ) |
| 171 | ); |
| 172 | |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | $total_pages = ceil( $total_items / $per_page ); |
| 177 | $items = $this->file_controller->get_files( $file_args ); |
| 178 | |
| 179 | $this->items = $items; |
| 180 | |
| 181 | $this->set_pagination_args( |
| 182 | array( |
| 183 | 'per_page' => $per_page, |
| 184 | 'total_items' => $total_items, |
| 185 | 'total_pages' => $total_pages, |
| 186 | ) |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Gets a list of columns. |
| 192 | * |
| 193 | * @return array |
| 194 | */ |
| 195 | public function get_columns(): array { |
| 196 | $columns = array( |
| 197 | 'cb' => '<input type="checkbox" />', |
| 198 | 'source' => esc_html__( 'Source', 'woocommerce' ), |
| 199 | 'created' => esc_html__( 'Date created', 'woocommerce' ), |
| 200 | 'modified' => esc_html__( 'Date modified', 'woocommerce' ), |
| 201 | 'size' => esc_html__( 'File size', 'woocommerce' ), |
| 202 | ); |
| 203 | |
| 204 | return $columns; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Gets a list of sortable columns. |
| 209 | * |
| 210 | * @return array |
| 211 | */ |
| 212 | protected function get_sortable_columns(): array { |
| 213 | $sortable = array( |
| 214 | 'source' => array( 'source' ), |
| 215 | 'created' => array( 'created' ), |
| 216 | 'modified' => array( 'modified', true ), |
| 217 | 'size' => array( 'size' ), |
| 218 | ); |
| 219 | |
| 220 | return $sortable; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Render the checkbox column. |
| 225 | * |
| 226 | * @param File $item The current log file being rendered. |
| 227 | * |
| 228 | * @return string |
| 229 | */ |
| 230 | public function column_cb( $item ): string { |
| 231 | ob_start(); |
| 232 | ?> |
| 233 | <input |
| 234 | id="cb-select-<?php echo esc_attr( $item->get_file_id() ); ?>" |
| 235 | type="checkbox" |
| 236 | name="file_id[]" |
| 237 | value="<?php echo esc_attr( $item->get_file_id() ); ?>" |
| 238 | /> |
| 239 | <label for="cb-select-<?php echo esc_attr( $item->get_file_id() ); ?>"> |
| 240 | <span class="screen-reader-text"> |
| 241 | <?php |
| 242 | printf( |
| 243 | // translators: 1. a date, 2. a slug-style name for a file. |
| 244 | esc_html__( 'Select the %1$s log file for %2$s', 'woocommerce' ), |
| 245 | esc_html( gmdate( get_option( 'date_format' ), $item->get_created_timestamp() ) ), |
| 246 | esc_html( $item->get_source() ) |
| 247 | ); |
| 248 | ?> |
| 249 | </span> |
| 250 | </label> |
| 251 | <?php |
| 252 | return ob_get_clean(); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Render the source column. |
| 257 | * |
| 258 | * @param File $item The current log file being rendered. |
| 259 | * |
| 260 | * @return string |
| 261 | */ |
| 262 | public function column_source( $item ): string { |
| 263 | $log_file = $item->get_file_id(); |
| 264 | $single_file_url = add_query_arg( |
| 265 | array( |
| 266 | 'view' => 'single_file', |
| 267 | 'file_id' => $log_file, |
| 268 | ), |
| 269 | $this->page_controller->get_logs_tab_url() |
| 270 | ); |
| 271 | $rotation = ''; |
| 272 | if ( ! is_null( $item->get_rotation() ) ) { |
| 273 | $rotation = sprintf( |
| 274 | ' – <span class="post-state">%d</span>', |
| 275 | $item->get_rotation() |
| 276 | ); |
| 277 | } |
| 278 | |
| 279 | return sprintf( |
| 280 | '<a class="row-title" href="%1$s">%2$s</a>%3$s', |
| 281 | esc_url( $single_file_url ), |
| 282 | esc_html( $item->get_source() ), |
| 283 | $rotation |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Render the created column. |
| 289 | * |
| 290 | * @param File $item The current log file being rendered. |
| 291 | * |
| 292 | * @return string |
| 293 | */ |
| 294 | public function column_created( $item ): string { |
| 295 | $timestamp = $item->get_created_timestamp(); |
| 296 | |
| 297 | return gmdate( 'Y-m-d', $timestamp ); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Render the modified column. |
| 302 | * |
| 303 | * @param File $item The current log file being rendered. |
| 304 | * |
| 305 | * @return string |
| 306 | */ |
| 307 | public function column_modified( $item ): string { |
| 308 | $timestamp = $item->get_modified_timestamp(); |
| 309 | |
| 310 | return gmdate( 'Y-m-d H:i:s', $timestamp ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Render the size column. |
| 315 | * |
| 316 | * @param File $item The current log file being rendered. |
| 317 | * |
| 318 | * @return string |
| 319 | */ |
| 320 | public function column_size( $item ): string { |
| 321 | $size = $item->get_file_size(); |
| 322 | |
| 323 | return size_format( $size ); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Helper to get the default value for the per_page arg. |
| 328 | * |
| 329 | * @return int |
| 330 | */ |
| 331 | public function get_per_page_default(): int { |
| 332 | return $this->file_controller::DEFAULTS_GET_FILES['per_page']; |
| 333 | } |
| 334 | } |
| 335 |