/var/www/html/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/SearchListTable.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
<?php
declare( strict_types );

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

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

use 
WP_List_Table;

/**
 * SearchListTable class.
 */
class SearchListTable extends WP_List_Table {
    
/**
     * The user option key for saving the preferred number of search results displayed per page.
     *
     * @const string
     */
    
public const PER_PAGE_USER_OPTION_KEY 'woocommerce_logging_search_results_per_page';

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

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

    
/**
     * SearchListTable 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' => 'wc-logs-search-result',
                
'plural'   => 'wc-logs-search-results',
                
'ajax'     => false,
            )
        );
    }

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

    
/**
     * Set up the column header info.
     *
     * @return void
     */
    
public function prepare_column_headers(): void {
        
$this->_column_headers = array(
            
$this->get_columns(),
            array(),
            array(),
            
$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()
        );

        
$args = array(
            
'per_page' => $per_page,
            
'offset'   => ( $this->get_pagenum() - ) * $per_page,
        );

        
$file_args $this->page_controller->get_query_params(
            array( 
'date_end''date_filter''date_start''order''orderby''search''source' )
        );
        
$search    $file_args['search'];
        unset( 
$file_args['search'] );

        
$total_items $this->file_controller->search_within_files$search$args$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;
        }

        if ( 
$total_items >= $this->file_controller::SEARCH_MAX_RESULTS ) {
            
printf(
                
'<div class="notice notice-info"><p>%s</p></div>',
                
sprintf(
                    
// translators: %s is a number.
                    
esc_html__'The number of search results has reached the limit of %s. Try refining your search.''woocommerce' ),
                    
esc_htmlnumber_format_i18n$this->file_controller::SEARCH_MAX_RESULTS ) )
                )
            );
        }

        
$total_pages ceil$total_items $per_page );
        
$results     $this->file_controller->search_within_files$search$args$file_args );
        
$this->items $results;

        
$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(
            
'file_id'     => esc_html__'File''woocommerce' ),
            
'line_number' => esc_html__'Line #''woocommerce' ),
            
'line'        => esc_html__'Matched Line''woocommerce' ),
        );

        return 
$columns;
    }

    
/**
     * Render the file_id column.
     *
     * @param array $item The current search result being rendered.
     *
     * @return string
     */
    
public function column_file_id( array $item ): string {
        
// Add a word break after the rotation number, if it exists.
        
$file_id preg_replace'/\.([0-9])+\-/''.\1<wbr>-'$item['file_id'] );

        return 
wp_kses$file_id, array( 'wbr' => array() ) );
    }

    
/**
     * Render the line_number column.
     *
     * @param array $item The current search result being rendered.
     *
     * @return string
     */
    
public function column_line_number( array $item ): string {
        
$match_url add_query_arg(
            array(
                
'view'    => 'single_file',
                
'file_id' => $item['file_id'],
            ),
            
$this->page_controller->get_logs_tab_url() . '#L' absint$item['line_number'] )
        );

        return 
sprintf(
            
'<a href="%1$s">%2$s</a>',
            
esc_url$match_url ),
            
sprintf(
                
// translators: %s is a line number in a file.
                
esc_html__'Line %s''woocommerce' ),
                
number_format_i18nabsint$item['line_number'] ) )
            )
        );
    }

    
/**
     * Render the line column.
     *
     * @param array $item The current search result being rendered.
     *
     * @return string
     */
    
public function column_line( array $item ): string {
        
$params $this->page_controller->get_query_params( array( 'search' ) );
        
$line   $item['line'];

        
// Highlight matches within the line.
        
$pattern preg_quote$params['search'], '/' );
        
preg_match_all"/$pattern/i"$line$matchesPREG_OFFSET_CAPTURE );
        if ( 
is_array$matches[0] ) && count$matches[0] ) >= ) {
            
$length_change 0;

            foreach ( 
$matches[0] as $match ) {
                
$replace        '<span class="search-match">' $match[0] . '</span>';
                
$offset         $match[1] + $length_change;
                
$orig_length    strlen$match[0] );
                
$replace_length strlen$replace );

                
$line substr_replace$line$replace$offset$orig_length );

                
$length_change += $replace_length $orig_length;
            }
        }

        return 
wp_kses_post$line );
    }

    
/**
     * 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_SEARCH_WITHIN_FILES['per_page'];
    }
}