abstract-class-wc-admin-list-table.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * List tables.
  4. *
  5. * @package WooCommerce/Admin
  6. * @version 3.3.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. if ( class_exists( 'WC_Admin_List_Table', false ) ) {
  12. return;
  13. }
  14. /**
  15. * WC_Admin_List_Table Class.
  16. */
  17. abstract class WC_Admin_List_Table {
  18. /**
  19. * Post type.
  20. *
  21. * @var string
  22. */
  23. protected $list_table_type = '';
  24. /**
  25. * Object being shown on the row.
  26. *
  27. * @var object|null
  28. */
  29. protected $object = null;
  30. /**
  31. * Constructor.
  32. */
  33. public function __construct() {
  34. if ( $this->list_table_type ) {
  35. add_action( 'manage_posts_extra_tablenav', array( $this, 'maybe_render_blank_state' ) );
  36. add_filter( 'view_mode_post_types', array( $this, 'disable_view_mode' ) );
  37. add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
  38. add_filter( 'request', array( $this, 'request_query' ) );
  39. add_filter( 'post_row_actions', array( $this, 'row_actions' ), 100, 2 );
  40. add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 );
  41. add_filter( 'list_table_primary_column', array( $this, 'list_table_primary_column' ), 10, 2 );
  42. add_filter( 'manage_edit-' . $this->list_table_type . '_sortable_columns', array( $this, 'define_sortable_columns' ) );
  43. add_filter( 'manage_' . $this->list_table_type . '_posts_columns', array( $this, 'define_columns' ) );
  44. add_filter( 'bulk_actions-edit-' . $this->list_table_type, array( $this, 'define_bulk_actions' ) );
  45. add_action( 'manage_' . $this->list_table_type . '_posts_custom_column', array( $this, 'render_columns' ), 10, 2 );
  46. add_filter( 'handle_bulk_actions-edit-' . $this->list_table_type, array( $this, 'handle_bulk_actions' ), 10, 3 );
  47. }
  48. }
  49. /**
  50. * Show blank slate.
  51. *
  52. * @param string $which String which tablenav is being shown.
  53. */
  54. public function maybe_render_blank_state( $which ) {
  55. global $post_type;
  56. if ( $post_type === $this->list_table_type && 'bottom' === $which ) {
  57. $counts = (array) wp_count_posts( $post_type );
  58. unset( $counts['auto-draft'] );
  59. $count = array_sum( $counts );
  60. if ( 0 < $count ) {
  61. return;
  62. }
  63. $this->render_blank_state();
  64. echo '<style type="text/css">#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display: none; } </style>';
  65. }
  66. }
  67. /**
  68. * Render blank state. Extend to add content.
  69. */
  70. protected function render_blank_state() {}
  71. /**
  72. * Removes this type from list of post types that support "View Mode" switching.
  73. * View mode is seen on posts where you can switch between list or excerpt. Our post types don't support
  74. * it, so we want to hide the useless UI from the screen options tab.
  75. *
  76. * @param array $post_types Array of post types supporting view mode.
  77. * @return array Array of post types supporting view mode, without this type.
  78. */
  79. public function disable_view_mode( $post_types ) {
  80. unset( $post_types[ $this->list_table_type ] );
  81. return $post_types;
  82. }
  83. /**
  84. * See if we should render search filters or not.
  85. */
  86. public function restrict_manage_posts() {
  87. global $typenow;
  88. if ( $this->list_table_type === $typenow ) {
  89. $this->render_filters();
  90. }
  91. }
  92. /**
  93. * Handle any filters.
  94. *
  95. * @param array $query_vars Query vars.
  96. * @return array
  97. */
  98. public function request_query( $query_vars ) {
  99. global $typenow;
  100. if ( $this->list_table_type === $typenow ) {
  101. return $this->query_filters( $query_vars );
  102. }
  103. return $query_vars;
  104. }
  105. /**
  106. * Render any custom filters and search inputs for the list table.
  107. */
  108. protected function render_filters() {}
  109. /**
  110. * Handle any custom filters.
  111. *
  112. * @param array $query_vars Query vars.
  113. * @return array
  114. */
  115. protected function query_filters( $query_vars ) {
  116. return $query_vars;
  117. }
  118. /**
  119. * Set row actions.
  120. *
  121. * @param array $actions Array of actions.
  122. * @param WP_Post $post Current post object.
  123. * @return array
  124. */
  125. public function row_actions( $actions, $post ) {
  126. if ( $this->list_table_type === $post->post_type ) {
  127. return $this->get_row_actions( $actions, $post );
  128. }
  129. return $actions;
  130. }
  131. /**
  132. * Get row actions to show in the list table.
  133. *
  134. * @param array $actions Array of actions.
  135. * @param WP_Post $post Current post object.
  136. * @return array
  137. */
  138. protected function get_row_actions( $actions, $post ) {
  139. return $actions;
  140. }
  141. /**
  142. * Adjust which columns are displayed by default.
  143. *
  144. * @param array $hidden Current hidden columns.
  145. * @param object $screen Current screen.
  146. * @return array
  147. */
  148. public function default_hidden_columns( $hidden, $screen ) {
  149. if ( isset( $screen->id ) && 'edit-' . $this->list_table_type === $screen->id ) {
  150. $hidden = array_merge( $hidden, $this->define_hidden_columns() );
  151. }
  152. return $hidden;
  153. }
  154. /**
  155. * Set list table primary column.
  156. *
  157. * @param string $default Default value.
  158. * @param string $screen_id Current screen ID.
  159. * @return string
  160. */
  161. public function list_table_primary_column( $default, $screen_id ) {
  162. if ( 'edit-' . $this->list_table_type === $screen_id && $this->get_primary_column() ) {
  163. return $this->get_primary_column();
  164. }
  165. return $default;
  166. }
  167. /**
  168. * Define primary column.
  169. *
  170. * @return array
  171. */
  172. protected function get_primary_column() {
  173. return '';
  174. }
  175. /**
  176. * Define hidden columns.
  177. *
  178. * @return array
  179. */
  180. protected function define_hidden_columns() {
  181. return array();
  182. }
  183. /**
  184. * Define which columns are sortable.
  185. *
  186. * @param array $columns Existing columns.
  187. * @return array
  188. */
  189. public function define_sortable_columns( $columns ) {
  190. return $columns;
  191. }
  192. /**
  193. * Define which columns to show on this screen.
  194. *
  195. * @param array $columns Existing columns.
  196. * @return array
  197. */
  198. public function define_columns( $columns ) {
  199. return $columns;
  200. }
  201. /**
  202. * Define bulk actions.
  203. *
  204. * @param array $actions Existing actions.
  205. * @return array
  206. */
  207. public function define_bulk_actions( $actions ) {
  208. return $actions;
  209. }
  210. /**
  211. * Pre-fetch any data for the row each column has access to it.
  212. *
  213. * @param int $post_id Post ID being shown.
  214. */
  215. protected function prepare_row_data( $post_id ) {}
  216. /**
  217. * Render individual columns.
  218. *
  219. * @param string $column Column ID to render.
  220. * @param int $post_id Post ID being shown.
  221. */
  222. public function render_columns( $column, $post_id ) {
  223. $this->prepare_row_data( $post_id );
  224. if ( ! $this->object ) {
  225. return;
  226. }
  227. if ( is_callable( array( $this, 'render_' . $column . '_column' ) ) ) {
  228. $this->{"render_{$column}_column"}();
  229. }
  230. }
  231. /**
  232. * Handle bulk actions.
  233. *
  234. * @param string $redirect_to URL to redirect to.
  235. * @param string $action Action name.
  236. * @param array $ids List of ids.
  237. * @return string
  238. */
  239. public function handle_bulk_actions( $redirect_to, $action, $ids ) {
  240. return esc_url_raw( $redirect_to );
  241. }
  242. }