WPSEO_GSC_Table

Class WPSEO_GSC_Table.

Defined (1)

The class is defined in the following location(s).

/admin/google_search_console/class-gsc-table.php  
  1. class WPSEO_GSC_Table extends WP_List_Table { 
  2.  
  3. const FREE_MODAL_HEIGHT = 140; 
  4.  
  5. /** 
  6. * @var string 
  7. */ 
  8. private $search_string; 
  9.  
  10. /** 
  11. * @var array 
  12. */ 
  13. protected $_column_headers; 
  14.  
  15. /** 
  16. * The category that is displayed 
  17. * @var mixed|string 
  18. */ 
  19. private $current_view; 
  20.  
  21. /** 
  22. * @var integer 
  23. */ 
  24. private $per_page = 50; 
  25.  
  26. /** 
  27. * @var integer 
  28. */ 
  29. private $current_page = 1; 
  30.  
  31. /** 
  32. * Search Console table class constructor (subclasses list table). 
  33. * @param string $platform Platform (desktop, mobile, feature phone). 
  34. * @param string $category Type of the issues. 
  35. * @param array $items Set of the issues to display. 
  36. */ 
  37. public function __construct( $platform, $category, array $items ) { 
  38. parent::__construct(); 
  39.  
  40. // Adding the thickbox. 
  41. add_thickbox(); 
  42.  
  43. // Set search string. 
  44. $search_string = filter_input( INPUT_GET, 's' ); 
  45.  
  46. if ( $search_string !== '' ) { 
  47. $this->search_string = $search_string; 
  48.  
  49. $this->current_view = $category; 
  50.  
  51. // Set the crawl issue source. 
  52. $this->show_fields( $platform ); 
  53.  
  54. $this->items = $items; 
  55.  
  56. /** 
  57. * Getting the screen id from this table 
  58. * @return string 
  59. */ 
  60. public function get_screen_id() { 
  61. return $this->screen->id; 
  62.  
  63. /** 
  64. * Setup the table variables, fetch the items from the database, search, sort and format the items. 
  65. */ 
  66. public function prepare_items() { 
  67. // Get variables needed for pagination. 
  68. $this->per_page = $this->get_items_per_page( 'errors_per_page', $this->per_page ); 
  69. $this->current_page = intval( ( $paged = filter_input( INPUT_GET, 'paged' ) ) ? $paged : 1 ); 
  70.  
  71. $this->setup_columns(); 
  72. $this->views(); 
  73. $this->parse_items(); 
  74.  
  75. /** 
  76. * Set the table columns 
  77. * @return array 
  78. */ 
  79. public function get_columns() { 
  80. $columns = array( 
  81. 'cb' => '<input type="checkbox" />',  
  82. 'url' => __( 'URL', 'wordpress-seo' ),  
  83. 'last_crawled' => __( 'Last crawled', 'wordpress-seo' ),  
  84. 'first_detected' => __( 'First detected', 'wordpress-seo' ),  
  85. 'response_code' => __( 'Response code', 'wordpress-seo' ),  
  86. ); 
  87.  
  88. return $columns; 
  89.  
  90. /** 
  91. * Return the columns that are sortable 
  92. * @return array 
  93. */ 
  94. protected function get_sortable_columns() { 
  95. $sortable_columns = array( 
  96. 'url' => array( 'url', false ),  
  97. 'last_crawled' => array( 'last_crawled', false ),  
  98. 'first_detected' => array( 'first_detected', false ),  
  99. 'response_code' => array( 'response_code', false ),  
  100. ); 
  101.  
  102. return $sortable_columns; 
  103.  
  104. /** 
  105. * Return available bulk actions 
  106. * @return array 
  107. */ 
  108. protected function get_bulk_actions() { 
  109. return array( 
  110. 'mark_as_fixed' => __( 'Mark as fixed', 'wordpress-seo' ),  
  111. ); 
  112.  
  113. /** 
  114. * Default method to display a column 
  115. * @param array $item Data array. 
  116. * @param string $column_name Column name key. 
  117. * @return mixed 
  118. */ 
  119. protected function column_default( $item, $column_name ) { 
  120. return $item[ $column_name ]; 
  121.  
  122. /** 
  123. * Checkbox column 
  124. * @param array $item Item data array. 
  125. * @return string 
  126. */ 
  127. protected function column_cb( $item ) { 
  128. return sprintf( 
  129. '<input type="checkbox" name="wpseo_crawl_issues[]" id="cb-%1$s" value="%2$s" /><label for="cb-%1$s" class="screen-reader-text">%3$s</label>',  
  130. md5( $item['url'] ),  
  131. $item['url'],  
  132. __( 'Select redirect', 'wordpress-seo' ) 
  133. ); 
  134.  
  135. /** 
  136. * Formatting the output of the column last crawled into a dateformat 
  137. * @param array $item Item data array. 
  138. * @return string 
  139. */ 
  140. protected function column_last_crawled( $item ) { 
  141. return date_i18n( get_option( 'date_format' ), strtotime( $item['last_crawled'] ) ); 
  142.  
  143. /** 
  144. * Formatting the output of the column first detected into a dateformat 
  145. * @param array $item Item data array. 
  146. * @return string 
  147. */ 
  148. protected function column_first_detected( $item ) { 
  149. return date_i18n( get_option( 'date_format' ), strtotime( $item['first_detected'] ) ); 
  150.  
  151. /** 
  152. * URL column 
  153. * @param array $item Item data array. 
  154. * @return string 
  155. */ 
  156. protected function column_url( $item ) { 
  157. $actions = array(); 
  158.  
  159. if ( $this->can_create_redirect() ) { 
  160. /** Gets the modal box */ 
  161. $modal = $this->get_modal_box( $item['url'] ); 
  162. $modal->load_view( md5( $item['url'] ) ); 
  163.  
  164. $actions['create_redirect'] = '<a href="#TB_inline?width=600&height=' . $modal->get_height() . '&inlineId=redirect-' . md5( $item['url'] ) . '" class="thickbox wpseo-open-gsc-redirect-modal aria-button-if-js">' . __( 'Create redirect', 'wordpress-seo' ) . '</a>'; 
  165.  
  166. $actions['view'] = '<a href="' . home_url( $item['url'] ) . '" target="_blank">' . __( 'View', 'wordpress-seo' ) . '</a>'; 
  167. $actions['markasfixed'] = '<a href="javascript:wpseo_mark_as_fixed(\'' . urlencode( $item['url'] ) . '\');">' . __( 'Mark as fixed', 'wordpress-seo' ) . '</a>'; 
  168.  
  169. return sprintf( 
  170. '<span class="value">%1$s</span> %2$s',  
  171. $item['url'],  
  172. $this->row_actions( $actions ) 
  173. ); 
  174.  
  175. /** 
  176. * Running the setup of the columns 
  177. */ 
  178. private function setup_columns() { 
  179. $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); 
  180.  
  181. /** 
  182. * Check if the current category allow creating redirects 
  183. * @return bool 
  184. */ 
  185. private function can_create_redirect() { 
  186. return in_array( $this->current_view, array( 'soft_404', 'not_found', 'access_denied' ) ); 
  187.  
  188. /** 
  189. * Setting the table navigation 
  190. * @param int $total_items Total number of items. 
  191. * @param int $posts_per_page Number of items per page. 
  192. */ 
  193. private function set_pagination( $total_items, $posts_per_page ) { 
  194. $this->set_pagination_args( array( 
  195. 'total_items' => $total_items,  
  196. 'total_pages' => ceil( ( $total_items / $posts_per_page ) ),  
  197. 'per_page' => $posts_per_page,  
  198. ) ); 
  199.  
  200. /** 
  201. * Setting the items 
  202. */ 
  203. private function parse_items() { 
  204. if ( is_array( $this->items ) && count( $this->items ) > 0 ) { 
  205. if ( ! empty( $this->search_string ) ) { 
  206. $this->do_search(); 
  207.  
  208. $this->set_pagination( count( $this->items ), $this->per_page ); 
  209.  
  210. $this->sort_items(); 
  211. $this->paginate_items(); 
  212.  
  213. /** 
  214. * Search through the items 
  215. */ 
  216. private function do_search() { 
  217. $results = array(); 
  218.  
  219. foreach ( $this->items as $item ) { 
  220. foreach ( $item as $value ) { 
  221. if ( stristr( $value, $this->search_string ) !== false ) { 
  222. $results[] = $item; 
  223. continue; 
  224.  
  225. $this->items = $results; 
  226.  
  227. /** 
  228. * Running the pagination 
  229. */ 
  230. private function paginate_items() { 
  231. // Setting the starting point. If starting point is below 1, overwrite it with value 0, otherwise it will be sliced of at the back. 
  232. $slice_start = ( $this->current_page - 1 ); 
  233. if ( $slice_start < 0 ) { 
  234. $slice_start = 0; 
  235.  
  236. // Apply 'pagination'. 
  237. $this->items = array_slice( $this->items, ( $slice_start * $this->per_page ), $this->per_page ); 
  238.  
  239. /** 
  240. * Sort the items by callback 
  241. */ 
  242. private function sort_items() { 
  243. // Sort the results. 
  244. usort( $this->items, array( $this, 'do_reorder' ) ); 
  245.  
  246. /** 
  247. * Doing the sorting of the issues 
  248. * @param array $a First data set for comparison. 
  249. * @param array $b Second data set for comparison. 
  250. * @return int 
  251. */ 
  252. private function do_reorder( $a, $b ) { 
  253. // If no sort, default to title. 
  254. $orderby = ( $orderby = filter_input( INPUT_GET, 'orderby' ) ) ? $orderby : 'url'; 
  255.  
  256. // If no order, default to asc. 
  257. $order = ( $order = filter_input( INPUT_GET, 'order' ) ) ? $order : 'asc'; 
  258.  
  259. // When there is a raw field of it, sort by this field. 
  260. if ( array_key_exists( $orderby . '_raw', $a ) && array_key_exists( $orderby . '_raw', $b ) ) { 
  261. $orderby = $orderby . '_raw'; 
  262.  
  263. // Determine sort order. 
  264. $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); 
  265.  
  266. // Send final sort direction to usort. 
  267. return ( $order === 'asc' ) ? $result : ( - $result ); 
  268.  
  269. /** 
  270. * Checks if premium is loaded, if not the nopremium modal will be shown. Otherwise it will load the premium one. 
  271. * @param string $url URL string. 
  272. * @return WPSEO_GSC_Modal Instance of the GSC modal. 
  273. */ 
  274. private function get_modal_box( $url ) { 
  275. if ( defined( 'WPSEO_PREMIUM_FILE' ) && class_exists( 'WPSEO_Premium_GSC_Modal' ) ) { 
  276. static $premium_modal; 
  277.  
  278. if ( ! $premium_modal ) { 
  279. $premium_modal = new WPSEO_Premium_GSC_Modal(); 
  280.  
  281. return $premium_modal->show( $url ); 
  282.  
  283. return new WPSEO_GSC_Modal( 
  284. dirname( __FILE__ ) . '/views/gsc-redirect-nopremium.php',  
  285. self::FREE_MODAL_HEIGHT,  
  286. array( 'url' => $url ) 
  287. ); 
  288.  
  289. /** 
  290. * Showing the hidden fields used by the AJAX requests 
  291. * @param string $platform Platform (desktop, mobile, feature phone). 
  292. */ 
  293. private function show_fields( $platform ) { 
  294. echo "<input type='hidden' name='wpseo_gsc_nonce' value='" . wp_create_nonce( 'wpseo_gsc_nonce' ) . "' />"; 
  295. echo "<input id='field_platform' type='hidden' name='platform' value='{$platform}' />"; 
  296. echo "<input id='field_category' type='hidden' name='category' value='{$this->current_view}' />";