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
SearchListTable.php
230 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 | * SearchListTable class. |
| 12 | */ |
| 13 | class SearchListTable extends WP_List_Table { |
| 14 | /** |
| 15 | * The user option key for saving the preferred number of search results displayed per page. |
| 16 | * |
| 17 | * @const string |
| 18 | */ |
| 19 | public const PER_PAGE_USER_OPTION_KEY = 'woocommerce_logging_search_results_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 | * SearchListTable 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' => 'wc-logs-search-result', |
| 48 | 'plural' => 'wc-logs-search-results', |
| 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 search results.', 'woocommerce' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set up the column header info. |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function prepare_column_headers(): void { |
| 69 | $this->_column_headers = array( |
| 70 | $this->get_columns(), |
| 71 | array(), |
| 72 | array(), |
| 73 | $this->get_primary_column(), |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Prepares the list of items for displaying. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function prepare_items(): void { |
| 83 | $per_page = $this->get_items_per_page( |
| 84 | self::PER_PAGE_USER_OPTION_KEY, |
| 85 | $this->get_per_page_default() |
| 86 | ); |
| 87 | |
| 88 | $args = array( |
| 89 | 'per_page' => $per_page, |
| 90 | 'offset' => ( $this->get_pagenum() - 1 ) * $per_page, |
| 91 | ); |
| 92 | |
| 93 | $file_args = $this->page_controller->get_query_params( |
| 94 | array( 'date_end', 'date_filter', 'date_start', 'order', 'orderby', 'search', 'source' ) |
| 95 | ); |
| 96 | $search = $file_args['search']; |
| 97 | unset( $file_args['search'] ); |
| 98 | |
| 99 | $total_items = $this->file_controller->search_within_files( $search, $args, $file_args, true ); |
| 100 | if ( is_wp_error( $total_items ) ) { |
| 101 | printf( |
| 102 | '<div class="notice notice-warning"><p>%s</p></div>', |
| 103 | esc_html( $total_items->get_error_message() ) |
| 104 | ); |
| 105 | |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if ( $total_items >= $this->file_controller::SEARCH_MAX_RESULTS ) { |
| 110 | printf( |
| 111 | '<div class="notice notice-info"><p>%s</p></div>', |
| 112 | sprintf( |
| 113 | // translators: %s is a number. |
| 114 | esc_html__( 'The number of search results has reached the limit of %s. Try refining your search.', 'woocommerce' ), |
| 115 | esc_html( number_format_i18n( $this->file_controller::SEARCH_MAX_RESULTS ) ) |
| 116 | ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | $total_pages = ceil( $total_items / $per_page ); |
| 121 | $results = $this->file_controller->search_within_files( $search, $args, $file_args ); |
| 122 | $this->items = $results; |
| 123 | |
| 124 | $this->set_pagination_args( |
| 125 | array( |
| 126 | 'per_page' => $per_page, |
| 127 | 'total_items' => $total_items, |
| 128 | 'total_pages' => $total_pages, |
| 129 | ) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets a list of columns. |
| 135 | * |
| 136 | * @return array |
| 137 | */ |
| 138 | public function get_columns(): array { |
| 139 | $columns = array( |
| 140 | 'file_id' => esc_html__( 'File', 'woocommerce' ), |
| 141 | 'line_number' => esc_html__( 'Line #', 'woocommerce' ), |
| 142 | 'line' => esc_html__( 'Matched Line', 'woocommerce' ), |
| 143 | ); |
| 144 | |
| 145 | return $columns; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Render the file_id column. |
| 150 | * |
| 151 | * @param array $item The current search result being rendered. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public function column_file_id( array $item ): string { |
| 156 | // Add a word break after the rotation number, if it exists. |
| 157 | $file_id = preg_replace( '/\.([0-9])+\-/', '.\1<wbr>-', $item['file_id'] ); |
| 158 | |
| 159 | return wp_kses( $file_id, array( 'wbr' => array() ) ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Render the line_number column. |
| 164 | * |
| 165 | * @param array $item The current search result being rendered. |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function column_line_number( array $item ): string { |
| 170 | $match_url = add_query_arg( |
| 171 | array( |
| 172 | 'view' => 'single_file', |
| 173 | 'file_id' => $item['file_id'], |
| 174 | ), |
| 175 | $this->page_controller->get_logs_tab_url() . '#L' . absint( $item['line_number'] ) |
| 176 | ); |
| 177 | |
| 178 | return sprintf( |
| 179 | '<a href="%1$s">%2$s</a>', |
| 180 | esc_url( $match_url ), |
| 181 | sprintf( |
| 182 | // translators: %s is a line number in a file. |
| 183 | esc_html__( 'Line %s', 'woocommerce' ), |
| 184 | number_format_i18n( absint( $item['line_number'] ) ) |
| 185 | ) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Render the line column. |
| 191 | * |
| 192 | * @param array $item The current search result being rendered. |
| 193 | * |
| 194 | * @return string |
| 195 | */ |
| 196 | public function column_line( array $item ): string { |
| 197 | $params = $this->page_controller->get_query_params( array( 'search' ) ); |
| 198 | $line = $item['line']; |
| 199 | |
| 200 | // Highlight matches within the line. |
| 201 | $pattern = preg_quote( $params['search'], '/' ); |
| 202 | preg_match_all( "/$pattern/i", $line, $matches, PREG_OFFSET_CAPTURE ); |
| 203 | if ( is_array( $matches[0] ) && count( $matches[0] ) >= 1 ) { |
| 204 | $length_change = 0; |
| 205 | |
| 206 | foreach ( $matches[0] as $match ) { |
| 207 | $replace = '<span class="search-match">' . $match[0] . '</span>'; |
| 208 | $offset = $match[1] + $length_change; |
| 209 | $orig_length = strlen( $match[0] ); |
| 210 | $replace_length = strlen( $replace ); |
| 211 | |
| 212 | $line = substr_replace( $line, $replace, $offset, $orig_length ); |
| 213 | |
| 214 | $length_change += $replace_length - $orig_length; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return wp_kses_post( $line ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Helper to get the default value for the per_page arg. |
| 223 | * |
| 224 | * @return int |
| 225 | */ |
| 226 | public function get_per_page_default(): int { |
| 227 | return $this->file_controller::DEFAULTS_SEARCH_WITHIN_FILES['per_page']; |
| 228 | } |
| 229 | } |
| 230 |