WPSEO_Bulk_List_Table

Implements table for bulk editing.

Defined (1)

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

/admin/class-bulk-editor-list-table.php  
  1. class WPSEO_Bulk_List_Table extends WP_List_Table { 
  2.  
  3. /** 
  4. * The nonce that was passed with the request 
  5. * @var string 
  6. */ 
  7. private $nonce; 
  8.  
  9. /** 
  10. * Array of post types for which the current user has `edit_others_posts` capabilities. 
  11. * @var array 
  12. */ 
  13. private $all_posts; 
  14.  
  15. /** 
  16. * Array of post types for which the current user has `edit_posts` capabilities, but not `edit_others_posts`. 
  17. * @var array 
  18. */ 
  19. private $own_posts; 
  20.  
  21. /** 
  22. * Saves all the metadata into this array. 
  23. * @var array 
  24. */ 
  25. protected $meta_data = array(); 
  26.  
  27. /** 
  28. * The current requested page_url 
  29. * @var string 
  30. */ 
  31. private $request_url; 
  32.  
  33. /** 
  34. * The current page (depending on $_GET['paged']) if current tab is for current page_type, else it will be 1 
  35. * @var integer 
  36. */ 
  37. private $current_page; 
  38.  
  39. /** 
  40. * The current post filter, if is used (depending on $_GET['post_type_filter']) 
  41. * @var string 
  42. */ 
  43. private $current_filter; 
  44.  
  45. /** 
  46. * The current post status, if is used (depending on $_GET['post_status']) 
  47. * @var string 
  48. */ 
  49. private $current_status; 
  50.  
  51. /** 
  52. * The current sorting, if used (depending on $_GET['order'] and $_GET['orderby']) 
  53. * @var string 
  54. */ 
  55. private $current_order; 
  56.  
  57. /** 
  58. * The page_type for current class instance (for example: title / description). 
  59. * @var string 
  60. */ 
  61. protected $page_type; 
  62.  
  63. /** 
  64. * Based on the page_type ($this->page_type) there will be constructed an url part, for subpages and 
  65. * navigation 
  66. * @var string 
  67. */ 
  68. protected $page_url; 
  69.  
  70. /** 
  71. * The settings which will be used in the __construct. 
  72. * @var array 
  73. */ 
  74. protected $settings; 
  75.  
  76. /** 
  77. * @var array 
  78. */ 
  79. protected $pagination = array(); 
  80.  
  81. /** 
  82. * Class constructor 
  83. */ 
  84. function __construct() { 
  85. parent::__construct( $this->settings ); 
  86.  
  87. $this->request_url = $_SERVER['REQUEST_URI']; 
  88. $this->current_page = ( ! empty( $_GET['paged'] ) ) ? $_GET['paged'] : 1; 
  89. $this->current_filter = ( ! empty( $_GET['post_type_filter'] ) ) ? $_GET['post_type_filter'] : 1; 
  90. $this->current_status = ( ! empty( $_GET['post_status'] ) ) ? $_GET['post_status'] : 1; 
  91. $this->current_order = array( 
  92. 'order' => ( ! empty( $_GET['order'] ) ) ? $_GET['order'] : 'asc',  
  93. 'orderby' => ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'post_title',  
  94. ); 
  95.  
  96. $this->verify_nonce(); 
  97.  
  98. $this->nonce = wp_create_nonce( 'bulk-editor-table' ); 
  99. $this->page_url = "&nonce={$this->nonce}&type={$this->page_type}#top#{$this->page_type}"; 
  100.  
  101. $this->populate_editable_post_types(); 
  102.  
  103.  
  104. /** 
  105. * Verifies nonce if additional parameters have been sent. 
  106. * Shows an error notification if the nonce check fails. 
  107. */ 
  108. private function verify_nonce() { 
  109. if ( $this->should_verify_nonce() && ! wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'bulk-editor-table' ) ) { 
  110. Yoast_Notification_Center::get()->add_notification( 
  111. new Yoast_Notification( 
  112. __( 'You are not allowed to access this page.', 'wordpress-seo' ),  
  113. array( 'type' => Yoast_Notification::ERROR ) 
  114. ); 
  115. Yoast_Notification_Center::get()->display_notifications(); 
  116. die; 
  117.  
  118. /** 
  119. * Checks if additional parameters have been sent to determine if nonce should be checked or not. 
  120. * @return bool 
  121. */ 
  122. private function should_verify_nonce() { 
  123. $possible_params = array( 
  124. 'type',  
  125. 'paged',  
  126. 'post_type_filter',  
  127. 'post_status',  
  128. 'order',  
  129. 'orderby',  
  130. ); 
  131.  
  132. foreach ( $possible_params as $param_name ) { 
  133. if ( filter_input( INPUT_GET, $param_name ) ) { 
  134. return true; 
  135.  
  136. /** 
  137. * Prepares the data and renders the page. 
  138. */ 
  139. public function show_page() { 
  140. $this->prepare_page_navigation(); 
  141. $this->prepare_items(); 
  142.  
  143. $this->views(); 
  144. $this->display(); 
  145.  
  146. /** 
  147. * Used in the constructor to build a reference list of post types the current user can edit. 
  148. */ 
  149. protected function populate_editable_post_types() { 
  150. $post_types = get_post_types( array( 'public' => true, 'exclude_from_search' => false ), 'object' ); 
  151.  
  152. $this->all_posts = array(); 
  153. $this->own_posts = array(); 
  154.  
  155. if ( is_array( $post_types ) && $post_types !== array() ) { 
  156. foreach ( $post_types as $post_type ) { 
  157. if ( ! current_user_can( $post_type->cap->edit_posts ) ) { 
  158. continue; 
  159.  
  160. if ( current_user_can( $post_type->cap->edit_others_posts ) ) { 
  161. $this->all_posts[] = esc_sql( $post_type->name ); 
  162. else { 
  163. $this->own_posts[] = esc_sql( $post_type->name ); 
  164.  
  165.  
  166. /** 
  167. * Will shown the navigation for the table like pagenavigation and pagefilter; 
  168. * @param string $which Table nav location (such as top). 
  169. */ 
  170. function display_tablenav( $which ) { 
  171. $post_status = sanitize_text_field( filter_input( INPUT_GET, 'post_status' ) ); 
  172. ?> 
  173. <div class="tablenav <?php echo esc_attr( $which ); ?>"> 
  174.  
  175. <?php if ( 'top' === $which ) { ?> 
  176. <form id="posts-filter" action="" method="get"> 
  177. <input type="hidden" name="nonce" value="<?php echo $this->nonce; ?>"/> 
  178. <input type="hidden" name="page" value="wpseo_tools"/> 
  179. <input type="hidden" name="tool" value="bulk-editor"/> 
  180. <input type="hidden" name="type" value="<?php echo esc_attr( $this->page_type ); ?>"/> 
  181. <input type="hidden" name="orderby" 
  182. value="<?php echo esc_attr( filter_input( INPUT_GET, 'orderby' ) ); ?>"/> 
  183. <input type="hidden" name="order" 
  184. value="<?php echo esc_attr( filter_input( INPUT_GET, 'order' ) ); ?>"/> 
  185. <input type="hidden" name="post_type_filter" 
  186. value="<?php echo esc_attr( filter_input( INPUT_GET, 'post_type_filter' ) ); ?>"/> 
  187. <?php if ( ! empty( $post_status ) ) { ?> 
  188. <input type="hidden" name="post_status" value="<?php echo esc_attr( $post_status ); ?>"/> 
  189. <?php } ?> 
  190. <?php } ?> 
  191.  
  192. <?php 
  193. $this->extra_tablenav( $which ); 
  194. $this->pagination( $which ); 
  195. ?> 
  196.  
  197. <br class="clear"/> 
  198. <?php if ( 'top' === $which ) { ?> 
  199. </form> 
  200. <?php } ?> 
  201. </div> 
  202.  
  203. <?php 
  204.  
  205. /** 
  206. * This function builds the base sql subquery used in this class. 
  207. * This function takes into account the post types in which the current user can 
  208. * edit all posts, and the ones the current user can only edit his/her own. 
  209. * @return string $subquery The subquery, which should always be used in $wpdb->prepare(), passing the current user_id in as the first parameter. 
  210. */ 
  211. function get_base_subquery() { 
  212. global $wpdb; 
  213.  
  214. $all_posts_string = "'" . implode( "', '", $this->all_posts ) . "'"; 
  215. $own_posts_string = "'" . implode( "', '", $this->own_posts ) . "'"; 
  216.  
  217. $post_author = esc_sql( (int) get_current_user_id() ); 
  218.  
  219. $subquery = "( 
  220. SELECT * 
  221. FROM {$wpdb->posts} 
  222. WHERE post_type IN ({$all_posts_string}) 
  223. UNION ALL 
  224. SELECT * 
  225. FROM {$wpdb->posts} 
  226. WHERE post_type IN ({$own_posts_string}) AND post_author = {$post_author} 
  227. ) sub_base"; 
  228.  
  229. return $subquery; 
  230.  
  231.  
  232. /** 
  233. * @return array 
  234. */ 
  235. function get_views() { 
  236. global $wpdb; 
  237.  
  238. $status_links = array(); 
  239.  
  240. $states = get_post_stati( array( 'show_in_admin_all_list' => true ) ); 
  241. $states['trash'] = 'trash'; 
  242. $states = esc_sql( $states ); 
  243. $all_states = "'" . implode( "', '", $states ) . "'"; 
  244.  
  245. $subquery = $this->get_base_subquery(); 
  246.  
  247. $total_posts = $wpdb->get_var( 
  248. SELECT COUNT(ID) FROM {$subquery} 
  249. WHERE post_status IN ({$all_states}) 
  250. ); 
  251.  
  252.  
  253. $post_status = filter_input( INPUT_GET, 'post_status' ); 
  254. $class = empty( $post_status ) ? ' class="current"' : ''; 
  255. $status_links['all'] = '<a href="' . esc_url( admin_url( 'admin.php?page=wpseo_tools&tool=bulk-editor' . $this->page_url ) ) . '"' . $class . '>' . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts', 'wordpress-seo' ), number_format_i18n( $total_posts ) ) . '</a>'; 
  256.  
  257. $post_stati = get_post_stati( array( 'show_in_admin_all_list' => true ), 'objects' ); 
  258. if ( is_array( $post_stati ) && $post_stati !== array() ) { 
  259. foreach ( $post_stati as $status ) { 
  260.  
  261. $status_name = esc_sql( $status->name ); 
  262.  
  263. $total = (int) $wpdb->get_var( 
  264. $wpdb->prepare( 
  265. SELECT COUNT(ID) FROM {$subquery} 
  266. WHERE post_status = %s 
  267. ",  
  268. $status_name 
  269. ); 
  270.  
  271. if ( $total === 0 ) { 
  272. continue; 
  273.  
  274. $class = ''; 
  275. if ( $status_name === $post_status ) { 
  276. $class = ' class="current"'; 
  277.  
  278. $status_links[ $status_name ] = '<a href="' . esc_url( add_query_arg( array( 'post_status' => $status_name ), admin_url( 'admin.php?page=wpseo_tools&tool=bulk-editor' . $this->page_url ) ) ) . '"' . $class . '>' . sprintf( translate_nooped_plural( $status->label_count, $total ), number_format_i18n( $total ) ) . '</a>'; 
  279. unset( $post_stati, $status, $status_name, $total, $class ); 
  280.  
  281. $trashed_posts = $wpdb->get_var( 
  282. SELECT COUNT(ID) FROM {$subquery} 
  283. WHERE post_status IN ('trash') 
  284. ); 
  285.  
  286. $class = ''; 
  287. if ( 'trash' === $post_status ) { 
  288. $class = 'class="current"'; 
  289. $status_links['trash'] = '<a href="' . esc_url( admin_url( 'admin.php?page=wpseo_tools&tool=bulk-editor&post_status=trash' . $this->page_url ) ) . '"' . $class . '>' . sprintf( _nx( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>', $trashed_posts, 'posts', 'wordpress-seo' ), number_format_i18n( $trashed_posts ) ) . '</a>'; 
  290.  
  291. return $status_links; 
  292.  
  293.  
  294. /** 
  295. * @param string $which Table nav location (such as top). 
  296. */ 
  297. function extra_tablenav( $which ) { 
  298.  
  299. if ( 'top' === $which ) { 
  300. $post_types = get_post_types( array( 'public' => true, 'exclude_from_search' => false ) ); 
  301.  
  302. $instance_type = esc_attr( $this->page_type ); 
  303.  
  304. if ( is_array( $post_types ) && $post_types !== array() ) { 
  305. global $wpdb; 
  306.  
  307. echo '<div class="alignleft actions">'; 
  308.  
  309. $post_types = esc_sql( $post_types ); 
  310. $post_types = "'" . implode( "', '", $post_types ) . "'"; 
  311.  
  312. $states = get_post_stati( array( 'show_in_admin_all_list' => true ) ); 
  313. $states['trash'] = 'trash'; 
  314. $states = esc_sql( $states ); 
  315. $all_states = "'" . implode( "', '", $states ) . "'"; 
  316.  
  317. $subquery = $this->get_base_subquery(); 
  318.  
  319. $post_types = $wpdb->get_results( 
  320. SELECT DISTINCT post_type FROM {$subquery} 
  321. WHERE post_status IN ({$all_states}) 
  322. ORDER BY 'post_type' ASC 
  323. ); 
  324.  
  325. $post_type_filter = filter_input( INPUT_GET, 'post_type_filter' ); 
  326. $selected = ( ! empty( $post_type_filter ) ) ? sanitize_text_field( $post_type_filter ) : '-1'; 
  327.  
  328. $options = '<option value="-1">' . __( 'Show All Post Types', 'wordpress-seo' ) . '</option>'; 
  329.  
  330. if ( is_array( $post_types ) && $post_types !== array() ) { 
  331. foreach ( $post_types as $post_type ) { 
  332. $obj = get_post_type_object( $post_type->post_type ); 
  333. $options .= sprintf( '<option value="%2$s" %3$s>%1$s</option>', $obj->labels->name, $post_type->post_type, selected( $selected, $post_type->post_type, false ) ); 
  334.  
  335. printf( 
  336. '<label for="%1$s" class="screen-reader-text">%2$s</label>',  
  337. 'post-type-filter-' . $instance_type,  
  338. __( 'Filter by post type', 'wordpress-seo' ) 
  339. ); 
  340. echo sprintf( '<select name="post_type_filter" id="post-type-filter-%2$s">%1$s</select>', $options, $instance_type ); 
  341. submit_button( __( 'Filter', 'wordpress-seo' ), 'button', false, false, array( 'id' => 'post-query-submit' ) ); 
  342. echo '</div>'; 
  343.  
  344. /** 
  345. * @return array 
  346. */ 
  347. function get_sortable_columns() { 
  348. return array( 
  349. 'col_page_title' => array( 'post_title', true ),  
  350. 'col_post_type' => array( 'post_type', false ),  
  351. 'col_post_date' => array( 'post_date', false ),  
  352. ); 
  353.  
  354. /** 
  355. * Sets the correct pagenumber and pageurl for the navigation 
  356. */ 
  357. function prepare_page_navigation() { 
  358.  
  359. $request_url = $this->request_url . $this->page_url; 
  360.  
  361. $current_page = $this->current_page; 
  362. $current_filter = $this->current_filter; 
  363. $current_status = $this->current_status; 
  364. $current_order = $this->current_order; 
  365.  
  366. // If current type doesn't compare with objects page_type, than we have to unset some vars in the requested url (which will be use for internal table urls). 
  367. if ( $_GET['type'] != $this->page_type ) { 
  368. $request_url = remove_query_arg( 'paged', $request_url ); // Page will be set with value 1 below. 
  369. $request_url = remove_query_arg( 'post_type_filter', $request_url ); 
  370. $request_url = remove_query_arg( 'post_status', $request_url ); 
  371. $request_url = remove_query_arg( 'orderby', $request_url ); 
  372. $request_url = remove_query_arg( 'order', $request_url ); 
  373. $request_url = add_query_arg( 'pages', 1, $request_url ); 
  374.  
  375. $current_page = 1; 
  376. $current_filter = '-1'; 
  377. $current_status = ''; 
  378. $current_order = array( 'orderby' => 'post_title', 'order' => 'asc' ); 
  379.  
  380.  
  381. $_SERVER['REQUEST_URI'] = $request_url; 
  382.  
  383. $_GET['paged'] = $current_page; 
  384. $_REQUEST['paged'] = $current_page; 
  385. $_REQUEST['post_type_filter'] = $current_filter; 
  386. $_GET['post_type_filter'] = $current_filter; 
  387. $_GET['post_status'] = $current_status; 
  388. $_GET['orderby'] = $current_order['orderby']; 
  389. $_GET['order'] = $current_order['order']; 
  390.  
  391.  
  392. /** 
  393. * Preparing the requested pagerows and setting the needed variables 
  394. */ 
  395. function prepare_items() { 
  396.  
  397. $post_type_clause = $this->get_post_type_clause(); 
  398. $all_states = $this->get_all_states(); 
  399. $subquery = $this->get_base_subquery(); 
  400.  
  401. // Setting the column headers. 
  402. $this->set_column_headers(); 
  403.  
  404. // Count the total number of needed items and setting pagination given $total_items. 
  405. $total_items = $this->count_items( $subquery, $all_states, $post_type_clause ); 
  406. $this->set_pagination( $total_items ); 
  407.  
  408. // Getting items given $query. 
  409. $query = $this->parse_item_query( $subquery, $all_states, $post_type_clause ); 
  410. $this->get_items( $query ); 
  411.  
  412. // Get the metadata for the current items ($this->items). 
  413. $this->get_meta_data(); 
  414.  
  415.  
  416. /** 
  417. * Getting the columns for first row 
  418. * @return array 
  419. */ 
  420. public function get_columns() { 
  421. return $this->merge_columns(); 
  422.  
  423. /** 
  424. * Setting the column headers 
  425. */ 
  426. protected function set_column_headers() { 
  427. $columns = $this->get_columns(); 
  428. $hidden = array(); 
  429. $sortable = $this->get_sortable_columns(); 
  430. $this->_column_headers = array( $columns, $hidden, $sortable ); 
  431.  
  432. /** 
  433. * Counting total items 
  434. * @param string $subquery SQL FROM part. 
  435. * @param string $all_states SQL IN part. 
  436. * @param string $post_type_clause SQL post type part. 
  437. * @return mixed 
  438. */ 
  439. protected function count_items( $subquery, $all_states, $post_type_clause ) { 
  440. global $wpdb; 
  441. $total_items = $wpdb->get_var( 
  442. SELECT COUNT(ID) 
  443. FROM {$subquery} 
  444. WHERE post_status IN ({$all_states}) $post_type_clause 
  445. ); 
  446.  
  447. return $total_items; 
  448.  
  449. /** 
  450. * Getting the post_type_clause filter 
  451. * @return string 
  452. */ 
  453. protected function get_post_type_clause() { 
  454. // Filter Block. 
  455. $post_types = null; 
  456. $post_type_clause = ''; 
  457. $post_type_filter = filter_input( INPUT_GET, 'post_type_filter' ); 
  458.  
  459. if ( ! empty( $post_type_filter ) && get_post_type_object( sanitize_text_field( $post_type_filter ) ) ) { 
  460. $post_types = esc_sql( sanitize_text_field( $post_type_filter ) ); 
  461. $post_type_clause = "AND post_type IN ('{$post_types}')"; 
  462.  
  463. return $post_type_clause; 
  464.  
  465. /** 
  466. * Setting the pagination. 
  467. * Total items is the number of all visible items. 
  468. * @param int $total_items Total items counts. 
  469. */ 
  470. protected function set_pagination( $total_items ) { 
  471.  
  472. // Calculate items per page. 
  473. $per_page = $this->get_items_per_page( 'wpseo_posts_per_page', 10 ); 
  474. $paged = esc_sql( sanitize_text_field( filter_input( INPUT_GET, 'paged' ) ) ); 
  475.  
  476. if ( empty( $paged ) || ! is_numeric( $paged ) || $paged <= 0 ) { 
  477. $paged = 1; 
  478.  
  479. $this->set_pagination_args( 
  480. array( 
  481. 'total_items' => $total_items,  
  482. 'total_pages' => ceil( $total_items / $per_page ),  
  483. 'per_page' => $per_page,  
  484. ); 
  485.  
  486. $this->pagination = array( 
  487. 'per_page' => $per_page,  
  488. 'offset' => ( $paged - 1 ) * $per_page,  
  489. ); 
  490.  
  491.  
  492. /** 
  493. * Parse the query to get items from database. 
  494. * Based on given parameters there will be parse a query which will get all the pages/posts and other post_types 
  495. * from the database. 
  496. * @param string $subquery SQL FROM part. 
  497. * @param string $all_states SQL IN part. 
  498. * @param string $post_type_clause SQL post type part. 
  499. * @return string 
  500. */ 
  501. protected function parse_item_query( $subquery, $all_states, $post_type_clause ) { 
  502. // Order By block. 
  503. $orderby = filter_input( INPUT_GET, 'orderby' ); 
  504.  
  505. $orderby = ! empty( $orderby ) ? esc_sql( sanitize_text_field( $orderby ) ) : 'post_title'; 
  506. $orderby = $this->sanitize_orderby( $orderby ); 
  507.  
  508. // Order clause. 
  509. $order = filter_input( INPUT_GET, 'order' ); 
  510. $order = ! empty( $order ) ? esc_sql( strtoupper( sanitize_text_field( $order ) ) ) : 'ASC'; 
  511. $order = $this->sanitize_order( $order ); 
  512.  
  513. // Get all needed results. 
  514. $query = " 
  515. SELECT ID, post_title, post_type, post_status, post_modified, post_date 
  516. FROM {$subquery} 
  517. WHERE post_status IN ({$all_states}) $post_type_clause 
  518. ORDER BY {$orderby} {$order} 
  519. LIMIT %d, %d 
  520. "; 
  521.  
  522. return $query; 
  523.  
  524. /** 
  525. * Heavily restricts the possible columns by which a user can order the table in the bulk editor, thereby preventing a possible CSRF vulnerability. 
  526. * @param string $orderby The column by which we want to order. 
  527. * @return string $orderby 
  528. */ 
  529. protected function sanitize_orderby( $orderby ) { 
  530. $valid_column_names = array( 
  531. 'post_title',  
  532. 'post_type',  
  533. 'post_date',  
  534. ); 
  535.  
  536. if ( in_array( $orderby, $valid_column_names ) ) { 
  537. return $orderby; 
  538.  
  539. return 'post_title'; 
  540.  
  541. /** 
  542. * Makes sure the order clause is always ASC or DESC for the bulk editor table, thereby preventing a possible CSRF vulnerability. 
  543. * @param string $order Whether we want to sort ascending or descending. 
  544. * @return string $order SQL order string (ASC, DESC). 
  545. */ 
  546. protected function sanitize_order( $order ) { 
  547. if ( in_array( strtoupper( $order ), array( 'ASC', 'DESC' ) ) ) { 
  548. return $order; 
  549.  
  550. return 'ASC'; 
  551.  
  552. /** 
  553. * Getting all the items. 
  554. * @param string $query SQL query to use. 
  555. */ 
  556. protected function get_items( $query ) { 
  557. global $wpdb; 
  558.  
  559. $this->items = $wpdb->get_results( 
  560. $wpdb->prepare( 
  561. $query,  
  562. $this->pagination['offset'],  
  563. $this->pagination['per_page'] 
  564. ); 
  565.  
  566. /** 
  567. * Getting all the states. 
  568. * @return string 
  569. */ 
  570. protected function get_all_states() { 
  571. $states = get_post_stati( array( 'show_in_admin_all_list' => true ) ); 
  572. $states['trash'] = 'trash'; 
  573.  
  574. if ( ! empty( $_GET['post_status'] ) ) { 
  575. $requested_state = sanitize_text_field( $_GET['post_status'] ); 
  576. if ( in_array( $requested_state, $states ) ) { 
  577. $states = array( $requested_state ); 
  578.  
  579. $states = esc_sql( $states ); 
  580. $all_states = "'" . implode( "', '", $states ) . "'"; 
  581.  
  582. return $all_states; 
  583.  
  584.  
  585. /** 
  586. * Based on $this->items and the defined columns, the table rows will be displayed. 
  587. */ 
  588. function display_rows() { 
  589.  
  590. $records = $this->items; 
  591.  
  592. list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); 
  593.  
  594. if ( ( is_array( $records ) && $records !== array() ) && ( is_array( $columns ) && $columns !== array() ) ) { 
  595.  
  596. foreach ( $records as $rec ) { 
  597.  
  598. echo '<tr id="record_', $rec->ID, '">'; 
  599.  
  600. foreach ( $columns as $column_name => $column_display_name ) { 
  601.  
  602. $classes = ''; 
  603. if ( $primary === $column_name ) { 
  604. $classes .= ' has-row-actions column-primary'; 
  605.  
  606. $attributes = $this->column_attributes( $column_name, $hidden, $classes, $column_display_name ); 
  607.  
  608. $column_value = $this->parse_column( $column_name, $rec ); 
  609.  
  610. if ( method_exists( $this, 'parse_page_specific_column' ) && empty( $column_value ) ) { 
  611. $column_value = $this->parse_page_specific_column( $column_name, $rec, $attributes ); 
  612.  
  613. if ( ! empty( $column_value ) ) { 
  614. echo sprintf( '<td %2$s>%1$s</td>', $column_value, $attributes ); 
  615.  
  616. echo '</tr>'; 
  617.  
  618. /** 
  619. * Getting the attributes for each table cell. 
  620. * @param string $column_name Column name string. 
  621. * @param array $hidden Set of hidden columns. 
  622. * @param string $classes Additional CSS classes. 
  623. * @param string $column_display_name Column display name string. 
  624. * @return string 
  625. */ 
  626. protected function column_attributes( $column_name, $hidden, $classes, $column_display_name ) { 
  627.  
  628. $attributes = ''; 
  629. $class = array( $column_name, "column-$column_name$classes" ); 
  630.  
  631. if ( in_array( $column_name, $hidden ) ) { 
  632. $class[] = 'hidden'; 
  633.  
  634. if ( ! empty( $class ) ) { 
  635. $attributes = 'class="' . implode( ' ', $class ) . '"'; 
  636.  
  637. $attributes .= ' data-colname="' . esc_attr( $column_display_name ) . '"'; 
  638.  
  639. return $attributes; 
  640.  
  641. /** 
  642. * Parsing the title. 
  643. * @param WP_Post $rec Post object. 
  644. * @return string 
  645. */ 
  646. protected function parse_page_title_column( $rec ) { 
  647.  
  648. $title = empty( $rec->post_title ) ? __( '(no title)', 'wordpress-seo' ) : $rec->post_title; 
  649.  
  650. $return = sprintf( '<strong>%1$s</strong>', stripslashes( wp_strip_all_tags( $title ) ) ); 
  651.  
  652. $post_type_object = get_post_type_object( $rec->post_type ); 
  653. $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $rec->ID ); 
  654.  
  655. $actions = array(); 
  656.  
  657. if ( $can_edit_post && 'trash' !== $rec->post_status ) { 
  658. $actions['edit'] = sprintf( 
  659. '<a href="%s" aria-label="%s">%s</a>',  
  660. esc_url( get_edit_post_link( $rec->ID, true ) ),  
  661. /** translators: %s: post title */ 
  662. esc_attr( sprintf( __( 'Edit “%s”', 'wordpress-seo' ), $title ) ),  
  663. __( 'Edit', 'wordpress-seo' ) 
  664. ); 
  665.  
  666. if ( $post_type_object->public ) { 
  667. if ( in_array( $rec->post_status, array( 'pending', 'draft', 'future' ) ) ) { 
  668. if ( $can_edit_post ) { 
  669. $actions['view'] = sprintf( 
  670. '<a href="%s" aria-label="%s">%s</a>',  
  671. esc_url( add_query_arg( 'preview', 'true', get_permalink( $rec->ID ) ) ),  
  672. /** translators: %s: post title */ 
  673. esc_attr( sprintf( __( 'Preview “%s”', 'wordpress-seo' ), $title ) ),  
  674. __( 'Preview', 'wordpress-seo' ) 
  675. ); 
  676. elseif ( 'trash' !== $rec->post_status ) { 
  677. $actions['view'] = sprintf( 
  678. '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',  
  679. esc_url( get_permalink( $rec->ID ) ),  
  680. /** translators: %s: post title */ 
  681. esc_attr( sprintf( __( 'View “%s”', 'wordpress-seo' ), $title ) ),  
  682. __( 'View', 'wordpress-seo' ) 
  683. ); 
  684.  
  685. $return .= $this->row_actions( $actions ); 
  686.  
  687. return $return; 
  688.  
  689.  
  690. /** 
  691. * Parsing the column based on the $column_name. 
  692. * @param string $column_name Column name. 
  693. * @param WP_Post $rec Post object. 
  694. * @return string 
  695. */ 
  696. protected function parse_column( $column_name, $rec ) { 
  697.  
  698. static $date_format; 
  699.  
  700. if ( $date_format == null ) { 
  701. $date_format = get_option( 'date_format' ); 
  702.  
  703. switch ( $column_name ) { 
  704. case 'col_page_title': 
  705. $column_value = $this->parse_page_title_column( $rec ); 
  706. break; 
  707.  
  708. case 'col_page_slug': 
  709. $permalink = get_permalink( $rec->ID ); 
  710. $display_slug = str_replace( get_bloginfo( 'url' ), '', $permalink ); 
  711. $column_value = sprintf( '<a href="%2$s" target="_blank">%1$s</a>', stripslashes( $display_slug ), esc_url( $permalink ) ); 
  712. break; 
  713.  
  714. case 'col_post_type': 
  715. $post_type = get_post_type_object( $rec->post_type ); 
  716. $column_value = $post_type->labels->singular_name; 
  717. break; 
  718.  
  719. case 'col_post_status': 
  720. $post_status = get_post_status_object( $rec->post_status ); 
  721. $column_value = $post_status->label; 
  722. break; 
  723.  
  724. case 'col_post_date': 
  725. $column_value = date_i18n( $date_format, strtotime( $rec->post_date ) ); 
  726. break; 
  727.  
  728. case 'col_row_action': 
  729. $column_value = sprintf( 
  730. '<a href="#" role="button" class="wpseo-save" data-id="%1$s">%2$s</a> <span aria-hidden="true">|</span> <a href="#" role="button" class="wpseo-save-all">%3$s</a>',  
  731. $rec->ID,  
  732. esc_html__( 'Save', 'wordpress-seo' ),  
  733. esc_html__( 'Save all', 'wordpress-seo' ) 
  734. ); 
  735. break; 
  736.  
  737. if ( ! empty( $column_value ) ) { 
  738. return $column_value; 
  739.  
  740. /** 
  741. * Parse the field where the existing meta-data value is displayed. 
  742. * @param integer $record_id Record ID. 
  743. * @param string $attributes HTML attributes. 
  744. * @param bool|array $values Optional values data array. 
  745. * @return string 
  746. */ 
  747. protected function parse_meta_data_field( $record_id, $attributes, $values = false ) { 
  748.  
  749. // Fill meta data if exists in $this->meta_data. 
  750. $meta_data = ( ! empty( $this->meta_data[ $record_id ] ) ) ? $this->meta_data[ $record_id ] : array(); 
  751. $meta_key = WPSEO_Meta::$meta_prefix . $this->target_db_field; 
  752. $meta_value = ( ! empty( $meta_data[ $meta_key ] ) ) ? $meta_data[ $meta_key ] : ''; 
  753.  
  754. if ( ! empty( $values ) ) { 
  755. $meta_value = $values[ $meta_value ]; 
  756.  
  757. return sprintf( '<td %2$s id="wpseo-existing-%4$s-%3$s">%1$s</td>', $meta_value, $attributes, $record_id, $this->target_db_field ); 
  758.  
  759. /** 
  760. * Method for setting the meta data, which belongs to the records that will be shown on the current page. 
  761. * This method will loop through the current items ($this->items) for getting the post_id. With this data 
  762. * ($needed_ids) the method will query the meta-data table for getting the title. 
  763. */ 
  764. protected function get_meta_data() { 
  765.  
  766. $post_ids = $this->get_post_ids(); 
  767. $meta_data = $this->get_meta_data_result( $post_ids ); 
  768.  
  769. $this->parse_meta_data( $meta_data ); 
  770.  
  771. // Little housekeeping. 
  772. unset( $post_ids, $meta_data ); 
  773.  
  774.  
  775. /** 
  776. * Getting all post_ids from to $this->items. 
  777. * @return string 
  778. */ 
  779. protected function get_post_ids() { 
  780. $needed_ids = array(); 
  781. foreach ( $this->items as $item ) { 
  782. $needed_ids[] = $item->ID; 
  783.  
  784. $post_ids = "'" . implode( "', '", $needed_ids ) . "'"; 
  785.  
  786. return $post_ids; 
  787.  
  788. /** 
  789. * Getting the meta_data from database. 
  790. * @param string $post_ids Post IDs string for SQL IN part. 
  791. * @return mixed 
  792. */ 
  793. protected function get_meta_data_result( $post_ids ) { 
  794. global $wpdb; 
  795.  
  796. $meta_data = $wpdb->get_results( 
  797. SELECT * 
  798. FROM {$wpdb->postmeta} 
  799. WHERE post_id IN({$post_ids}) && meta_key = '" . WPSEO_Meta::$meta_prefix . $this->target_db_field . "' 
  800. ); 
  801.  
  802. return $meta_data; 
  803.  
  804. /** 
  805. * Setting $this->meta_data. 
  806. * @param array $meta_data Meta data set. 
  807. */ 
  808. protected function parse_meta_data( $meta_data ) { 
  809.  
  810. foreach ( $meta_data as $row ) { 
  811. $this->meta_data[ $row->post_id ][ $row->meta_key ] = $row->meta_value; 
  812.  
  813.  
  814. /** 
  815. * This method will merge general array with given parameter $columns. 
  816. * @param array $columns Optional columns set. 
  817. * @return array 
  818. */ 
  819. protected function merge_columns( $columns = array() ) { 
  820. $columns = array_merge( 
  821. array( 
  822. 'col_page_title' => __( 'WP Page Title', 'wordpress-seo' ),  
  823. 'col_post_type' => __( 'Post Type', 'wordpress-seo' ),  
  824. 'col_post_status' => __( 'Post Status', 'wordpress-seo' ),  
  825. 'col_post_date' => __( 'Publication date', 'wordpress-seo' ),  
  826. 'col_page_slug' => __( 'Page URL/Slug', 'wordpress-seo' ),  
  827. ),  
  828. $columns 
  829. ); 
  830.  
  831. $columns['col_row_action'] = __( 'Action', 'wordpress-seo' ); 
  832.  
  833. return $columns; 
  834. } /** End of class */