PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/google_search_console/class-gsc-table.php

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