]> _ Git - couzy.git/blob
668a95de54c4dfed5ceea46f6ece0c9621f4d190
[couzy.git] /
1 <?php
2 /**
3  * @package WPSEO\Admin|Google_Search_Console
4  */
5
6 if ( ! class_exists( 'WP_List_Table' ) ) {
7         require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
8 }
9
10 /**
11  * Class WPSEO_GSC_Table
12  */
13 class WPSEO_GSC_Table extends WP_List_Table {
14
15         /**
16          * @var string
17          */
18         private $search_string;
19
20         /**
21          * @var array
22          */
23         protected $_column_headers;
24
25         /**
26          * The category that is displayed
27          *
28          * @var mixed|string
29          */
30         private $current_view;
31
32         /**
33          * @var integer
34          */
35         private $per_page = 50;
36
37         /**
38          * @var integer
39          */
40         private $current_page = 1;
41
42         /**
43          * @var array
44          */
45         private $modal_heights = array(
46                 'create'         => 350,
47                 'no_premium'     => 125,
48                 'already_exists' => 150,
49         );
50
51         /**
52          * The constructor
53          *
54          * @param string $platform
55          * @param string $category
56          * @param array  $items
57          */
58         public function __construct( $platform, $category, array $items ) {
59                 parent::__construct();
60
61                 // Adding the thickbox.
62                 add_thickbox();
63
64                 // Set search string.
65                 if ( ( $search_string = filter_input( INPUT_GET, 's' ) ) != '' ) {
66                         $this->search_string = $search_string;
67                 }
68
69                 $this->current_view = $category;
70
71                 // Set the crawl issue source.
72                 $this->show_fields( $platform );
73
74                 $this->items = $items;
75         }
76
77         /**
78          * Getting the screen id from this table
79          *
80          * @return string
81          */
82         public function get_screen_id() {
83                 return $this->screen->id;
84         }
85
86         /**
87          * Setup the table variables, fetch the items from the database, search, sort and format the items.
88          */
89         public function prepare_items() {
90                 // Get variables needed for pagination.
91                 $this->per_page     = $this->get_items_per_page( 'errors_per_page', $this->per_page );
92                 $this->current_page = intval( ( $paged = filter_input( INPUT_GET, 'paged' ) ) ? $paged : 1 );
93
94                 $this->setup_columns();
95                 $this->views();
96                 $this->parse_items();
97         }
98
99         /**
100          * Set the table columns
101          *
102          * @return array
103          */
104         public function get_columns() {
105                 $columns = array(
106                         'cb'             => '<input type="checkbox" />',
107                         'url'            => __( 'URL', 'wordpress-seo' ),
108                         'last_crawled'   => __( 'Last crawled', 'wordpress-seo' ),
109                         'first_detected' => __( 'First detected', 'wordpress-seo' ),
110                         'response_code'  => __( 'Response code', 'wordpress-seo' ),
111                 );
112
113                 return $columns;
114         }
115
116         /**
117          * Return the columns that are sortable
118          *
119          * @return array
120          */
121         protected function get_sortable_columns() {
122                 $sortable_columns = array(
123                         'url'            => array( 'url', false ),
124                         'last_crawled'   => array( 'last_crawled', false ),
125                         'first_detected' => array( 'first_detected', false ),
126                         'response_code'  => array( 'response_code', false ),
127                 );
128
129                 return $sortable_columns;
130         }
131
132         /**
133          * Return available bulk actions
134          *
135          * @return array
136          */
137         protected function get_bulk_actions() {
138                 return array(
139                         'mark_as_fixed' => __( 'Mark as fixed', 'wordpress-seo' ),
140                 );
141         }
142
143         /**
144          * Default method to display a column
145          *
146          * @param array  $item
147          * @param string $column_name
148          *
149          * @return mixed
150          */
151         protected function column_default( $item, $column_name ) {
152                 return $item[ $column_name ];
153         }
154
155         /**
156          * Checkbox column
157          *
158          * @param array $item
159          *
160          * @return string
161          */
162         protected function column_cb( $item ) {
163                 return sprintf(
164                         '<input type="checkbox" name="wpseo_crawl_issues[]" value="%s" />', $item['url']
165                 );
166         }
167
168         /**
169          * Formatting the output of the column last crawled into a dateformat
170          *
171          * @param array $item
172          *
173          * @return string
174          */
175         protected function column_last_crawled( $item ) {
176                 return date_i18n( get_option( 'date_format' ), strtotime( $item['last_crawled'] ) );
177         }
178
179         /**
180          * Formatting the output of the column first detected into a dateformat
181          *
182          * @param array $item
183          *
184          * @return string
185          */
186         protected function column_first_detected( $item ) {
187                 return date_i18n( get_option( 'date_format' ), strtotime( $item['first_detected'] ) );
188         }
189
190         /**
191          * URL column
192          *
193          * @param array $item
194          *
195          * @return string
196          */
197         protected function column_url( $item ) {
198                 $actions = array();
199
200                 if ( $this->can_create_redirect() ) {
201                         /**
202                          * Modal box
203                          */
204                         $modal_height = $this->modal_box( $item['url'] );
205
206                         $actions['create_redirect'] = '<a title="' . __( 'Create a redirect', 'wordpress-seo' ) . '" href="#TB_inline?width=600&height=' . $this->modal_heights[ $modal_height ] . '&inlineId=redirect-' . md5( $item['url'] ) . '" class="thickbox">' . __( 'Create redirect', 'wordpress-seo' ) . '</a>';
207                 }
208
209                 $actions['view']        = '<a href="' . $item['url'] . '" target="_blank">' . __( 'View', 'wordpress-seo' ) . '</a>';
210                 $actions['markasfixed'] = '<a href="javascript:wpseo_mark_as_fixed(\'' . urlencode( $item['url'] ) . '\');">' . __( 'Mark as fixed', 'wordpress-seo' ) . '</a>';
211
212                 return sprintf(
213                         '<span class="value">%1$s</span> %2$s',
214                         $item['url'],
215                         $this->row_actions( $actions )
216                 );
217         }
218
219         /**
220          * Running the setup of the columns
221          */
222         private function setup_columns() {
223                 $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
224         }
225
226         /**
227          * Check if the current category allow creating redirects
228          * @return bool
229          */
230         private function can_create_redirect() {
231                 return in_array( $this->current_view, array( 'soft_404', 'not_found', 'access_denied' ) );
232         }
233
234         /**
235          * Setting the table navigation
236          *
237          * @param int $total_items
238          * @param int $posts_per_page
239          */
240         private function set_pagination( $total_items, $posts_per_page ) {
241                 $this->set_pagination_args( array(
242                         'total_items' => $total_items,
243                         'total_pages' => ceil( ( $total_items / $posts_per_page ) ),
244                         'per_page'    => $posts_per_page,
245                 ) );
246         }
247
248         /**
249          * Setting the items
250          */
251         private function parse_items() {
252                 if ( is_array( $this->items ) && count( $this->items ) > 0 ) {
253                         if ( ! empty( $this->search_string ) ) {
254                                 $this->do_search();
255                         }
256
257                         $this->set_pagination( count( $this->items ), $this->per_page );
258
259                         $this->sort_items();
260                         $this->paginate_items();
261                 }
262         }
263
264         /**
265          * Search through the items
266          */
267         private function do_search() {
268                 $results = array();
269
270                 foreach ( $this->items as $item ) {
271                         foreach ( $item as $value ) {
272                                 if ( stristr( $value, $this->search_string ) !== false ) {
273                                         $results[] = $item;
274                                         continue;
275                                 }
276                         }
277                 }
278
279                 $this->items = $results;
280         }
281
282         /**
283          * Running the pagination
284          */
285         private function paginate_items() {
286                 // Setting the starting point. If starting point is below 1, overwrite it with value 0, otherwise it will be sliced of at the back.
287                 $slice_start = ( $this->current_page - 1 );
288                 if ( $slice_start < 0 ) {
289                         $slice_start = 0;
290                 }
291
292                 // Apply 'pagination'.
293                 $this->items = array_slice( $this->items, ( $slice_start * $this->per_page ), $this->per_page );
294         }
295
296         /**
297          * Sort the items by callback
298          */
299         private function sort_items() {
300                 // Sort the results.
301                 usort( $this->items, array( $this, 'do_reorder' ) );
302         }
303
304         /**
305          * Doing the sorting of the issues
306          *
307          * @param array $a
308          * @param array $b
309          *
310          * @return int
311          */
312         private function do_reorder( $a, $b ) {
313                 // If no sort, default to title.
314                 $orderby = ( $orderby = filter_input( INPUT_GET, 'orderby' ) ) ? $orderby : 'url';
315
316                 // If no order, default to asc.
317                 $order = ( $order = filter_input( INPUT_GET, 'order' ) ) ? $order : 'asc';
318
319                 // When there is a raw field of it, sort by this field.
320                 if ( array_key_exists( $orderby . '_raw', $a ) && array_key_exists( $orderby . '_raw', $b ) ) {
321                         $orderby = $orderby . '_raw';
322                 }
323
324                 // Determine sort order.
325                 $result = strcmp( $a[ $orderby ], $b[ $orderby ] );
326
327                 // Send final sort direction to usort.
328                 return ( $order === 'asc' ) ? $result : ( - $result );
329         }
330
331         /**
332          * Modal box
333          *
334          * @param string $url
335          *
336          * @return string
337          */
338         private function modal_box( $url ) {
339                 $current_redirect = false;
340                 $view_type        = $this->modal_box_type( $url, $current_redirect );
341
342                 require WPSEO_PATH . '/admin/google_search_console/views/gsc-create-redirect.php';
343
344                 return $view_type;
345         }
346
347         /**
348          * Determine which model box type should be rendered
349          *
350          * @param string $url
351          * @param string $current_redirect
352          *
353          * @return string
354          */
355         private function modal_box_type( $url, &$current_redirect ) {
356                 if ( defined( 'WPSEO_PREMIUM_FILE' ) && class_exists( 'WPSEO_URL_Redirect_Manager' ) ) {
357                         static $redirect_manager;
358
359                         if ( ! $redirect_manager ) {
360                                 $redirect_manager = new WPSEO_URL_Redirect_Manager();
361                         }
362
363                         if ( $current_redirect = $redirect_manager->search_url( $url ) ) {
364                                 return 'already_exists';
365                         }
366
367                         return 'create';
368                 }
369
370                 return 'no_premium';
371         }
372
373
374         /**
375          * Showing the hidden fields used by the AJAX requests
376          *
377          * @param string $platform
378          */
379         private function show_fields( $platform ) {
380                 echo "<input type='hidden' name='wpseo_gsc_nonce' value='" . wp_create_nonce( 'wpseo_gsc_nonce' ) . "' />";
381                 echo "<input id='field_platform' type='hidden' name='platform' value='{$platform}' />";
382                 echo "<input id='field_category' type='hidden' name='category' value='{$this->current_view}' />";
383         }
384
385 }