class-bulk-editor-list-table.php 28 KB

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